From c9b3839f3d0d65c04d4513ec31dc3b951c728b4c Mon Sep 17 00:00:00 2001 From: Livio Amstutz Date: Thu, 25 Mar 2021 16:39:19 +0100 Subject: [PATCH] fix: operator reconciling (#1478) * fix(operator): align backup and restore commands (#1465) * fix: crd mode broke backup and restore commands * fix: remove obscure gitops-per-operator flags (cherry picked from commit 041cacc4af8aeb89723a049bc4dbfcbc33cd987f) * fix: gitops backup and restore need a kubernetes client too (#1475) (cherry picked from commit 50bc317d2797b819124cdb1119db76bb00ab0df6) Co-authored-by: Elio Bischof --- cmd/zitadelctl/cmds/backup.go | 45 +++++-------- cmd/zitadelctl/cmds/backuplist.go | 5 ++ cmd/zitadelctl/cmds/restore.go | 81 +++++++++--------------- cmd/zitadelctl/cmds/takeoff.go | 34 ++++------ operator/database/kinds/orb/adapt.go | 2 +- operator/database/kinds/orb/desired.go | 1 - operator/database/kinds/orb/reconcile.go | 3 +- operator/zitadel/kinds/orb/adapt.go | 2 +- operator/zitadel/kinds/orb/desired.go | 1 - operator/zitadel/kinds/orb/reconcile.go | 3 +- 10 files changed, 73 insertions(+), 104 deletions(-) diff --git a/cmd/zitadelctl/cmds/backup.go b/cmd/zitadelctl/cmds/backup.go index d19564b60c..fc5b80f67e 100644 --- a/cmd/zitadelctl/cmds/backup.go +++ b/cmd/zitadelctl/cmds/backup.go @@ -1,9 +1,10 @@ package cmds import ( - "io/ioutil" + "errors" + + "github.com/caos/orbos/pkg/kubernetes/cli" - "github.com/caos/orbos/pkg/kubernetes" "github.com/caos/zitadel/operator/api" "github.com/caos/zitadel/operator/crtlgitops" "github.com/spf13/cobra" @@ -11,9 +12,8 @@ import ( func BackupCommand(getRv GetRootValues) *cobra.Command { var ( - kubeconfig string - backup string - cmd = &cobra.Command{ + backup string + cmd = &cobra.Command{ Use: "backup", Short: "Instant backup", Long: "Instant backup", @@ -21,7 +21,6 @@ func BackupCommand(getRv GetRootValues) *cobra.Command { ) flags := cmd.Flags() - flags.StringVar(&kubeconfig, "kubeconfig", "~/.kube/config", "Kubeconfig of cluster where the backup should be done") flags.StringVar(&backup, "backup", "", "Name used for backup folder") cmd.RunE = func(cmd *cobra.Command, args []string) (err error) { @@ -38,11 +37,12 @@ func BackupCommand(getRv GetRootValues) *cobra.Command { gitClient := rv.GitClient version := rv.Version - if err := gitClient.Configure(orbConfig.URL, []byte(orbConfig.Repokey)); err != nil { - return err + if !rv.Gitops { + return errors.New("backup command is only supported with the --gitops flag yet") } - if err := gitClient.Clone(); err != nil { + k8sClient, _, err := cli.Client(monitor, orbConfig, gitClient, rv.Kubeconfig, rv.Gitops) + if err != nil { return err } @@ -52,26 +52,15 @@ func BackupCommand(getRv GetRootValues) *cobra.Command { } if found { - value, err := ioutil.ReadFile(kubeconfig) - if err != nil { - monitor.Error(err) - return nil + if err := crtlgitops.Backup( + monitor, + orbConfig.Path, + k8sClient, + backup, + &version, + ); err != nil { + return err } - kubeconfigStr := string(value) - - k8sClient := kubernetes.NewK8sClient(monitor, &kubeconfigStr) - if k8sClient.Available() { - if err := crtlgitops.Backup( - monitor, - orbConfig.Path, - k8sClient, - backup, - &version, - ); err != nil { - return err - } - } - } return nil } diff --git a/cmd/zitadelctl/cmds/backuplist.go b/cmd/zitadelctl/cmds/backuplist.go index 9d489ab39c..6b19e14005 100644 --- a/cmd/zitadelctl/cmds/backuplist.go +++ b/cmd/zitadelctl/cmds/backuplist.go @@ -1,6 +1,7 @@ package cmds import ( + "errors" "fmt" "sort" @@ -30,6 +31,10 @@ func BackupListCommand(getRv GetRootValues) *cobra.Command { orbConfig := rv.OrbConfig gitClient := rv.GitClient + if !rv.Gitops { + return errors.New("backuplist command is only supported with the --gitops flag yet") + } + if err := gitClient.Configure(orbConfig.URL, []byte(orbConfig.Repokey)); err != nil { monitor.Error(err) return nil diff --git a/cmd/zitadelctl/cmds/restore.go b/cmd/zitadelctl/cmds/restore.go index 5eb556af22..b1a7273f11 100644 --- a/cmd/zitadelctl/cmds/restore.go +++ b/cmd/zitadelctl/cmds/restore.go @@ -2,12 +2,10 @@ package cmds import ( "errors" - "io/ioutil" + + "github.com/caos/orbos/pkg/kubernetes/cli" "github.com/caos/zitadel/operator/crtlgitops" - "github.com/caos/zitadel/operator/helpers" - - "github.com/caos/orbos/pkg/kubernetes" "github.com/caos/zitadel/pkg/databases" "github.com/manifoldco/promptui" "github.com/spf13/cobra" @@ -15,10 +13,8 @@ import ( func RestoreCommand(getRv GetRootValues) *cobra.Command { var ( - backup string - kubeconfig string - gitOpsMode bool - cmd = &cobra.Command{ + backup string + cmd = &cobra.Command{ Use: "restore", Short: "Restore from backup", Long: "Restore from backup", @@ -27,8 +23,6 @@ func RestoreCommand(getRv GetRootValues) *cobra.Command { flags := cmd.Flags() flags.StringVar(&backup, "backup", "", "Backup used for db restore") - flags.StringVar(&kubeconfig, "kubeconfig", "~/.kube/config", "Kubeconfig for ZITADEL operator deployment") - flags.BoolVar(&gitOpsMode, "gitops", false, "defines if the operator should run in gitops mode") cmd.RunE = func(cmd *cobra.Command, args []string) error { rv, err := getRv() @@ -39,68 +33,55 @@ func RestoreCommand(getRv GetRootValues) *cobra.Command { err = rv.ErrFunc(err) }() + // TODO: Why? monitor := rv.Monitor orbConfig := rv.OrbConfig gitClient := rv.GitClient version := rv.Version - kubeconfig = helpers.PruneHome(kubeconfig) - - if err := gitClient.Configure(orbConfig.URL, []byte(orbConfig.Repokey)); err != nil { - monitor.Error(err) - return nil + if !rv.Gitops { + return errors.New("restore command is only supported with the --gitops flag yet") } - if err := gitClient.Clone(); err != nil { - monitor.Error(err) - return nil + k8sClient, _, err := cli.Client(monitor, orbConfig, gitClient, rv.Kubeconfig, rv.Gitops) + if err != nil { + return err } - value, err := ioutil.ReadFile(kubeconfig) + list, err := databases.ListBackups(monitor, gitClient) if err != nil { monitor.Error(err) return nil } - kubeconfigStr := string(value) - k8sClient := kubernetes.NewK8sClient(monitor, &kubeconfigStr) - if k8sClient.Available() { - list, err := databases.ListBackups(monitor, gitClient) + if backup == "" { + prompt := promptui.Select{ + Label: "Select backup to restore", + Items: list, + } + + _, result, err := prompt.Run() if err != nil { monitor.Error(err) return nil } - - if backup == "" { - prompt := promptui.Select{ - Label: "Select backup to restore", - Items: list, - } - - _, result, err := prompt.Run() - if err != nil { - monitor.Error(err) - return nil - } - backup = result - } - existing := false - for _, listedBackup := range list { - if listedBackup == backup { - existing = true - } + backup = result + } + existing := false + for _, listedBackup := range list { + if listedBackup == backup { + existing = true } + } - if !existing { - monitor.Error(errors.New("chosen backup is not existing")) - return nil - } - - if err := crtlgitops.Restore(monitor, gitClient, orbConfig, k8sClient, backup, gitOpsMode, &version); err != nil { - monitor.Error(err) - } + if !existing { + monitor.Error(errors.New("chosen backup is not existing")) return nil } + + if err := crtlgitops.Restore(monitor, gitClient, orbConfig, k8sClient, backup, rv.Gitops, &version); err != nil { + monitor.Error(err) + } return nil } return cmd diff --git a/cmd/zitadelctl/cmds/takeoff.go b/cmd/zitadelctl/cmds/takeoff.go index cc49e1c00a..cd9d3f7228 100644 --- a/cmd/zitadelctl/cmds/takeoff.go +++ b/cmd/zitadelctl/cmds/takeoff.go @@ -2,7 +2,7 @@ package cmds import ( "github.com/caos/orbos/pkg/kubernetes/cli" - "github.com/ghodss/yaml" + "gopkg.in/yaml.v3" orbdb "github.com/caos/zitadel/operator/database/kinds/orb" @@ -17,19 +17,13 @@ import ( func TakeoffCommand(getRv GetRootValues) *cobra.Command { var ( - gitOpsZitadel bool - gitOpsDatabase bool - cmd = &cobra.Command{ + cmd = &cobra.Command{ Use: "takeoff", Short: "Launch a ZITADEL operator on the orb", Long: "Ensures a desired state of the resources on the orb", } ) - flags := cmd.Flags() - flags.BoolVar(&gitOpsZitadel, "gitops-zitadel", false, "defines if the zitadel operator should run in gitops mode") - flags.BoolVar(&gitOpsDatabase, "gitops-database", false, "defines if the database operator should run in gitops mode") - cmd.RunE = func(cmd *cobra.Command, args []string) error { rv, err := getRv() if err != nil { @@ -48,7 +42,7 @@ func TakeoffCommand(getRv GetRootValues) *cobra.Command { orbConfig, gitClient, rv.Kubeconfig, - gitOpsZitadel || gitOpsDatabase, + rv.Gitops, ) if err != nil { return err @@ -59,7 +53,7 @@ func TakeoffCommand(getRv GetRootValues) *cobra.Command { return err } - if gitOpsZitadel || gitOpsDatabase { + if rv.Gitops { orbConfigBytes, err := yaml.Marshal(orbConfig) if err != nil { @@ -77,7 +71,7 @@ func TakeoffCommand(getRv GetRootValues) *cobra.Command { gitClient, k8sClient, rv.Version, - rv.Gitops || gitOpsZitadel, + rv.Gitops, ); err != nil { monitor.Error(err) } @@ -87,7 +81,7 @@ func TakeoffCommand(getRv GetRootValues) *cobra.Command { gitClient, k8sClient, rv.Version, - rv.Gitops || gitOpsDatabase, + rv.Gitops, ); err != nil { monitor.Error(err) } @@ -113,11 +107,10 @@ func deployOperator(monitor mntr.Monitor, gitClient *git.Client, k8sClient kuber return err } spec := desired.Spec - spec.GitOps = gitops // at takeoff the artifacts have to be applied spec.SelfReconciling = true - if err := orbzit.Reconcile(monitor, spec)(k8sClient); err != nil { + if err := orbzit.Reconcile(monitor, spec, gitops)(k8sClient); err != nil { return err } } @@ -126,10 +119,9 @@ func deployOperator(monitor mntr.Monitor, gitClient *git.Client, k8sClient kuber spec := &orbzit.Spec{ Version: version, SelfReconciling: true, - GitOps: gitops, } - if err := orbzit.Reconcile(monitor, spec)(k8sClient); err != nil { + if err := orbzit.Reconcile(monitor, spec, gitops)(k8sClient); err != nil { return err } } @@ -153,13 +145,14 @@ func deployDatabase(monitor mntr.Monitor, gitClient *git.Client, k8sClient kuber return err } spec := desired.Spec - spec.GitOps = gitops // at takeoff the artifacts have to be applied spec.SelfReconciling = true if err := orbdb.Reconcile( monitor, - spec)(k8sClient); err != nil { + spec, + gitops, + )(k8sClient); err != nil { return err } } @@ -168,12 +161,13 @@ func deployDatabase(monitor mntr.Monitor, gitClient *git.Client, k8sClient kuber spec := &orbdb.Spec{ Version: version, SelfReconciling: true, - GitOps: gitops, } if err := orbdb.Reconcile( monitor, - spec)(k8sClient); err != nil { + spec, + gitops, + )(k8sClient); err != nil { return err } } diff --git a/operator/database/kinds/orb/adapt.go b/operator/database/kinds/orb/adapt.go index a7ce0668b6..5d10860b58 100644 --- a/operator/database/kinds/orb/adapt.go +++ b/operator/database/kinds/orb/adapt.go @@ -99,7 +99,7 @@ func AdaptFunc(timestamp string, binaryVersion *string, gitops bool, features .. case "operator": queriers = append(queriers, operator.ResourceQueryToZitadelQuery(queryNS), - operator.EnsureFuncToQueryFunc(Reconcile(monitor, desiredKind.Spec)), + operator.EnsureFuncToQueryFunc(Reconcile(monitor, desiredKind.Spec, gitops)), ) } } diff --git a/operator/database/kinds/orb/desired.go b/operator/database/kinds/orb/desired.go index 6048714d4b..15a982876d 100644 --- a/operator/database/kinds/orb/desired.go +++ b/operator/database/kinds/orb/desired.go @@ -19,7 +19,6 @@ type Spec struct { Tolerations []corev1.Toleration `json:"tolerations,omitempty" yaml:"tolerations,omitempty"` Version string `json:"version,omitempty" yaml:"version,omitempty"` SelfReconciling bool `json:"selfReconciling" yaml:"selfReconciling"` - GitOps bool `json:"gitOps,omitempty" yaml:"gitOps,omitempty"` //Use this registry to pull the Database operator image from //@default: ghcr.io CustomImageRegistry string `json:"customImageRegistry,omitempty" yaml:"customImageRegistry,omitempty"` diff --git a/operator/database/kinds/orb/reconcile.go b/operator/database/kinds/orb/reconcile.go index 295f7e4569..851478a509 100644 --- a/operator/database/kinds/orb/reconcile.go +++ b/operator/database/kinds/orb/reconcile.go @@ -13,6 +13,7 @@ import ( func Reconcile( monitor mntr.Monitor, spec *Spec, + gitops bool, ) operator.EnsureFunc { return func(k8sClient kubernetes.ClientInt) (err error) { recMonitor := monitor.WithField("version", spec.Version) @@ -36,7 +37,7 @@ func Reconcile( }, } - if err := zitadelKubernetes.EnsureDatabaseArtifacts(monitor, treelabels.MustForAPI(desiredTree, mustDatabaseOperator(&spec.Version)), k8sClient, spec.Version, spec.NodeSelector, spec.Tolerations, imageRegistry, spec.GitOps); err != nil { + if err := zitadelKubernetes.EnsureDatabaseArtifacts(monitor, treelabels.MustForAPI(desiredTree, mustDatabaseOperator(&spec.Version)), k8sClient, spec.Version, spec.NodeSelector, spec.Tolerations, imageRegistry, gitops); err != nil { recMonitor.Error(errors.Wrap(err, "Failed to deploy database-operator into k8s-cluster")) return err } diff --git a/operator/zitadel/kinds/orb/adapt.go b/operator/zitadel/kinds/orb/adapt.go index df84879e45..6009f4db39 100644 --- a/operator/zitadel/kinds/orb/adapt.go +++ b/operator/zitadel/kinds/orb/adapt.go @@ -112,7 +112,7 @@ func AdaptFunc( case "operator": queriers = append(queriers, operator.ResourceQueryToZitadelQuery(queryNS), - operator.EnsureFuncToQueryFunc(Reconcile(monitor, desiredKind.Spec)), + operator.EnsureFuncToQueryFunc(Reconcile(monitor, desiredKind.Spec, gitops)), ) } } diff --git a/operator/zitadel/kinds/orb/desired.go b/operator/zitadel/kinds/orb/desired.go index 5af706a5dd..3903ed1c32 100644 --- a/operator/zitadel/kinds/orb/desired.go +++ b/operator/zitadel/kinds/orb/desired.go @@ -19,7 +19,6 @@ type Spec struct { Tolerations []corev1.Toleration `json:"tolerations,omitempty" yaml:"tolerations,omitempty"` Version string `json:"version,omitempty" yaml:"version,omitempty"` SelfReconciling bool `json:"selfReconciling" yaml:"selfReconciling"` - GitOps bool `json:"gitops,omitempty" yaml:"gitops,omitempty"` //Use this registry to pull the zitadel operator image from //@default: ghcr.io CustomImageRegistry string `json:"customImageRegistry,omitempty" yaml:"customImageRegistry,omitempty"` diff --git a/operator/zitadel/kinds/orb/reconcile.go b/operator/zitadel/kinds/orb/reconcile.go index 977afa2496..4063823b44 100644 --- a/operator/zitadel/kinds/orb/reconcile.go +++ b/operator/zitadel/kinds/orb/reconcile.go @@ -13,6 +13,7 @@ import ( func Reconcile( monitor mntr.Monitor, spec *Spec, + gitops bool, ) operator.EnsureFunc { return func(k8sClient kubernetes2.ClientInt) (err error) { recMonitor := monitor.WithField("version", spec.Version) @@ -36,7 +37,7 @@ func Reconcile( }, } - if err := kubernetes.EnsureZitadelOperatorArtifacts(monitor, treelabels.MustForAPI(desiredTree, mustZITADELOperator(&spec.Version)), k8sClient, spec.Version, spec.NodeSelector, spec.Tolerations, imageRegistry, spec.GitOps); err != nil { + if err := kubernetes.EnsureZitadelOperatorArtifacts(monitor, treelabels.MustForAPI(desiredTree, mustZITADELOperator(&spec.Version)), k8sClient, spec.Version, spec.NodeSelector, spec.Tolerations, imageRegistry, gitops); err != nil { recMonitor.Error(errors.Wrap(err, "Failed to deploy zitadel-operator into k8s-cluster")) return err }