Skip to content

Commit

Permalink
Fixing vdoctl version when VDO is running on the cluster without CSI/…
Browse files Browse the repository at this point in the history
…CPI configured (#63)
  • Loading branch information
DimpleRajaVamsi authored Oct 11, 2021
1 parent 58ac1c6 commit 227bcea
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 61 deletions.
1 change: 1 addition & 0 deletions vdoctl/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ Currently the command supports deployment on vanilla k8s cluster`,

func init() {
deployCmd.Flags().StringVar(&specfile, "spec", "", "url to vdo deployment spec file")
_ = deployCmd.MarkFlagRequired("spec")
rootCmd.AddCommand(deployCmd)
}
2 changes: 1 addition & 1 deletion vdoctl/cmd/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var driversCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()

err := IsVDODeployed(ctx)
err, _ := IsVDODeployed(ctx)
if err != nil {
if apierrors.IsNotFound(err) {
fmt.Println(VDO_NOT_DEPLOYED)
Expand Down
9 changes: 6 additions & 3 deletions vdoctl/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var statusCmd = &cobra.Command{

ctx := context.Background()

err := IsVDODeployed(ctx)
err, _ := IsVDODeployed(ctx)
if err != nil {
if apierrors.IsNotFound(err) {
fmt.Println(VDO_NOT_DEPLOYED)
Expand Down Expand Up @@ -87,11 +87,14 @@ var statusCmd = &cobra.Command{
},
}

func IsVDODeployed(ctx context.Context) error {
func IsVDODeployed(ctx context.Context) (error, *v12.Deployment) {
deployment := &v12.Deployment{}
ns := types.NamespacedName{Namespace: VdoNamespace, Name: VdoDeploymentName}
err := K8sClient.Get(ctx, ns, deployment)
return err
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
Expand Down
117 changes: 60 additions & 57 deletions vdoctl/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
vdov1alpha1 "github.com/vmware-tanzu/vsphere-kubernetes-drivers-operator/api/v1alpha1"
Expand All @@ -28,6 +27,7 @@ import (
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes/scheme"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand All @@ -38,37 +38,47 @@ const (
VdoDeploymentName = "vdo-controller-manager"
)

var (
vdoVersion = "Not Configured"
vsphereVersion []string
k8sVersion = "Not Configured"
csiVersion = "Not Configured"
cpiVersion = "Not Configured"
)

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "command to get VDO version",
Long: "This command helps to get the version of the configurations created by VDO.\nIt includes brief detail about the version of CloudProvider and StorageProvider along with VC and Kubernetes details",
Run: func(cmd *cobra.Command, args []string) {

var vdoConfigList vdov1alpha1.VDOConfigList

ctx := vdocontext.VDOContext{
Context: context.Background(),
Logger: ctrllog.Log.WithName("vdoctl:version"),
}

err := IsVDODeployed(ctx)
k8sVersion = getK8sVersion()
err, vdoDeployment := IsVDODeployed(ctx)
if err != nil {
if apierrors.IsNotFound(err) {
fmt.Println(VDO_NOT_DEPLOYED)
showVersionInfo()
return
} else {
cobra.CheckErr(err)
}
}

vdoVersion = getVdoVersion(vdoDeployment)

var vdoConfigList vdov1alpha1.VDOConfigList
err = K8sClient.List(ctx, &vdoConfigList)
if err != nil {
cobra.CheckErr(err)
}

if len(vdoConfigList.Items) <= 0 {
fmt.Println("VDO is not configured. you can use `vdoctl configure drivers` to configure VDO")
showVersionInfo()
return
}

Expand All @@ -92,48 +102,6 @@ var versionCmd = &cobra.Command{
},
}

deploymentReq := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: VdoDeploymentName,
Namespace: VdoNamespace,
},
}

// Fetch VDO deployment object
deployment := &v12.Deployment{}
err = K8sClient.Get(ctx, deploymentReq.NamespacedName, deployment)
if err != nil {
cobra.CheckErr(err)
}

var deploymentData string

for _, v := range deployment.Annotations {
if strings.Contains(v, "image") {
deploymentData = v
break
}
}

var deploymentMap map[string]interface{}
if err = json.Unmarshal([]byte(deploymentData), &deploymentMap); err != nil {
cobra.CheckErr(err)
}

containerInfo := deploymentMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["containers"].([]interface{})

var imageName interface{}
for i, container := range containerInfo {
if container.(map[string]interface{})["name"] == "manager" {
imageName = containerInfo[i].(map[string]interface{})["image"]
break
}
}

vdoVersion := strings.Split(fmt.Sprint(imageName), ":")

fmt.Printf("VDO Version : %s", vdoVersion[len(vdoVersion)-1])

configMap := &v1.ConfigMap{}
err = K8sClient.Get(ctx, req.NamespacedName, configMap)
if err != nil {
Expand All @@ -145,28 +113,63 @@ var versionCmd = &cobra.Command{
cobra.CheckErr(err)
}

vSphereVersion, _ := r.FetchVsphereVersions(ctx, req, &vdoConfig)
fmt.Printf("\nvSphere Versions : %s", vSphereVersion)

k8sVersion, _ := r.Fetchk8sVersions(ctx)
fmt.Printf("\nkubernetes Version : %s", k8sVersion)
vsphereVersion, _ = r.FetchVsphereVersions(ctx, req, &vdoConfig)

err = r.FetchCsiDeploymentYamls(ctx, matrixConfig, vSphereVersion, k8sVersion)
err = r.FetchCsiDeploymentYamls(ctx, matrixConfig, vsphereVersion, k8sVersion)
if err != nil {
cobra.CheckErr(err)
}
fmt.Printf("\nCSI Version : %s", r.CurrentCSIDeployedVersion)
csiVersion = r.CurrentCSIDeployedVersion

if len(vdoConfig.Spec.CloudProvider.VsphereCloudConfigs) > 0 {
err = r.FetchCpiDeploymentYamls(ctx, matrixConfig, vSphereVersion, k8sVersion)
err = r.FetchCpiDeploymentYamls(ctx, matrixConfig, vsphereVersion, k8sVersion)
if err != nil {
cobra.CheckErr(err)
}
fmt.Printf("\nCPI Version : %s\n", r.CurrentCPIDeployedVersion)
cpiVersion = r.CurrentCPIDeployedVersion
}
showVersionInfo()
},
}

func getK8sVersion() string {
discoveryClient, err := discovery.NewDiscoveryClientForConfig(ClientConfig)
if err != nil {
cobra.CheckErr(err)
}

k8sServerVersion, err := discoveryClient.ServerVersion()
if err != nil {
cobra.CheckErr(err)
}

return k8sServerVersion.Major + "." + k8sServerVersion.Minor
}

func getVdoVersion(vdoDeployment *v12.Deployment) string {
containersList := vdoDeployment.Spec.Template.Spec.Containers
var containerImage string
for _, container := range containersList {
if strings.Contains(container.Name, "manager") {
containerImage = container.Image
break
}
}
if containerImage == "" {
cobra.CheckErr("Unable to find the VDO manager container")
}
vdoVersionInfo := strings.Split(fmt.Sprint(containerImage), ":")
return vdoVersionInfo[len(vdoVersionInfo)-1]
}

func showVersionInfo() {
fmt.Printf("kubernetes Version : %s", k8sVersion)
fmt.Printf("\nVDO Version : %s", vdoVersion)
fmt.Printf("\nvSphere Versions : %s", vsphereVersion)
fmt.Printf("\nCSI Version : %s", csiVersion)
fmt.Printf("\nCPI Version : %s\n", cpiVersion)
}

func init() {
rootCmd.AddCommand(versionCmd)
}

0 comments on commit 227bcea

Please sign in to comment.