Skip to content

Commit

Permalink
AzureManagedCluster spec.controlPlaneEndpoint is immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfrancis committed Dec 5, 2022
1 parent c74fa34 commit 582a8b3
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
19 changes: 19 additions & 0 deletions exp/api/v1beta1/azuremanagedcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/feature"
"sigs.k8s.io/cluster-api-provider-azure/util/maps"
webhookutils "sigs.k8s.io/cluster-api-provider-azure/util/webhook"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
Expand Down Expand Up @@ -70,6 +71,24 @@ func (r *AzureManagedCluster) ValidateUpdate(oldRaw runtime.Object) error {
fmt.Sprintf("annotations with '%s' prefix are immutable", azure.CustomHeaderPrefix)))
}

if old.Spec.ControlPlaneEndpoint.Host != "" {
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "ControlPlaneEndpoint", "Host"),
old.Spec.ControlPlaneEndpoint.Host,
r.Spec.ControlPlaneEndpoint.Host); err != nil {
allErrs = append(allErrs, err)
}
}

if old.Spec.ControlPlaneEndpoint.Port != 0 {
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "ControlPlaneEndpoint", "Port"),
old.Spec.ControlPlaneEndpoint.Port,
r.Spec.ControlPlaneEndpoint.Port); err != nil {
allErrs = append(allErrs, err)
}
}

if len(allErrs) != 0 {
return apierrors.NewInvalid(GroupVersion.WithKind("AzureManagedCluster").GroupKind(), r.Name, allErrs)
}
Expand Down
64 changes: 64 additions & 0 deletions exp/api/v1beta1/azuremanagedcluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilfeature "k8s.io/component-base/featuregate/testing"
"sigs.k8s.io/cluster-api-provider-azure/feature"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

func TestAzureManagedCluster_ValidateUpdate(t *testing.T) {
Expand Down Expand Up @@ -119,6 +120,69 @@ func TestAzureManagedCluster_ValidateUpdate(t *testing.T) {
},
wantErr: false,
},
{
name: "ControlPlaneEndpoint.Port is immutable",
oldAMC: &AzureManagedCluster{
ObjectMeta: metav1.ObjectMeta{},
Spec: AzureManagedClusterSpec{
ControlPlaneEndpoint: clusterv1.APIEndpoint{
Host: "aks-8622-h4h26c44.hcp.eastus.azmk8s.io",
Port: 443,
},
},
},
amc: &AzureManagedCluster{
ObjectMeta: metav1.ObjectMeta{},
Spec: AzureManagedClusterSpec{
ControlPlaneEndpoint: clusterv1.APIEndpoint{
Host: "aks-8622-h4h26c44.hcp.eastus.azmk8s.io",
Port: 444,
},
},
},
wantErr: true,
},
{
name: "ControlPlaneEndpoint.Host is immutable",
oldAMC: &AzureManagedCluster{
ObjectMeta: metav1.ObjectMeta{},
Spec: AzureManagedClusterSpec{
ControlPlaneEndpoint: clusterv1.APIEndpoint{
Host: "aks-8622-h4h26c44.hcp.eastus.azmk8s.io",
Port: 443,
},
},
},
amc: &AzureManagedCluster{
ObjectMeta: metav1.ObjectMeta{},
Spec: AzureManagedClusterSpec{
ControlPlaneEndpoint: clusterv1.APIEndpoint{
Host: "this-is-not-allowed",
Port: 443,
},
},
},
wantErr: true,
},
{
name: "ControlPlaneEndpoint update from zero values are allowed",
oldAMC: &AzureManagedCluster{
ObjectMeta: metav1.ObjectMeta{},
Spec: AzureManagedClusterSpec{
ControlPlaneEndpoint: clusterv1.APIEndpoint{},
},
},
amc: &AzureManagedCluster{
ObjectMeta: metav1.ObjectMeta{},
Spec: AzureManagedClusterSpec{
ControlPlaneEndpoint: clusterv1.APIEndpoint{
Host: "aks-8622-h4h26c44.hcp.eastus.azmk8s.io",
Port: 443,
},
},
},
wantErr: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down
18 changes: 18 additions & 0 deletions exp/api/v1beta1/azuremanagedcontrolplane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,24 @@ func (m *AzureManagedControlPlane) ValidateUpdate(oldRaw runtime.Object, client
}
}

if old.Spec.ControlPlaneEndpoint.Host != "" {
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "ControlPlaneEndpoint", "Host"),
old.Spec.ControlPlaneEndpoint.Host,
m.Spec.ControlPlaneEndpoint.Host); err != nil {
allErrs = append(allErrs, err)
}
}

if old.Spec.ControlPlaneEndpoint.Port != 0 {
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "ControlPlaneEndpoint", "Port"),
old.Spec.ControlPlaneEndpoint.Port,
m.Spec.ControlPlaneEndpoint.Port); err != nil {
allErrs = append(allErrs, err)
}
}

if errs := m.validateVirtualNetworkUpdate(old); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
Expand Down
76 changes: 76 additions & 0 deletions exp/api/v1beta1/azuremanagedcontrolplane_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
utilfeature "k8s.io/component-base/featuregate/testing"
"k8s.io/utils/pointer"
"sigs.k8s.io/cluster-api-provider-azure/feature"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

func TestDefaultingWebhook(t *testing.T) {
Expand Down Expand Up @@ -903,6 +904,81 @@ func TestAzureManagedControlPlane_ValidateUpdate(t *testing.T) {
},
wantErr: false,
},
{
name: "AzureManagedControlPlane ControlPlaneEndpoint.Port is mutable",
oldAMCP: &AzureManagedControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
},
Spec: AzureManagedControlPlaneSpec{
ControlPlaneEndpoint: clusterv1.APIEndpoint{
Host: "aks-8622-h4h26c44.hcp.eastus.azmk8s.io",
Port: 443,
},
},
},
amcp: &AzureManagedControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
},
Spec: AzureManagedControlPlaneSpec{
ControlPlaneEndpoint: clusterv1.APIEndpoint{
Host: "aks-8622-h4h26c44.hcp.eastus.azmk8s.io",
Port: 444,
},
},
},
wantErr: true,
},
{
name: "AzureManagedControlPlane ControlPlaneEndpoint.Host is mutable",
oldAMCP: &AzureManagedControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
},
Spec: AzureManagedControlPlaneSpec{
ControlPlaneEndpoint: clusterv1.APIEndpoint{
Host: "aks-8622-h4h26c44.hcp.eastus.azmk8s.io",
Port: 443,
},
},
},
amcp: &AzureManagedControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
},
Spec: AzureManagedControlPlaneSpec{
ControlPlaneEndpoint: clusterv1.APIEndpoint{
Host: "this-is-not-allowed",
Port: 443,
},
},
},
wantErr: true,
},
{
name: "ControlPlaneEndpoint update from zero values are allowed",
oldAMCP: &AzureManagedControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
},
Spec: AzureManagedControlPlaneSpec{
ControlPlaneEndpoint: clusterv1.APIEndpoint{},
},
},
amcp: &AzureManagedControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
},
Spec: AzureManagedControlPlaneSpec{
ControlPlaneEndpoint: clusterv1.APIEndpoint{
Host: "aks-8622-h4h26c44.hcp.eastus.azmk8s.io",
Port: 443,
},
},
},
wantErr: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit 582a8b3

Please sign in to comment.