Skip to content

Commit

Permalink
Adding secretef Webhook For Backingstore and Namespacestore
Browse files Browse the repository at this point in the history
- Adding secretef Webhook For Backingstore and Namespacestore to validate that it have namespace in it.

Signed-off-by: liranmauda <liran.mauda@gmail.com>
  • Loading branch information
liranmauda committed Oct 23, 2022
1 parent 6fc994c commit d9fead1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/admission/validate_namespacestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func (nsv *ResourceValidator) ValidateUpdateNS() {
nsv.SetValidationResult(false, err.Error())
return
}
if err := validations.ValidateNamespacestoreSecretRefNamespace(*ns); err != nil && util.IsValidationError(err) {
nsv.SetValidationResult(false, err.Error())
return
}
}
}

Expand Down
38 changes: 36 additions & 2 deletions pkg/validations/backingstore_validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func ValidateBackingStore(bs nbv1.BackingStore) error {
if err := ValidateBSEmptyTargetBucket(bs); err != nil {
return err
}

switch bs.Spec.Type {
case nbv1.StoreTypePVPool:
if err := ValidatePvpoolNameLength(bs); err != nil {
Expand All @@ -39,10 +40,25 @@ func ValidateBackingStore(bs nbv1.BackingStore) error {
return err
}
case nbv1.StoreTypeS3Compatible:
return ValidateSigVersion(bs.Spec.S3Compatible.SignatureVersion)
if err := ValidateSigVersion(bs.Spec.S3Compatible.SignatureVersion); err != nil {
return err
}
if err := ValidateBackingstoreSecretRefNamespace(bs); err != nil {
return err
}
return nil
case nbv1.StoreTypeIBMCos:
return ValidateSigVersion(bs.Spec.IBMCos.SignatureVersion)
if err := ValidateSigVersion(bs.Spec.IBMCos.SignatureVersion); err != nil {
return err
}
if err := ValidateBackingstoreSecretRefNamespace(bs); err != nil {
return err
}
return nil
case nbv1.StoreTypeAWSS3, nbv1.StoreTypeAzureBlob, nbv1.StoreTypeGoogleCloudStorage:
if err := ValidateBackingstoreSecretRefNamespace(bs); err != nil {
return err
}
return nil
default:
return util.ValidationError{
Expand Down Expand Up @@ -272,3 +288,21 @@ func ValidateBackingstoreDeletion(bs nbv1.BackingStore, systemInfo nb.SystemInfo

return nil
}

// ValidateBackingstoreSecretRefNamespace validates that the secretref have namespace in it.
func ValidateBackingstoreSecretRefNamespace(bs nbv1.BackingStore) error{
secretRef, err := util.GetBackingStoreSecretByType(&bs);
if err != nil {
return util.ValidationError{
Msg: err.Error(),
}
}
if secretRef.Namespace == "" {
return util.ValidationError{
Msg: fmt.Sprintf("Secret ref %q in Backingstore %q must have namespace", secretRef.Name, bs.Name),
}
}

return nil

}
25 changes: 25 additions & 0 deletions pkg/validations/namespacestore_validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ func ValidateNamespaceStore(nsStore *nbv1.NamespaceStore) error {
if err := ValidateNSEmptyTargetBucket(*nsStore); err != nil {
return err
}

if nsStore.Spec.Type != nbv1.NSStoreTypeNSFS {
if err := ValidateNamespacestoreSecretRefNamespace(*nsStore); err != nil {
return err
}
}

switch nsStore.Spec.Type {

case nbv1.NSStoreTypeNSFS:
Expand Down Expand Up @@ -315,3 +322,21 @@ func ValidateNamespacestoreDeletion(ns nbv1.NamespaceStore, systemInfo nb.System

return nil
}

// ValidateNamespacestoreSecretRefNamespace validates that the secretref have namespace in it.
func ValidateNamespacestoreSecretRefNamespace(ns nbv1.NamespaceStore) error{
secretRef, err := util.GetNamespaceStoreSecretByType(&ns);
if err != nil {
return util.ValidationError{
Msg: err.Error(),
}
}
if secretRef.Namespace == "" {
return util.ValidationError{
Msg: fmt.Sprintf("Secret ref %q in NamespaceStore %q must have namespace", secretRef.Name, ns.Name),
}
}

return nil

}

0 comments on commit d9fead1

Please sign in to comment.