Skip to content

Commit 096f2e0

Browse files
authored
feat: update OCICluster webhook validations (#77)
1 parent 72c2995 commit 096f2e0

File tree

5 files changed

+522
-22
lines changed

5 files changed

+522
-22
lines changed

api/v1beta1/ocicluster_webhook.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (c *OCICluster) ValidateCreate() error {
5858

5959
var allErrs field.ErrorList
6060

61-
allErrs = append(allErrs, c.validate()...)
61+
allErrs = append(allErrs, c.validate(nil)...)
6262

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

96-
allErrs = append(allErrs, c.validate()...)
96+
if c.Spec.CompartmentId != oldCluster.Spec.CompartmentId {
97+
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "compartmentId"), c.Spec.CompartmentId, "field is immutable"))
98+
}
99+
100+
allErrs = append(allErrs, c.validate(oldCluster)...)
97101

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

105-
func (c *OCICluster) validate() field.ErrorList {
109+
func (c *OCICluster) validate(old *OCICluster) field.ErrorList {
106110
var allErrs field.ErrorList
107111

112+
var oldNetworkSpec NetworkSpec
113+
if old != nil {
114+
oldNetworkSpec = old.Spec.NetworkSpec
115+
}
116+
117+
allErrs = append(allErrs, validateNetworkSpec(c.Spec.NetworkSpec, oldNetworkSpec, field.NewPath("spec").Child("networkSpec"))...)
118+
108119
if len(c.Spec.CompartmentId) <= 0 {
109120
allErrs = append(
110121
allErrs,
@@ -125,6 +136,12 @@ func (c *OCICluster) validate() field.ErrorList {
125136
field.Invalid(field.NewPath("spec", "ociResourceIdentifier"), c.Spec.OCIResourceIdentifier, "field is required"))
126137
}
127138

139+
if !validRegion(c.Spec.Region) {
140+
allErrs = append(
141+
allErrs,
142+
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"))
143+
}
144+
128145
if len(allErrs) == 0 {
129146
return nil
130147
}

0 commit comments

Comments
 (0)