Skip to content

Commit

Permalink
Merge pull request #5356 from n0npax/improve-storageclass-test-coverage
Browse files Browse the repository at this point in the history
Improve storageclass test coverage
  • Loading branch information
tstromberg authored Sep 20, 2019
2 parents 11af1a3 + af5005a commit 35f45d1
Show file tree
Hide file tree
Showing 3 changed files with 340 additions and 37 deletions.
8 changes: 6 additions & 2 deletions cmd/minikube/cmd/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,20 @@ func EnableOrDisableStorageClasses(name, val string) error {
if name == "storage-provisioner-gluster" {
class = "glusterfile"
}
storagev1, err := storageclass.GetStoragev1()
if err != nil {
return errors.Wrapf(err, "Error getting storagev1 interface %v ", err)
}

if enable {
// Only StorageClass for 'name' should be marked as default
err := storageclass.SetDefaultStorageClass(class)
err = storageclass.SetDefaultStorageClass(storagev1, class)
if err != nil {
return errors.Wrapf(err, "Error making %s the default storage class", class)
}
} else {
// Unset the StorageClass as default
err := storageclass.DisableDefaultStorageClass(class)
err := storageclass.DisableDefaultStorageClass(storagev1, class)
if err != nil {
return errors.Wrapf(err, "Error disabling %s as the default storage class", class)
}
Expand Down
68 changes: 33 additions & 35 deletions pkg/minikube/storageclass/storageclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,67 +23,42 @@ import (
v1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

storagev1 "k8s.io/client-go/kubernetes/typed/storage/v1"
"k8s.io/client-go/tools/clientcmd"
)

func annotateDefaultStorageClass(client *kubernetes.Clientset, class *v1.StorageClass, enable bool) error {
func annotateDefaultStorageClass(storage storagev1.StorageV1Interface, class *v1.StorageClass, enable bool) error {
isDefault := strconv.FormatBool(enable)

metav1.SetMetaDataAnnotation(&class.ObjectMeta, "storageclass.beta.kubernetes.io/is-default-class", isDefault)
_, err := client.StorageV1().StorageClasses().Update(class)
_, err := storage.StorageClasses().Update(class)

return err
}

// DisableDefaultStorageClass disables the default storage class provisioner
// The addon-manager and kubectl apply cannot delete storageclasses
func DisableDefaultStorageClass(class string) error {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
config, err := kubeConfig.ClientConfig()
if err != nil {
return errors.Wrap(err, "Error creating kubeConfig")
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return errors.Wrap(err, "Error creating new client from kubeConfig.ClientConfig()")
}

sc, err := client.StorageV1().StorageClasses().Get(class, metav1.GetOptions{})
func DisableDefaultStorageClass(storage storagev1.StorageV1Interface, class string) error {
sc, err := storage.StorageClasses().Get(class, metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "Error getting storage class %s", class)
}

err = annotateDefaultStorageClass(client, sc, false)
err = annotateDefaultStorageClass(storage, sc, false)
if err != nil {
return errors.Wrapf(err, "Error marking storage class %s as non-default", class)
}

return nil
}

// SetDefaultStorageClass makes sure only 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()
if err != nil {
return errors.Wrap(err, "Error creating kubeConfig")
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return errors.Wrap(err, "Error creating new client from kubeConfig.ClientConfig()")
}

scList, err := client.StorageV1().StorageClasses().List(metav1.ListOptions{})
func SetDefaultStorageClass(storage storagev1.StorageV1Interface, name string) error {
scList, err := storage.StorageClasses().List(metav1.ListOptions{})
if err != nil {
return errors.Wrap(err, "Error listing StorageClasses")
}

for _, sc := range scList.Items {
err = annotateDefaultStorageClass(client, &sc, sc.Name == name)
err = annotateDefaultStorageClass(storage, &sc, sc.Name == name)
if err != nil {
isDefault := "non-default"
if sc.Name == name {
Expand All @@ -92,6 +67,29 @@ func SetDefaultStorageClass(name string) error {
return errors.Wrapf(err, "Error while marking storage class %s as %s", sc.Name, isDefault)
}
}

return nil
}

// GetStorageV1 return storage v1 interface for client
func GetStoragev1() (storagev1.StorageV1Interface, error) {
client, err := getClient()
if err != nil {
return nil, err
}
sv1 := client.StorageV1()
return sv1, nil
}

func getClient() (*kubernetes.Clientset, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
config, err := kubeConfig.ClientConfig()
if err != nil {
return nil, errors.Wrap(err, "Error creating kubeConfig")
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, errors.Wrap(err, "Error creating new client from kubeConfig.ClientConfig()")
}
return client, nil
}
Loading

0 comments on commit 35f45d1

Please sign in to comment.