diff --git a/cmd/minikube/cmd/config/config.go b/cmd/minikube/cmd/config/config.go index 763922f60e..a61325e135 100644 --- a/cmd/minikube/cmd/config/config.go +++ b/cmd/minikube/cmd/config/config.go @@ -144,7 +144,7 @@ var settings = []Setting{ name: "default-storageclass", set: SetBool, validations: []setFn{IsValidAddon}, - callbacks: []setFn{EnableOrDisableAddon}, + callbacks: []setFn{EnableOrDisableStorageClasses}, }, { name: "heapster", @@ -186,7 +186,7 @@ var settings = []Setting{ name: "default-storageclass", set: SetBool, validations: []setFn{IsValidAddon}, - callbacks: []setFn{EnableOrDisableDefaultStorageClass}, + callbacks: []setFn{EnableOrDisableStorageClasses}, }, { name: "storage-provisioner", diff --git a/cmd/minikube/cmd/config/util.go b/cmd/minikube/cmd/config/util.go index aaafd11b7b..683fd5d260 100644 --- a/cmd/minikube/cmd/config/util.go +++ b/cmd/minikube/cmd/config/util.go @@ -26,6 +26,7 @@ import ( "k8s.io/minikube/pkg/minikube/assets" "k8s.io/minikube/pkg/minikube/cluster" "k8s.io/minikube/pkg/minikube/config" + "k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/machine" "k8s.io/minikube/pkg/minikube/storageclass" ) @@ -138,38 +139,28 @@ func EnableOrDisableAddon(name string, val string) error { return nil } -func EnableOrDisableDefaultStorageClass(name, val string) error { - enable, err := strconv.ParseBool(val) - if err != nil { - return errors.Wrap(err, "Error parsing boolean") - } - - // Special logic to disable the default storage class - if !enable { - err := storageclass.DisableDefaultStorageClass() - if err != nil { - return errors.Wrap(err, "Error disabling default storage class") - } - } - return EnableOrDisableAddon(name, val) -} - func EnableOrDisableStorageClasses(name, val string) error { enable, err := strconv.ParseBool(val) if err != nil { return errors.Wrap(err, "Error parsing boolean") } - // Only StorageClass for 'name' should stay enabled - if enable { - class := "standard" - if name == "storage-provisioner-gluster" { - class = "glusterfile" - } + class := constants.DefaultStorageClassProvisioner + if name == "storage-provisioner-gluster" { + class = "glusterfile" + } - err := storageclass.DisableAllOtherStorageClasses(class) + if enable { + // Only StorageClass for 'name' should be marked as default + err := storageclass.SetDefaultStorageClass(class) if err != nil { - return errors.Wrap(err, "Error disabling storage classes") + return errors.Wrapf(err, "Error making %s the default storage class", class) + } + } else { + // Unset the StorageClass as default + err := storageclass.DisableDefaultStorageClass(class) + if err != nil { + return errors.Wrapf(err, "Error disabling %s as the default storage class", class) } } diff --git a/deploy/addons/storage-provisioner-gluster/storage-provisioner-glusterfile.yaml b/deploy/addons/storage-provisioner-gluster/storage-provisioner-glusterfile.yaml index f6d672f98f..88c7a5ab89 100644 --- a/deploy/addons/storage-provisioner-gluster/storage-provisioner-glusterfile.yaml +++ b/deploy/addons/storage-provisioner-gluster/storage-provisioner-glusterfile.yaml @@ -6,7 +6,7 @@ metadata: labels: k8s-app: storage-provisioner-gluster kubernetes.io/minikube-addons: storage-provisioner-gluster - addonmanager.kubernetes.io/mode: Reconcile + addonmanager.kubernetes.io/mode: EnsureExists annotations: storageclass.beta.kubernetes.io/is-default-class: "true" provisioner: gluster.org/glusterfile diff --git a/pkg/minikube/storageclass/storageclass.go b/pkg/minikube/storageclass/storageclass.go index ab807861f1..7762cb9b85 100644 --- a/pkg/minikube/storageclass/storageclass.go +++ b/pkg/minikube/storageclass/storageclass.go @@ -17,17 +17,28 @@ limitations under the License. package storageclass import ( + "strconv" + "github.com/pkg/errors" + "k8s.io/api/storage/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" - "k8s.io/minikube/pkg/minikube/constants" ) +func annotateDefaultStorageClass(client *kubernetes.Clientset, class *v1.StorageClass, enable bool) error { + isDefault := strconv.FormatBool(enable) + + metav1.SetMetaDataAnnotation(&class.ObjectMeta, "storageclass.beta.kubernetes.io/is-default-class", isDefault) + _, err := client.Storage().StorageClasses().Update(class) + + return err +} + // DisableDefaultStorageClass disables the default storage class provisioner // The addon-manager and kubectl apply cannot delete storageclasses -func DisableDefaultStorageClass() error { +func DisableDefaultStorageClass(class string) error { loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}) config, err := kubeConfig.ClientConfig() @@ -39,15 +50,22 @@ func DisableDefaultStorageClass() error { return errors.Wrap(err, "Error creating new client from kubeConfig.ClientConfig()") } - err = client.Storage().StorageClasses().Delete(constants.DefaultStorageClassProvisioner, &metav1.DeleteOptions{}) + sc, err := client.Storage().StorageClasses().Get(class, metav1.GetOptions{}) if err != nil { - return errors.Wrapf(err, "Error deleting default storage class %s", constants.DefaultStorageClassProvisioner) + return errors.Wrapf(err, "Error getting storage class %s", class) + } + + err = annotateDefaultStorageClass(client, sc, false) + if err != nil { + return errors.Wrapf(err, "Error marking storage class %s as non-default", class) } return nil } -func DisableAllOtherStorageClasses(name string) error { +// SetDefaultStorageClass makes sure onlt the class with @name is marked as +// default. +func SetDefaultStorageClass(name string) error { loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}) config, err := kubeConfig.ClientConfig() @@ -65,13 +83,13 @@ func DisableAllOtherStorageClasses(name string) error { } for _, sc := range scList.Items { - if sc.Name == name { - continue - } - - err = client.Storage().StorageClasses().Delete(sc.Name, &metav1.DeleteOptions{}) + err = annotateDefaultStorageClass(client, &sc, sc.Name == name) if err != nil { - return errors.Wrapf(err, "Error deleting storage class %s", sc.Name) + isDefault := "non-default" + if sc.Name == name { + isDefault = "default" + } + return errors.Wrapf(err, "Error while marking storage class %s as %s", sc.Name, isDefault) } }