From 009062c51a400c49bbfc7cc54c1535d04af59738 Mon Sep 17 00:00:00 2001 From: ej Date: Thu, 26 Sep 2024 19:01:49 +0000 Subject: [PATCH] Correction statefulset ; uninstall par modules --- src/cmd/args.go | 3 +- src/cmd/uninstallCmd.go | 10 ++-- src/install/uninstall.go | 21 +++---- src/kubectl/deployment.go | 2 +- src/kubectl/object.go | 3 +- src/kubectl/stateful_test.go | 72 ++++++++++++++--------- src/kubectl/statefulset.go | 86 +++++++++++++-------------- test/kubectl/mongo_pending.json | 100 ++++++++++++++++++++++++++++++++ 8 files changed, 208 insertions(+), 89 deletions(-) create mode 100644 test/kubectl/mongo_pending.json diff --git a/src/cmd/args.go b/src/cmd/args.go index d15812d..28ecaaf 100644 --- a/src/cmd/args.go +++ b/src/cmd/args.go @@ -39,7 +39,7 @@ func cobraUninstallCmd() *cobra.Command{ Args: cobra.MaximumNArgs(0), RunE: func(cmd *cobra.Command, args []string) error { log.Log().Info().Msg("oc-deploy :") - return UninstallCmd(context) + return UninstallCmd(context, modules) }, Example: "oc-deploy uninstall --context ex1", } @@ -89,6 +89,7 @@ func Execute() { cmdInstall.Flags().StringArrayVarP(&modules, "modules", "m", []string{}, "modules, ...") cmdUninstall.Flags().StringVarP(&context, "context", "c", "opencloud", "Nom du context") + cmdUninstall.Flags().StringArrayVarP(&modules, "modules", "m", []string{}, "modules, ...") cmdGenerate.Flags().StringVarP(&context, "context", "c", "opencloud", "Nom du context") cmdGenerate.Flags().StringVarP(&version, "version", "v", "latest", "Version") diff --git a/src/cmd/uninstallCmd.go b/src/cmd/uninstallCmd.go index f47f9db..7189456 100644 --- a/src/cmd/uninstallCmd.go +++ b/src/cmd/uninstallCmd.go @@ -2,19 +2,19 @@ package cmd import ( "fmt" - // "strings" - // "github.com/spf13/cobra" log "oc-deploy/log_wrapper" - // "oc-deploy/versionOc" "oc-deploy/install" ) -func UninstallCmd(context string) error { +func UninstallCmd(context string, modules []string) error { log.Log().Info().Msg("Uninstall >> ") log.Log().Info().Msg(" << Contexte : " + context) + if len(modules) > 0 { + log.Log().Info().Msg(fmt.Sprintf(" << Modules : %s", modules)) + } workspace := fmt.Sprintf("workspace_%s", context) obj := install.InstallClass{Workspace: workspace} @@ -38,7 +38,7 @@ func UninstallCmd(context string) error { log.Log().Fatal().Msg(" >> " + err.Error()) } - err = obj.UninstallCharts() + err = obj.UninstallCharts(modules) if err != nil { log.Log().Fatal().Msg(" >> " + err.Error()) } diff --git a/src/install/uninstall.go b/src/install/uninstall.go index 0872933..db146ef 100644 --- a/src/install/uninstall.go +++ b/src/install/uninstall.go @@ -6,6 +6,7 @@ import ( "sync" log "oc-deploy/log_wrapper" + "oc-deploy/utils" "oc-deploy/versionOc" "oc-deploy/tool" "oc-deploy/chart" @@ -39,20 +40,23 @@ func (this *InstallClass) NewUninstall() (string, error) { return dst, nil } -func (this *InstallClass) UninstallCharts() (error) { +func (this *InstallClass) UninstallCharts(modules []string) (error) { helm_bin, _ := this.getToolBin("helm") kubectl_bin, _ := this.getToolBin("kubectl") var wg sync.WaitGroup for _, v := range this.charts { - for _, v1 := range v.Charts { - wg.Add(1) - go func() { - defer wg.Done() - this.uninstallChart(helm_bin, kubectl_bin, v1) - } () + for _, v1 := range v.Charts { + if len(modules) == 0 || utils.StringInSlice(v1.Name, modules) { + wg.Add(1) + + go func() { + defer wg.Done() + this.uninstallChart(helm_bin, kubectl_bin, v1) + } () + } } } wg.Wait() @@ -69,9 +73,6 @@ func (this *InstallClass) uninstallChart(helm_bin string, kubectl_bin string, ch data := helm.HelmChart{Name: chart.Name} - // helmchart := helm.HelmChart{Bin: helm_bin, - // Name: chart.Name} - res, err := helm_cmd.ChartUninstall(data) if err != nil { log.Log().Error().Msg(fmt.Sprintf(" >> %s %s (%s)", data.Name, "KO", err)) diff --git a/src/kubectl/deployment.go b/src/kubectl/deployment.go index 4198471..2a013b9 100644 --- a/src/kubectl/deployment.go +++ b/src/kubectl/deployment.go @@ -34,7 +34,7 @@ func (this KubectlCommand) getDeployment(data KubectlObject) (map[string]any, er m["name"] = data.Name m["kind"] = kind m["replicas"] = status.Replicas - m["UnavailableReplicas"] = status.UnavailableReplicas + m["unavailableReplicas"] = status.UnavailableReplicas return m, nil } diff --git a/src/kubectl/object.go b/src/kubectl/object.go index aba7c5e..dfb0ab6 100644 --- a/src/kubectl/object.go +++ b/src/kubectl/object.go @@ -22,6 +22,7 @@ type getOutput struct { type getStatusOutput struct { Replicas int `json:"replicas"` UnavailableReplicas int `json:"unavailableReplicas"` + AvailableReplicas int `json:"availableReplicas"` } func (this KubectlCommand) Get(data KubectlObject) (map[string]any, error) { @@ -43,7 +44,7 @@ func (this KubectlCommand) Wait(data KubectlObject) (error) { if err != nil { return err } - ko := m["UnavailableReplicas"].(int) + ko := m["unavailableReplicas"].(int) if ko == 0 { return nil } diff --git a/src/kubectl/stateful_test.go b/src/kubectl/stateful_test.go index bd16b0d..5f51093 100644 --- a/src/kubectl/stateful_test.go +++ b/src/kubectl/stateful_test.go @@ -1,28 +1,44 @@ -package kubectl - -import ( - "os" - "path/filepath" - - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestKubectStatefulset(t *testing.T) { - - fileName := filepath.Join(TEST_SRC_DIR, "statefulset.json") - cmd_json, _ := os.ReadFile(fileName) - - cmd := getCmdKubectl(true, string(cmd_json)) - - - data := KubectlObject{Name: "dep1", Kind: "Statefulset"} - - res, err := cmd.getDeployment(data) - - // map[string]interface {}(map[string]interface {}{"UnavailableReplicas":0, "kind":"StatefulSet", "name":"dep1", "replicas":1}) - assert.Nilf(t, err, "error message %s", err) - assert.Equal(t, "StatefulSet", res["kind"], "TestKubectDeployment error") - assert.Equal(t, 1, res["replicas"], "TestKubectDeployment error") -} +package kubectl + +import ( + "os" + "path/filepath" + + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestKubectStatefulset(t *testing.T) { + + fileName := filepath.Join(TEST_SRC_DIR, "statefulset.json") + cmd_json, _ := os.ReadFile(fileName) + + cmd := getCmdKubectl(true, string(cmd_json)) + + + data := KubectlObject{Name: "dep1", Kind: "Statefulset"} + + res, err := cmd.getStatefulSet(data) + + assert.Nilf(t, err, "error message %s", err) + assert.Equal(t, "StatefulSet", res["kind"], "TestKubectStatefulset error") + assert.Equal(t, 1, res["replicas"], "TestKubectStatefulset error") +} + +func TestKubectStatefulsetPending(t *testing.T) { + + fileName := filepath.Join(TEST_SRC_DIR, "mongo_pending.json") + cmd_json, _ := os.ReadFile(fileName) + + cmd := getCmdKubectl(true, string(cmd_json)) + + data := KubectlObject{Name: "dep1", Kind: "Statefulset"} + + res, err := cmd.getStatefulSet(data) + + assert.Nilf(t, err, "error message %s", err) + assert.Equal(t, 1, res["unavailableReplicas"], "TestKubectStatefulsetPending error") + assert.Equal(t, "StatefulSet", res["kind"], "TestKubectStatefulsetPending error") + assert.Equal(t, 1, res["replicas"], "TestKubectStatefulsetPending error") +} diff --git a/src/kubectl/statefulset.go b/src/kubectl/statefulset.go index c6399a6..93702c7 100644 --- a/src/kubectl/statefulset.go +++ b/src/kubectl/statefulset.go @@ -1,43 +1,43 @@ -package kubectl - -import ( - "fmt" - "strings" - "errors" - "encoding/json" - - log "oc-deploy/log_wrapper" -) - -func (this KubectlCommand) getStatefulSet(data KubectlObject) (map[string]any, error) { - - bin := this.Bin - name := data.Name - - msg := fmt.Sprintf("%s get statefulset %s -o json", bin, name) - log.Log().Debug().Msg(msg) - - m := make(map[string]any) - - cmd_args := strings.Split(msg, " ") - cmd := this.Exec(cmd_args[0], cmd_args[1:]...) - stdout, err := cmd.CombinedOutput() - if err != nil { - return m, errors.New(string(stdout)) - } - - var objmap getOutput - - json.Unmarshal(stdout, &objmap) - - kind := objmap.Kind - status := objmap.Status - - m["name"] = name - m["kind"] = kind - m["replicas"] = status.Replicas - m["UnavailableReplicas"] = status.UnavailableReplicas - - return m, nil -} - +package kubectl + +import ( + "fmt" + "strings" + "errors" + "encoding/json" + + log "oc-deploy/log_wrapper" +) + +func (this KubectlCommand) getStatefulSet(data KubectlObject) (map[string]any, error) { + + bin := this.Bin + name := data.Name + + msg := fmt.Sprintf("%s get statefulset %s -o json", bin, name) + log.Log().Debug().Msg(msg) + + m := make(map[string]any) + + cmd_args := strings.Split(msg, " ") + cmd := this.Exec(cmd_args[0], cmd_args[1:]...) + stdout, err := cmd.CombinedOutput() + if err != nil { + return m, errors.New(string(stdout)) + } + + var objmap getOutput + + json.Unmarshal(stdout, &objmap) + + kind := objmap.Kind + status := objmap.Status + + m["name"] = name + m["kind"] = kind + m["replicas"] = status.Replicas + m["unavailableReplicas"] = status.Replicas - status.AvailableReplicas + + return m, nil +} + diff --git a/test/kubectl/mongo_pending.json b/test/kubectl/mongo_pending.json new file mode 100644 index 0000000..2ac5b8a --- /dev/null +++ b/test/kubectl/mongo_pending.json @@ -0,0 +1,100 @@ +{ + "apiVersion": "apps/v1", + "kind": "StatefulSet", + "metadata": { + "annotations": { + "meta.helm.sh/release-name": "oc-mongo", + "meta.helm.sh/release-namespace": "default" + }, + "creationTimestamp": "2024-09-26T07:48:00Z", + "generation": 1, + "labels": { + "app": "mongo", + "app.kubernetes.io/managed-by": "Helm" + }, + "name": "mongo", + "namespace": "default", + "resourceVersion": "13762492", + "uid": "9a354ea1-f91f-4b4e-9a8c-622dde93e448" + }, + "spec": { + "persistentVolumeClaimRetentionPolicy": { + "whenDeleted": "Retain", + "whenScaled": "Retain" + }, + "podManagementPolicy": "OrderedReady", + "replicas": 1, + "revisionHistoryLimit": 10, + "selector": { + "matchLabels": { + "app": "mongo" + } + }, + "serviceName": "mongo", + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "app": "mongo" + } + }, + "spec": { + "containers": [ + { + "image": "harbor.dtf/mongo:v0.1.0", + "imagePullPolicy": "IfNotPresent", + "name": "mongo", + "ports": [ + { + "containerPort": 27017, + "protocol": "TCP" + } + ], + "resources": {}, + "terminationMessagePath": "/dev/termination-log", + "terminationMessagePolicy": "File", + "volumeMounts": [ + { + "mountPath": "/data/db", + "name": "mongo-persistent-storage" + }, + { + "mountPath": "/data/configdb", + "name": "mongo-persistent-storage" + } + ] + } + ], + "dnsPolicy": "ClusterFirst", + "restartPolicy": "Always", + "schedulerName": "default-scheduler", + "securityContext": {}, + "terminationGracePeriodSeconds": 30, + "volumes": [ + { + "name": "mongo-persistent-storage", + "persistentVolumeClaim": { + "claimName": "mongo-pvc-helm" + } + } + ] + } + }, + "updateStrategy": { + "rollingUpdate": { + "partition": 0 + }, + "type": "RollingUpdate" + } + }, + "status": { + "availableReplicas": 0, + "collisionCount": 0, + "currentReplicas": 1, + "currentRevision": "mongo-7989cd65cb", + "observedGeneration": 1, + "replicas": 1, + "updateRevision": "mongo-7989cd65cb", + "updatedReplicas": 1 + } +}