From 5bc11fac76f442e15722081c68d0827af8f7df67 Mon Sep 17 00:00:00 2001 From: Shubham Bajpai Date: Tue, 2 Jun 2020 11:37:03 +0530 Subject: [PATCH] refact(upgrade): add control plane checks in pre-upgrade (#8) Signed-off-by: shubham --- pkg/upgrade/upgrader/cstor_cspc.go | 9 +++++++-- pkg/upgrade/upgrader/cstor_volume.go | 6 +++++- pkg/upgrade/upgrader/helper.go | 24 ++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/pkg/upgrade/upgrader/cstor_cspc.go b/pkg/upgrade/upgrader/cstor_cspc.go index 7edd9e27..2f9353a9 100644 --- a/pkg/upgrade/upgrader/cstor_cspc.go +++ b/pkg/upgrade/upgrader/cstor_cspc.go @@ -17,11 +17,12 @@ limitations under the License. package upgrader import ( + "time" + apis "github.com/openebs/api/pkg/apis/cstor/v1" "github.com/openebs/upgrade/pkg/upgrade/patch" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/klog" - "time" ) // CSPCPatch is the patch required to upgrade CSPC @@ -60,7 +61,11 @@ func NewCSPCPatch(opts ...CSPCPatchOptions) *CSPCPatch { // PreUpgrade ... func (obj *CSPCPatch) PreUpgrade() error { - err := obj.CSPC.PreChecks(obj.From, obj.To) + err := isOperatorUpgraded("cspc-operator", obj.Namespace, obj.To, obj.KubeClientset) + if err != nil { + return err + } + err = obj.CSPC.PreChecks(obj.From, obj.To) return err } diff --git a/pkg/upgrade/upgrader/cstor_volume.go b/pkg/upgrade/upgrader/cstor_volume.go index 3ce36409..62680147 100644 --- a/pkg/upgrade/upgrader/cstor_volume.go +++ b/pkg/upgrade/upgrader/cstor_volume.go @@ -66,7 +66,11 @@ func NewCStorVolumePatch(opts ...CStorVolumePatchOptions) *CStorVolumePatch { // PreUpgrade ... func (obj *CStorVolumePatch) PreUpgrade() error { - err := obj.CVC.PreChecks(obj.From, obj.To) + err := isOperatorUpgraded("cvc-operator", obj.Namespace, obj.To, obj.KubeClientset) + if err != nil { + return err + } + err = obj.CVC.PreChecks(obj.From, obj.To) if err != nil { return err } diff --git a/pkg/upgrade/upgrader/helper.go b/pkg/upgrade/upgrader/helper.go index 5186b23c..23e9e3be 100644 --- a/pkg/upgrade/upgrader/helper.go +++ b/pkg/upgrade/upgrader/helper.go @@ -22,7 +22,9 @@ import ( "strings" "github.com/pkg/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/strategicpatch" + "k8s.io/client-go/kubernetes" ) func getImageURL(url, prefix string) (string, error) { @@ -62,3 +64,25 @@ func GetPatchData(oldObj, newObj interface{}) ([]byte, error) { } return patchBytes, nil } + +func isOperatorUpgraded(componentName string, namespace string, + toVersion string, kubeClient kubernetes.Interface) error { + operatorPods, err := kubeClient.CoreV1(). + Pods(namespace). + List(metav1.ListOptions{ + LabelSelector: "openebs.io/component-name=" + componentName, + }) + if err != nil { + return err + } + if len(operatorPods.Items) == 0 { + return fmt.Errorf("operator pod missing for %s", componentName) + } + for _, pod := range operatorPods.Items { + if pod.Labels["openebs.io/version"] != toVersion { + return fmt.Errorf("%s is in %s version, please upgrade it to %s version", + componentName, pod.Labels["openebs.io/version"], toVersion) + } + } + return nil +}