Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed get/set Default flag for StorageClasses #204

Merged
merged 2 commits into from
Jun 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions pkg/util/k8sutil/storageclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,67 @@
package k8sutil

import (
"fmt"
"strconv"
"time"

"github.com/arangodb/arangosync/pkg/retry"
"k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
storagev1 "k8s.io/client-go/kubernetes/typed/storage/v1"
)

const (
annStorageClassIsDefault = "storageclass.kubernetes.io/is-default-class"
var (
annStorageClassIsDefault = []string{
// Make sure first entry is the one we'll put in
"storageclass.kubernetes.io/is-default-class",
"storageclass.beta.kubernetes.io/is-default-class",
}
)

// StorageClassIsDefault returns true if the given storage class is marked default,
// false otherwise.
func StorageClassIsDefault(sc *v1.StorageClass) bool {
value, found := sc.GetObjectMeta().GetAnnotations()[annStorageClassIsDefault]
if !found {
return false
}
boolValue, err := strconv.ParseBool(value)
if err != nil {
return false
for _, key := range annStorageClassIsDefault {
if value, found := sc.GetObjectMeta().GetAnnotations()[key]; found {
if boolValue, err := strconv.ParseBool(value); err == nil && boolValue {
return true
}
}
}
return boolValue
return false
}

// PatchStorageClassIsDefault changes the default flag of the given storage class.
func PatchStorageClassIsDefault(cli storagev1.StorageV1Interface, name string, isDefault bool) error {
jsonPatch := fmt.Sprintf(`{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"%v"}}}`, isDefault)
if _, err := cli.StorageClasses().Patch(name, types.StrategicMergePatchType, []byte(jsonPatch)); err != nil {
stcs := cli.StorageClasses()
op := func() error {
// Fetch current version of StorageClass
current, err := stcs.Get(name, metav1.GetOptions{})
if IsNotFound(err) {
return retry.Permanent(maskAny(err))
} else if err != nil {
return maskAny(err)
}
// Tweak annotations
ann := current.GetAnnotations()
if ann == nil {
ann = make(map[string]string)
}
for _, key := range annStorageClassIsDefault {
delete(ann, key)
}
ann[annStorageClassIsDefault[0]] = strconv.FormatBool(isDefault)
current.SetAnnotations(ann)
// Save StorageClass
if _, err := stcs.Update(current); IsConflict(err) {
// StorageClass has been modified since we read it
return maskAny(err)
} else if err != nil {
return retry.Permanent(maskAny(err))
}
return nil
}
if err := retry.Retry(op, time.Second*15); err != nil {
return maskAny(err)
}
return nil
Expand Down