From 89144af6fd05ae0d2e1219b0706ab3e3f2a55eab Mon Sep 17 00:00:00 2001 From: jjanik <11janci@seznam.cz> Date: Fri, 4 Sep 2020 16:59:11 +0200 Subject: [PATCH] second --- pkg/addons/config.go | 1 + pkg/addons/validations.go | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pkg/addons/config.go b/pkg/addons/config.go index eb5e6ab8e876..935f9c540b8b 100644 --- a/pkg/addons/config.go +++ b/pkg/addons/config.go @@ -179,6 +179,7 @@ var Addons = []*Addon{ { name: "csi-hostpath-driver", set: SetBool, + validations: []setFn{HasVolumesnapshotsEnabled}, callbacks: []setFn{enableOrDisableAddon, verifyAddonStatus}, }, } diff --git a/pkg/addons/validations.go b/pkg/addons/validations.go index 2661ac819798..8b434d2592ab 100644 --- a/pkg/addons/validations.go +++ b/pkg/addons/validations.go @@ -18,11 +18,15 @@ package addons import ( "fmt" - + "k8s.io/minikube/pkg/minikube/assets" "k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/cruntime" + "k8s.io/minikube/pkg/minikube/out" + "strconv" ) +const volumesnapshotsAddon = "volumesnapshots" + // containerdOnlyMsg is the message shown when a containerd-only addon is enabled const containerdOnlyAddonMsg = ` This addon can only be enabled with the containerd runtime backend. To enable this backend, please first stop minikube with: @@ -33,6 +37,12 @@ and then start minikube again with the following flags: minikube start --container-runtime=containerd --docker-opt containerd=/var/run/containerd/containerd.sock` +// volumesnapshotsDisabledMsg is the message shown when csi-hostpath-driver addon is enabled without the volumesnapshots addon +const volumesnapshotsDisabledMsg = `[WARNING] For full functionality, the 'csi-hostpath-driver' addon requires the 'volumesnapshots' addon to be enabled. + +You can enable 'volumesnapshots' addon by running: 'minikube addons enable volumesnapshots' +` + // IsRuntimeContainerd is a validator which returns an error if the current runtime is not containerd func IsRuntimeContainerd(cc *config.ClusterConfig, _, _ string) error { r, err := cruntime.New(cruntime.Config{Type: cc.KubernetesConfig.ContainerRuntime}) @@ -46,6 +56,20 @@ func IsRuntimeContainerd(cc *config.ClusterConfig, _, _ string) error { return nil } +// IsRuntimeContainerd is a validator which returns an error if the current runtime is not containerd +func HasVolumesnapshotsEnabled(cc *config.ClusterConfig, _, value string) error { + isCsiDriverEnabled, _ := strconv.ParseBool(value) + // assets.Addons[x].IsEnabled() returns the current status of the addon or default value. + // config.AddonList contains list of addons to be enabled. + isVolumesnapshotsEnabled := assets.Addons[volumesnapshotsAddon].IsEnabled(cc) || contains(config.AddonList, volumesnapshotsAddon) + if isCsiDriverEnabled && !isVolumesnapshotsEnabled { + // just print out a warning directly, we don't want to return any errors since + // that would prevent the addon from being enabled (callbacks wouldn't be run) + out.WarningT(volumesnapshotsDisabledMsg) + } + return nil +} + // isAddonValid returns the addon, true if it is valid // otherwise returns nil, false func isAddonValid(name string) (*Addon, bool) { @@ -56,3 +80,12 @@ func isAddonValid(name string) (*Addon, bool) { } return nil, false } + +func contains(slice []string, val string) bool { + for _, item := range slice { + if item == val { + return true + } + } + return false +}