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
parent
b0c7341373
commit
b85b6d5eeb
|
@ -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",
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue