Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(penumbra) add support #417

Closed
wants to merge 10 commits into from
Closed
4 changes: 2 additions & 2 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '>=1.20.2'
go-version: '>=1.21'
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand All @@ -29,6 +29,6 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '>=1.20.2'
go-version: '>=1.21'
- name: unit tests
run: make test
2 changes: 1 addition & 1 deletion .github/workflows/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '>=1.20.2'
go-version: '>=1.21'
- run: make generate manifests

- uses: CatChen/check-git-status-action@v1
Expand Down
23 changes: 23 additions & 0 deletions api/v1/cosmosfullnode_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ type FullNodeSpec struct {
// +optional
Type FullNodeType `json:"type"`

// Different kinds of chains supported by the operator
// 'Cosmos' configures the node using defaults for a Cosmos based chain
// 'Custom' configures the node with more flexible options that allow for support other chains
// If not set, defaults to Cosmos
// +kubebuilder:validation:Enum:=Cosmos;Custom
// +optional
ChainType ChainType `json:"chainType"`

// Blockchain-specific configuration.
ChainSpec ChainSpec `json:"chain"`

Expand Down Expand Up @@ -102,6 +110,13 @@ const (
Sentry FullNodeType = "Sentry"
)

type ChainType string

const (
Cosmos ChainType = "Cosmos"
Custom ChainType = "Custom"
)

// FullNodeStatus defines the observed state of CosmosFullNode
type FullNodeStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Expand Down Expand Up @@ -450,6 +465,14 @@ type ChainSpec struct {
// +optional
LogFormat *string `json:"logFormat"`

// Specify shell (sh) script commands to initialize the chain
// +optional
InitScript *string `json:"initScript"`

// Specify shell (sh) script commands to start the chain
// +optional
StartScript *string `json:"startScript"`

// URL to address book file to download from the internet.
// The operator detects and properly handles the following file extensions:
// .json, .json.gz, .tar, .tar.gz, .tar.gzip, .zip
Expand Down
10 changes: 10 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions config/crd/bases/cosmos.strange.love_cosmosfullnodes.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---

Check notice on line 1 in config/crd/bases/cosmos.strange.love_cosmosfullnodes.yaml

View workflow job for this annotation

GitHub Actions / verify

File: config/crd/bases/cosmos.strange.love_cosmosfullnodes.yaml

Check notice on line 1 in config/crd/bases/cosmos.strange.love_cosmosfullnodes.yaml

View workflow job for this annotation

GitHub Actions / verify

File: config/crd/bases/cosmos.strange.love_cosmosfullnodes.yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
Expand Down Expand Up @@ -283,6 +283,10 @@
Therefore, this option was introduced to mitigate those edge cases, so that you can specify the home directory
to match the chain's default home dir.
type: string
initScript:
description: Specify shell (sh) script commands to initialize
the chain
type: string
logFormat:
description: |-
One of plain or json.
Expand Down Expand Up @@ -345,6 +349,9 @@
.tar, .tar.gz, .tar.gzip, .tar.lz4
Use SnapshotScript if the snapshot archive is unconventional or requires special handling.
type: string
startScript:
description: Specify shell (sh) script commands to start the chain
type: string
versions:
description: |-
Versions of the chain and which height they should be applied.
Expand Down Expand Up @@ -376,6 +383,15 @@
- chainID
- network
type: object
chainType:
description: Different kinds of chains supported by the operator 'Cosmos'
configures the node using defaults for a Cosmos based chain 'Custom'
configures the node with more flexible options that allow for support
other chains If not set, defaults to Cosmos
enum:
- Cosmos
- Custom
type: string
instanceOverrides:
additionalProperties:
description: InstanceOverridesSpec allows overriding an instance
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fullnode
package commands

import (
_ "embed"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package fullnode
package commands

import (
"testing"

"github.com/samber/lo"
cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -33,7 +34,7 @@ func TestDownloadAddrbookCommand(t *testing.T) {

t.Run("download", func(t *testing.T) {
cfg := cosmosv1.ChainSpec{
AddrbookURL: ptr("https://example.com/addrbook.json"),
AddrbookURL: lo.ToPtr("https://example.com/addrbook.json"),
}
cmd, args := DownloadAddrbookCommand(cfg)
require.Equal(t, "sh", cmd)
Expand All @@ -53,8 +54,8 @@ func TestDownloadAddrbookCommand(t *testing.T) {
t.Run("custom", func(t *testing.T) {
cfg := cosmosv1.ChainSpec{
// Keeping this to assert that custom script takes precedence.
AddrbookURL: ptr("https://example.com/addrbook.json"),
AddrbookScript: ptr("echo hi"),
AddrbookURL: lo.ToPtr("https://example.com/addrbook.json"),
AddrbookScript: lo.ToPtr("echo hi"),
}
cmd, args := DownloadAddrbookCommand(cfg)
require.Equal(t, "sh", cmd)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fullnode
package commands

import (
_ "embed"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package fullnode
package commands

import (
"testing"

"github.com/samber/lo"
cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -34,7 +35,7 @@ func TestDownloadGenesisCommand(t *testing.T) {

t.Run("download", func(t *testing.T) {
cfg := cosmosv1.ChainSpec{
GenesisURL: ptr("https://example.com/genesis.json"),
GenesisURL: lo.ToPtr("https://example.com/genesis.json"),
}
cmd, args := DownloadGenesisCommand(cfg)
require.Equal(t, "sh", cmd)
Expand All @@ -54,8 +55,8 @@ func TestDownloadGenesisCommand(t *testing.T) {
t.Run("custom", func(t *testing.T) {
cfg := cosmosv1.ChainSpec{
// Keeping this to assert that custom script takes precedence.
GenesisURL: ptr("https://example.com/genesis.json"),
GenesisScript: ptr("echo hi"),
GenesisURL: lo.ToPtr("https://example.com/genesis.json"),
GenesisScript: lo.ToPtr("echo hi"),
}
cmd, args := DownloadGenesisCommand(cfg)
require.Equal(t, "sh", cmd)
Expand Down
47 changes: 47 additions & 0 deletions internal/fullnode/commands/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package commands

import (
_ "embed"
"fmt"

cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1"
)

const initScriptWrapper = `
set -eu

echo CHAIN_HOME: $CHAIN_HOME

if [ ! -d "$CHAIN_HOME/.penumbra" ]; then
echo "Initializing chain..."
%s
else
echo "Skipping chain init; already initialized."
fi
echo "Done"
`

const cosmosInitScriptWrapper = `
set -eu
if [ ! -d "$CHAIN_HOME/data" ]; then
echo "Initializing chain..."
%s --home "$CHAIN_HOME"
else
echo "Skipping chain init; already initialized."
fi

echo "Initializing into tmp dir for downstream processing..."
%s --home "$HOME/.tmp"`

func InitCommand(chainSpec cosmosv1.ChainSpec, moniker string) (string, []string) {
args := []string{"-c"}
switch {
case chainSpec.InitScript != nil:
args = append(args, fmt.Sprintf(initScriptWrapper, *chainSpec.InitScript))
default:
initScript := fmt.Sprintf("%s init --chain-id %s %s", chainSpec.Binary, chainSpec.ChainID, moniker)
args = append(args, fmt.Sprintf(cosmosInitScriptWrapper, initScript, initScript))
}

return "sh", args
}
52 changes: 52 additions & 0 deletions internal/fullnode/commands/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package commands

import (
"testing"

cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1"
"github.com/stretchr/testify/require"
)

func TestInitCommand(t *testing.T) {
t.Parallel()

t.Run("given cosmos chain", func(t *testing.T) {
t.Run("returns init command", func(t *testing.T) {
var cfg = cosmosv1.ChainSpec{
Binary: "gaiad",
ChainID: "cosmoshub-4",
}

cmd, args := InitCommand(cfg, "strangelove")
require.Equal(t, "sh", cmd)

require.Len(t, args, 2)

require.Equal(t, "-c", args[0])

got := args[1]
require.Contains(t, got, "if [ ! -d \"$CHAIN_HOME/data\" ]; then")
require.Contains(t, got, "gaiad init --chain-id cosmoshub-4 strangelove --home \"$CHAIN_HOME\"")
})
})

t.Run("given custom chain", func(t *testing.T) {
t.Run("returns init command", func(t *testing.T) {
var initScript = "pd testnet --testnet-dir /home/operator/cosmos/.penumbra/testnet_data join --external-address 127.0.0.1:26656 --moniker strangelove"
var cfg = cosmosv1.ChainSpec{
InitScript: &initScript,
}

cmd, args := InitCommand(cfg, "strangelove")
require.Equal(t, "sh", cmd)

require.Len(t, args, 2)

require.Equal(t, "-c", args[0])

got := args[1]
require.Contains(t, got, "if [ ! -d \"$CHAIN_HOME/.penumbra\" ]; then")
require.Contains(t, got, "pd testnet --testnet-dir /home/operator/cosmos/.penumbra/testnet_data join --external-address 127.0.0.1:26656 --moniker strangelove")
})
})
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fullnode
package commands

import (
_ "embed"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package fullnode
package commands

import (
"testing"

"github.com/samber/lo"
cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1"
"github.com/stretchr/testify/require"
)
Expand All @@ -19,7 +20,7 @@ fi`
)
t.Run("snapshot url", func(t *testing.T) {
var cfg cosmosv1.ChainSpec
cfg.SnapshotURL = ptr(testURL)
cfg.SnapshotURL = lo.ToPtr(testURL)

cmd, args := DownloadSnapshotCommand(cfg)
require.Equal(t, "sh", cmd)
Expand All @@ -38,8 +39,8 @@ fi`

t.Run("snapshot script", func(t *testing.T) {
var cfg cosmosv1.ChainSpec
cfg.SnapshotURL = ptr(testURL) // Asserts SnapshotScript takes precedence.
cfg.SnapshotScript = ptr("echo hello")
cfg.SnapshotURL = lo.ToPtr(testURL) // Asserts SnapshotScript takes precedence.
cfg.SnapshotScript = lo.ToPtr("echo hello")

_, args := DownloadSnapshotCommand(cfg)
require.Len(t, args, 2)
Expand Down
55 changes: 55 additions & 0 deletions internal/fullnode/commands/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package commands

import (
"fmt"
"strings"

cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1"
)

const startScriptWrapper = `
%s
`

func StartCmdAndArgs(crd *cosmosv1.CosmosFullNode, homeDir string) (string, []string) {
if crd.Spec.ChainSpec.StartScript == nil {
var (
binary = crd.Spec.ChainSpec.Binary
args = StartCommandArgs(crd, homeDir)
privvalSleep int32 = 10
)
if v := crd.Spec.ChainSpec.PrivvalSleepSeconds; v != nil {
privvalSleep = *v
}

if crd.Spec.Type == cosmosv1.Sentry && privvalSleep > 0 {
shellBody := fmt.Sprintf(`sleep %d
%s %s`, privvalSleep, binary, strings.Join(args, " "))
return "sh", []string{"-c", shellBody}
}

return binary, args
} else {
args := []string{"-c"}
args = append(args, fmt.Sprintf(startScriptWrapper, *crd.Spec.ChainSpec.StartScript))
return "sh", args
}
}

func StartCommandArgs(crd *cosmosv1.CosmosFullNode, homeDir string) []string {
args := []string{"start", "--home", homeDir}
cfg := crd.Spec.ChainSpec
if cfg.SkipInvariants {
args = append(args, "--x-crisis-skip-assert-invariants")
}
if lvl := cfg.LogLevel; lvl != nil {
args = append(args, "--log_level", *lvl)
}
if format := cfg.LogFormat; format != nil {
args = append(args, "--log_format", *format)
}
if len(crd.Spec.ChainSpec.AdditionalStartArgs) > 0 {
args = append(args, crd.Spec.ChainSpec.AdditionalStartArgs...)
}
return args
}
Loading
Loading