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

[controller] Fixed default Storage Class validation #32

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions images/webhooks/src/validators/storageClassUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ package validators

import (
"context"
"fmt"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"webhooks/v1alpha1"

"github.com/slok/kubewebhook/v2/pkg/model"
Expand All @@ -42,6 +46,29 @@ func StorageClassUpdate(ctx context.Context, _ *model.AdmissionReview, obj metav
}
}

if sc.Spec.IsDefault == true {
config, err := rest.InClusterConfig()
if err != nil {
klog.Fatal(err.Error())
}

staticClient, err := kubernetes.NewForConfig(config)
if err != nil {
klog.Fatal(err)
}

storageClasses, _ := staticClient.StorageV1().StorageClasses().List(context.TODO(), metav1.ListOptions{})
for _, storageClass := range storageClasses.Items {
for label, value := range storageClass.GetObjectMeta().GetAnnotations() {
if label == "storageclass.kubernetes.io/is-default-class" && value == "true" && storageClass.Name != sc.Name {
klog.Infof("Default StorageClass already set: %s", storageClass.Name)
return &kwhvalidating.ValidatorResult{Valid: false, Message: fmt.Sprintf("Default StorageClass already set: %s", storageClass.Name)},
nil
}
}
}
}

if thinExists && sc.Spec.LVM.Type == "Thick" {
return &kwhvalidating.ValidatorResult{Valid: false, Message: "there must be only thick pools with Thick LVM type"},
nil
Expand Down
1 change: 1 addition & 0 deletions templates/webhooks/rbac-for-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ rules:
- storage.k8s.io
verbs:
- get
- list
resources:
- storageclasses

Expand Down