diff --git a/pkg/migrate/cstor/helper.go b/pkg/migrate/cstor/helper.go new file mode 100644 index 00000000..8c4d8c4e --- /dev/null +++ b/pkg/migrate/cstor/helper.go @@ -0,0 +1,42 @@ +/* +Copyright 2020 The OpenEBS Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package migrate + +import ( + "encoding/json" + "fmt" + + "k8s.io/apimachinery/pkg/util/strategicpatch" +) + +// GetPatchData returns patch data by +// marshalling and taking diff of two objects +func GetPatchData(oldObj, newObj interface{}) ([]byte, error) { + oldData, err := json.Marshal(oldObj) + if err != nil { + return nil, fmt.Errorf("marshal old object failed: %v", err) + } + newData, err := json.Marshal(newObj) + if err != nil { + return nil, fmt.Errorf("mashal new object failed: %v", err) + } + patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, oldObj) + if err != nil { + return nil, fmt.Errorf("CreateTwoWayMergePatch failed: %v", err) + } + return patchBytes, nil +} diff --git a/pkg/migrate/cstor/pool.go b/pkg/migrate/cstor/pool.go index 0d923760..7d657221 100644 --- a/pkg/migrate/cstor/pool.go +++ b/pkg/migrate/cstor/pool.go @@ -256,7 +256,25 @@ func (c *CSPCMigrator) csptocspi( if err != nil { return err } - return nil + // remove the finalizers from csp object for cleanup + // as pool pod is no longer running to remove them. + return removeCSPFinalizers(cspObj) +} + +func removeCSPFinalizers(cspObj *apis.CStorPool) error { + cspClient := csp.KubeClient() + newCSP := cspObj.DeepCopy() + newCSP.Finalizers = []string{} + patchData, err := GetPatchData(cspObj, newCSP) + if err != nil { + return err + } + _, err = cspClient.Patch( + cspObj.Name, + k8stypes.MergePatchType, + patchData, + ) + return err } // get csp for cspi on the basis of cspLabel, which is the combination of @@ -304,13 +322,15 @@ func (c *CSPCMigrator) scaleDownDeployment(cspObj *apis.CStorPool, openebsNamesp Times(60). Wait(5 * time.Second). Try(func(attempt uint) error { - cspDeploy, err1 := c.KubeClientset.AppsV1(). - Deployments(openebsNamespace). - Get(cspDeployList.Items[0].Name, metav1.GetOptions{}) + cspPods, err1 := c.KubeClientset.CoreV1(). + Pods(openebsNamespace). + List(metav1.ListOptions{ + LabelSelector: "openebs.io/cstor-pool=" + cspObj.Name, + }) if err1 != nil { return errors.Wrapf(err1, "failed to get csp deploy") } - if cspDeploy.Status.ReadyReplicas != 0 { + if len(cspPods.Items) != 0 { return errors.Errorf("failed to scale down csp deployment") } return nil