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 <[email protected]>
This commit is contained in:
Livio Amstutz
2021-03-25 16:39:19 +01:00
committed by GitHub
co-authored by Elio Bischof
parent ae66e40ace
commit c9b3839f3d
10 changed files with 73 additions and 104 deletions
+31 -50
View File
@@ -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