From 14532276388e5315945129b0b15a4cef21e67d7f Mon Sep 17 00:00:00 2001 From: Rida Zuber Date: Wed, 8 Dec 2021 16:44:08 +0530 Subject: [PATCH 1/5] Support configuration of default compatibility matrix --- go.mod | 1 + go.sum | 1 + vdoctl/cmd/deploy.go | 2 +- vdoctl/cmd/drivers.go | 33 ++++++++++++++++++++++++++++++--- vdoctl/cmd/status.go | 2 +- 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index f944924..7755acc 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/vmware-tanzu/vsphere-kubernetes-drivers-operator go 1.16 require ( + github.com/fatih/color v1.7.0 github.com/fsnotify/fsnotify v1.5.1 // indirect github.com/go-logr/logr v0.3.0 github.com/hashicorp/go-version v1.3.0 diff --git a/go.sum b/go.sum index cc63848..869b52c 100644 --- a/go.sum +++ b/go.sum @@ -131,6 +131,7 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7 github.com/evanphx/json-patch v4.5.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/evanphx/json-patch v4.9.0+incompatible h1:kLcOMZeuLAJvL2BPWLMIj5oaZQobrkAqrL+WFZwQses= github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= +github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/form3tech-oss/jwt-go v3.2.2+incompatible h1:TcekIExNqud5crz4xD2pavyTgWiPvpYe4Xau31I0PRk= github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= diff --git a/vdoctl/cmd/deploy.go b/vdoctl/cmd/deploy.go index c7f94a1..8d5f77f 100644 --- a/vdoctl/cmd/deploy.go +++ b/vdoctl/cmd/deploy.go @@ -69,7 +69,7 @@ Currently the command supports deployment on vanilla k8s cluster`, cobra.CheckErr(applyErr) } - fmt.Println("Tip: now that you have deployed VDO, you might want to try 'vdoctl configure drivers' to configure vsphere drivers") + fmt.Println("Tip: now that you have deployed VDO, you might want to try 'vdoctl configure compatibility-matrix' to configure vsphere drivers") }, } diff --git a/vdoctl/cmd/drivers.go b/vdoctl/cmd/drivers.go index 14f911b..d21c4df 100644 --- a/vdoctl/cmd/drivers.go +++ b/vdoctl/cmd/drivers.go @@ -19,6 +19,7 @@ import ( "context" "errors" "fmt" + "github.com/fatih/color" "k8s.io/apimachinery/pkg/types" "regexp" "strings" @@ -59,6 +60,7 @@ const ( vdoConfigName = "vdo-config" secretType = "kubernetes.io/basic-auth" ClusterDistribution = "OpenShift" + defaultMatrixPath = "https://github.com/vmware-tanzu/vsphere-kubernetes-drivers-operator/releases/download/{VERSION}/compatibility.yaml" ) // driversCmd represents the drivers command @@ -73,13 +75,28 @@ var driversCmd = &cobra.Command{ // Check the vdoDeployment Namespace and confirm if VDO operator is running in the env getVdoNamespace(ctx) - err, _ := IsVDODeployed(ctx) + var isConfigRequired bool + configKey := types.NamespacedName{ + Namespace: VdoCurrentNamespace, + Name: CompatMatrixConfigMap, + } + cm := v1.ConfigMap{} + + _ = K8sClient.Get(ctx, configKey, &cm) + if len(cm.Data) == 0 { + isConfigRequired = true + color.Yellow("Since you have not configured the Compatibility Matrix so the default values of the matrix will be taken, If you want to configure the matrix manually then you can skip this step and run 'vdoctl configure compatibility matrix") + } + + err, deployment := IsVDODeployed(ctx) if err != nil { if apierrors.IsNotFound(err) { fmt.Println(VDO_NOT_DEPLOYED) return } else { - cobra.CheckErr(err) + if !isConfigRequired { + cobra.CheckErr(err) + } } } @@ -106,7 +123,7 @@ var driversCmd = &cobra.Command{ labels := credentials{ username: "Username", password: "Password", - vcIp: "VC IP", + vcIp: "VC IP/ FQDN", topology: v1alpha1.TopologyInfo{ Zone: "Zones", Region: "Regions", @@ -116,6 +133,16 @@ var driversCmd = &cobra.Command{ isCPIRequired := utils.PromptGetInput("Do you want to configure CloudProvider? (Y/N)", errors.New("invalid input"), utils.IsString) + // Configure compatibility matrix , if not configured + if isConfigRequired { + vdoVersion := getVdoVersion(deployment) + currentMatrixPath := strings.Replace(defaultMatrixPath, "{VERSION}", vdoVersion, 1) + err = CreateConfigMap(currentMatrixPath, K8sClient, ctx, utils.IsURL) + if err != nil { + cobra.CheckErr(err) + } + } + if strings.EqualFold(isCPIRequired, "Y") { fmt.Printf("Please provide the vcenter IP for configuring CloudProvider \n") diff --git a/vdoctl/cmd/status.go b/vdoctl/cmd/status.go index 8ec6d87..3804178 100644 --- a/vdoctl/cmd/status.go +++ b/vdoctl/cmd/status.go @@ -95,7 +95,7 @@ func IsVDODeployed(ctx context.Context) (error, *v12.Deployment) { ns := types.NamespacedName{Namespace: VdoCurrentNamespace, Name: VdoDeploymentName} err := K8sClient.Get(ctx, ns, deployment) if deployment.Status.Replicas != deployment.Status.AvailableReplicas { - return fmt.Errorf("not enough replicas of VDO"), nil + return fmt.Errorf("not enough replicas of VDO"), deployment } return err, deployment } From 6f772ef3eae429aaaa1eb4a4f5e5c48c1e73b6b7 Mon Sep 17 00:00:00 2001 From: Rida Zuber Date: Thu, 9 Dec 2021 11:45:49 +0530 Subject: [PATCH 2/5] Incorporating review comments --- vdoctl/cmd/deploy.go | 2 +- vdoctl/cmd/drivers.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/vdoctl/cmd/deploy.go b/vdoctl/cmd/deploy.go index 8d5f77f..1978122 100644 --- a/vdoctl/cmd/deploy.go +++ b/vdoctl/cmd/deploy.go @@ -69,7 +69,7 @@ Currently the command supports deployment on vanilla k8s cluster`, cobra.CheckErr(applyErr) } - fmt.Println("Tip: now that you have deployed VDO, you might want to try 'vdoctl configure compatibility-matrix' to configure vsphere drivers") + fmt.Println("Tip: now that you have deployed VDO, you might want to try 'vdoctl configure compatibility-matrix' to configure compatibility matrix") }, } diff --git a/vdoctl/cmd/drivers.go b/vdoctl/cmd/drivers.go index d21c4df..e9318a2 100644 --- a/vdoctl/cmd/drivers.go +++ b/vdoctl/cmd/drivers.go @@ -85,7 +85,7 @@ var driversCmd = &cobra.Command{ _ = K8sClient.Get(ctx, configKey, &cm) if len(cm.Data) == 0 { isConfigRequired = true - color.Yellow("Since you have not configured the Compatibility Matrix so the default values of the matrix will be taken, If you want to configure the matrix manually then you can skip this step and run 'vdoctl configure compatibility matrix") + color.Yellow("VDO will use the default compatibility matrix to determine the versions. If you wish to configure compatibility matrix please run 'vdoctl configure compatibility matrix'") } err, deployment := IsVDODeployed(ctx) From c016fc0f9d42a711e99c0e821069849ae7668896 Mon Sep 17 00:00:00 2001 From: Rida Zuber Date: Wed, 15 Dec 2021 01:19:09 +0530 Subject: [PATCH 3/5] Minor changes --- vdoctl/cmd/configure_matrix.go | 10 ++++++---- vdoctl/cmd/drivers.go | 36 +++++++++++++++------------------- vdoctl/cmd/status.go | 2 +- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/vdoctl/cmd/configure_matrix.go b/vdoctl/cmd/configure_matrix.go index 4271310..610e601 100644 --- a/vdoctl/cmd/configure_matrix.go +++ b/vdoctl/cmd/configure_matrix.go @@ -36,6 +36,8 @@ const ( CompatMatrixConfigMAp = "compat-matrix-config" LocalFilepath = "Local filepath" WebURL = "Web URL" + DefaultConfig = "default" + UserConfig = "user" ) // matrixConfigureCmd represents the compat command @@ -75,7 +77,7 @@ var matrixConfigureCmd = &cobra.Command{ cobra.CheckErr(err) } - err = CreateConfigMap(filePath, K8sClient, ctx, flag) + err = CreateConfigMap(filePath, K8sClient, ctx, flag, UserConfig) if err != nil { cobra.CheckErr(err) } @@ -88,7 +90,7 @@ func init() { configureCmd.AddCommand(matrixConfigureCmd) } -func CreateConfigMap(filepath string, client runtimeclient.Client, ctx context.Context, flag utils.ValidationFlags) error { +func CreateConfigMap(filepath string, client runtimeclient.Client, ctx context.Context, flag utils.ValidationFlags, configFlag string) error { configMapKey := types.NamespacedName{ Namespace: VdoCurrentNamespace, @@ -97,13 +99,13 @@ func CreateConfigMap(filepath string, client runtimeclient.Client, ctx context.C var data map[string]string if flag == utils.IsURL { - data = map[string]string{"versionConfigURL": filepath, "auto-upgrade": "disabled"} + data = map[string]string{"versionConfigURL": filepath, "auto-upgrade": "disabled", "configured-by": configFlag} } else { fileBytes, err := vdoClient.GenerateYamlFromFilePath(filepath) if err != nil { cobra.CheckErr(fmt.Sprintf("unable to read the matrix from %s", filepath)) } - data = map[string]string{"versionConfigContent": string(fileBytes), "auto-upgrade": "disabled"} + data = map[string]string{"versionConfigContent": string(fileBytes), "auto-upgrade": "disabled", "configured-by": configFlag} } configMapObj := metav1.ObjectMeta{Name: configMapKey.Name, Namespace: configMapKey.Namespace} diff --git a/vdoctl/cmd/drivers.go b/vdoctl/cmd/drivers.go index e9318a2..83df264 100644 --- a/vdoctl/cmd/drivers.go +++ b/vdoctl/cmd/drivers.go @@ -19,8 +19,8 @@ import ( "context" "errors" "fmt" - "github.com/fatih/color" "k8s.io/apimachinery/pkg/types" + "os" "regexp" "strings" @@ -60,7 +60,6 @@ const ( vdoConfigName = "vdo-config" secretType = "kubernetes.io/basic-auth" ClusterDistribution = "OpenShift" - defaultMatrixPath = "https://github.com/vmware-tanzu/vsphere-kubernetes-drivers-operator/releases/download/{VERSION}/compatibility.yaml" ) // driversCmd represents the drivers command @@ -75,7 +74,6 @@ var driversCmd = &cobra.Command{ // Check the vdoDeployment Namespace and confirm if VDO operator is running in the env getVdoNamespace(ctx) - var isConfigRequired bool configKey := types.NamespacedName{ Namespace: VdoCurrentNamespace, Name: CompatMatrixConfigMap, @@ -83,20 +81,28 @@ var driversCmd = &cobra.Command{ cm := v1.ConfigMap{} _ = K8sClient.Get(ctx, configKey, &cm) - if len(cm.Data) == 0 { - isConfigRequired = true - color.Yellow("VDO will use the default compatibility matrix to determine the versions. If you wish to configure compatibility matrix please run 'vdoctl configure compatibility matrix'") + configFlag := cm.Data["configured-by"] + + if !strings.EqualFold(configFlag, UserConfig) { + isConfigRequired := utils.PromptGetInput("Compatibility matrix is not configured. VDO will use the default compatibility matrix. Do you want to configure compatibility matrix? (Y/N) ", errors.New("invalid input"), utils.IsString) + if strings.EqualFold(isConfigRequired, "Y") { + os.Exit(0) + } else { + cm.Data["configured-by"] = DefaultConfig + err := K8sClient.Update(ctx, &cm, &client.UpdateOptions{}) + if err != nil { + cobra.CheckErr(fmt.Sprintf("Error received in updating config Map %s", err)) + } + } } - err, deployment := IsVDODeployed(ctx) + err, _ := IsVDODeployed(ctx) if err != nil { if apierrors.IsNotFound(err) { fmt.Println(VDO_NOT_DEPLOYED) return } else { - if !isConfigRequired { - cobra.CheckErr(err) - } + cobra.CheckErr(err) } } @@ -133,16 +139,6 @@ var driversCmd = &cobra.Command{ isCPIRequired := utils.PromptGetInput("Do you want to configure CloudProvider? (Y/N)", errors.New("invalid input"), utils.IsString) - // Configure compatibility matrix , if not configured - if isConfigRequired { - vdoVersion := getVdoVersion(deployment) - currentMatrixPath := strings.Replace(defaultMatrixPath, "{VERSION}", vdoVersion, 1) - err = CreateConfigMap(currentMatrixPath, K8sClient, ctx, utils.IsURL) - if err != nil { - cobra.CheckErr(err) - } - } - if strings.EqualFold(isCPIRequired, "Y") { fmt.Printf("Please provide the vcenter IP for configuring CloudProvider \n") diff --git a/vdoctl/cmd/status.go b/vdoctl/cmd/status.go index 3804178..8ec6d87 100644 --- a/vdoctl/cmd/status.go +++ b/vdoctl/cmd/status.go @@ -95,7 +95,7 @@ func IsVDODeployed(ctx context.Context) (error, *v12.Deployment) { ns := types.NamespacedName{Namespace: VdoCurrentNamespace, Name: VdoDeploymentName} err := K8sClient.Get(ctx, ns, deployment) if deployment.Status.Replicas != deployment.Status.AvailableReplicas { - return fmt.Errorf("not enough replicas of VDO"), deployment + return fmt.Errorf("not enough replicas of VDO"), nil } return err, deployment } From 4d3bf2dffc6abb3a7414210cdc81484a85a94337 Mon Sep 17 00:00:00 2001 From: Rida Zuber Date: Wed, 15 Dec 2021 03:46:45 +0530 Subject: [PATCH 4/5] Incorporating review comments --- go.mod | 1 - go.sum | 1 - vdoctl/cmd/configure_matrix.go | 8 ++++++-- vdoctl/cmd/delete_vdo.go | 7 +++++-- vdoctl/cmd/drivers.go | 13 ++++++++----- vdoctl/cmd/root.go | 7 ++++--- vdoctl/cmd/status.go | 7 +++++-- vdoctl/cmd/update_matrix.go | 7 +++++-- vdoctl/cmd/version.go | 5 ++++- 9 files changed, 37 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index 7755acc..f944924 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/vmware-tanzu/vsphere-kubernetes-drivers-operator go 1.16 require ( - github.com/fatih/color v1.7.0 github.com/fsnotify/fsnotify v1.5.1 // indirect github.com/go-logr/logr v0.3.0 github.com/hashicorp/go-version v1.3.0 diff --git a/go.sum b/go.sum index 869b52c..cc63848 100644 --- a/go.sum +++ b/go.sum @@ -131,7 +131,6 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7 github.com/evanphx/json-patch v4.5.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/evanphx/json-patch v4.9.0+incompatible h1:kLcOMZeuLAJvL2BPWLMIj5oaZQobrkAqrL+WFZwQses= github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= -github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/form3tech-oss/jwt-go v3.2.2+incompatible h1:TcekIExNqud5crz4xD2pavyTgWiPvpYe4Xau31I0PRk= github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= diff --git a/vdoctl/cmd/configure_matrix.go b/vdoctl/cmd/configure_matrix.go index 610e601..c5c3bf1 100644 --- a/vdoctl/cmd/configure_matrix.go +++ b/vdoctl/cmd/configure_matrix.go @@ -38,6 +38,7 @@ const ( WebURL = "Web URL" DefaultConfig = "default" UserConfig = "user" + DefaultNs = "vmware-system-vdo" ) // matrixConfigureCmd represents the compat command @@ -50,7 +51,10 @@ var matrixConfigureCmd = &cobra.Command{ ctx := context.Background() // Check the vdoDeployment Namespace and confirm if VDO operator is running in the env - getVdoNamespace(ctx) + err := getVdoNamespace(ctx) + if err != nil { + VdoCurrentNamespace = DefaultNs + } configKey := types.NamespacedName{ Namespace: VdoCurrentNamespace, @@ -72,7 +76,7 @@ var matrixConfigureCmd = &cobra.Command{ } filePath := utils.PromptGetInput(item, errors.New("invalid input"), flag) - err := CreateNamespace(K8sClient, ctx) + err = CreateNamespace(K8sClient, ctx) if err != nil { cobra.CheckErr(err) } diff --git a/vdoctl/cmd/delete_vdo.go b/vdoctl/cmd/delete_vdo.go index ba3766a..5a2069f 100644 --- a/vdoctl/cmd/delete_vdo.go +++ b/vdoctl/cmd/delete_vdo.go @@ -23,7 +23,10 @@ Currently the command supports vanilla k8s cluster`, ctx := context.Background() // Check the vdoDeployment Namespace and confirm if VDO operator is running in the env - getVdoNamespace(ctx) + err := getVdoNamespace(ctx) + if err != nil { + cobra.CheckErr(err) + } ns := corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ @@ -31,7 +34,7 @@ Currently the command supports vanilla k8s cluster`, }, } - err := K8sClient.Delete(ctx, &ns, &client.DeleteOptions{}) + err = K8sClient.Delete(ctx, &ns, &client.DeleteOptions{}) if err != nil && !apierrors.IsNotFound(err) { cobra.CheckErr(fmt.Errorf("Error occurred deleting VDO, %v", err)) } diff --git a/vdoctl/cmd/drivers.go b/vdoctl/cmd/drivers.go index 83df264..a0b64b6 100644 --- a/vdoctl/cmd/drivers.go +++ b/vdoctl/cmd/drivers.go @@ -72,7 +72,10 @@ var driversCmd = &cobra.Command{ ctx := context.Background() // Check the vdoDeployment Namespace and confirm if VDO operator is running in the env - getVdoNamespace(ctx) + err := getVdoNamespace(ctx) + if err != nil { + cobra.CheckErr(err) + } configKey := types.NamespacedName{ Namespace: VdoCurrentNamespace, @@ -84,19 +87,19 @@ var driversCmd = &cobra.Command{ configFlag := cm.Data["configured-by"] if !strings.EqualFold(configFlag, UserConfig) { - isConfigRequired := utils.PromptGetInput("Compatibility matrix is not configured. VDO will use the default compatibility matrix. Do you want to configure compatibility matrix? (Y/N) ", errors.New("invalid input"), utils.IsString) + isConfigRequired := utils.PromptGetInput("Compatibility matrix is not configured. VDO will use the default compatibility matrix. You can run 'vdoctl update compatibility-matrix' command to configure. Do you want to continue with default compatibility matrix? (Y/N) ", errors.New("invalid input"), utils.IsString) if strings.EqualFold(isConfigRequired, "Y") { - os.Exit(0) - } else { cm.Data["configured-by"] = DefaultConfig err := K8sClient.Update(ctx, &cm, &client.UpdateOptions{}) if err != nil { cobra.CheckErr(fmt.Sprintf("Error received in updating config Map %s", err)) } + } else { + os.Exit(0) } } - err, _ := IsVDODeployed(ctx) + err, _ = IsVDODeployed(ctx) if err != nil { if apierrors.IsNotFound(err) { fmt.Println(VDO_NOT_DEPLOYED) diff --git a/vdoctl/cmd/root.go b/vdoctl/cmd/root.go index c54199e..de84b45 100644 --- a/vdoctl/cmd/root.go +++ b/vdoctl/cmd/root.go @@ -160,12 +160,12 @@ func addKnownTypes(scheme *runtime.Scheme) error { } // getVdoNamespace identifies the namespace in which vdo-operator is deployed -func getVdoNamespace(ctx context.Context) { +func getVdoNamespace(ctx context.Context) error { // List Deployments deploymentList := &appsv1.DeploymentList{} err := K8sClient.List(ctx, deploymentList) if err != nil { - cobra.CheckErr(err) + return err } //Filter out the deployment using vdo-controller name @@ -178,6 +178,7 @@ func getVdoNamespace(ctx context.Context) { // If the controller namespace is not identified then it is assumed that vdo is not deployed if VdoCurrentNamespace == "" { - cobra.CheckErr("VDO is currently not deployed, please deploy using `vdo deploy` command") + return fmt.Errorf("VDO is currently not deployed, please deploy using `vdo deploy` command") } + return nil } diff --git a/vdoctl/cmd/status.go b/vdoctl/cmd/status.go index 8ec6d87..ed50642 100644 --- a/vdoctl/cmd/status.go +++ b/vdoctl/cmd/status.go @@ -40,9 +40,12 @@ var statusCmd = &cobra.Command{ ctx := context.Background() // Check the vdoDeployment Namespace and confirm if VDO operator is running in the env - getVdoNamespace(ctx) + err := getVdoNamespace(ctx) + if err != nil { + cobra.CheckErr(err) + } - err, _ := IsVDODeployed(ctx) + err, _ = IsVDODeployed(ctx) if err != nil { if apierrors.IsNotFound(err) { fmt.Println(VDO_NOT_DEPLOYED) diff --git a/vdoctl/cmd/update_matrix.go b/vdoctl/cmd/update_matrix.go index 95e7331..fc26d66 100644 --- a/vdoctl/cmd/update_matrix.go +++ b/vdoctl/cmd/update_matrix.go @@ -57,12 +57,15 @@ func updateMatrix(args []string) { ctxNew := context.Background() // Check the vdoDeployment Namespace and confirm if VDO operator is running in the env - getVdoNamespace(ctxNew) + err := getVdoNamespace(ctxNew) + if err != nil { + cobra.CheckErr(err) + } // Check for volumes which have PWX or ROX access mode, // If any then manual steps are required before updating the driver volumeAttachmentList := storagev1.VolumeAttachmentList{} - err := K8sClient.List(ctxNew, &volumeAttachmentList) + err = K8sClient.List(ctxNew, &volumeAttachmentList) if err != nil { cobra.CheckErr("unable to read the volume list to do pre-check for upgrade") } diff --git a/vdoctl/cmd/version.go b/vdoctl/cmd/version.go index e07a04b..e8f8ff1 100644 --- a/vdoctl/cmd/version.go +++ b/vdoctl/cmd/version.go @@ -57,7 +57,10 @@ var versionCmd = &cobra.Command{ } // Check the vdoDeployment Namespace and confirm if VDO operator is running in the env - getVdoNamespace(ctx) + err := getVdoNamespace(ctx) + if err != nil { + cobra.CheckErr(err) + } k8sVersion = getK8sVersion() err, vdoDeployment := IsVDODeployed(ctx) From ac817af0b899f8ca130a123f9fc4c47eaa34315d Mon Sep 17 00:00:00 2001 From: Rida Zuber Date: Mon, 20 Dec 2021 14:24:20 +0530 Subject: [PATCH 5/5] Incorporating review comments --- vdoctl/cmd/configure_matrix.go | 9 ++++----- vdoctl/cmd/delete_vdo.go | 4 ++-- vdoctl/cmd/drivers.go | 30 +++++++++--------------------- vdoctl/cmd/root.go | 16 +++++++++++----- vdoctl/cmd/status.go | 21 ++------------------- vdoctl/cmd/update_matrix.go | 4 ++-- vdoctl/cmd/version.go | 4 ++-- 7 files changed, 32 insertions(+), 56 deletions(-) diff --git a/vdoctl/cmd/configure_matrix.go b/vdoctl/cmd/configure_matrix.go index c5c3bf1..b40e913 100644 --- a/vdoctl/cmd/configure_matrix.go +++ b/vdoctl/cmd/configure_matrix.go @@ -36,7 +36,6 @@ const ( CompatMatrixConfigMAp = "compat-matrix-config" LocalFilepath = "Local filepath" WebURL = "Web URL" - DefaultConfig = "default" UserConfig = "user" DefaultNs = "vmware-system-vdo" ) @@ -50,8 +49,8 @@ var matrixConfigureCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { ctx := context.Background() - // Check the vdoDeployment Namespace and confirm if VDO operator is running in the env - err := getVdoNamespace(ctx) + // Confirm if VDO operator is running in the env and get the vdoDeployment Namespace + err, _ := IsVDODeployed(ctx) if err != nil { VdoCurrentNamespace = DefaultNs } @@ -81,7 +80,7 @@ var matrixConfigureCmd = &cobra.Command{ cobra.CheckErr(err) } - err = CreateConfigMap(filePath, K8sClient, ctx, flag, UserConfig) + err = CreateConfigMap(ctx, filePath, K8sClient, flag, UserConfig) if err != nil { cobra.CheckErr(err) } @@ -94,7 +93,7 @@ func init() { configureCmd.AddCommand(matrixConfigureCmd) } -func CreateConfigMap(filepath string, client runtimeclient.Client, ctx context.Context, flag utils.ValidationFlags, configFlag string) error { +func CreateConfigMap(ctx context.Context, filepath string, client runtimeclient.Client, flag utils.ValidationFlags, configFlag string) error { configMapKey := types.NamespacedName{ Namespace: VdoCurrentNamespace, diff --git a/vdoctl/cmd/delete_vdo.go b/vdoctl/cmd/delete_vdo.go index 5a2069f..27874e9 100644 --- a/vdoctl/cmd/delete_vdo.go +++ b/vdoctl/cmd/delete_vdo.go @@ -22,8 +22,8 @@ Currently the command supports vanilla k8s cluster`, Run: func(cmd *cobra.Command, args []string) { ctx := context.Background() - // Check the vdoDeployment Namespace and confirm if VDO operator is running in the env - err := getVdoNamespace(ctx) + // Confirm if VDO operator is running in the env and get the vdoDeployment Namespace + err, _ := IsVDODeployed(ctx) if err != nil { cobra.CheckErr(err) } diff --git a/vdoctl/cmd/drivers.go b/vdoctl/cmd/drivers.go index a0b64b6..66dd905 100644 --- a/vdoctl/cmd/drivers.go +++ b/vdoctl/cmd/drivers.go @@ -71,10 +71,14 @@ var driversCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { ctx := context.Background() - // Check the vdoDeployment Namespace and confirm if VDO operator is running in the env - err := getVdoNamespace(ctx) + err, _ := IsVDODeployed(ctx) if err != nil { - cobra.CheckErr(err) + if apierrors.IsNotFound(err) { + fmt.Println(VDO_NOT_DEPLOYED) + return + } else { + cobra.CheckErr(err) + } } configKey := types.NamespacedName{ @@ -87,28 +91,12 @@ var driversCmd = &cobra.Command{ configFlag := cm.Data["configured-by"] if !strings.EqualFold(configFlag, UserConfig) { - isConfigRequired := utils.PromptGetInput("Compatibility matrix is not configured. VDO will use the default compatibility matrix. You can run 'vdoctl update compatibility-matrix' command to configure. Do you want to continue with default compatibility matrix? (Y/N) ", errors.New("invalid input"), utils.IsString) - if strings.EqualFold(isConfigRequired, "Y") { - cm.Data["configured-by"] = DefaultConfig - err := K8sClient.Update(ctx, &cm, &client.UpdateOptions{}) - if err != nil { - cobra.CheckErr(fmt.Sprintf("Error received in updating config Map %s", err)) - } - } else { + isConfigRequired := utils.PromptGetInput("VDO is configured with default compatiblity matrix. you can update compatiblity-matrix using 'vdoctl update compatiblity-matrix'. Do you want to use the defaults for compatiblity matrix (Y/N) ? ", errors.New("invalid input"), utils.IsString) + if !strings.EqualFold(isConfigRequired, "Y") { os.Exit(0) } } - err, _ = IsVDODeployed(ctx) - if err != nil { - if apierrors.IsNotFound(err) { - fmt.Println(VDO_NOT_DEPLOYED) - return - } else { - cobra.CheckErr(err) - } - } - var vdoConfigList v1alpha1.VDOConfigList err = K8sClient.List(ctx, &vdoConfigList) diff --git a/vdoctl/cmd/root.go b/vdoctl/cmd/root.go index de84b45..f565ac3 100644 --- a/vdoctl/cmd/root.go +++ b/vdoctl/cmd/root.go @@ -159,18 +159,19 @@ func addKnownTypes(scheme *runtime.Scheme) error { return nil } -// getVdoNamespace identifies the namespace in which vdo-operator is deployed -func getVdoNamespace(ctx context.Context) error { +func IsVDODeployed(ctx context.Context) (error, *appsv1.Deployment) { // List Deployments deploymentList := &appsv1.DeploymentList{} + vdoDeployment := &appsv1.Deployment{} err := K8sClient.List(ctx, deploymentList) if err != nil { - return err + return err, nil } //Filter out the deployment using vdo-controller name for _, deployment := range deploymentList.Items { if deployment.Name == VdoDeploymentName { + vdoDeployment = &deployment VdoCurrentNamespace = deployment.Namespace break } @@ -178,7 +179,12 @@ func getVdoNamespace(ctx context.Context) error { // If the controller namespace is not identified then it is assumed that vdo is not deployed if VdoCurrentNamespace == "" { - return fmt.Errorf("VDO is currently not deployed, please deploy using `vdo deploy` command") + return fmt.Errorf("VDO is currently not deployed, please deploy using `vdo deploy` command"), nil } - return nil + + if vdoDeployment.Status.Replicas != vdoDeployment.Status.AvailableReplicas { + return fmt.Errorf("not enough replicas of VDO"), nil + } + return err, vdoDeployment + } diff --git a/vdoctl/cmd/status.go b/vdoctl/cmd/status.go index ed50642..9196e43 100644 --- a/vdoctl/cmd/status.go +++ b/vdoctl/cmd/status.go @@ -20,9 +20,7 @@ import ( "fmt" "github.com/spf13/cobra" vdov1alpha1 "github.com/vmware-tanzu/vsphere-kubernetes-drivers-operator/api/v1alpha1" - v12 "k8s.io/api/apps/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/types" ) const VDO_NOT_DEPLOYED = "VDO is not deployed. you can run `vdoctl deploy` command to deploy VDO" @@ -39,13 +37,8 @@ var statusCmd = &cobra.Command{ ctx := context.Background() - // Check the vdoDeployment Namespace and confirm if VDO operator is running in the env - err := getVdoNamespace(ctx) - if err != nil { - cobra.CheckErr(err) - } - - err, _ = IsVDODeployed(ctx) + // Confirm if VDO operator is running in the env and get the vdoDeployment Namespace + err, _ := IsVDODeployed(ctx) if err != nil { if apierrors.IsNotFound(err) { fmt.Println(VDO_NOT_DEPLOYED) @@ -93,16 +86,6 @@ var statusCmd = &cobra.Command{ }, } -func IsVDODeployed(ctx context.Context) (error, *v12.Deployment) { - deployment := &v12.Deployment{} - ns := types.NamespacedName{Namespace: VdoCurrentNamespace, Name: VdoDeploymentName} - err := K8sClient.Get(ctx, ns, deployment) - if deployment.Status.Replicas != deployment.Status.AvailableReplicas { - return fmt.Errorf("not enough replicas of VDO"), nil - } - return err, deployment -} - // Fetch VC IP of given VsphereCloudConfig func fetchVcenterIp(vsphereCloudConfigList vdov1alpha1.VsphereCloudConfigList, configName string) { for _, vsphereCloudConfig := range vsphereCloudConfigList.Items { diff --git a/vdoctl/cmd/update_matrix.go b/vdoctl/cmd/update_matrix.go index fc26d66..8aaa619 100644 --- a/vdoctl/cmd/update_matrix.go +++ b/vdoctl/cmd/update_matrix.go @@ -56,8 +56,8 @@ func updateMatrix(args []string) { updatedMatrix := args[0] ctxNew := context.Background() - // Check the vdoDeployment Namespace and confirm if VDO operator is running in the env - err := getVdoNamespace(ctxNew) + // Confirm if VDO operator is running in the env and get the vdoDeployment Namespace + err, _ := IsVDODeployed(ctxNew) if err != nil { cobra.CheckErr(err) } diff --git a/vdoctl/cmd/version.go b/vdoctl/cmd/version.go index e8f8ff1..be7ac95 100644 --- a/vdoctl/cmd/version.go +++ b/vdoctl/cmd/version.go @@ -56,8 +56,8 @@ var versionCmd = &cobra.Command{ Logger: ctrllog.Log.WithName("vdoctl:version"), } - // Check the vdoDeployment Namespace and confirm if VDO operator is running in the env - err := getVdoNamespace(ctx) + // // Confirm if VDO operator is running in the env and get the vdoDeployment Namespace + err, _ := IsVDODeployed(ctx) if err != nil { cobra.CheckErr(err) }