From 8e2f178036ba0ce1cb5d3d9f5e499c0b55c152f4 Mon Sep 17 00:00:00 2001 From: lizonglingo Date: Sun, 6 Aug 2023 00:06:59 +0800 Subject: [PATCH 1/5] Feat: delete the sync Deployment after deploy complete Signed-off-by: lizonglingo --- pkg/controllers/client.go | 160 +++++++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 2 deletions(-) diff --git a/pkg/controllers/client.go b/pkg/controllers/client.go index dd640f8a..f73672a6 100644 --- a/pkg/controllers/client.go +++ b/pkg/controllers/client.go @@ -4,16 +4,28 @@ import ( "bytes" "context" "fmt" + "strings" + "time" - "github.com/opencurve/curve-operator/pkg/daemon" - "github.com/opencurve/curve-operator/pkg/k8sutil" "github.com/pkg/errors" apps "k8s.io/api/apps/v1" + batch "k8s.io/api/batch/v1" v1 "k8s.io/api/core/v1" kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/tools/remotecommand" + + "github.com/opencurve/curve-operator/pkg/chunkserver" + "github.com/opencurve/curve-operator/pkg/config" + "github.com/opencurve/curve-operator/pkg/daemon" + "github.com/opencurve/curve-operator/pkg/etcd" + "github.com/opencurve/curve-operator/pkg/k8sutil" + "github.com/opencurve/curve-operator/pkg/mds" + "github.com/opencurve/curve-operator/pkg/metaserver" + "github.com/opencurve/curve-operator/pkg/monitor" + "github.com/opencurve/curve-operator/pkg/snapshotclone" + "github.com/opencurve/curve-operator/pkg/topology" ) const ( @@ -86,6 +98,10 @@ func createSyncDeployment(c *daemon.Cluster) error { return err } } + + // delete the SyncConfigDeployment after the cluster is deployed. + go deleteSyncDeployment(c, newDeployment.GetName()) + // update condition type and phase etc. return nil } @@ -154,3 +170,143 @@ func getReadConfigJobLabel(c *daemon.Cluster) map[string]string { labels["curve"] = c.Kind return labels } + +// deleteSyncDeployment delete the SyncConfigDeployment after the cluster is deployed. +func deleteSyncDeployment(c *daemon.Cluster, deployName string) { + + time.Sleep(1 * time.Minute) + + clusterKind := c.Kind + nodesCount := len(c.Nodes) + chunkServerCount := len(c.Chunkserver.Nodes) * len(c.Chunkserver.Devices) + + if clusterKind == config.KIND_CURVEBS { + logger.Infof("node count is %d, wanted chunk server count is %d", nodesCount, chunkServerCount) + } + + checkTicker := time.NewTicker(30 * time.Second) + for { + isAllReady := true + chunkSrvReady, etcdReady, mdsReady, metaSrvReady, snapShotCloneReady, prometheusReady, grafanaReady, nodeExporterReady := + 0, 0, 0, 0, 0, 0, 0, 0 + + jobPreChunkFileCompleted, jobProPhysicalPoolCompleted, jobProLogicPoolCompleted := 0, 0, 0 + + deploymentList, err := c.Context.Clientset.AppsV1().Deployments(c.Namespace).List(metav1.ListOptions{}) + if err != nil { + logger.Errorf("failed to list deployment in namespace %s for delete curve-sync-config", c.Namespace) + } + + jobs, err := c.Context.Clientset.BatchV1().Jobs(c.Namespace).List(metav1.ListOptions{}) + if err != nil { + logger.Errorf("failed to list jobs in namespace %s for delete curve-sync-config", c.Namespace) + } + + for _, job := range jobs.Items { + switch { + case strings.HasPrefix(job.Name, topology.JOB_PYHSICAL_POOL): + if isJobCompleted(job) { + jobProPhysicalPoolCompleted++ + } + case strings.HasPrefix(job.Name, topology.JOB_LOGICAL_POOL): + if isJobCompleted(job) { + jobProLogicPoolCompleted++ + } + case strings.HasPrefix(job.Name, chunkserver.PrepareJobName): + if isJobCompleted(job) { + jobPreChunkFileCompleted++ + } + } + } + + for _, deploy := range deploymentList.Items { + switch { + case strings.HasPrefix(deploy.Name, chunkserver.AppName): + if isAllReplicasReady(deploy) { + chunkSrvReady++ + } + case strings.HasPrefix(deploy.Name, etcd.AppName): + if isAllReplicasReady(deploy) { + etcdReady++ + } + case strings.HasPrefix(deploy.Name, monitor.GrafanaAppName): + if isAllReplicasReady(deploy) { + grafanaReady++ + } + case strings.HasPrefix(deploy.Name, mds.AppName): + if isAllReplicasReady(deploy) { + mdsReady++ + } + case strings.HasPrefix(deploy.Name, metaserver.AppName): + if isAllReplicasReady(deploy) { + metaSrvReady++ + } + case strings.HasPrefix(deploy.Name, monitor.PromAppName): + if isAllReplicasReady(deploy) { + prometheusReady++ + } + case strings.HasPrefix(deploy.Name, snapshotclone.AppName): + if isAllReplicasReady(deploy) { + snapShotCloneReady++ + } + case strings.HasPrefix(deploy.Name, monitor.NodeExporterAppName): + if isAllReplicasReady(deploy) { + nodeExporterReady++ + } + } + } + + if c.SnapShotClone.Enable { + if snapShotCloneReady != nodesCount { + isAllReady = false + } + } + + if c.Monitor.Enable { + if grafanaReady == 0 || prometheusReady == 0 || nodeExporterReady != nodesCount { + isAllReady = false + } + } + + if clusterKind == config.KIND_CURVEBS && + (chunkSrvReady != chunkServerCount || jobPreChunkFileCompleted != chunkServerCount || + jobProLogicPoolCompleted == 0 || jobProPhysicalPoolCompleted == 0) { + isAllReady = false + } + + if clusterKind == config.KIND_CURVEFS && + (metaSrvReady != nodesCount || jobProLogicPoolCompleted == 0) { + isAllReady = false + } + + if etcdReady != nodesCount || mdsReady != nodesCount { + isAllReady = false + } + + if isAllReady { + break + } + <-checkTicker.C + } + + err := c.Context.Clientset.AppsV1().Deployments(c.Namespace).Delete(deployName, &metav1.DeleteOptions{}) + if err != nil { + logger.Errorf("failed to delete deployment about \"curve-sync-config\", error: %s", err) + } + + logger.Infof("cluster is deployed, deployment about \"curve-sync-config\" will be deleted") +} + +func isAllReplicasReady(deployment apps.Deployment) bool { + if deployment.Status.Replicas == deployment.Status.ReadyReplicas { + return true + } + return false +} + +func isJobCompleted(job batch.Job) bool { + if *job.Spec.Completions == job.Status.Succeeded { + return true + } + return false +} From a554418315f6223da3dbe58075c750d37ae005df Mon Sep 17 00:00:00 2001 From: lizonglingo Date: Sun, 6 Aug 2023 18:11:27 +0800 Subject: [PATCH 2/5] Style: reconstruct code style Signed-off-by: lizonglingo --- pkg/controllers/client.go | 140 +++++++++++++++++++++----------------- 1 file changed, 79 insertions(+), 61 deletions(-) diff --git a/pkg/controllers/client.go b/pkg/controllers/client.go index f73672a6..70a90648 100644 --- a/pkg/controllers/client.go +++ b/pkg/controllers/client.go @@ -100,7 +100,7 @@ func createSyncDeployment(c *daemon.Cluster) error { } // delete the SyncConfigDeployment after the cluster is deployed. - go deleteSyncDeployment(c, newDeployment.GetName()) + go deleteSyncConfigDeployment(c, newDeployment.GetName()) // update condition type and phase etc. return nil @@ -171,27 +171,42 @@ func getReadConfigJobLabel(c *daemon.Cluster) map[string]string { return labels } +type checkClusterDeployedInfo struct { + mdsReady int + etcdReady int + + metaServerReady int + chunkServerReady int + snapShotCloneReady int + + grafanaReady int + prometheusReady int + nodeExporterReady int + + jobPreChunkFileCompleted int + jobProLogicPoolCompleted int + jobProPhysicalPoolCompleted int +} + // deleteSyncDeployment delete the SyncConfigDeployment after the cluster is deployed. -func deleteSyncDeployment(c *daemon.Cluster, deployName string) { +func deleteSyncConfigDeployment(c *daemon.Cluster, syncConfigDeployment string) { - time.Sleep(1 * time.Minute) + wantChunkServer := len(c.Chunkserver.Devices) * len(c.Chunkserver.Nodes) + nodeCount := len(c.Nodes) - clusterKind := c.Kind - nodesCount := len(c.Nodes) - chunkServerCount := len(c.Chunkserver.Nodes) * len(c.Chunkserver.Devices) + time.Sleep(1 * time.Minute) - if clusterKind == config.KIND_CURVEBS { - logger.Infof("node count is %d, wanted chunk server count is %d", nodesCount, chunkServerCount) + if c.Kind == config.KIND_CURVEBS { + logger.Debugf("node count is %d, wanted chunk server count is %d", nodeCount, wantChunkServer) + } else if c.Kind == config.KIND_CURVEFS { + logger.Debugf("node count is %d", nodeCount) } checkTicker := time.NewTicker(30 * time.Second) - for { - isAllReady := true - chunkSrvReady, etcdReady, mdsReady, metaSrvReady, snapShotCloneReady, prometheusReady, grafanaReady, nodeExporterReady := - 0, 0, 0, 0, 0, 0, 0, 0 - - jobPreChunkFileCompleted, jobProPhysicalPoolCompleted, jobProLogicPoolCompleted := 0, 0, 0 + for { + isAllReadyOrCompleted := true + info := &checkClusterDeployedInfo{} deploymentList, err := c.Context.Clientset.AppsV1().Deployments(c.Namespace).List(metav1.ListOptions{}) if err != nil { logger.Errorf("failed to list deployment in namespace %s for delete curve-sync-config", c.Namespace) @@ -202,94 +217,97 @@ func deleteSyncDeployment(c *daemon.Cluster, deployName string) { logger.Errorf("failed to list jobs in namespace %s for delete curve-sync-config", c.Namespace) } - for _, job := range jobs.Items { - switch { - case strings.HasPrefix(job.Name, topology.JOB_PYHSICAL_POOL): - if isJobCompleted(job) { - jobProPhysicalPoolCompleted++ - } - case strings.HasPrefix(job.Name, topology.JOB_LOGICAL_POOL): - if isJobCompleted(job) { - jobProLogicPoolCompleted++ - } - case strings.HasPrefix(job.Name, chunkserver.PrepareJobName): - if isJobCompleted(job) { - jobPreChunkFileCompleted++ - } - } - } - for _, deploy := range deploymentList.Items { switch { - case strings.HasPrefix(deploy.Name, chunkserver.AppName): - if isAllReplicasReady(deploy) { - chunkSrvReady++ - } case strings.HasPrefix(deploy.Name, etcd.AppName): if isAllReplicasReady(deploy) { - etcdReady++ + info.etcdReady++ } - case strings.HasPrefix(deploy.Name, monitor.GrafanaAppName): + case strings.HasPrefix(deploy.Name, mds.AppName): if isAllReplicasReady(deploy) { - grafanaReady++ + info.mdsReady++ } - case strings.HasPrefix(deploy.Name, mds.AppName): + case strings.HasPrefix(deploy.Name, chunkserver.AppName): if isAllReplicasReady(deploy) { - mdsReady++ + info.chunkServerReady++ } case strings.HasPrefix(deploy.Name, metaserver.AppName): if isAllReplicasReady(deploy) { - metaSrvReady++ + info.metaServerReady++ } - case strings.HasPrefix(deploy.Name, monitor.PromAppName): + case strings.HasPrefix(deploy.Name, snapshotclone.AppName): if isAllReplicasReady(deploy) { - prometheusReady++ + info.snapShotCloneReady++ } - case strings.HasPrefix(deploy.Name, snapshotclone.AppName): + case strings.HasPrefix(deploy.Name, monitor.GrafanaAppName): + if isAllReplicasReady(deploy) { + info.grafanaReady++ + } + case strings.HasPrefix(deploy.Name, monitor.PromAppName): if isAllReplicasReady(deploy) { - snapShotCloneReady++ + info.prometheusReady++ } case strings.HasPrefix(deploy.Name, monitor.NodeExporterAppName): if isAllReplicasReady(deploy) { - nodeExporterReady++ + info.nodeExporterReady++ + } + } + } + + for _, job := range jobs.Items { + switch { + case strings.HasPrefix(job.Name, topology.JOB_PYHSICAL_POOL): + if isJobCompleted(job) { + info.jobProPhysicalPoolCompleted++ + } + case strings.HasPrefix(job.Name, topology.JOB_LOGICAL_POOL): + if isJobCompleted(job) { + info.jobProLogicPoolCompleted++ + } + case strings.HasPrefix(job.Name, chunkserver.PrepareJobName): + if isJobCompleted(job) { + info.jobPreChunkFileCompleted++ } } } if c.SnapShotClone.Enable { - if snapShotCloneReady != nodesCount { - isAllReady = false + if info.snapShotCloneReady != nodeCount { + isAllReadyOrCompleted = false } } if c.Monitor.Enable { - if grafanaReady == 0 || prometheusReady == 0 || nodeExporterReady != nodesCount { - isAllReady = false + if info.grafanaReady == 0 || + info.prometheusReady == 0 || + info.nodeExporterReady != nodeCount { + isAllReadyOrCompleted = false } } - if clusterKind == config.KIND_CURVEBS && - (chunkSrvReady != chunkServerCount || jobPreChunkFileCompleted != chunkServerCount || - jobProLogicPoolCompleted == 0 || jobProPhysicalPoolCompleted == 0) { - isAllReady = false + if c.Kind == config.KIND_CURVEBS && (info.chunkServerReady != wantChunkServer || + info.jobPreChunkFileCompleted != wantChunkServer || + info.jobProLogicPoolCompleted == 0 || + info.jobProPhysicalPoolCompleted == 0) { + isAllReadyOrCompleted = false } - if clusterKind == config.KIND_CURVEFS && - (metaSrvReady != nodesCount || jobProLogicPoolCompleted == 0) { - isAllReady = false + if c.Kind == config.KIND_CURVEFS && + (info.metaServerReady != nodeCount || info.jobProLogicPoolCompleted == 0) { + isAllReadyOrCompleted = false } - if etcdReady != nodesCount || mdsReady != nodesCount { - isAllReady = false + if info.etcdReady != nodeCount || info.mdsReady != nodeCount { + isAllReadyOrCompleted = false } - if isAllReady { + if isAllReadyOrCompleted { break } <-checkTicker.C } - err := c.Context.Clientset.AppsV1().Deployments(c.Namespace).Delete(deployName, &metav1.DeleteOptions{}) + err := c.Context.Clientset.AppsV1().Deployments(c.Namespace).Delete(syncConfigDeployment, &metav1.DeleteOptions{}) if err != nil { logger.Errorf("failed to delete deployment about \"curve-sync-config\", error: %s", err) } From e61caeffc5aeafc39f6cd90a9c952817990e91d3 Mon Sep 17 00:00:00 2001 From: DKCBBB <40293922+lizonglingo@users.noreply.github.com> Date: Tue, 8 Aug 2023 00:40:38 +0800 Subject: [PATCH 3/5] Stype: update some annotation Signed-off-by: DKCBBB <40293922+lizonglingo@users.noreply.github.com> --- pkg/controllers/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/controllers/client.go b/pkg/controllers/client.go index 70a90648..93ad4aa4 100644 --- a/pkg/controllers/client.go +++ b/pkg/controllers/client.go @@ -188,7 +188,7 @@ type checkClusterDeployedInfo struct { jobProPhysicalPoolCompleted int } -// deleteSyncDeployment delete the SyncConfigDeployment after the cluster is deployed. +// deleteSyncConfigDeployment delete the SyncConfigDeployment after the cluster is deployed. func deleteSyncConfigDeployment(c *daemon.Cluster, syncConfigDeployment string) { wantChunkServer := len(c.Chunkserver.Devices) * len(c.Chunkserver.Nodes) From 1cf1d79d08c4cbda7a54db0095c95eeb20f05a79 Mon Sep 17 00:00:00 2001 From: lizonglingo Date: Wed, 16 Aug 2023 10:14:39 +0800 Subject: [PATCH 4/5] Fix: some changes based on PR reviews(#78) --- pkg/controllers/client.go | 176 +++---------------------------------- pkg/controllers/cluster.go | 17 ++++ 2 files changed, 29 insertions(+), 164 deletions(-) diff --git a/pkg/controllers/client.go b/pkg/controllers/client.go index 93ad4aa4..9cd54e57 100644 --- a/pkg/controllers/client.go +++ b/pkg/controllers/client.go @@ -4,28 +4,18 @@ import ( "bytes" "context" "fmt" - "strings" - "time" "github.com/pkg/errors" apps "k8s.io/api/apps/v1" - batch "k8s.io/api/batch/v1" v1 "k8s.io/api/core/v1" kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/tools/remotecommand" + "k8s.io/client-go/util/retry" - "github.com/opencurve/curve-operator/pkg/chunkserver" - "github.com/opencurve/curve-operator/pkg/config" "github.com/opencurve/curve-operator/pkg/daemon" - "github.com/opencurve/curve-operator/pkg/etcd" "github.com/opencurve/curve-operator/pkg/k8sutil" - "github.com/opencurve/curve-operator/pkg/mds" - "github.com/opencurve/curve-operator/pkg/metaserver" - "github.com/opencurve/curve-operator/pkg/monitor" - "github.com/opencurve/curve-operator/pkg/snapshotclone" - "github.com/opencurve/curve-operator/pkg/topology" ) const ( @@ -99,9 +89,6 @@ func createSyncDeployment(c *daemon.Cluster) error { } } - // delete the SyncConfigDeployment after the cluster is deployed. - go deleteSyncConfigDeployment(c, newDeployment.GetName()) - // update condition type and phase etc. return nil } @@ -171,160 +158,21 @@ func getReadConfigJobLabel(c *daemon.Cluster) map[string]string { return labels } -type checkClusterDeployedInfo struct { - mdsReady int - etcdReady int - - metaServerReady int - chunkServerReady int - snapShotCloneReady int - - grafanaReady int - prometheusReady int - nodeExporterReady int - - jobPreChunkFileCompleted int - jobProLogicPoolCompleted int - jobProPhysicalPoolCompleted int -} - // deleteSyncConfigDeployment delete the SyncConfigDeployment after the cluster is deployed. -func deleteSyncConfigDeployment(c *daemon.Cluster, syncConfigDeployment string) { - - wantChunkServer := len(c.Chunkserver.Devices) * len(c.Chunkserver.Nodes) - nodeCount := len(c.Nodes) - - time.Sleep(1 * time.Minute) - - if c.Kind == config.KIND_CURVEBS { - logger.Debugf("node count is %d, wanted chunk server count is %d", nodeCount, wantChunkServer) - } else if c.Kind == config.KIND_CURVEFS { - logger.Debugf("node count is %d", nodeCount) - } - - checkTicker := time.NewTicker(30 * time.Second) - - for { - isAllReadyOrCompleted := true - info := &checkClusterDeployedInfo{} - deploymentList, err := c.Context.Clientset.AppsV1().Deployments(c.Namespace).List(metav1.ListOptions{}) - if err != nil { - logger.Errorf("failed to list deployment in namespace %s for delete curve-sync-config", c.Namespace) - } - - jobs, err := c.Context.Clientset.BatchV1().Jobs(c.Namespace).List(metav1.ListOptions{}) - if err != nil { - logger.Errorf("failed to list jobs in namespace %s for delete curve-sync-config", c.Namespace) - } - - for _, deploy := range deploymentList.Items { - switch { - case strings.HasPrefix(deploy.Name, etcd.AppName): - if isAllReplicasReady(deploy) { - info.etcdReady++ - } - case strings.HasPrefix(deploy.Name, mds.AppName): - if isAllReplicasReady(deploy) { - info.mdsReady++ - } - case strings.HasPrefix(deploy.Name, chunkserver.AppName): - if isAllReplicasReady(deploy) { - info.chunkServerReady++ - } - case strings.HasPrefix(deploy.Name, metaserver.AppName): - if isAllReplicasReady(deploy) { - info.metaServerReady++ - } - case strings.HasPrefix(deploy.Name, snapshotclone.AppName): - if isAllReplicasReady(deploy) { - info.snapShotCloneReady++ - } - case strings.HasPrefix(deploy.Name, monitor.GrafanaAppName): - if isAllReplicasReady(deploy) { - info.grafanaReady++ - } - case strings.HasPrefix(deploy.Name, monitor.PromAppName): - if isAllReplicasReady(deploy) { - info.prometheusReady++ - } - case strings.HasPrefix(deploy.Name, monitor.NodeExporterAppName): - if isAllReplicasReady(deploy) { - info.nodeExporterReady++ - } - } - } - - for _, job := range jobs.Items { - switch { - case strings.HasPrefix(job.Name, topology.JOB_PYHSICAL_POOL): - if isJobCompleted(job) { - info.jobProPhysicalPoolCompleted++ - } - case strings.HasPrefix(job.Name, topology.JOB_LOGICAL_POOL): - if isJobCompleted(job) { - info.jobProLogicPoolCompleted++ - } - case strings.HasPrefix(job.Name, chunkserver.PrepareJobName): - if isJobCompleted(job) { - info.jobPreChunkFileCompleted++ - } - } - } - - if c.SnapShotClone.Enable { - if info.snapShotCloneReady != nodeCount { - isAllReadyOrCompleted = false - } - } - - if c.Monitor.Enable { - if info.grafanaReady == 0 || - info.prometheusReady == 0 || - info.nodeExporterReady != nodeCount { - isAllReadyOrCompleted = false - } - } - - if c.Kind == config.KIND_CURVEBS && (info.chunkServerReady != wantChunkServer || - info.jobPreChunkFileCompleted != wantChunkServer || - info.jobProLogicPoolCompleted == 0 || - info.jobProPhysicalPoolCompleted == 0) { - isAllReadyOrCompleted = false - } - - if c.Kind == config.KIND_CURVEFS && - (info.metaServerReady != nodeCount || info.jobProLogicPoolCompleted == 0) { - isAllReadyOrCompleted = false - } - - if info.etcdReady != nodeCount || info.mdsReady != nodeCount { - isAllReadyOrCompleted = false - } - - if isAllReadyOrCompleted { - break - } - <-checkTicker.C - } +func deleteSyncConfigDeployment(c *daemon.Cluster, syncConfigDeployment string) error { + err := retry.OnError(retry.DefaultRetry, func(err error) bool { + // retrying for any error that occurs + return true + }, func() error { + return c.Context.Clientset.AppsV1().Deployments(c.Namespace).Delete(syncConfigDeployment, &metav1.DeleteOptions{}) + }) - err := c.Context.Clientset.AppsV1().Deployments(c.Namespace).Delete(syncConfigDeployment, &metav1.DeleteOptions{}) if err != nil { - logger.Errorf("failed to delete deployment about \"curve-sync-config\", error: %s", err) + return errors.Wrapf(err, "failed to delete deployment %s after the %s has been deployed", + SyncConfigDeployment, c.Kind) } - logger.Infof("cluster is deployed, deployment about \"curve-sync-config\" will be deleted") -} - -func isAllReplicasReady(deployment apps.Deployment) bool { - if deployment.Status.Replicas == deployment.Status.ReadyReplicas { - return true - } - return false -} + logger.Infof("the %s has been deployed and the deployment %s has been deleted", c.Kind, SyncConfigDeployment) -func isJobCompleted(job batch.Job) bool { - if *job.Spec.Completions == job.Status.Succeeded { - return true - } - return false + return nil } diff --git a/pkg/controllers/cluster.go b/pkg/controllers/cluster.go index af086766..4e195ddd 100644 --- a/pkg/controllers/cluster.go +++ b/pkg/controllers/cluster.go @@ -115,6 +115,12 @@ func reconcileCurveDaemons(c *daemon.Cluster) error { return err } + // clean up the cluster install environment + err = cleanClusterInstallEnv(c) + if err != nil { + return err + } + return nil } @@ -147,5 +153,16 @@ func reconcileCurveFSDaemons(c *daemon.Cluster) error { return err } + // clean up the cluster install environment + err = cleanClusterInstallEnv(c) + if err != nil { + return err + } + return nil } + +// cleanClusterInstallEnv clean up the cluster install environment +func cleanClusterInstallEnv(c *daemon.Cluster) error { + return deleteSyncConfigDeployment(c, SyncConfigDeployment) +} From 9dcf3bc0666b46c37c97d52a3f97bda340060a9d Mon Sep 17 00:00:00 2001 From: DKCBBB <40293922+lizonglingo@users.noreply.github.com> Date: Fri, 25 Aug 2023 18:48:54 +0800 Subject: [PATCH 5/5] Style: adjusted the directory of the func for func DeleteSyncConfigDeployment() --- pkg/controllers/client.go | 20 -------------------- pkg/controllers/cluster.go | 4 +++- pkg/k8sutil/deployment.go | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/pkg/controllers/client.go b/pkg/controllers/client.go index 9cd54e57..94f080df 100644 --- a/pkg/controllers/client.go +++ b/pkg/controllers/client.go @@ -12,7 +12,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/tools/remotecommand" - "k8s.io/client-go/util/retry" "github.com/opencurve/curve-operator/pkg/daemon" "github.com/opencurve/curve-operator/pkg/k8sutil" @@ -157,22 +156,3 @@ func getReadConfigJobLabel(c *daemon.Cluster) map[string]string { labels["curve"] = c.Kind return labels } - -// deleteSyncConfigDeployment delete the SyncConfigDeployment after the cluster is deployed. -func deleteSyncConfigDeployment(c *daemon.Cluster, syncConfigDeployment string) error { - err := retry.OnError(retry.DefaultRetry, func(err error) bool { - // retrying for any error that occurs - return true - }, func() error { - return c.Context.Clientset.AppsV1().Deployments(c.Namespace).Delete(syncConfigDeployment, &metav1.DeleteOptions{}) - }) - - if err != nil { - return errors.Wrapf(err, "failed to delete deployment %s after the %s has been deployed", - SyncConfigDeployment, c.Kind) - } - - logger.Infof("the %s has been deployed and the deployment %s has been deleted", c.Kind, SyncConfigDeployment) - - return nil -} diff --git a/pkg/controllers/cluster.go b/pkg/controllers/cluster.go index 4e195ddd..a0ac3b8e 100644 --- a/pkg/controllers/cluster.go +++ b/pkg/controllers/cluster.go @@ -1,6 +1,7 @@ package controllers import ( + "context" "time" "github.com/coreos/pkg/capnslog" @@ -8,6 +9,7 @@ import ( "github.com/opencurve/curve-operator/pkg/chunkserver" "github.com/opencurve/curve-operator/pkg/daemon" "github.com/opencurve/curve-operator/pkg/etcd" + "github.com/opencurve/curve-operator/pkg/k8sutil" "github.com/opencurve/curve-operator/pkg/mds" "github.com/opencurve/curve-operator/pkg/metaserver" "github.com/opencurve/curve-operator/pkg/monitor" @@ -164,5 +166,5 @@ func reconcileCurveFSDaemons(c *daemon.Cluster) error { // cleanClusterInstallEnv clean up the cluster install environment func cleanClusterInstallEnv(c *daemon.Cluster) error { - return deleteSyncConfigDeployment(c, SyncConfigDeployment) + return k8sutil.DeleteSyncConfigDeployment(context.TODO(), &c.Context, SyncConfigDeployment, c.Namespace) } diff --git a/pkg/k8sutil/deployment.go b/pkg/k8sutil/deployment.go index c98181a9..97204726 100644 --- a/pkg/k8sutil/deployment.go +++ b/pkg/k8sutil/deployment.go @@ -7,6 +7,7 @@ import ( appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/util/retry" "github.com/opencurve/curve-operator/pkg/clusterd" "github.com/opencurve/curve-operator/pkg/k8sutil/patch" @@ -99,3 +100,22 @@ func WaitForDeploymentToStart(ctx context.Context, clusterdContext *clusterd.Con } return fmt.Errorf("gave up waiting for deployment %q to update", deployment.Name) } + +// DeleteSyncConfigDeployment delete the SyncConfigDeployment after the cluster is deployed. +func DeleteSyncConfigDeployment(ctx context.Context, clusterdContext *clusterd.Context, syncConfigDeployment, namespace string) error { + err := retry.OnError(retry.DefaultRetry, func(err error) bool { + // retrying for any error that occurs + return true + }, func() error { + return clusterdContext.Clientset.AppsV1().Deployments(namespace).Delete(syncConfigDeployment, &metav1.DeleteOptions{}) + }) + + if err != nil { + return fmt.Errorf("failed to delete deployment %q after the curve cluster has been deployed. %v", + syncConfigDeployment, err) + } + + logger.Infof("the curve cluster has been deployed and the deployment %q has been deleted", syncConfigDeployment) + + return nil +}