Skip to content

Commit

Permalink
Add unique check in LSC validation webhook
Browse files Browse the repository at this point in the history
Signed-off-by: v.oleynikov <vasily.oleynikov@flant.com>
  • Loading branch information
duckhawk committed Jul 19, 2024
1 parent cb15e2a commit 5c06317
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions images/webhooks/src/handlers/lscValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ package handlers

import (
"context"
"fmt"
slv "github.com/deckhouse/sds-local-volume/api/v1alpha1"
"k8s.io/klog/v2"
"slices"

"github.com/slok/kubewebhook/v2/pkg/model"
kwhvalidating "github.com/slok/kubewebhook/v2/pkg/webhook/validating"
Expand All @@ -32,9 +35,21 @@ func LSCValidate(ctx context.Context, _ *model.AdmissionReview, obj metav1.Objec
return &kwhvalidating.ValidatorResult{}, nil
}

errMsg := ""
var lvmVolumeGroupUnique []string

thickExists := false
thinExists := false
for _, lvmGroup := range lsc.Spec.LVM.LVMVolumeGroups {
if slices.Contains(lvmVolumeGroupUnique, lvmGroup.Name) {
errMsg = fmt.Sprintf("There must be unique LVMVolumeGroup names (%s duplicates)", lvmGroup.Name)
klog.Info(errMsg)
return &kwhvalidating.ValidatorResult{Valid: false, Message: errMsg},
nil
}

lvmVolumeGroupUnique = append(lvmVolumeGroupUnique, lvmGroup.Name)

if lvmGroup.Thin == nil {
thickExists = true
} else {
Expand All @@ -43,17 +58,23 @@ func LSCValidate(ctx context.Context, _ *model.AdmissionReview, obj metav1.Objec
}

if thinExists && lsc.Spec.LVM.Type == "Thick" {
return &kwhvalidating.ValidatorResult{Valid: false, Message: "there must be only thick pools with Thick LVM type"},
errMsg = "There must be only thick pools with Thick LVM type"
klog.Info(errMsg)
return &kwhvalidating.ValidatorResult{Valid: false, Message: errMsg},
nil
}

if thickExists && lsc.Spec.LVM.Type == "Thin" {
return &kwhvalidating.ValidatorResult{Valid: false, Message: "there must be only thin pools with Thin LVM type"},
errMsg = "There must be only thin pools with Thin LVM type"
klog.Info(errMsg)
return &kwhvalidating.ValidatorResult{Valid: false, Message: errMsg},
nil
}

if thickExists == true && thinExists == true {
return &kwhvalidating.ValidatorResult{Valid: false, Message: "there must be only thin or thick pools simultaneously"},
errMsg = "There must be only thin or thick pools simultaneously"
klog.Info(errMsg)
return &kwhvalidating.ValidatorResult{Valid: false, Message: errMsg},
nil
} else {
return &kwhvalidating.ValidatorResult{Valid: true},
Expand Down

0 comments on commit 5c06317

Please sign in to comment.