diff --git a/images/webhooks/src/handlers/nscValidator.go b/images/webhooks/src/handlers/nscValidator.go index 7fedc73..13e7a8b 100644 --- a/images/webhooks/src/handlers/nscValidator.go +++ b/images/webhooks/src/handlers/nscValidator.go @@ -24,43 +24,83 @@ func NSCValidate(ctx context.Context, arReview *model.AdmissionReview, obj metav return &kwhvalidating.ValidatorResult{}, nil } - if nsc.Spec.Connection.NFSVersion == "3" { - cl, err := NewKubeClient("") - if err != nil { - klog.Fatal(err) + v3presents := false + v3enabled := false + + cl, err := NewKubeClient("") + if err != nil { + klog.Fatal(err) + } + + listClasses := &cn.NFSStorageClassList{} + err = cl.List(ctx, listClasses) + + if nsc.ObjectMeta.DeletionTimestamp == nil && arReview.Operation != "delete" && nsc.Spec.Connection.NFSVersion == "3" { + v3presents = true + } + + for _, itemClass := range listClasses.Items { + if itemClass.Name == nsc.Name { + continue + } + if itemClass.Spec.Connection.NFSVersion == "3" { + v3presents = true } + } + + klog.Infof("NFSv3 NFSStorageClass exists: %t", v3presents) + + nfsModuleConfig := &dh.ModuleConfig{} + + err = cl.Get(ctx, types.NamespacedName{Name: csiNfsModuleName, Namespace: ""}, nfsModuleConfig) + if err != nil { + klog.Fatal(err) + } + + if value, exists := nfsModuleConfig.Spec.Settings["v3support"]; exists && value == true { + v3enabled = true + } else { + v3enabled = false + } - nfsModuleConfig := &dh.ModuleConfig{} + klog.Infof("NFSv3 support enabled: %t", v3enabled) - err = cl.Get(ctx, types.NamespacedName{Name: csiNfsModuleName, Namespace: ""}, nfsModuleConfig) + if v3presents && !v3enabled { + klog.Info("Enabling v3 support") + patchBytes, err := json.Marshal(map[string]interface{}{ + "spec": map[string]interface{}{ + "settings": map[string]interface{}{ + "v3support": true, + }, + }, + }) if err != nil { - klog.Fatal(err) + klog.Fatalf("Error marshalling patch: %s", err.Error()) } - if value, exists := nfsModuleConfig.Spec.Settings["v3support"]; exists && value == true { - klog.Info("v3 support is enabled") - } else { - klog.Info("Enabling v3 support") - patchBytes, err := json.Marshal(map[string]interface{}{ - "spec": map[string]interface{}{ - "settings": map[string]interface{}{ - "v3support": true, - }, + err = cl.Patch(context.TODO(), nfsModuleConfig, client.RawPatch(types.MergePatchType, patchBytes)) + if err != nil { + klog.Fatalf("Error patching object: %s", err.Error()) + } + } else if !v3presents && v3enabled { + klog.Info("Disabling v3 support") + patchBytes, err := json.Marshal(map[string]interface{}{ + "spec": map[string]interface{}{ + "settings": map[string]interface{}{ + "v3support": false, }, - }) - - if err != nil { - klog.Fatalf("Error marshalling patch: %s", err.Error()) - } - - err = cl.Patch(context.TODO(), nfsModuleConfig, client.RawPatch(types.MergePatchType, patchBytes)) - if err != nil { - klog.Fatalf("Error patching object: %s", err.Error()) - } + }, + }) + if err != nil { + klog.Fatalf("Error marshalling patch: %s", err.Error()) } - klog.Info() + err = cl.Patch(context.TODO(), nfsModuleConfig, client.RawPatch(types.MergePatchType, patchBytes)) + if err != nil { + klog.Fatalf("Error patching object: %s", err.Error()) + } } + return &kwhvalidating.ValidatorResult{Valid: true}, nil } diff --git a/templates/webhooks/webhook.yaml b/templates/webhooks/webhook.yaml index 4b4126d..0ecd1b7 100644 --- a/templates/webhooks/webhook.yaml +++ b/templates/webhooks/webhook.yaml @@ -31,7 +31,7 @@ webhooks: rules: - apiGroups: ["storage.deckhouse.io"] apiVersions: ["v1alpha1"] - operations: ["CREATE", "UPDATE"] + operations: ["CREATE", "UPDATE", "DELETE"] resources: ["nfsstorageclasses"] scope: "Cluster" clientConfig: