Skip to content

Commit

Permalink
Fixed retrieving feature flag of container from hardcoded image name
Browse files Browse the repository at this point in the history
Signed-off-by: Lintong Jiang <[email protected]>
  • Loading branch information
Lintong Jiang committed Nov 19, 2020
1 parent d1e4391 commit eb2e9c7
Show file tree
Hide file tree
Showing 10 changed files with 1,030 additions and 491 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/evanphx/json-patch v4.5.0+incompatible
github.com/google/uuid v1.1.1
github.com/hashicorp/go-hclog v0.8.0 // indirect
github.com/hashicorp/go-version v1.2.1
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ github.com/hashicorp/go-hclog v0.8.0/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrj
github.com/hashicorp/go-plugin v0.0.0-20190610192547-a1bc61569a26 h1:sADP8l/FAtMyWJ9GIcQT/04Ae80ZZ75ogOrtW0DIZhc=
github.com/hashicorp/go-plugin v0.0.0-20190610192547-a1bc61569a26/go.mod h1:++UyYGoz3o5w9ZzAdZxtQKrWWP+iqPBn3cQptSMzBuY=
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru v0.0.0-20180201235237-0fb14efe8c47/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
Expand Down
137 changes: 54 additions & 83 deletions pkg/cmd/backupdriver/cli/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package install
import (
"context"
"fmt"
"strings"

"github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/constants"

"github.com/pkg/errors"
Expand Down Expand Up @@ -115,66 +113,41 @@ func NewCommand(f client.Factory) *cobra.Command {
}

func (o *InstallOptions) Run(c *cobra.Command, f client.Factory) error {
var resources *unstructured.UnstructuredList

// Check vSphere CSI driver version
isCSIInstalled, isVersionOk, err := cmd.CheckCSIInstalled(f)
kubeClient, err := f.KubeClient()
if err != nil {
fmt.Println("CSI driver check failed")
isCSIInstalled = false
isVersionOk = false
}
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)
return errors.Wrap(err, "Failed to get kubeClient")
}

// Check velero version
veleroVersion, err := cmd.GetVeleroVersion(f, o.Namespace)
if err != nil || veleroVersion == "" {
fmt.Println("Failed to get velero version.")
} else {
if cmd.CompareVersion(veleroVersion, constants.VeleroMinVersion) == -1 {
fmt.Printf("WARNING: Velero version %s is prior to %s. Velero Plug-in for vSphere requires velero version to be %s or above.\n", veleroVersion, constants.VeleroMinVersion, constants.VeleroMinVersion)
}
}
// Start with a few of prerequisite checks before installing backup-driver
fmt.Println("The prerequisite checks for backup-driver started")

// Check velero vsphere plugin image repo
err = o.CheckPluginImageRepo(f)
if err != nil {
fmt.Printf("Failed to check plugin image repo, error msg: %s. Using default image %s\n", err.Error(), o.Image)
} else {
fmt.Printf("Using image %s.\n", o.Image)
}
// Check vSphere CSI driver version
_ = cmd.CheckVSphereCSIDriverVersion(kubeClient)

// Check cluster flavor. Add the PV secret to pod in Guest Cluster
clusterFlavor, err := utils.GetClusterFlavor(nil)
// Assign master node affinity and host network to Supervisor deployment
if clusterFlavor == constants.Supervisor {
fmt.Printf("Supervisor Cluster. Assign master node affinity and enable host network.")
o.MasterAffinity = true
o.HostNetwork = true
}
// Check velero version
_ = cmd.CheckVeleroVersion(kubeClient, o.Namespace)

// Get feature flags from velero deployed in the namespace.
featureFlags, err := cmd.GetVeleroFeatureFlags(f, o.Namespace)
if err != nil {
fmt.Printf("Failed to decipher velero feature flags: %v, assuming none.\n", err)
// 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 {
return err
}

// Create Config Map with the feature states.
err = cmd.CreateFeatureStateConfigMap(featureFlags, f, o.Namespace)
if err != nil {
fmt.Printf("Failed to create feature state config map from velero feature flags: %v.\n", err)
// Check feature flags for backup-driver
if err := o.CheckFeatureFlagsForBackupDriver(kubeClient); err != nil {
return err
}

fmt.Println("The prerequisite checks for backup-driver completed")

vo, err := o.AsBackupDriverOptions()
if err != nil {
return err
}

var resources *unstructured.UnstructuredList
resources, err = install.AllBackupDriverResources(vo, true)
if err != nil {
return err
Expand All @@ -188,19 +161,19 @@ func (o *InstallOptions) Run(c *cobra.Command, f client.Factory) error {
if err != nil {
return err
}
factory := client.NewDynamicFactory(dynamicClient)
dynamicFactory := client.NewDynamicFactory(dynamicClient)

errorMsg := fmt.Sprintf("\n\nError installing backup-driver. Use `kubectl logs deploy/%s -n %s` to check the logs",
constants.BackupDriverForPlugin, o.Namespace)

err = install.Install(factory, resources, os.Stdout)
err = install.Install(dynamicFactory, resources, os.Stdout)
if err != nil {
return errors.Wrap(err, errorMsg)
}

fmt.Printf("Waiting for %s deployment to be ready.\n", constants.BackupDriverForPlugin)

if _, err = install.DeploymentIsReady(factory, o.Namespace); err != nil {
if _, err = install.DeploymentIsReady(dynamicFactory, o.Namespace); err != nil {
return errors.Wrap(err, errorMsg)
}

Expand All @@ -210,39 +183,6 @@ func (o *InstallOptions) Run(c *cobra.Command, f client.Factory) error {
return nil
}

func (o *InstallOptions) CheckPluginImageRepo(f client.Factory) error {
clientset, err := f.KubeClient()
if err != nil {
errMsg := fmt.Sprint("Failed to get clientset.")
return errors.New(errMsg)
}
deployment, err := clientset.AppsV1().Deployments(o.Namespace).Get(context.TODO(), constants.VeleroDeployment, metav1.GetOptions{})
if err != nil {
errMsg := fmt.Sprintf("Failed to get velero deployment in namespace %s", o.Namespace)
return errors.New(errMsg)
}

var repo string
var tag string
var image string
for _, container := range deployment.Spec.Template.Spec.InitContainers {
if strings.Contains(container.Image, constants.VeleroPluginForVsphere) {
image = container.Image
repo = utils.GetRepo(image)
tag = strings.Split(image, ":")[1]
break
}
}

if repo != "" && tag != "" {
o.Image = repo + "/" + constants.BackupDriverForPlugin + ":" + tag
return nil
} else {
errMsg := fmt.Sprintf("Failed to get repo and tag from velero plugin image %s.", image)
return errors.New(errMsg)
}
}

//Complete completes options for a command.
func (o *InstallOptions) Complete(args []string, f client.Factory) error {
fileName := "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
Expand Down Expand Up @@ -285,3 +225,34 @@ func (o *InstallOptions) Complete(args []string, f client.Factory) error {

return nil
}

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

// Assign master node affinity and host network to Supervisor deployment
if clusterFlavor == constants.Supervisor {
fmt.Printf("Supervisor Cluster. Assign master node affinity and enable host network.")
o.MasterAffinity = true
o.HostNetwork = true
}

return nil
}

func (o *InstallOptions) CheckFeatureFlagsForBackupDriver(kubeClient kubernetes.Interface) error {
featureFlags, err := cmd.GetVeleroFeatureFlags(kubeClient, o.Namespace)
if err != nil {
fmt.Printf("Failed to decipher velero feature flags: %v, assuming none.\n", err)
}

// Create Config Map with the feature states.
if err := cmd.CreateFeatureStateConfigMap(kubeClient, featureFlags, o.Namespace); err != nil {
fmt.Printf("Failed to create feature state config map from velero feature flags: %v.\n", err)
return err
}

return nil
}
Loading

0 comments on commit eb2e9c7

Please sign in to comment.