Skip to content

Commit 4ae7195

Browse files
authored
feat: remove CompartmentId mandatory from ocicluster_types (#67)
CompartmentId is still mandatory, but the admission webhook is now handling the validation.
1 parent 46ad859 commit 4ae7195

File tree

7 files changed

+24
-14
lines changed

7 files changed

+24
-14
lines changed

api/v1beta1/ocicluster_types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ type OCIClusterSpec struct {
4646
DefinedTags map[string]map[string]string `json:"definedTags,omitempty"`
4747

4848
// Compartment to create the cluster network.
49-
CompartmentId string `mandatory:"true" json:"compartmentId"`
49+
// +optional
50+
CompartmentId string `json:"compartmentId"`
5051

5152
// Region the cluster operates in. It must be one of available regions in Region Identifier format.
5253
// See https://docs.oracle.com/en-us/iaas/Content/General/Concepts/regions.htm

api/v1beta1/ocicluster_webhook.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,15 @@ func (c *OCICluster) ValidateUpdate(old runtime.Object) error {
9393
func (c *OCICluster) validate() field.ErrorList {
9494
var allErrs field.ErrorList
9595

96-
// simple validity test for compartment
97-
if !validOcid(c.Spec.CompartmentId) {
96+
if len(c.Spec.CompartmentId) <= 0 {
97+
allErrs = append(
98+
allErrs,
99+
field.Invalid(field.NewPath("spec", "compartmentId"), c.Spec.CompartmentId, "field is required"))
100+
}
101+
102+
// Handle case where CompartmentId exists, but isn't valid
103+
// the separate "blank" check above is a more clear error for the user
104+
if len(c.Spec.CompartmentId) > 0 && !validOcid(c.Spec.CompartmentId) {
98105
allErrs = append(
99106
allErrs,
100107
field.Invalid(field.NewPath("spec", "compartmentId"), c.Spec.CompartmentId, "field is invalid"))

api/v1beta1/ocicluster_webhook_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestOCICluster_ValidateCreate(t *testing.T) {
3434
expectErr bool
3535
}{
3636
{
37-
name: "shouldn't allow bad ImageId",
37+
name: "shouldn't allow bad CompartmentId",
3838
c: &OCICluster{
3939
ObjectMeta: metav1.ObjectMeta{},
4040
Spec: OCIClusterSpec{
@@ -43,6 +43,14 @@ func TestOCICluster_ValidateCreate(t *testing.T) {
4343
},
4444
expectErr: true,
4545
},
46+
{
47+
name: "shouldn't allow blank CompartmentId",
48+
c: &OCICluster{
49+
ObjectMeta: metav1.ObjectMeta{},
50+
Spec: OCIClusterSpec{},
51+
},
52+
expectErr: true,
53+
},
4654
{
4755
name: "should succeed",
4856
c: &OCICluster{

config/crd/bases/infrastructure.cluster.x-k8s.io_ociclusters.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,6 @@ spec:
936936
description: Region the cluster operates in. It must be one of available
937937
regions in Region Identifier format. See https://docs.oracle.com/en-us/iaas/Content/General/Concepts/regions.htm
938938
type: string
939-
required:
940-
- compartmentId
941939
type: object
942940
status:
943941
description: OCIClusterStatus defines the observed state of OCICluster

config/crd/bases/infrastructure.cluster.x-k8s.io_ociclustertemplates.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,8 +1063,6 @@ spec:
10631063
description: Region the cluster operates in. It must be one
10641064
of available regions in Region Identifier format. See https://docs.oracle.com/en-us/iaas/Content/General/Concepts/regions.htm
10651065
type: string
1066-
required:
1067-
- compartmentId
10681066
type: object
10691067
required:
10701068
- spec

templates/clusterclass-example.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ spec:
7676
matchResources:
7777
infrastructureCluster: true
7878
jsonPatches:
79-
- op: replace
79+
- op: add
8080
path: "/spec/template/spec/compartmentId"
8181
valueFrom:
8282
variable: compartmentId
@@ -162,8 +162,7 @@ metadata:
162162
name: ocicluster
163163
spec:
164164
template:
165-
spec:
166-
compartmentId: REPLACE
165+
spec: {}
167166
---
168167
kind: KubeadmControlPlaneTemplate
169168
apiVersion: controlplane.cluster.x-k8s.io/v1beta1

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ spec:
7676
matchResources:
7777
infrastructureCluster: true
7878
jsonPatches:
79-
- op: replace
79+
- op: add
8080
path: "/spec/template/spec/compartmentId"
8181
valueFrom:
8282
variable: compartmentId
@@ -162,8 +162,7 @@ metadata:
162162
name: ocicluster
163163
spec:
164164
template:
165-
spec:
166-
compartmentId: REPLACE
165+
spec: {}
167166
---
168167
kind: KubeadmControlPlaneTemplate
169168
apiVersion: controlplane.cluster.x-k8s.io/v1beta1

0 commit comments

Comments
 (0)