Skip to content

Commit

Permalink
[controller] Version 3 logic changes (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: v.oleynikov <[email protected]>
  • Loading branch information
duckhawk authored Jul 26, 2024
1 parent 76363c2 commit e0cf18f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 28 deletions.
94 changes: 67 additions & 27 deletions images/webhooks/src/handlers/nscValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion templates/webhooks/webhook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ webhooks:
rules:
- apiGroups: ["storage.deckhouse.io"]
apiVersions: ["v1alpha1"]
operations: ["CREATE", "UPDATE"]
operations: ["CREATE", "UPDATE", "DELETE"]
resources: ["nfsstorageclasses"]
scope: "Cluster"
clientConfig:
Expand Down

0 comments on commit e0cf18f

Please sign in to comment.