Skip to content

Commit

Permalink
azureadapplication: make .spec.tenant immutable
Browse files Browse the repository at this point in the history
These external resources may become dangling when transitioning between tenants,
so we want to ensure that they are properly clean up.

This is done with a transition rule that disallows changing the value once created.
In other words, the field is only allowed to be set during creation.

See https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#transition-rules
  • Loading branch information
tronghn committed Dec 10, 2024
1 parent 4e1812b commit 8875af3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions charts/templates/nais.io_azureadapplications.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,20 @@ spec:
description: |-
Tenant is an optional alias for targeting a tenant matching an instance of Azurerator that targets said tenant.
Can be omitted if only running a single instance or targeting the default tenant.
Immutable once set.
type: string
x-kubernetes-validations:
- message: tenant is immutable once set; delete and recreate AzureAdApplication
to change tenant
rule: self == oldSelf
required:
- secretName
type: object
x-kubernetes-validations:
- message: tenant can only be set on creation; delete and recreate AzureAdApplication
to set tenant
rule: (has(oldSelf.tenant) && has(self.tenant)) || (!has(oldSelf.tenant)
&& !has(self.tenant))
status:
description: AzureAdApplicationStatus defines the observed state of AzureAdApplication
properties:
Expand Down
10 changes: 10 additions & 0 deletions config/crd/bases/nais.io_azureadapplications.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,20 @@ spec:
description: |-
Tenant is an optional alias for targeting a tenant matching an instance of Azurerator that targets said tenant.
Can be omitted if only running a single instance or targeting the default tenant.
Immutable once set.
type: string
x-kubernetes-validations:
- message: tenant is immutable once set; delete and recreate AzureAdApplication
to change tenant
rule: self == oldSelf
required:
- secretName
type: object
x-kubernetes-validations:
- message: tenant can only be set on creation; delete and recreate AzureAdApplication
to set tenant
rule: (has(oldSelf.tenant) && has(self.tenant)) || (!has(oldSelf.tenant)
&& !has(self.tenant))
status:
description: AzureAdApplicationStatus defines the observed state of AzureAdApplication
properties:
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/nais.io/v1/azureadapplication_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type AzureAdApplicationList struct {
}

// AzureAdApplicationSpec defines the desired state of AzureAdApplication
// +kubebuilder:validation:XValidation:rule="(has(oldSelf.tenant) && has(self.tenant)) || (!has(oldSelf.tenant) && !has(self.tenant))", message="tenant can only be set on creation; delete and recreate AzureAdApplication to set tenant"
type AzureAdApplicationSpec struct {
// AllowAllUsers denotes whether all users within the tenant should be allowed to access this AzureAdApplication. Defaults to false.
AllowAllUsers *bool `json:"allowAllUsers,omitempty"`
Expand All @@ -66,6 +67,9 @@ type AzureAdApplicationSpec struct {
SinglePageApplication *bool `json:"singlePageApplication,omitempty"`
// Tenant is an optional alias for targeting a tenant matching an instance of Azurerator that targets said tenant.
// Can be omitted if only running a single instance or targeting the default tenant.
// Immutable once set.
// +kubebuilder:validation:Optional
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="tenant is immutable once set; delete and recreate AzureAdApplication to change tenant"
Tenant string `json:"tenant,omitempty"`
}

Expand Down

0 comments on commit 8875af3

Please sign in to comment.