@@ -17,31 +17,31 @@ limitations under the License.
1717package webhooks
1818
1919import (
20- "context"
21- "fmt"
22- "reflect"
23-
24- apierrors "k8s.io/apimachinery/pkg/api/errors"
25- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26- "k8s.io/apimachinery/pkg/runtime"
27- "k8s.io/apimachinery/pkg/util/validation/field"
28- ctrl "sigs.k8s.io/controller-runtime"
29- "sigs.k8s.io/controller-runtime/pkg/webhook"
30- "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
31-
32- addonsv1 "sigs.k8s.io/cluster-api/api/addons/v1beta2"
33- "sigs.k8s.io/cluster-api/feature"
20+ "context"
21+ "fmt"
22+ "reflect"
23+
24+ apierrors "k8s.io/apimachinery/pkg/api/errors"
25+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+ "k8s.io/apimachinery/pkg/runtime"
27+ "k8s.io/apimachinery/pkg/util/validation/field"
28+ ctrl "sigs.k8s.io/controller-runtime"
29+ "sigs.k8s.io/controller-runtime/pkg/webhook"
30+ "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
31+
32+ addonsv1 "sigs.k8s.io/cluster-api/api/addons/v1beta2"
33+ // "sigs.k8s.io/cluster-api/feature" // This import is no longer needed
3434)
3535
3636// ClusterResourceSet implements a validation and defaulting webhook for ClusterResourceSet.
3737type ClusterResourceSet struct {}
3838
3939func (webhook * ClusterResourceSet ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
40- return ctrl .NewWebhookManagedBy (mgr ).
41- For (& addonsv1.ClusterResourceSet {}).
42- WithDefaulter (webhook ).
43- WithValidator (webhook ).
44- Complete ()
40+ return ctrl .NewWebhookManagedBy (mgr ).
41+ For (& addonsv1.ClusterResourceSet {}).
42+ WithDefaulter (webhook ).
43+ WithValidator (webhook ).
44+ Complete ()
4545}
4646
4747// +kubebuilder:webhook:verbs=create;update,path=/validate-addons-cluster-x-k8s-io-v1beta2-clusterresourceset,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=addons.cluster.x-k8s.io,resources=clusterresourcesets,versions=v1beta2,name=validation.clusterresourceset.addons.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
@@ -52,90 +52,83 @@ var _ webhook.CustomValidator = &ClusterResourceSet{}
5252
5353// Default implements webhook.Defaulter so a webhook will be registered for the type.
5454func (webhook * ClusterResourceSet ) Default (_ context.Context , obj runtime.Object ) error {
55- crs , ok := obj .(* addonsv1.ClusterResourceSet )
56- if ! ok {
57- return apierrors .NewBadRequest (fmt .Sprintf ("expected a ClusterResourceSet but got a %T" , obj ))
58- }
59- // ClusterResourceSet Strategy defaults to ApplyOnce.
60- if crs .Spec .Strategy == "" {
61- crs .Spec .Strategy = string (addonsv1 .ClusterResourceSetStrategyApplyOnce )
62- }
63- return nil
55+ crs , ok := obj .(* addonsv1.ClusterResourceSet )
56+ if ! ok {
57+ return apierrors .NewBadRequest (fmt .Sprintf ("expected a ClusterResourceSet but got a %T" , obj ))
58+ }
59+ // ClusterResourceSet Strategy defaults to ApplyOnce.
60+ if crs .Spec .Strategy == "" {
61+ crs .Spec .Strategy = string (addonsv1 .ClusterResourceSetStrategyApplyOnce )
62+ }
63+ return nil
6464}
6565
6666// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
6767func (webhook * ClusterResourceSet ) ValidateCreate (_ context.Context , newObj runtime.Object ) (admission.Warnings , error ) {
68- newCRS , ok := newObj .(* addonsv1.ClusterResourceSet )
69- if ! ok {
70- return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected a ClusterResourceSet but got a %T" , newObj ))
71- }
72- return nil , webhook .validate (nil , newCRS )
68+ newCRS , ok := newObj .(* addonsv1.ClusterResourceSet )
69+ if ! ok {
70+ return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected a ClusterResourceSet but got a %T" , newObj ))
71+ }
72+ return nil , webhook .validate (nil , newCRS )
7373}
7474
7575// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
7676func (webhook * ClusterResourceSet ) ValidateUpdate (_ context.Context , oldObj , newObj runtime.Object ) (admission.Warnings , error ) {
77- oldCRS , ok := oldObj .(* addonsv1.ClusterResourceSet )
78- if ! ok {
79- return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected a ClusterResourceSet but got a %T" , oldObj ))
80- }
81- newCRS , ok := newObj .(* addonsv1.ClusterResourceSet )
82- if ! ok {
83- return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected a ClusterResourceSet but got a %T" , newObj ))
84- }
85- return nil , webhook .validate (oldCRS , newCRS )
77+ oldCRS , ok := oldObj .(* addonsv1.ClusterResourceSet )
78+ if ! ok {
79+ return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected a ClusterResourceSet but got a %T" , oldObj ))
80+ }
81+ newCRS , ok := newObj .(* addonsv1.ClusterResourceSet )
82+ if ! ok {
83+ return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected a ClusterResourceSet but got a %T" , newObj ))
84+ }
85+ return nil , webhook .validate (oldCRS , newCRS )
8686}
8787
8888// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
8989func (webhook * ClusterResourceSet ) ValidateDelete (_ context.Context , _ runtime.Object ) (admission.Warnings , error ) {
90- return nil , nil
90+ return nil , nil
9191}
9292
9393func (webhook * ClusterResourceSet ) validate (oldCRS , newCRS * addonsv1.ClusterResourceSet ) error {
94- var allErrs field.ErrorList
95-
96- // NOTE: ClusterResourceSet is behind ClusterResourceSet feature gate flag; the web hook
97- // must prevent creating new objects when the feature flag is disabled.
98- if ! feature .Gates .Enabled (feature .ClusterResourceSet ) {
99- return field .Forbidden (
100- field .NewPath ("spec" ),
101- "can be set only if the ClusterResourceSet feature flag is enabled" ,
102- )
103- }
104-
105- // Validate selector parses as Selector
106- selector , err := metav1 .LabelSelectorAsSelector (& newCRS .Spec .ClusterSelector )
107- if err != nil {
108- allErrs = append (
109- allErrs ,
110- field .Invalid (field .NewPath ("spec" , "clusterSelector" ), newCRS .Spec .ClusterSelector , err .Error ()),
111- )
112- }
113-
114- // Validate that the selector isn't empty as null selectors do not select any objects.
115- if selector != nil && selector .Empty () {
116- allErrs = append (
117- allErrs ,
118- field .Invalid (field .NewPath ("spec" , "clusterSelector" ), newCRS .Spec .ClusterSelector , "selector must not be empty" ),
119- )
120- }
121-
122- if oldCRS != nil && oldCRS .Spec .Strategy != "" && oldCRS .Spec .Strategy != newCRS .Spec .Strategy {
123- allErrs = append (
124- allErrs ,
125- field .Invalid (field .NewPath ("spec" , "strategy" ), newCRS .Spec .Strategy , "field is immutable" ),
126- )
127- }
128-
129- if oldCRS != nil && ! reflect .DeepEqual (oldCRS .Spec .ClusterSelector , newCRS .Spec .ClusterSelector ) {
130- allErrs = append (
131- allErrs ,
132- field .Invalid (field .NewPath ("spec" , "clusterSelector" ), newCRS .Spec .ClusterSelector , "field is immutable" ),
133- )
134- }
135-
136- if len (allErrs ) == 0 {
137- return nil
138- }
139-
140- return apierrors .NewInvalid (addonsv1 .GroupVersion .WithKind ("ClusterResourceSet" ).GroupKind (), newCRS .Name , allErrs )
94+ var allErrs field.ErrorList
95+
96+ // NOTE: The "if !feature.Gates.Enabled(feature.ClusterResourceSet)" block has been removed.
97+
98+ // Validate selector parses as Selector
99+ selector , err := metav1 .LabelSelectorAsSelector (& newCRS .Spec .ClusterSelector )
100+ if err != nil {
101+ allErrs = append (
102+ allErrs ,
103+ field .Invalid (field .NewPath ("spec" , "clusterSelector" ), newCRS .Spec .ClusterSelector , err .Error ()),
104+ )
105+ }
106+
107+ // Validate that the selector isn't empty as null selectors do not select any objects.
108+ if selector != nil && selector .Empty () {
109+ allErrs = append (
110+ allErrs ,
111+ field .Invalid (field .NewPath ("spec" , "clusterSelector" ), newCRS .Spec .ClusterSelector , "selector must not be empty" ),
112+ )
113+ }
114+
115+ if oldCRS != nil && oldCRS .Spec .Strategy != "" && oldCRS .Spec .Strategy != newCRS .Spec .Strategy {
116+ allErrs = append (
117+ allErrs ,
118+ field .Invalid (field .NewPath ("spec" , "strategy" ), newCRS .Spec .Strategy , "field is immutable" ),
119+ )
120+ }
121+
122+ if oldCRS != nil && ! reflect .DeepEqual (oldCRS .Spec .ClusterSelector , newCRS .Spec .ClusterSelector ) {
123+ allErrs = append (
124+ allErrs ,
125+ field .Invalid (field .NewPath ("spec" , "clusterSelector" ), newCRS .Spec .ClusterSelector , "field is immutable" ),
126+ )
127+ }
128+
129+ if len (allErrs ) == 0 {
130+ return nil
131+ }
132+
133+ return apierrors .NewInvalid (addonsv1 .GroupVersion .WithKind ("ClusterResourceSet" ).GroupKind (), newCRS .Name , allErrs )
141134}
0 commit comments