Skip to content

Commit

Permalink
feat: update OCICluster webhook validations (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
joekr committed May 2, 2022
1 parent 72c2995 commit 096f2e0
Show file tree
Hide file tree
Showing 5 changed files with 522 additions and 22 deletions.
23 changes: 20 additions & 3 deletions api/v1beta1/ocicluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (c *OCICluster) ValidateCreate() error {

var allErrs field.ErrorList

allErrs = append(allErrs, c.validate()...)
allErrs = append(allErrs, c.validate(nil)...)

if len(allErrs) == 0 {
return nil
Expand Down Expand Up @@ -93,7 +93,11 @@ func (c *OCICluster) ValidateUpdate(old runtime.Object) error {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "ociResourceIdentifier"), c.Spec.OCIResourceIdentifier, "field is immutable"))
}

allErrs = append(allErrs, c.validate()...)
if c.Spec.CompartmentId != oldCluster.Spec.CompartmentId {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "compartmentId"), c.Spec.CompartmentId, "field is immutable"))
}

allErrs = append(allErrs, c.validate(oldCluster)...)

if len(allErrs) == 0 {
return nil
Expand All @@ -102,9 +106,16 @@ func (c *OCICluster) ValidateUpdate(old runtime.Object) error {
return apierrors.NewInvalid(c.GroupVersionKind().GroupKind(), c.Name, allErrs)
}

func (c *OCICluster) validate() field.ErrorList {
func (c *OCICluster) validate(old *OCICluster) field.ErrorList {
var allErrs field.ErrorList

var oldNetworkSpec NetworkSpec
if old != nil {
oldNetworkSpec = old.Spec.NetworkSpec
}

allErrs = append(allErrs, validateNetworkSpec(c.Spec.NetworkSpec, oldNetworkSpec, field.NewPath("spec").Child("networkSpec"))...)

if len(c.Spec.CompartmentId) <= 0 {
allErrs = append(
allErrs,
Expand All @@ -125,6 +136,12 @@ func (c *OCICluster) validate() field.ErrorList {
field.Invalid(field.NewPath("spec", "ociResourceIdentifier"), c.Spec.OCIResourceIdentifier, "field is required"))
}

if !validRegion(c.Spec.Region) {
allErrs = append(
allErrs,
field.Invalid(field.NewPath("spec", "region"), c.Spec.Region, "field is invalid. See https://docs.oracle.com/en-us/iaas/Content/General/Concepts/regions.htm"))
}

if len(allErrs) == 0 {
return nil
}
Expand Down
Loading

0 comments on commit 096f2e0

Please sign in to comment.