Skip to content

Commit

Permalink
refactor: encapsulate all commands
Browse files Browse the repository at this point in the history
Add new functions to always create new commands, such that we not reuse commands and not rely on globals
  • Loading branch information
ChrisKujawa committed Dec 9, 2022
1 parent 80b5597 commit c7b468e
Show file tree
Hide file tree
Showing 16 changed files with 648 additions and 657 deletions.
82 changes: 45 additions & 37 deletions go-chaos/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cmd

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -33,7 +34,44 @@ import (
"github.com/zeebe-io/zeebe-chaos/go-chaos/internal"
)

func init() {
func AddBackupCommand(rootCmd *cobra.Command, flags Flags) {

var backupCommand = &cobra.Command{
Use: "backup",
Short: "Controls Zeebe backups",
Long: "Can be used to take backups and query their status",
}

var setupBackupCommand = &cobra.Command{
Use: "setup",
Short: "Configures a zeebe cluster's backup settings",
RunE: setupBackup,
}

var takeBackupCommand = &cobra.Command{
Use: "take",
Short: "Trigger a backup",
RunE: func(cmd *cobra.Command, args []string) error {
return takeBackup(flags)
},
}

var waitForBackupCommand = &cobra.Command{
Use: "wait",
Short: "Wait for a backup to complete or fail",
RunE: func(cmd *cobra.Command, args []string) error {
return waitForBackup(flags)
},
}

var restoreBackupCommand = &cobra.Command{
Use: "restore",
Short: "Restore from a given backup id",
RunE: func(cmd *cobra.Command, args []string) error {
return restoreFromBackup(flags)
},
}

rootCmd.AddCommand(backupCommand)
backupCommand.AddCommand(setupBackupCommand)
backupCommand.AddCommand(takeBackupCommand)
Expand All @@ -44,36 +82,6 @@ func init() {
restoreBackupCommand.Flags().StringVar(&flags.backupId, "backupId", strconv.FormatInt(time.Now().UnixMilli(), 10), "optionally specify the backup id to use, uses the current timestamp by default")
}

var backupCommand = &cobra.Command{
Use: "backup",
Short: "Controls Zeebe backups",
Long: "Can be used to take backups and query their status",
}

var setupBackupCommand = &cobra.Command{
Use: "setup",
Short: "Configures a zeebe cluster's backup settings",
RunE: setupBackup,
}

var takeBackupCommand = &cobra.Command{
Use: "take",
Short: "Trigger a backup",
RunE: takeBackup,
}

var waitForBackupCommand = &cobra.Command{
Use: "wait",
Short: "Wait for a backup to complete or fail",
RunE: waitForBackup,
}

var restoreBackupCommand = &cobra.Command{
Use: "restore",
Short: "Restore from a given backup id",
RunE: restoreFromBackup,
}

func setupBackup(cmd *cobra.Command, _ []string) error {
k8Client, err := internal.CreateK8Client()
if err != nil {
Expand Down Expand Up @@ -172,7 +180,7 @@ func createBackupSecret(cmd *cobra.Command, k8Client internal.K8Client, namespac
)
}

func takeBackup(*cobra.Command, []string) error {
func takeBackup(flags Flags) error {
k8Client, err := internal.CreateK8Client()
if err != nil {
panic(err)
Expand Down Expand Up @@ -201,7 +209,7 @@ func takeBackup(*cobra.Command, []string) error {
return err
}

func waitForBackup(*cobra.Command, []string) error {
func waitForBackup(flags Flags) error {
k8Client, err := internal.CreateK8Client()
if err != nil {
panic(err)
Expand Down Expand Up @@ -230,7 +238,7 @@ func waitForBackup(*cobra.Command, []string) error {

}

func restoreFromBackup(cmd *cobra.Command, _ []string) error {
func restoreFromBackup(flags Flags) error {
k8Client, err := internal.CreateK8Client()
if err != nil {
panic(err)
Expand Down Expand Up @@ -268,7 +276,7 @@ func restoreFromBackup(cmd *cobra.Command, _ []string) error {
Name: "restore-from-backup",
Image: sfs.Spec.Template.Spec.Containers[0].Image,
ImagePullPolicy: core.PullAlways,
Env: restoreEnvFromSfs(sfs),
Env: restoreEnvFromSfs(flags, sfs),
EnvFrom: []core.EnvFromSource{{SecretRef: &core.SecretEnvSource{LocalObjectReference: core.LocalObjectReference{Name: "zeebe-backup-store-s3"}}}},
VolumeMounts: []core.VolumeMount{
{
Expand All @@ -280,7 +288,7 @@ func restoreFromBackup(cmd *cobra.Command, _ []string) error {
}
sfs.Spec.Template.Spec.InitContainers = []core.Container{deleteContainer, restoreContainer}

_, err = k8Client.Clientset.AppsV1().StatefulSets(namespace).Update(cmd.Context(), sfs, meta.UpdateOptions{})
_, err = k8Client.Clientset.AppsV1().StatefulSets(namespace).Update(context.TODO(), sfs, meta.UpdateOptions{})
if err != nil {
return err
}
Expand All @@ -304,7 +312,7 @@ func restoreFromBackup(cmd *cobra.Command, _ []string) error {
return nil
}

func restoreEnvFromSfs(sfs *apps.StatefulSet) []core.EnvVar {
func restoreEnvFromSfs(flags Flags, sfs *apps.StatefulSet) []core.EnvVar {
zeebeEnv := sfs.Spec.Template.Spec.Containers[0].Env
restoreEnv := make([]core.EnvVar, 0)
for _, env := range zeebeEnv {
Expand Down
54 changes: 27 additions & 27 deletions go-chaos/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,34 @@ import (
"github.com/zeebe-io/zeebe-chaos/go-chaos/backend"
)

func init() {
rootCmd.AddCommand(connect)
connect.AddCommand(connectBrokers)
connect.AddCommand(connectGateway)
}
func AddConnectCmd(rootCmd *cobra.Command) {
var connect = &cobra.Command{
Use: "connect",
Short: "Connect Zeebe nodes",
Long: `Connect all Zeebe nodes again, after they have been disconnected uses sub-commands to connect brokers, gateways, etc.`,
}

var connect = &cobra.Command{
Use: "connect",
Short: "Connect Zeebe nodes",
Long: `Connect all Zeebe nodes again, after they have been disconnected uses sub-commands to connect brokers, gateways, etc.`,
}
var connectBrokers = &cobra.Command{
Use: "brokers",
Short: "Connect Zeebe Brokers",
Long: `Connect all Zeebe Brokers again, after they have been disconnected.`,
Run: func(cmd *cobra.Command, args []string) {
err := backend.ConnectBrokers()
ensureNoError(err)
},
}

var connectBrokers = &cobra.Command{
Use: "brokers",
Short: "Connect Zeebe Brokers",
Long: `Connect all Zeebe Brokers again, after they have been disconnected.`,
Run: func(cmd *cobra.Command, args []string) {
err := backend.ConnectBrokers()
ensureNoError(err)
},
}
var connectGateway = &cobra.Command{
Use: "gateway",
Short: "Connect Zeebe Gateway",
Long: `Connect all Zeebe Gateway again, after it has been disconnected.`,
Run: func(cmd *cobra.Command, args []string) {
err := backend.ConnectGateway()
ensureNoError(err)
},
}

var connectGateway = &cobra.Command{
Use: "gateway",
Short: "Connect Zeebe Gateway",
Long: `Connect all Zeebe Gateway again, after it has been disconnected.`,
Run: func(cmd *cobra.Command, args []string) {
err := backend.ConnectGateway()
ensureNoError(err)
},
rootCmd.AddCommand(connect)
connect.AddCommand(connectBrokers)
connect.AddCommand(connectGateway)
}
Loading

0 comments on commit c7b468e

Please sign in to comment.