-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
631 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
Copyright © 2023 Strangelove Crypto, Inc. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"cosmossdk.io/log" | ||
"cosmossdk.io/store/rootmulti" | ||
dbm "github.com/cosmos/cosmos-db" | ||
"github.com/spf13/cobra" | ||
cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const ( | ||
namespaceFile = "/var/run/secrets/kubernetes.io/serviceaccount/namespace" | ||
|
||
flagBackend = "backend" | ||
) | ||
|
||
// VersionCheckCmd gets the height of this node and updates the status of the crd. | ||
// It panics if the wrong image is specified for the pod for the height, | ||
// restarting the pod so that the correct image is used from the patched height. | ||
// this command is intended to be run as an init container. | ||
func VersionCheckCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "versioncheck", | ||
Short: "Confirm correct image used for current node height", | ||
Long: `Open the Cosmos SDK chain database, get the height, update the crd status with the height, then check the image for the height and panic if it is incorrect.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("height called") | ||
|
||
dataDir := os.Getenv("DATA_DIR") | ||
backend, _ := cmd.Flags().GetString(flagBackend) | ||
|
||
s, err := os.Stat(dataDir) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to stat %s: %w", dataDir, err)) | ||
} | ||
|
||
if !s.IsDir() { | ||
panic(fmt.Errorf("%s is not a directory", dataDir)) | ||
} | ||
|
||
db, err := dbm.NewDB("application", getBackend(backend), dataDir) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to open db: %w", err)) | ||
} | ||
|
||
store := rootmulti.NewStore(db, log.NewNopLogger(), nil) | ||
|
||
height := store.LatestVersion() | ||
fmt.Printf("%d", height) | ||
|
||
nsbz, err := os.ReadFile(namespaceFile) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to read namespace from service account: %w", err)) | ||
} | ||
ns := string(nsbz) | ||
|
||
config, err := rest.InClusterConfig() | ||
if err != nil { | ||
panic(fmt.Errorf("failed to get in cluster config: %w", err)) | ||
} | ||
|
||
clientset, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to create kube clientset: %w", err)) | ||
} | ||
|
||
ctx := cmd.Context() | ||
|
||
thisPod, err := clientset.CoreV1().Pods(ns).Get(ctx, os.Getenv("HOSTNAME"), metav1.GetOptions{}) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to get this pod: %w", err)) | ||
} | ||
|
||
cosmosFullNodeName := thisPod.Labels["app.kubernetes.io/name"] | ||
|
||
kClient, err := client.New(config, client.Options{}) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to create kube client: %w", err)) | ||
} | ||
|
||
namespacedName := types.NamespacedName{ | ||
Namespace: ns, | ||
Name: cosmosFullNodeName, | ||
} | ||
|
||
crd := new(cosmosv1.CosmosFullNode) | ||
if err := kClient.Get(ctx, namespacedName, crd); err != nil { | ||
panic(fmt.Errorf("failed to get crd: %w", err)) | ||
} | ||
|
||
if err := kClient.Status().Patch( | ||
ctx, crd, | ||
client.RawPatch( | ||
types.StrategicMergePatchType, | ||
[]byte(fmt.Sprintf( | ||
`{"syncInfo":{"pods":[{"pod":"%s","time":"%s","height":%d,"inSync": false}]}}`, | ||
thisPod.Name, | ||
time.Now().Format(time.RFC3339), | ||
height, | ||
)), | ||
), | ||
); err != nil { | ||
panic(fmt.Errorf("failed to patch status: %w", err)) | ||
} | ||
|
||
var image string | ||
for _, v := range crd.Spec.ChainSpec.Versions { | ||
if uint64(height) < v.UpgradeHeight { | ||
break | ||
} | ||
image = v.Image | ||
} | ||
|
||
thisPodImage := thisPod.Spec.Containers[0].Image | ||
if thisPodImage != image { | ||
panic(fmt.Errorf("image mismatch for height %d: %s != %s", height, thisPodImage, image)) | ||
} | ||
|
||
fmt.Printf("Verified correct image for height %d: %s\n", height, image) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringP(flagBackend, "b", "goleveldb", "Database backend") | ||
|
||
return cmd | ||
} | ||
|
||
func getBackend(backend string) dbm.BackendType { | ||
switch backend { | ||
case "goleveldb": | ||
return dbm.GoLevelDBBackend | ||
case "memdb": | ||
return dbm.MemDBBackend | ||
case "rocksdb": | ||
return dbm.RocksDBBackend | ||
case "pebbledb": | ||
return dbm.PebbleDBBackend | ||
default: | ||
panic(fmt.Errorf("unknown backend %s", backend)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.