Correction statefulset ; uninstall par modules

This commit is contained in:
ej 2024-09-26 19:01:49 +00:00
parent 1b9b79c67f
commit 009062c51a
8 changed files with 208 additions and 89 deletions

View File

@ -39,7 +39,7 @@ func cobraUninstallCmd() *cobra.Command{
Args: cobra.MaximumNArgs(0), Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
log.Log().Info().Msg("oc-deploy :") log.Log().Info().Msg("oc-deploy :")
return UninstallCmd(context) return UninstallCmd(context, modules)
}, },
Example: "oc-deploy uninstall --context ex1", Example: "oc-deploy uninstall --context ex1",
} }
@ -89,6 +89,7 @@ func Execute() {
cmdInstall.Flags().StringArrayVarP(&modules, "modules", "m", []string{}, "modules, ...") cmdInstall.Flags().StringArrayVarP(&modules, "modules", "m", []string{}, "modules, ...")
cmdUninstall.Flags().StringVarP(&context, "context", "c", "opencloud", "Nom du context") 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(&context, "context", "c", "opencloud", "Nom du context")
cmdGenerate.Flags().StringVarP(&version, "version", "v", "latest", "Version") cmdGenerate.Flags().StringVarP(&version, "version", "v", "latest", "Version")

View File

@ -2,19 +2,19 @@ package cmd
import ( import (
"fmt" "fmt"
// "strings"
// "github.com/spf13/cobra"
log "oc-deploy/log_wrapper" log "oc-deploy/log_wrapper"
// "oc-deploy/versionOc"
"oc-deploy/install" "oc-deploy/install"
) )
func UninstallCmd(context string) error { func UninstallCmd(context string, modules []string) error {
log.Log().Info().Msg("Uninstall >> ") log.Log().Info().Msg("Uninstall >> ")
log.Log().Info().Msg(" << Contexte : " + context) 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) workspace := fmt.Sprintf("workspace_%s", context)
obj := install.InstallClass{Workspace: workspace} obj := install.InstallClass{Workspace: workspace}
@ -38,7 +38,7 @@ func UninstallCmd(context string) error {
log.Log().Fatal().Msg(" >> " + err.Error()) log.Log().Fatal().Msg(" >> " + err.Error())
} }
err = obj.UninstallCharts() err = obj.UninstallCharts(modules)
if err != nil { if err != nil {
log.Log().Fatal().Msg(" >> " + err.Error()) log.Log().Fatal().Msg(" >> " + err.Error())
} }

View File

@ -6,6 +6,7 @@ import (
"sync" "sync"
log "oc-deploy/log_wrapper" log "oc-deploy/log_wrapper"
"oc-deploy/utils"
"oc-deploy/versionOc" "oc-deploy/versionOc"
"oc-deploy/tool" "oc-deploy/tool"
"oc-deploy/chart" "oc-deploy/chart"
@ -39,14 +40,16 @@ func (this *InstallClass) NewUninstall() (string, error) {
return dst, nil return dst, nil
} }
func (this *InstallClass) UninstallCharts() (error) { func (this *InstallClass) UninstallCharts(modules []string) (error) {
helm_bin, _ := this.getToolBin("helm") helm_bin, _ := this.getToolBin("helm")
kubectl_bin, _ := this.getToolBin("kubectl") kubectl_bin, _ := this.getToolBin("kubectl")
var wg sync.WaitGroup var wg sync.WaitGroup
for _, v := range this.charts { for _, v := range this.charts {
for _, v1 := range v.Charts { for _, v1 := range v.Charts {
if len(modules) == 0 || utils.StringInSlice(v1.Name, modules) {
wg.Add(1) wg.Add(1)
go func() { go func() {
@ -55,6 +58,7 @@ func (this *InstallClass) UninstallCharts() (error) {
} () } ()
} }
} }
}
wg.Wait() wg.Wait()
return nil return nil
} }
@ -69,9 +73,6 @@ func (this *InstallClass) uninstallChart(helm_bin string, kubectl_bin string, ch
data := helm.HelmChart{Name: chart.Name} data := helm.HelmChart{Name: chart.Name}
// helmchart := helm.HelmChart{Bin: helm_bin,
// Name: chart.Name}
res, err := helm_cmd.ChartUninstall(data) res, err := helm_cmd.ChartUninstall(data)
if err != nil { if err != nil {
log.Log().Error().Msg(fmt.Sprintf(" >> %s %s (%s)", data.Name, "KO", err)) log.Log().Error().Msg(fmt.Sprintf(" >> %s %s (%s)", data.Name, "KO", err))

View File

@ -34,7 +34,7 @@ func (this KubectlCommand) getDeployment(data KubectlObject) (map[string]any, er
m["name"] = data.Name m["name"] = data.Name
m["kind"] = kind m["kind"] = kind
m["replicas"] = status.Replicas m["replicas"] = status.Replicas
m["UnavailableReplicas"] = status.UnavailableReplicas m["unavailableReplicas"] = status.UnavailableReplicas
return m, nil return m, nil
} }

View File

@ -22,6 +22,7 @@ type getOutput struct {
type getStatusOutput struct { type getStatusOutput struct {
Replicas int `json:"replicas"` Replicas int `json:"replicas"`
UnavailableReplicas int `json:"unavailableReplicas"` UnavailableReplicas int `json:"unavailableReplicas"`
AvailableReplicas int `json:"availableReplicas"`
} }
func (this KubectlCommand) Get(data KubectlObject) (map[string]any, error) { func (this KubectlCommand) Get(data KubectlObject) (map[string]any, error) {
@ -43,7 +44,7 @@ func (this KubectlCommand) Wait(data KubectlObject) (error) {
if err != nil { if err != nil {
return err return err
} }
ko := m["UnavailableReplicas"].(int) ko := m["unavailableReplicas"].(int)
if ko == 0 { if ko == 0 {
return nil return nil
} }

View File

@ -19,10 +19,26 @@ func TestKubectStatefulset(t *testing.T) {
data := KubectlObject{Name: "dep1", Kind: "Statefulset"} data := KubectlObject{Name: "dep1", Kind: "Statefulset"}
res, err := cmd.getDeployment(data) res, err := cmd.getStatefulSet(data)
// map[string]interface {}(map[string]interface {}{"UnavailableReplicas":0, "kind":"StatefulSet", "name":"dep1", "replicas":1})
assert.Nilf(t, err, "error message %s", err) assert.Nilf(t, err, "error message %s", err)
assert.Equal(t, "StatefulSet", res["kind"], "TestKubectDeployment error") assert.Equal(t, "StatefulSet", res["kind"], "TestKubectStatefulset error")
assert.Equal(t, 1, res["replicas"], "TestKubectDeployment 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")
} }

View File

@ -36,7 +36,7 @@ func (this KubectlCommand) getStatefulSet(data KubectlObject) (map[string]any, e
m["name"] = name m["name"] = name
m["kind"] = kind m["kind"] = kind
m["replicas"] = status.Replicas m["replicas"] = status.Replicas
m["UnavailableReplicas"] = status.UnavailableReplicas m["unavailableReplicas"] = status.Replicas - status.AvailableReplicas
return m, nil return m, nil
} }

View File

@ -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
}
}