mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 00:17:32 +00:00
fix(zitadelctl): implementation for commands in crd mode (#1685)
* fix(zitadelctl): implement takedown command * fix(zitadelctl): correct destroy flow * fix(zitadelctl): correct backup commands to read crds beforehand * fix: add of destroyfile * fix: clean for userlist * fix: determine mode by --gitops flag for backups * refactor: return error instead of higher order function * fix(destroy): needs no self-reconciling Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(destroy): needs no self-reconciling Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> * fix(logs): fix double handled error Co-authored-by: Elio Bischof <eliobischof@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
package cmds
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/caos/orbos/pkg/git"
|
||||
|
||||
"github.com/caos/orbos/pkg/kubernetes/cli"
|
||||
"github.com/caos/zitadel/pkg/databases"
|
||||
|
||||
"github.com/caos/zitadel/operator/crtlgitops"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -36,25 +32,27 @@ func BackupCommand(getRv GetRootValues) *cobra.Command {
|
||||
monitor := rv.Monitor
|
||||
orbConfig := rv.OrbConfig
|
||||
gitClient := rv.GitClient
|
||||
version := rv.Version
|
||||
|
||||
if !rv.Gitops {
|
||||
return errors.New("backup command is only supported with the --gitops flag yet")
|
||||
}
|
||||
|
||||
k8sClient, err := cli.Client(monitor, orbConfig, gitClient, rv.Kubeconfig, rv.Gitops, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if gitClient.Exists(git.DatabaseFile) {
|
||||
|
||||
if err := crtlgitops.Backup(
|
||||
if rv.Gitops {
|
||||
if err := databases.GitOpsInstantBackup(
|
||||
monitor,
|
||||
k8sClient,
|
||||
gitClient,
|
||||
backup,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
} else {
|
||||
if err := databases.CrdInstantBackup(
|
||||
monitor,
|
||||
orbConfig.Path,
|
||||
k8sClient,
|
||||
backup,
|
||||
&version,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -1,10 +1,11 @@
|
||||
package cmds
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/caos/orbos/pkg/kubernetes/cli"
|
||||
|
||||
"github.com/caos/zitadel/pkg/databases"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@@ -31,24 +32,26 @@ 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
|
||||
}
|
||||
|
||||
if err := gitClient.Clone(); err != nil {
|
||||
monitor.Error(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
backups, err := databases.ListBackups(monitor, gitClient)
|
||||
backups := make([]string, 0)
|
||||
k8sClient, err := cli.Client(monitor, orbConfig, rv.GitClient, rv.Kubeconfig, rv.Gitops, true)
|
||||
if err != nil {
|
||||
monitor.Error(err)
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
if rv.Gitops {
|
||||
backupsT, err := databases.GitOpsListBackups(monitor, gitClient, k8sClient)
|
||||
if err != nil {
|
||||
monitor.Error(err)
|
||||
return nil
|
||||
}
|
||||
backups = backupsT
|
||||
} else {
|
||||
backupsT, err := databases.CrdListBackups(monitor, k8sClient)
|
||||
if err != nil {
|
||||
monitor.Error(err)
|
||||
return nil
|
||||
}
|
||||
backups = backupsT
|
||||
}
|
||||
|
||||
sort.Slice(backups, func(i, j int) bool {
|
||||
|
165
cmd/zitadelctl/cmds/destroy.go
Normal file
165
cmd/zitadelctl/cmds/destroy.go
Normal file
@@ -0,0 +1,165 @@
|
||||
package cmds
|
||||
|
||||
import (
|
||||
"github.com/caos/orbos/mntr"
|
||||
"github.com/caos/orbos/pkg/git"
|
||||
"github.com/caos/orbos/pkg/kubernetes"
|
||||
"github.com/caos/orbos/pkg/kubernetes/cli"
|
||||
"github.com/caos/zitadel/operator/crtlcrd"
|
||||
"github.com/caos/zitadel/operator/crtlgitops"
|
||||
orbdb "github.com/caos/zitadel/operator/database/kinds/orb"
|
||||
orbzit "github.com/caos/zitadel/operator/zitadel/kinds/orb"
|
||||
kuberneteszit "github.com/caos/zitadel/pkg/kubernetes"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func TeardownCommand(getRv GetRootValues) *cobra.Command {
|
||||
|
||||
var (
|
||||
cmd = &cobra.Command{
|
||||
Use: "teardown",
|
||||
Short: "Tear down an Orb",
|
||||
Long: "Destroys a whole Orb",
|
||||
Aliases: []string{
|
||||
"shoot",
|
||||
"destroy",
|
||||
"devastate",
|
||||
"annihilate",
|
||||
"crush",
|
||||
"bulldoze",
|
||||
"total",
|
||||
"smash",
|
||||
"decimate",
|
||||
"kill",
|
||||
"trash",
|
||||
"wipe-off-the-map",
|
||||
"pulverize",
|
||||
"take-apart",
|
||||
"destruct",
|
||||
"obliterate",
|
||||
"disassemble",
|
||||
"explode",
|
||||
"blow-up",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
|
||||
|
||||
rv, err := getRv()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
err = rv.ErrFunc(err)
|
||||
}()
|
||||
|
||||
monitor := rv.Monitor
|
||||
orbConfig := rv.OrbConfig
|
||||
gitClient := rv.GitClient
|
||||
version := rv.Version
|
||||
|
||||
k8sClient, err := cli.Client(
|
||||
monitor,
|
||||
orbConfig,
|
||||
gitClient,
|
||||
rv.Kubeconfig,
|
||||
rv.Gitops,
|
||||
true,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
monitor.WithFields(map[string]interface{}{
|
||||
"version": version,
|
||||
}).Info("Destroying Orb")
|
||||
|
||||
if err := kuberneteszit.ScaleZitadelOperator(monitor, k8sClient, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := kuberneteszit.ScaleDatabaseOperator(monitor, k8sClient, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if rv.Gitops {
|
||||
if err := crtlgitops.DestroyOperator(monitor, orbConfig.Path, k8sClient, &version, rv.Gitops); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := crtlgitops.DestroyDatabase(monitor, orbConfig.Path, k8sClient, &version, rv.Gitops); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := crtlcrd.Destroy(monitor, k8sClient, version, "zitadel", "database"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := destroyOperator(monitor, gitClient, k8sClient, rv.Gitops); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := destroyDatabase(monitor, gitClient, k8sClient, rv.Gitops); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
func destroyOperator(monitor mntr.Monitor, gitClient *git.Client, k8sClient kubernetes.ClientInt, gitops bool) error {
|
||||
if gitops {
|
||||
if gitClient.Exists(git.ZitadelFile) {
|
||||
desiredTree, err := gitClient.ReadTree(git.ZitadelFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
desired, err := orbzit.ParseDesiredV0(desiredTree)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
spec := desired.Spec
|
||||
|
||||
_, del := orbzit.Reconcile(monitor, spec, gitops)
|
||||
if err := del(k8sClient); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_, del := orbzit.Reconcile(monitor, &orbzit.Spec{}, gitops)
|
||||
if err := del(k8sClient); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func destroyDatabase(monitor mntr.Monitor, gitClient *git.Client, k8sClient kubernetes.ClientInt, gitops bool) error {
|
||||
if gitops {
|
||||
if gitClient.Exists(git.DatabaseFile) {
|
||||
desiredTree, err := gitClient.ReadTree(git.DatabaseFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
desired, err := orbdb.ParseDesiredV0(desiredTree)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
spec := desired.Spec
|
||||
|
||||
_, del := orbdb.Reconcile(monitor, spec, gitops)
|
||||
if err := del(k8sClient); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_, del := orbdb.Reconcile(monitor, &orbdb.Spec{}, gitops)
|
||||
if err := del(k8sClient); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@@ -2,10 +2,10 @@ package cmds
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/caos/zitadel/pkg/zitadel"
|
||||
|
||||
"github.com/caos/orbos/pkg/kubernetes/cli"
|
||||
|
||||
"github.com/caos/zitadel/operator/crtlgitops"
|
||||
"github.com/caos/zitadel/pkg/databases"
|
||||
"github.com/manifoldco/promptui"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -39,19 +39,26 @@ func RestoreCommand(getRv GetRootValues) *cobra.Command {
|
||||
gitClient := rv.GitClient
|
||||
version := rv.Version
|
||||
|
||||
if !rv.Gitops {
|
||||
return errors.New("restore command is only supported with the --gitops flag yet")
|
||||
}
|
||||
|
||||
k8sClient, err := cli.Client(monitor, orbConfig, gitClient, rv.Kubeconfig, rv.Gitops, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
list, err := databases.ListBackups(monitor, gitClient)
|
||||
if err != nil {
|
||||
monitor.Error(err)
|
||||
return nil
|
||||
list := make([]string, 0)
|
||||
if rv.Gitops {
|
||||
listT, err := databases.GitOpsListBackups(monitor, gitClient, k8sClient)
|
||||
if err != nil {
|
||||
monitor.Error(err)
|
||||
return nil
|
||||
}
|
||||
list = listT
|
||||
} else {
|
||||
listT, err := databases.CrdListBackups(monitor, k8sClient)
|
||||
if err != nil {
|
||||
monitor.Error(err)
|
||||
return nil
|
||||
}
|
||||
list = listT
|
||||
}
|
||||
|
||||
if backup == "" {
|
||||
@@ -79,8 +86,17 @@ func RestoreCommand(getRv GetRootValues) *cobra.Command {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := crtlgitops.Restore(monitor, gitClient, orbConfig, k8sClient, backup, rv.Gitops, &version); err != nil {
|
||||
monitor.Error(err)
|
||||
if rv.Gitops {
|
||||
if err := zitadel.GitOpsClearMigrateRestore(monitor, gitClient, orbConfig, k8sClient, backup, &version); err != nil {
|
||||
monitor.Error(err)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := zitadel.CrdClearMigrateRestore(monitor, k8sClient, backup, &version); err != nil {
|
||||
monitor.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@@ -106,7 +106,8 @@ func deployOperator(monitor mntr.Monitor, gitClient *git.Client, k8sClient kuber
|
||||
|
||||
// at takeoff the artifacts have to be applied
|
||||
spec.SelfReconciling = true
|
||||
if err := orbzit.Reconcile(monitor, spec, gitops)(k8sClient); err != nil {
|
||||
rec, _ := orbzit.Reconcile(monitor, spec, gitops)
|
||||
if err := rec(k8sClient); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -117,7 +118,8 @@ func deployOperator(monitor mntr.Monitor, gitClient *git.Client, k8sClient kuber
|
||||
SelfReconciling: true,
|
||||
}
|
||||
|
||||
if err := orbzit.Reconcile(monitor, spec, gitops)(k8sClient); err != nil {
|
||||
rec, _ := orbzit.Reconcile(monitor, spec, gitops)
|
||||
if err := rec(k8sClient); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -140,11 +142,8 @@ func deployDatabase(monitor mntr.Monitor, gitClient *git.Client, k8sClient kuber
|
||||
|
||||
// at takeoff the artifacts have to be applied
|
||||
spec.SelfReconciling = true
|
||||
if err := orbdb.Reconcile(
|
||||
monitor,
|
||||
spec,
|
||||
gitops,
|
||||
)(k8sClient); err != nil {
|
||||
rec, _ := orbdb.Reconcile(monitor, desired.Spec, gitops)
|
||||
if err := rec(k8sClient); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -155,11 +154,8 @@ func deployDatabase(monitor mntr.Monitor, gitClient *git.Client, k8sClient kuber
|
||||
SelfReconciling: true,
|
||||
}
|
||||
|
||||
if err := orbdb.Reconcile(
|
||||
monitor,
|
||||
spec,
|
||||
gitops,
|
||||
)(k8sClient); err != nil {
|
||||
rec, _ := orbdb.Reconcile(monitor, spec, gitops)
|
||||
if err := rec(k8sClient); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@@ -27,6 +27,7 @@ func main() {
|
||||
cmds.BackupCommand(rootValues),
|
||||
cmds.StartDatabase(rootValues),
|
||||
cmds.ConfigCommand(rootValues, githubClientID, githubClientSecret),
|
||||
cmds.TeardownCommand(rootValues),
|
||||
)
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
|
Reference in New Issue
Block a user