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 <[email protected]>

* fix(destroy): needs no self-reconciling

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

* fix(logs): fix double handled error

Co-authored-by: Elio Bischof <[email protected]>

Co-authored-by: Elio Bischof <[email protected]>
This commit is contained in:
Stefan Benz
2021-07-20 15:47:48 +02:00
committed by GitHub
co-authored by Elio Bischof
parent 413ed80611
commit 25c9d7371d
48 changed files with 1084 additions and 275 deletions
+105 -15
View File
@@ -16,6 +16,14 @@ import (
mach "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
namespace = "caos-system"
)
func getZitadelOperatorNameLabels(apiLabels *labels.API) *labels.Name {
return toNameLabels(apiLabels, "zitadel-operator")
}
func EnsureZitadelOperatorArtifacts(
monitor mntr.Monitor,
apiLabels *labels.API,
@@ -31,7 +39,7 @@ func EnsureZitadelOperatorArtifacts(
"zitadel": version,
}).Debug("Ensuring zitadel artifacts")
nameLabels := labels.MustForName(labels.MustForComponent(apiLabels, "operator"), "zitadel-operator")
nameLabels := getZitadelOperatorNameLabels(apiLabels)
k8sNameLabels := labels.MustK8sMap(nameLabels)
k8sPodSelector := labels.MustK8sMap(labels.DeriveNameSelector(nameLabels, false))
@@ -42,7 +50,7 @@ func EnsureZitadelOperatorArtifacts(
if err := client.ApplyServiceAccount(&core.ServiceAccount{
ObjectMeta: mach.ObjectMeta{
Name: nameLabels.Name(),
Namespace: "caos-system",
Namespace: namespace,
},
}); err != nil {
return err
@@ -76,7 +84,7 @@ func EnsureZitadelOperatorArtifacts(
Subjects: []rbac.Subject{{
Kind: "ServiceAccount",
Name: nameLabels.Name(),
Namespace: "caos-system",
Namespace: namespace,
}},
}); err != nil {
return err
@@ -251,7 +259,7 @@ status:
deployment := &apps.Deployment{
ObjectMeta: mach.ObjectMeta{
Name: nameLabels.Name(),
Namespace: "caos-system",
Namespace: namespace,
Labels: k8sNameLabels,
},
Spec: apps.DeploymentSpec{
@@ -305,6 +313,40 @@ status:
return nil
}
func DestroyZitadelOperator(
monitor mntr.Monitor,
apiLabels *labels.API,
client kubernetes.ClientInt,
gitops bool,
) error {
nameLabels := getZitadelOperatorNameLabels(apiLabels)
monitor.WithFields(map[string]interface{}{}).Debug("Destroying zitadel artifacts")
if err := client.DeleteServiceAccount(namespace, nameLabels.Name()); err != nil {
return err
}
if err := client.DeleteClusterRole(nameLabels.Name()); err != nil {
return err
}
if err := client.DeleteClusterRoleBinding(nameLabels.Name()); err != nil {
return err
}
if !gitops {
if err := client.DeleteCRDResource("apiextensions.k8s.io", "v1beta1", "CustomResourceDefinition", "zitadels.caos.ch"); err != nil {
return err
}
}
if err := client.DeleteDeployment(namespace, nameLabels.Name()); err != nil {
return err
}
return nil
}
func ScaleZitadelOperator(
monitor mntr.Monitor,
@@ -312,7 +354,16 @@ func ScaleZitadelOperator(
replicaCount int,
) error {
monitor.Debug("Scaling zitadel-operator")
return client.ScaleDeployment("caos-system", "zitadel-operator", replicaCount)
return client.ScaleDeployment(namespace, "zitadel-operator", replicaCount)
}
func ScaleDatabaseOperator(
monitor mntr.Monitor,
client *kubernetes.Client,
replicaCount int,
) error {
monitor.Debug("Scaling database-operator")
return client.ScaleDeployment(namespace, "database-operator", replicaCount)
}
func int32Ptr(i int32) *int32 { return &i }
@@ -322,6 +373,10 @@ func toNameLabels(apiLabels *labels.API, operatorName string) *labels.Name {
return labels.MustForName(labels.MustForComponent(apiLabels, "operator"), operatorName)
}
func getDatabaseOperatorNameLabels(apiLabels *labels.API) *labels.Name {
return toNameLabels(apiLabels, "database-operator")
}
func EnsureDatabaseArtifacts(
monitor mntr.Monitor,
apiLabels *labels.API,
@@ -345,8 +400,8 @@ func EnsureDatabaseArtifacts(
if err := client.ApplyServiceAccount(&core.ServiceAccount{
ObjectMeta: mach.ObjectMeta{
Name: "database-operator",
Namespace: "caos-system",
Name: nameLabels.Name(),
Namespace: namespace,
Labels: k8sNameLabels,
},
}); err != nil {
@@ -355,7 +410,7 @@ func EnsureDatabaseArtifacts(
if err := client.ApplyClusterRole(&rbac.ClusterRole{
ObjectMeta: mach.ObjectMeta{
Name: "database-operator-clusterrole",
Name: nameLabels.Name(),
Labels: k8sNameLabels,
},
Rules: []rbac.PolicyRule{{
@@ -369,19 +424,19 @@ func EnsureDatabaseArtifacts(
if err := client.ApplyClusterRoleBinding(&rbac.ClusterRoleBinding{
ObjectMeta: mach.ObjectMeta{
Name: "database-operator-clusterrolebinding",
Name: nameLabels.Name(),
Labels: k8sNameLabels,
},
RoleRef: rbac.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "ClusterRole",
Name: "database-operator-clusterrole",
Name: nameLabels.Name(),
},
Subjects: []rbac.Subject{{
Kind: "ServiceAccount",
Name: "database-operator",
Namespace: "caos-system",
Name: nameLabels.Name(),
Namespace: namespace,
}},
}); err != nil {
return err
@@ -548,8 +603,8 @@ status:
deployment := &apps.Deployment{
ObjectMeta: mach.ObjectMeta{
Name: "database-operator",
Namespace: "caos-system",
Name: nameLabels.Name(),
Namespace: namespace,
Labels: k8sNameLabels,
},
Spec: apps.DeploymentSpec{
@@ -562,7 +617,7 @@ status:
Labels: labels.MustK8sMap(labels.AsSelectable(nameLabels)),
},
Spec: core.PodSpec{
ServiceAccountName: "database-operator",
ServiceAccountName: nameLabels.Name(),
Containers: []core.Container{{
Name: "database",
ImagePullPolicy: core.PullIfNotPresent,
@@ -604,3 +659,38 @@ status:
return nil
}
func DestroyDatabaseOperator(
monitor mntr.Monitor,
apiLabels *labels.API,
client kubernetes.ClientInt,
gitops bool,
) error {
nameLabels := getDatabaseOperatorNameLabels(apiLabels)
monitor.WithFields(map[string]interface{}{}).Debug("Destroying database artifacts")
if err := client.DeleteServiceAccount(namespace, nameLabels.Name()); err != nil {
return err
}
if err := client.DeleteClusterRole(nameLabels.Name()); err != nil {
return err
}
if err := client.DeleteClusterRoleBinding(nameLabels.Name()); err != nil {
return err
}
if !gitops {
if err := client.DeleteCRDResource("apiextensions.k8s.io", "v1beta1", "CustomResourceDefinition", "databases.caos.ch"); err != nil {
return err
}
}
if err := client.DeleteDeployment(namespace, nameLabels.Name()); err != nil {
return err
}
return nil
}