Skip to content

Commit

Permalink
Updated the version check of vSphere CSI to adapt both release and de…
Browse files Browse the repository at this point in the history
…v manifests

Signed-off-by: Lintong Jiang <[email protected]>
  • Loading branch information
Lintong Jiang committed Dec 12, 2020
1 parent 8df7800 commit 85f12f3
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 60 deletions.
15 changes: 8 additions & 7 deletions pkg/cmd/backupdriver/cli/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,21 @@ func (o *InstallOptions) Run(c *cobra.Command, f client.Factory) error {
// Start with a few of prerequisite checks before installing backup-driver
fmt.Println("The prerequisite checks for backup-driver started")

// Check vSphere CSI driver version
_ = cmd.CheckVSphereCSIDriverVersion(kubeClient)

// Check velero version
_ = cmd.CheckVeleroVersion(kubeClient, o.Namespace)

// Check velero-plugin-for-vsphere image repo and parse the corresponding image for backup-driver
o.Image, _ = cmd.CheckPluginImageRepo(kubeClient, o.Namespace, o.Image, constants.BackupDriverForPlugin)

// Check cluster flavor for backup-driver
if err := o.CheckClusterFlavorForBackupDriver(); err != nil {
clusterFlavor, err := o.CheckClusterFlavorForBackupDriver()
if err != nil {
return err
}

// Check vSphere CSI driver version
_ = cmd.CheckVSphereCSIDriverVersion(kubeClient, clusterFlavor)

// Check feature flags for backup-driver
if err := o.CheckFeatureFlagsForBackupDriver(kubeClient); err != nil {
return err
Expand Down Expand Up @@ -226,10 +227,10 @@ func (o *InstallOptions) Complete(args []string, f client.Factory) error {
return nil
}

func (o *InstallOptions) CheckClusterFlavorForBackupDriver() error {
func (o *InstallOptions) CheckClusterFlavorForBackupDriver() (constants.ClusterFlavor, error) {
clusterFlavor, err := utils.GetClusterFlavor(nil)
if err != nil {
return errors.Wrap(err, "Failed to get cluster flavor for backup-driver")
return constants.Unknown, errors.Wrap(err, "Failed to get cluster flavor for backup-driver")
}

// Assign master node affinity and host network to Supervisor deployment
Expand All @@ -239,7 +240,7 @@ func (o *InstallOptions) CheckClusterFlavorForBackupDriver() error {
o.HostNetwork = true
}

return nil
return clusterFlavor, nil
}

func (o *InstallOptions) CheckFeatureFlagsForBackupDriver(kubeClient kubernetes.Interface) error {
Expand Down
8 changes: 5 additions & 3 deletions pkg/cmd/datamgr/cli/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (o *InstallOptions) Run(c *cobra.Command, f client.Factory) error {
fmt.Println("The prerequisite checks for data-manager started")

// Check cluster flavor for data-manager
o.CheckClusterFlavorForDataManager()
clusterFlavor := o.CheckClusterFlavorForDataManager()

// Check feature flags for data-manager
_ = o.CheckFeatureFlagsForDataManager(kubeClient)
Expand All @@ -160,7 +160,7 @@ func (o *InstallOptions) Run(c *cobra.Command, f client.Factory) error {
}

// Check vSphere CSI driver version
_ = cmd.CheckVSphereCSIDriverVersion(kubeClient)
_ = cmd.CheckVSphereCSIDriverVersion(kubeClient, clusterFlavor)

// Check velero version
_ = cmd.CheckVeleroVersion(kubeClient, o.Namespace)
Expand Down Expand Up @@ -271,14 +271,16 @@ func (o *InstallOptions) getNumberOfNodes(kubeClient kubernetes.Interface) (int,
return len(nodeList.Items), nil
}

func (o *InstallOptions) CheckClusterFlavorForDataManager() {
func (o *InstallOptions) CheckClusterFlavorForDataManager() constants.ClusterFlavor {
clusterFlavor, _ := utils.GetClusterFlavor(nil)

// In case of Guest or Supervisor cluster, skip installing data manager
if clusterFlavor == constants.TkgGuest || clusterFlavor == constants.Supervisor {
fmt.Printf("The Cluster Flavor: %s. Skipping data manager installation.\n", clusterFlavor)
o.SkipInstall = true
}

return clusterFlavor
}

func (o *InstallOptions) CheckFeatureFlagsForDataManager(kubeClient kubernetes.Interface) error {
Expand Down
80 changes: 38 additions & 42 deletions pkg/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func GetVersionFromImage(containers []v1.Container, imageName string) string {
var tag = ""
for _, container := range containers {
if strings.Contains(container.Image, imageName) {
tag = strings.Split(container.Image, ":")[1]
tag = utils.GetComponentFromImage(container.Image, constants.ImageVersionComponent)
break
}
}
Expand Down Expand Up @@ -195,55 +195,56 @@ func CompareVersion(currentVersion string, minVersion string) int {
current, _ := version.NewVersion(currentVersion)
minimum, _ := version.NewVersion(minVersion)

if current == nil || minimum == nil {
return -1
}
return current.Compare(minimum)
}

func CheckCSIVersion(containers []v1.Container) (bool, bool, error) {
isVersionOK := false
func CheckCSIVersion(containers []v1.Container) error {
csi_driver_version := GetVersionFromImage(containers, "cloud-provider-vsphere/csi/release/driver")
if csi_driver_version == "" {
csi_driver_version = GetVersionFromImage(containers, "cloudnativestorage/vsphere-csi")
csi_driver_version = GetVersionFromImage(containers, "cloud-provider-vsphere/csi/ci/driver")
if csi_driver_version != "" {
fmt.Printf("Got pre-relase version %s from container cloudnativestorage/vsphere-csi, setting version to min version %s\n",
csi_driver_version, constants.CsiMinVersion)
fmt.Printf("Got a prerelease version %s from container cloud-provider-vsphere/csi/ci/driver. Ignored it\n",
csi_driver_version)
csi_driver_version = constants.CsiMinVersion
}
}

csi_syncer_version := GetVersionFromImage(containers, "cloud-provider-vsphere/csi/release/syncer")
if csi_syncer_version == "" {
csi_syncer_version = GetVersionFromImage(containers, "cloudnativestorage/syncer")
csi_syncer_version = GetVersionFromImage(containers, "cloud-provider-vsphere/csi/ci/syncer")
if csi_syncer_version != "" {
fmt.Printf("Got pre-relase version %s from container cloudnativestorage/syncer, setting version to min version %s\n",
csi_syncer_version, constants.CsiMinVersion)
fmt.Printf("Got a prerelease version %s from container cloud-provider-vsphere/csi/ci/syncer. Ignored it\n",
csi_syncer_version)
csi_syncer_version = constants.CsiMinVersion
}
}
if CompareVersion(csi_driver_version, constants.CsiMinVersion) >= 0 && CompareVersion(csi_syncer_version, constants.CsiMinVersion) >= 0 {
isVersionOK = true
}
return true, isVersionOK, nil
}

func CheckCSIInstalled(kubeClient kubernetes.Interface) (bool, bool, error) {
statefulsetList, err := kubeClient.AppsV1().StatefulSets("kube-system").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, false, err
if csi_driver_version == "" || csi_syncer_version == "" {
return errors.New("Expected CSI driver/syncer images not found")
}
for _, item := range statefulsetList.Items {
if item.GetName() == "vsphere-csi-controller" {
return CheckCSIVersion(item.Spec.Template.Spec.Containers)
}

if CompareVersion(csi_driver_version, constants.CsiMinVersion) < 0 || CompareVersion(csi_syncer_version, constants.CsiMinVersion) < 0 {
return errors.Errorf("The version of vSphere CSI controller is below the minimum requirement (%s)", constants.CsiMinVersion)
}
deploymentList, err := kubeClient.AppsV1().Deployments("kube-system").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, false, err

return nil
}

func CheckCSIInstalled(kubeClient kubernetes.Interface) error {
csiStatefulset, err := kubeClient.AppsV1().StatefulSets(constants.KubeSystemNamespace).Get(context.TODO(), constants.VSphereCSIController, metav1.GetOptions{})
if err == nil {
return CheckCSIVersion(csiStatefulset.Spec.Template.Spec.Containers)
}
for _, item := range deploymentList.Items {
if item.Name == "vsphere-csi-controller" {
return CheckCSIVersion(item.Spec.Template.Spec.Containers)
}

csiDeployment, err := kubeClient.AppsV1().Deployments(constants.KubeSystemNamespace).Get(context.TODO(), constants.VSphereCSIController, metav1.GetOptions{})
if err == nil {
return CheckCSIVersion(csiDeployment.Spec.Template.Spec.Containers)
}
return false, false, nil

return errors.Errorf("vSphere CSI controller, %s, is required by velero-plugin-for-vsphere. Please make sure the vSphere CSI controller is installed in the cluster", constants.VSphereCSIController)
}

func BuildConfig(master, kubeConfig string, f client.Factory) (*rest.Config, error) {
Expand Down Expand Up @@ -291,20 +292,15 @@ func GetCompatibleRepoAndTagFromPluginImage(kubeClient kubernetes.Interface, nam
return resultImage, nil
}

func CheckVSphereCSIDriverVersion(kubeClient kubernetes.Interface) error {
isCSIInstalled, isVersionOk, err := CheckCSIInstalled(kubeClient)
if err != nil {
fmt.Println("CSI driver check failed")
isCSIInstalled = false
isVersionOk = false
func CheckVSphereCSIDriverVersion(kubeClient kubernetes.Interface, clusterFlavor constants.ClusterFlavor) error {
if clusterFlavor != constants.VSphere {
fmt.Println("Skipped the version check of CSI driver if it is not in a Vanilla cluster")
return nil
}

if !isCSIInstalled {
fmt.Println("Velero Plug-in for vSphere requires vSphere CSI/CNS and vSphere 6.7U3 to function. Please install the vSphere CSI/CNS driver")
}

if !isVersionOk {
fmt.Printf("vSphere CSI driver version is prior to %s. Velero Plug-in for vSphere requires CSI driver version to be %s or above\n", constants.CsiMinVersion, constants.CsiMinVersion)
err := CheckCSIInstalled(kubeClient)
if err != nil {
fmt.Printf("Failed the version check of CSI driver. Error: %v\n", err)
}

return err
Expand Down
Loading

0 comments on commit 85f12f3

Please sign in to comment.