Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip system pool validation while cluster is paused/moved #2774

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/v1beta1/azuremanagedmachinepool_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ func (m *AzureManagedMachinePool) validateLastSystemNodePool(cli client.Client)
return nil
}

if ownerCluster.Spec.Paused {
return nil
}

opt1 := client.InNamespace(m.Namespace)
opt2 := client.MatchingLabels(map[string]string{
clusterv1.ClusterLabelName: clusterName,
Expand Down
96 changes: 96 additions & 0 deletions api/v1beta1/azuremanagedmachinepool_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ import (
"github.com/Azure/go-autorest/autorest/to"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilfeature "k8s.io/component-base/featuregate/testing"
"sigs.k8s.io/cluster-api-provider-azure/feature"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
capifeature "sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestAzureManagedMachinePoolDefaultingWebhook(t *testing.T) {
Expand Down Expand Up @@ -882,6 +885,87 @@ func TestAzureManagedMachinePool_ValidateCreateFailure(t *testing.T) {
}
}

func TestAzureManagedMachinePool_validateLastSystemNodePool(t *testing.T) {
CecileRobertMichon marked this conversation as resolved.
Show resolved Hide resolved
deletionTime := metav1.Now()
systemMachinePool := getManagedMachinePoolWithSystemMode()
tests := []struct {
name string
ammp *AzureManagedMachinePool
cluster *clusterv1.Cluster
wantErr bool
}{
{
name: "Test with paused cluster without deletion timestamp having one system pool node(valid delete:move operation)",
ammp: systemMachinePool,
cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: systemMachinePool.GetLabels()[clusterv1.ClusterLabelName],
Namespace: systemMachinePool.Namespace,
DeletionTimestamp: &deletionTime,
},
Spec: clusterv1.ClusterSpec{
Paused: true,
},
},
wantErr: false,
},
{
name: "Test with paused cluster with deletion timestamp having one system pool node(valid delete)",
ammp: systemMachinePool,
cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: systemMachinePool.GetLabels()[clusterv1.ClusterLabelName],
Namespace: systemMachinePool.Namespace,
DeletionTimestamp: &deletionTime,
},
Spec: clusterv1.ClusterSpec{
Paused: true,
},
},
wantErr: false,
},
{
name: "Test with running cluster without deletion timestamp having one system pool node(invalid delete)",
ammp: systemMachinePool,
cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: systemMachinePool.GetLabels()[clusterv1.ClusterLabelName],
Namespace: systemMachinePool.Namespace,
},
},
wantErr: true,
},
{
name: "Test with running cluster with deletion timestamp having one system pool node(valid delete)",
ammp: systemMachinePool,
cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: systemMachinePool.GetLabels()[clusterv1.ClusterLabelName],
Namespace: systemMachinePool.Namespace,
DeletionTimestamp: &deletionTime,
},
},
wantErr: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
scheme := runtime.NewScheme()
_ = AddToScheme(scheme)
_ = clusterv1.AddToScheme(scheme)
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(tc.cluster, tc.ammp).Build()
err := tc.ammp.validateLastSystemNodePool(fakeClient)
if tc.wantErr {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
})
}
}

func getKnownValidAzureManagedMachinePool() *AzureManagedMachinePool {
return &AzureManagedMachinePool{
Spec: AzureManagedMachinePoolSpec{
Expand All @@ -890,3 +974,15 @@ func getKnownValidAzureManagedMachinePool() *AzureManagedMachinePool {
},
}
}

func getManagedMachinePoolWithSystemMode() *AzureManagedMachinePool {
return &AzureManagedMachinePool{
ObjectMeta: metav1.ObjectMeta{
Namespace: metav1.NamespaceDefault,
Labels: map[string]string{
clusterv1.ClusterLabelName: "test-cluster",
LabelAgentPoolMode: string(NodePoolModeSystem),
},
},
}
}