Skip to content

Commit

Permalink
feat: remove CompartmentId mandatory from ocicluster_types (#67)
Browse files Browse the repository at this point in the history
CompartmentId is still mandatory, but the admission webhook
is now handling the validation.
  • Loading branch information
joekr committed Apr 23, 2022
1 parent 46ad859 commit 4ae7195
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 14 deletions.
3 changes: 2 additions & 1 deletion api/v1beta1/ocicluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ type OCIClusterSpec struct {
DefinedTags map[string]map[string]string `json:"definedTags,omitempty"`

// Compartment to create the cluster network.
CompartmentId string `mandatory:"true" json:"compartmentId"`
// +optional
CompartmentId string `json:"compartmentId"`

// Region the cluster operates in. It must be one of available regions in Region Identifier format.
// See https://docs.oracle.com/en-us/iaas/Content/General/Concepts/regions.htm
Expand Down
11 changes: 9 additions & 2 deletions api/v1beta1/ocicluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,15 @@ func (c *OCICluster) ValidateUpdate(old runtime.Object) error {
func (c *OCICluster) validate() field.ErrorList {
var allErrs field.ErrorList

// simple validity test for compartment
if !validOcid(c.Spec.CompartmentId) {
if len(c.Spec.CompartmentId) <= 0 {
allErrs = append(
allErrs,
field.Invalid(field.NewPath("spec", "compartmentId"), c.Spec.CompartmentId, "field is required"))
}

// Handle case where CompartmentId exists, but isn't valid
// the separate "blank" check above is a more clear error for the user
if len(c.Spec.CompartmentId) > 0 && !validOcid(c.Spec.CompartmentId) {
allErrs = append(
allErrs,
field.Invalid(field.NewPath("spec", "compartmentId"), c.Spec.CompartmentId, "field is invalid"))
Expand Down
10 changes: 9 additions & 1 deletion api/v1beta1/ocicluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestOCICluster_ValidateCreate(t *testing.T) {
expectErr bool
}{
{
name: "shouldn't allow bad ImageId",
name: "shouldn't allow bad CompartmentId",
c: &OCICluster{
ObjectMeta: metav1.ObjectMeta{},
Spec: OCIClusterSpec{
Expand All @@ -43,6 +43,14 @@ func TestOCICluster_ValidateCreate(t *testing.T) {
},
expectErr: true,
},
{
name: "shouldn't allow blank CompartmentId",
c: &OCICluster{
ObjectMeta: metav1.ObjectMeta{},
Spec: OCIClusterSpec{},
},
expectErr: true,
},
{
name: "should succeed",
c: &OCICluster{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -936,8 +936,6 @@ spec:
description: Region the cluster operates in. It must be one of available
regions in Region Identifier format. See https://docs.oracle.com/en-us/iaas/Content/General/Concepts/regions.htm
type: string
required:
- compartmentId
type: object
status:
description: OCIClusterStatus defines the observed state of OCICluster
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1063,8 +1063,6 @@ spec:
description: Region the cluster operates in. It must be one
of available regions in Region Identifier format. See https://docs.oracle.com/en-us/iaas/Content/General/Concepts/regions.htm
type: string
required:
- compartmentId
type: object
required:
- spec
Expand Down
5 changes: 2 additions & 3 deletions templates/clusterclass-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ spec:
matchResources:
infrastructureCluster: true
jsonPatches:
- op: replace
- op: add
path: "/spec/template/spec/compartmentId"
valueFrom:
variable: compartmentId
Expand Down Expand Up @@ -162,8 +162,7 @@ metadata:
name: ocicluster
spec:
template:
spec:
compartmentId: REPLACE
spec: {}
---
kind: KubeadmControlPlaneTemplate
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ spec:
matchResources:
infrastructureCluster: true
jsonPatches:
- op: replace
- op: add
path: "/spec/template/spec/compartmentId"
valueFrom:
variable: compartmentId
Expand Down Expand Up @@ -162,8 +162,7 @@ metadata:
name: ocicluster
spec:
template:
spec:
compartmentId: REPLACE
spec: {}
---
kind: KubeadmControlPlaneTemplate
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
Expand Down

0 comments on commit 4ae7195

Please sign in to comment.