Skip to content

Commit 1e5b0b0

Browse files
authored
Merge pull request #5099 from bryan-cox/fix-webhook-registration
Move webhook registration behind feature gate flag
2 parents 27c0476 + 32f73b1 commit 1e5b0b0

13 files changed

+52
-160
lines changed

api/v1beta1/azuremanagedcluster_webhook.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ package v1beta1
1818

1919
import (
2020
"k8s.io/apimachinery/pkg/runtime"
21-
"k8s.io/apimachinery/pkg/util/validation/field"
22-
capifeature "sigs.k8s.io/cluster-api/feature"
2321
ctrl "sigs.k8s.io/controller-runtime"
2422
"sigs.k8s.io/controller-runtime/pkg/webhook"
2523
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
26-
27-
"sigs.k8s.io/cluster-api-provider-azure/feature"
2824
)
2925

3026
// SetupWebhookWithManager sets up and registers the webhook with the manager.
@@ -40,14 +36,6 @@ var _ webhook.Validator = &AzureManagedCluster{}
4036

4137
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
4238
func (r *AzureManagedCluster) ValidateCreate() (admission.Warnings, error) {
43-
// NOTE: AzureManagedCluster relies upon MachinePools, which is behind a feature gate flag.
44-
// The webhook must prevent creating new objects in case the feature flag is disabled.
45-
if !feature.Gates.Enabled(capifeature.MachinePool) {
46-
return nil, field.Forbidden(
47-
field.NewPath("spec"),
48-
"can be set only if the Cluster API 'MachinePool' feature flag is enabled",
49-
)
50-
}
5139
return nil, nil
5240
}
5341

api/v1beta1/azuremanagedcluster_webhook_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
. "github.com/onsi/gomega"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424
utilfeature "k8s.io/component-base/featuregate/testing"
25-
"k8s.io/utils/ptr"
2625
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2726
capifeature "sigs.k8s.io/cluster-api/feature"
2827

@@ -142,12 +141,6 @@ func TestAzureManagedCluster_ValidateCreateFailure(t *testing.T) {
142141
featureGateEnabled *bool
143142
expectError bool
144143
}{
145-
{
146-
name: "feature gate explicitly disabled",
147-
amc: getKnownValidAzureManagedCluster(),
148-
featureGateEnabled: ptr.To(false),
149-
expectError: true,
150-
},
151144
{
152145
name: "feature gate implicitly enabled",
153146
amc: getKnownValidAzureManagedCluster(),

api/v1beta1/azuremanagedclustertemplate_webhook.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ package v1beta1
1818

1919
import (
2020
"k8s.io/apimachinery/pkg/runtime"
21-
"k8s.io/apimachinery/pkg/util/validation/field"
22-
capifeature "sigs.k8s.io/cluster-api/feature"
2321
ctrl "sigs.k8s.io/controller-runtime"
2422
"sigs.k8s.io/controller-runtime/pkg/webhook"
2523
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
26-
27-
"sigs.k8s.io/cluster-api-provider-azure/feature"
2824
)
2925

3026
// SetupWebhookWithManager sets up and registers the webhook with the manager.
@@ -40,14 +36,6 @@ var _ webhook.Validator = &AzureManagedClusterTemplate{}
4036

4137
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
4238
func (r *AzureManagedClusterTemplate) ValidateCreate() (admission.Warnings, error) {
43-
// NOTE: AzureManagedClusterTemplate relies upon MachinePools, which is behind a feature gate flag.
44-
// The webhook must prevent creating new objects in case the feature flag is disabled.
45-
if !feature.Gates.Enabled(capifeature.MachinePool) {
46-
return nil, field.Forbidden(
47-
field.NewPath("spec"),
48-
"cannot be set if the Cluster API 'MachinePool' feature flag is not enabled",
49-
)
50-
}
5139
return nil, nil
5240
}
5341

api/v1beta1/azuremanagedcontrolplane_webhook.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ import (
3131
"k8s.io/apimachinery/pkg/util/validation/field"
3232
"k8s.io/utils/ptr"
3333
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
34-
capifeature "sigs.k8s.io/cluster-api/feature"
3534
ctrl "sigs.k8s.io/controller-runtime"
3635
"sigs.k8s.io/controller-runtime/pkg/client"
3736
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3837

39-
"sigs.k8s.io/cluster-api-provider-azure/feature"
4038
"sigs.k8s.io/cluster-api-provider-azure/util/versions"
4139
webhookutils "sigs.k8s.io/cluster-api-provider-azure/util/webhook"
4240
)
@@ -100,14 +98,6 @@ func (mw *azureManagedControlPlaneWebhook) ValidateCreate(_ context.Context, obj
10098
if !ok {
10199
return nil, apierrors.NewBadRequest("expected an AzureManagedControlPlane")
102100
}
103-
// NOTE: AzureManagedControlPlane relies upon MachinePools, which is behind a feature gate flag.
104-
// The webhook must prevent creating new objects in case the feature flag is disabled.
105-
if !feature.Gates.Enabled(capifeature.MachinePool) {
106-
return nil, field.Forbidden(
107-
field.NewPath("spec"),
108-
"can be set only if the Cluster API 'MachinePool' feature flag is enabled",
109-
)
110-
}
111101

112102
return nil, m.Validate(mw.Client)
113103
}

api/v1beta1/azuremanagedcontrolplane_webhook_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,12 +1657,6 @@ func TestAzureManagedControlPlane_ValidateCreateFailure(t *testing.T) {
16571657
featureGateEnabled *bool
16581658
expectError bool
16591659
}{
1660-
{
1661-
name: "feature gate explicitly disabled",
1662-
amcp: getKnownValidAzureManagedControlPlane(),
1663-
featureGateEnabled: ptr.To(false),
1664-
expectError: true,
1665-
},
16661660
{
16671661
name: "feature gate implicitly enabled",
16681662
amcp: getKnownValidAzureManagedControlPlane(),

api/v1beta1/azuremanagedcontrolplanetemplate_webhook.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ import (
2323
apierrors "k8s.io/apimachinery/pkg/api/errors"
2424
"k8s.io/apimachinery/pkg/runtime"
2525
"k8s.io/apimachinery/pkg/util/validation/field"
26-
capifeature "sigs.k8s.io/cluster-api/feature"
2726
ctrl "sigs.k8s.io/controller-runtime"
2827
"sigs.k8s.io/controller-runtime/pkg/client"
2928
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3029

31-
"sigs.k8s.io/cluster-api-provider-azure/feature"
3230
"sigs.k8s.io/cluster-api-provider-azure/util/versions"
3331
webhookutils "sigs.k8s.io/cluster-api-provider-azure/util/webhook"
3432
)
@@ -66,14 +64,6 @@ func (mcpw *azureManagedControlPlaneTemplateWebhook) ValidateCreate(_ context.Co
6664
if !ok {
6765
return nil, apierrors.NewBadRequest("expected an AzureManagedControlPlaneTemplate")
6866
}
69-
// NOTE: AzureManagedControlPlaneTemplate relies upon MachinePools, which is behind a feature gate flag.
70-
// The webhook must prevent creating new objects in case the feature flag is disabled.
71-
if !feature.Gates.Enabled(capifeature.MachinePool) {
72-
return nil, field.Forbidden(
73-
field.NewPath("spec"),
74-
"can be set only if the Cluster API 'MachinePool' feature flag is enabled",
75-
)
76-
}
7767

7868
return nil, mcp.validateManagedControlPlaneTemplate(mcpw.Client)
7969
}

api/v1beta1/azuremanagedmachinepool_webhook.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ import (
3232
"k8s.io/utils/ptr"
3333
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3434
clusterctlv1alpha3 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
35-
capifeature "sigs.k8s.io/cluster-api/feature"
3635
ctrl "sigs.k8s.io/controller-runtime"
3736
"sigs.k8s.io/controller-runtime/pkg/client"
3837
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3938

40-
"sigs.k8s.io/cluster-api-provider-azure/feature"
4139
azureutil "sigs.k8s.io/cluster-api-provider-azure/util/azure"
4240
webhookutils "sigs.k8s.io/cluster-api-provider-azure/util/webhook"
4341
)
@@ -91,14 +89,6 @@ func (mw *azureManagedMachinePoolWebhook) ValidateCreate(_ context.Context, obj
9189
if !ok {
9290
return nil, apierrors.NewBadRequest("expected an AzureManagedMachinePool")
9391
}
94-
// NOTE: AzureManagedMachinePool relies upon MachinePools, which is behind a feature gate flag.
95-
// The webhook must prevent creating new objects in case the feature flag is disabled.
96-
if !feature.Gates.Enabled(capifeature.MachinePool) {
97-
return nil, field.Forbidden(
98-
field.NewPath("spec"),
99-
"can be set only if the Cluster API 'MachinePool' feature flag is enabled",
100-
)
101-
}
10292

10393
var errs []error
10494

api/v1beta1/azuremanagedmachinepool_webhook_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,12 +1311,6 @@ func TestAzureManagedMachinePool_ValidateCreateFailure(t *testing.T) {
13111311
featureGateEnabled *bool
13121312
expectError bool
13131313
}{
1314-
{
1315-
name: "feature gate explicitly disabled",
1316-
ammp: getKnownValidAzureManagedMachinePool(),
1317-
featureGateEnabled: ptr.To(false),
1318-
expectError: true,
1319-
},
13201314
{
13211315
name: "feature gate implicitly enabled",
13221316
ammp: getKnownValidAzureManagedMachinePool(),

api/v1beta1/azuremanagedmachinepooltemplate_webhook.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ import (
2525
kerrors "k8s.io/apimachinery/pkg/util/errors"
2626
"k8s.io/apimachinery/pkg/util/validation/field"
2727
"k8s.io/utils/ptr"
28-
capifeature "sigs.k8s.io/cluster-api/feature"
2928
ctrl "sigs.k8s.io/controller-runtime"
3029
"sigs.k8s.io/controller-runtime/pkg/client"
3130
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3231

33-
"sigs.k8s.io/cluster-api-provider-azure/feature"
3432
webhookutils "sigs.k8s.io/cluster-api-provider-azure/util/webhook"
3533
)
3634

@@ -79,13 +77,6 @@ func (mpw *azureManagedMachinePoolTemplateWebhook) ValidateCreate(_ context.Cont
7977
return nil, apierrors.NewBadRequest("expected an AzureManagedMachinePoolTemplate")
8078
}
8179

82-
if !feature.Gates.Enabled(capifeature.MachinePool) {
83-
return nil, field.Forbidden(
84-
field.NewPath("spec"),
85-
"can be set only if the Cluster API 'MachinePool' feature flag is enabled",
86-
)
87-
}
88-
8980
var errs []error
9081

9182
errs = append(errs, validateMaxPods(

exp/api/v1beta1/azuremachinepool_webhook.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ import (
2929
kerrors "k8s.io/apimachinery/pkg/util/errors"
3030
"k8s.io/apimachinery/pkg/util/intstr"
3131
"k8s.io/apimachinery/pkg/util/validation/field"
32-
capifeature "sigs.k8s.io/cluster-api/feature"
3332
ctrl "sigs.k8s.io/controller-runtime"
3433
"sigs.k8s.io/controller-runtime/pkg/client"
3534
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3635

3736
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
38-
"sigs.k8s.io/cluster-api-provider-azure/feature"
3937
azureutil "sigs.k8s.io/cluster-api-provider-azure/util/azure"
4038
)
4139

@@ -73,14 +71,7 @@ func (ampw *azureMachinePoolWebhook) ValidateCreate(_ context.Context, obj runti
7371
if !ok {
7472
return nil, apierrors.NewBadRequest("expected an AzureMachinePool")
7573
}
76-
// NOTE: AzureMachinePool is behind MachinePool feature gate flag; the webhook
77-
// must prevent creating new objects in case the feature flag is disabled.
78-
if !feature.Gates.Enabled(capifeature.MachinePool) {
79-
return nil, field.Forbidden(
80-
field.NewPath("spec"),
81-
"can be set only if the MachinePool feature flag is enabled",
82-
)
83-
}
74+
8475
return nil, amp.Validate(nil, ampw.Client)
8576
}
8677

exp/api/v1beta1/azuremachinepool_webhook_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -706,12 +706,6 @@ func TestAzureMachinePool_ValidateCreateFailure(t *testing.T) {
706706
featureGateEnabled *bool
707707
expectError bool
708708
}{
709-
{
710-
name: "feature gate explicitly disabled",
711-
amp: getKnownValidAzureMachinePool(),
712-
featureGateEnabled: ptr.To(false),
713-
expectError: true,
714-
},
715709
{
716710
name: "feature gate implicitly enabled",
717711
amp: getKnownValidAzureMachinePool(),

exp/api/v1beta1/azuremachinepoolmachine_webhook.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ package v1beta1
1919
import (
2020
"github.com/pkg/errors"
2121
"k8s.io/apimachinery/pkg/runtime"
22-
"k8s.io/apimachinery/pkg/util/validation/field"
23-
capifeature "sigs.k8s.io/cluster-api/feature"
2422
ctrl "sigs.k8s.io/controller-runtime"
2523
"sigs.k8s.io/controller-runtime/pkg/webhook"
2624
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
27-
28-
"sigs.k8s.io/cluster-api-provider-azure/feature"
2925
)
3026

3127
// SetupWebhookWithManager sets up and registers the webhook with the manager.
@@ -46,15 +42,6 @@ func (ampm *AzureMachinePoolMachine) ValidateCreate() (admission.Warnings, error
4642

4743
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
4844
func (ampm *AzureMachinePoolMachine) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
49-
// NOTE: AzureMachinePoolMachine is behind MachinePool feature gate flag; the webhook
50-
// must prevent creating new objects new case the feature flag is disabled.
51-
if !feature.Gates.Enabled(capifeature.MachinePool) {
52-
return nil, field.Forbidden(
53-
field.NewPath("spec"),
54-
"can be set only if the MachinePool feature flag is enabled",
55-
)
56-
}
57-
5845
oldMachine, ok := old.(*AzureMachinePoolMachine)
5946
if !ok {
6047
return nil, errors.New("expected and AzureMachinePoolMachine")

0 commit comments

Comments
 (0)