Skip to content

Commit 72c2995

Browse files
authored
feat: update OCIMachineTemplate webhook validations (#75)
1 parent 7397f4d commit 72c2995

File tree

5 files changed

+59
-8
lines changed

5 files changed

+59
-8
lines changed

api/v1beta1/ocimachinetemplate_webhook.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package v1beta1
2121

2222
import (
23+
"fmt"
24+
2325
apierrors "k8s.io/apimachinery/pkg/api/errors"
2426
"k8s.io/apimachinery/pkg/runtime"
2527
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -83,7 +85,7 @@ func (m *OCIMachineTemplate) validate() field.ErrorList {
8385
allErrs = append(
8486
allErrs,
8587
field.Invalid(
86-
field.NewPath("Spec", "Template", "Spec", "imageId"),
88+
field.NewPath("spec", "template", "spec", "imageId"),
8789
m.Spec.Template.Spec.ImageId,
8890
"field is invalid"),
8991
)
@@ -94,12 +96,22 @@ func (m *OCIMachineTemplate) validate() field.ErrorList {
9496
allErrs = append(
9597
allErrs,
9698
field.Invalid(
97-
field.NewPath("Spec", "Template", "Spec", "compartmentId"),
99+
field.NewPath("spec", "template", "spec", "compartmentId"),
98100
m.Spec.Template.Spec.CompartmentId,
99101
"field is invalid"),
100102
)
101103
}
102104

105+
if !validShape(m.Spec.Template.Spec.Shape) {
106+
allErrs = append(
107+
allErrs,
108+
field.Invalid(
109+
field.NewPath("spec", "template", "spec", "shape"),
110+
m.Spec.Template.Spec.Shape,
111+
fmt.Sprintf("shape is invalid - %s", m.Spec.Template.Spec.Shape)),
112+
)
113+
}
114+
103115
if len(allErrs) == 0 {
104116
return nil
105117
}

api/v1beta1/ocimachinetemplate_webhook_test.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package v1beta1
2121

2222
import (
23+
"strings"
2324
"testing"
2425

2526
"github.com/onsi/gomega"
@@ -29,6 +30,7 @@ import (
2930
var tests = []struct {
3031
name string
3132
inputTemplate *OCIMachineTemplate
33+
errorField string
3234
expectErr bool
3335
}{
3436
{
@@ -43,7 +45,8 @@ var tests = []struct {
4345
},
4446
},
4547
},
46-
expectErr: true,
48+
errorField: "imageId",
49+
expectErr: true,
4750
},
4851
{
4952
name: "shouldn't allow bad CompartmentId",
@@ -57,7 +60,23 @@ var tests = []struct {
5760
},
5861
},
5962
},
60-
expectErr: true,
63+
errorField: "compartmentId",
64+
expectErr: true,
65+
},
66+
{
67+
name: "shouldn't allow empty shape",
68+
inputTemplate: &OCIMachineTemplate{
69+
ObjectMeta: metav1.ObjectMeta{},
70+
Spec: OCIMachineTemplateSpec{
71+
Template: OCIMachineTemplateResource{
72+
Spec: OCIMachineSpec{
73+
Shape: "",
74+
},
75+
},
76+
},
77+
},
78+
errorField: "shape",
79+
expectErr: true,
6180
},
6281
{
6382
name: "should succeed",
@@ -68,6 +87,7 @@ var tests = []struct {
6887
Spec: OCIMachineSpec{
6988
ImageId: "ocid",
7089
CompartmentId: "ocid",
90+
Shape: "DVH.DenseIO2.52",
7191
},
7292
},
7393
},
@@ -82,7 +102,9 @@ func TestOCIMachineTemplate_ValidateCreate(t *testing.T) {
82102
g := gomega.NewWithT(t)
83103

84104
if test.expectErr {
85-
g.Expect(test.inputTemplate.ValidateCreate()).NotTo(gomega.Succeed())
105+
err := test.inputTemplate.ValidateCreate()
106+
g.Expect(err).NotTo(gomega.Succeed())
107+
g.Expect(strings.Contains(err.Error(), test.errorField)).To(gomega.BeTrue())
86108
} else {
87109
g.Expect(test.inputTemplate.ValidateCreate()).To(gomega.Succeed())
88110
}
@@ -96,7 +118,9 @@ func TestOCIMachineTemplate_ValidateUpdate(t *testing.T) {
96118
g := gomega.NewWithT(t)
97119

98120
if test.expectErr {
99-
g.Expect(test.inputTemplate.ValidateUpdate(nil)).NotTo(gomega.Succeed())
121+
err := test.inputTemplate.ValidateUpdate(nil)
122+
g.Expect(err).NotTo(gomega.Succeed())
123+
g.Expect(strings.Contains(err.Error(), test.errorField)).To(gomega.BeTrue())
100124
} else {
101125
g.Expect(test.inputTemplate.ValidateUpdate(nil)).To(gomega.Succeed())
102126
}

api/v1beta1/validator.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@
1919

2020
package v1beta1
2121

22+
// validOcid is a simple pre-flight
23+
// we will let the serverside handle the more complex and compete validation
2224
func validOcid(ocid string) bool {
23-
2425
if len(ocid) >= 4 && ocid[:4] == "ocid" {
2526
return true
2627
}
2728

2829
return false
2930
}
31+
32+
// validShape is a simple pre-flight
33+
// we will let the serverside handle the more complex and compete validation
34+
func validShape(shape string) bool {
35+
return len(shape) > 0
36+
}

templates/clusterclass-example.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ spec:
106106
names:
107107
- worker
108108
jsonPatches:
109-
- op: add
109+
- op: replace
110110
path: "/spec/template/spec/shape"
111111
valueFrom:
112112
variable: shape
@@ -200,6 +200,8 @@ metadata:
200200
spec:
201201
template:
202202
spec:
203+
# will be replaced by shape variable defined above
204+
shape: VM.Standard.E4.Flex
203205
metadata: {}
204206
shapeConfig: {}
205207

@@ -211,6 +213,8 @@ metadata:
211213
spec:
212214
template:
213215
spec:
216+
# will be replaced by shape variable defined above
217+
shape: VM.Standard.E4.Flex
214218
metadata: {}
215219
shapeConfig: {}
216220
---

test/e2e/data/infrastructure-oci/v1beta1/cluster-template-cluster-class/clusterclass-test-cluster-class.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ metadata:
200200
spec:
201201
template:
202202
spec:
203+
# will be replaced by shape variable defined above
204+
shape: VM.Standard.E4.Flex
203205
metadata: {}
204206
shapeConfig: {}
205207
---
@@ -210,6 +212,8 @@ metadata:
210212
spec:
211213
template:
212214
spec:
215+
# will be replaced by shape variable defined above
216+
shape: VM.Standard.E4.Flex
213217
metadata: {}
214218
shapeConfig: {}
215219
---

0 commit comments

Comments
 (0)