addons: mark the last enabled storage provider as default

When a storage provider is enabled (storage-provisioner-glusterfile),
mark it "is-default" and set "is-default" to "false" in all other
StorageClasses.
pull/3521/head
Niels de Vos 2018-12-31 16:35:15 +01:00
parent b0c7341373
commit b85b6d5eeb
4 changed files with 47 additions and 38 deletions

View File

@ -144,7 +144,7 @@ var settings = []Setting{
name: "default-storageclass", name: "default-storageclass",
set: SetBool, set: SetBool,
validations: []setFn{IsValidAddon}, validations: []setFn{IsValidAddon},
callbacks: []setFn{EnableOrDisableAddon}, callbacks: []setFn{EnableOrDisableStorageClasses},
}, },
{ {
name: "heapster", name: "heapster",
@ -186,7 +186,7 @@ var settings = []Setting{
name: "default-storageclass", name: "default-storageclass",
set: SetBool, set: SetBool,
validations: []setFn{IsValidAddon}, validations: []setFn{IsValidAddon},
callbacks: []setFn{EnableOrDisableDefaultStorageClass}, callbacks: []setFn{EnableOrDisableStorageClasses},
}, },
{ {
name: "storage-provisioner", name: "storage-provisioner",

View File

@ -26,6 +26,7 @@ import (
"k8s.io/minikube/pkg/minikube/assets" "k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/cluster" "k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/machine" "k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/storageclass" "k8s.io/minikube/pkg/minikube/storageclass"
) )
@ -138,38 +139,28 @@ func EnableOrDisableAddon(name string, val string) error {
return nil 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 { func EnableOrDisableStorageClasses(name, val string) error {
enable, err := strconv.ParseBool(val) enable, err := strconv.ParseBool(val)
if err != nil { if err != nil {
return errors.Wrap(err, "Error parsing boolean") return errors.Wrap(err, "Error parsing boolean")
} }
// Only StorageClass for 'name' should stay enabled class := constants.DefaultStorageClassProvisioner
if enable { if name == "storage-provisioner-gluster" {
class := "standard" class = "glusterfile"
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 { 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)
} }
} }

View File

@ -6,7 +6,7 @@ metadata:
labels: labels:
k8s-app: storage-provisioner-gluster k8s-app: storage-provisioner-gluster
kubernetes.io/minikube-addons: storage-provisioner-gluster kubernetes.io/minikube-addons: storage-provisioner-gluster
addonmanager.kubernetes.io/mode: Reconcile addonmanager.kubernetes.io/mode: EnsureExists
annotations: annotations:
storageclass.beta.kubernetes.io/is-default-class: "true" storageclass.beta.kubernetes.io/is-default-class: "true"
provisioner: gluster.org/glusterfile provisioner: gluster.org/glusterfile

View File

@ -17,17 +17,28 @@ limitations under the License.
package storageclass package storageclass
import ( import (
"strconv"
"github.com/pkg/errors" "github.com/pkg/errors"
"k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd" "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 // DisableDefaultStorageClass disables the default storage class provisioner
// The addon-manager and kubectl apply cannot delete storageclasses // The addon-manager and kubectl apply cannot delete storageclasses
func DisableDefaultStorageClass() error { func DisableDefaultStorageClass(class string) error {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}) kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
config, err := kubeConfig.ClientConfig() config, err := kubeConfig.ClientConfig()
@ -39,15 +50,22 @@ func DisableDefaultStorageClass() error {
return errors.Wrap(err, "Error creating new client from kubeConfig.ClientConfig()") 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 { 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 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() loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}) kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
config, err := kubeConfig.ClientConfig() config, err := kubeConfig.ClientConfig()
@ -65,13 +83,13 @@ func DisableAllOtherStorageClasses(name string) error {
} }
for _, sc := range scList.Items { for _, sc := range scList.Items {
if sc.Name == name { err = annotateDefaultStorageClass(client, &sc, sc.Name == name)
continue
}
err = client.Storage().StorageClasses().Delete(sc.Name, &metav1.DeleteOptions{})
if err != nil { 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)
} }
} }