99 "github.com/openshift/hypershift/support/supportedversion"
1010 hyperutil "github.com/openshift/hypershift/support/util"
1111
12- apierrors "k8s.io/apimachinery/pkg/api/errors"
1312 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14- "k8s.io/apimachinery/pkg/runtime"
1513 "k8s.io/apimachinery/pkg/runtime/schema"
1614 utilrand "k8s.io/apimachinery/pkg/util/rand"
1715
@@ -32,12 +30,7 @@ type nodePoolDefaulter struct {
3230 client client.Client
3331}
3432
35- func (defaulter * hostedClusterDefaulter ) Default (ctx context.Context , obj runtime.Object ) error {
36- hcluster , ok := obj .(* hyperv1.HostedCluster )
37- if ! ok {
38- return apierrors .NewBadRequest (fmt .Sprintf ("expected a HostedCluster but got a %T" , obj ))
39- }
40-
33+ func (defaulter * hostedClusterDefaulter ) Default (ctx context.Context , hcluster * hyperv1.HostedCluster ) error {
4134 if hcluster .Spec .Release .Image == "" {
4235 pullSpec , err := supportedversion .LookupLatestSupportedRelease (ctx , hcluster )
4336 if err != nil {
@@ -87,12 +80,7 @@ func (defaulter *hostedClusterDefaulter) Default(ctx context.Context, obj runtim
8780 return nil
8881}
8982
90- func (defaulter * nodePoolDefaulter ) Default (ctx context.Context , obj runtime.Object ) error {
91- np , ok := obj .(* hyperv1.NodePool )
92- if ! ok {
93- return apierrors .NewBadRequest (fmt .Sprintf ("expected a NodePool but got a %T" , obj ))
94- }
95-
83+ func (defaulter * nodePoolDefaulter ) Default (ctx context.Context , np * hyperv1.NodePool ) error {
9684 if np .Spec .Release .Image == "" {
9785 if np .Spec .ClusterName == "" {
9886 return fmt .Errorf ("nodePool.Spec.ClusterName is a required field" )
@@ -131,24 +119,21 @@ func (defaulter *nodePoolDefaulter) Default(ctx context.Context, obj runtime.Obj
131119
132120// SetupWebhookWithManager sets up HostedCluster webhooks.
133121func SetupWebhookWithManager (mgr ctrl.Manager , imageMetaDataProvider * hyperutil.RegistryClientImageMetadataProvider , logger logr.Logger ) error {
134- err := ctrl .NewWebhookManagedBy (mgr ).
135- For (& hyperv1.HostedCluster {}).
122+ err := ctrl .NewWebhookManagedBy (mgr , & hyperv1.HostedCluster {}).
136123 WithDefaulter (& hostedClusterDefaulter {}).
137124 WithValidator (& hostedClusterValidator {}).
138125 Complete ()
139126 if err != nil {
140127 return fmt .Errorf ("unable to register hostedcluster webhook: %w" , err )
141128 }
142- err = ctrl .NewWebhookManagedBy (mgr ).
143- For (& hyperv1.NodePool {}).
129+ err = ctrl .NewWebhookManagedBy (mgr , & hyperv1.NodePool {}).
144130 WithDefaulter (& nodePoolDefaulter {client : mgr .GetClient ()}).
145131 WithValidator (newNodePoolValidator (logger )).
146132 Complete ()
147133 if err != nil {
148134 return fmt .Errorf ("unable to register nodepool webhook: %w" , err )
149135 }
150- err = ctrl .NewWebhookManagedBy (mgr ).
151- For (& hyperv1.HostedControlPlane {}).
136+ err = ctrl .NewWebhookManagedBy (mgr , & hyperv1.HostedControlPlane {}).
152137 Complete ()
153138 if err != nil {
154139 return fmt .Errorf ("unable to register hostedcontrolplane webhook: %w" , err )
@@ -167,21 +152,14 @@ func SetupWebhookWithManager(mgr ctrl.Manager, imageMetaDataProvider *hyperutil.
167152 })
168153
169154 // Register conversion webhook handler for CRD version conversions (HyperShift and CAPI types)
170- mgr .GetWebhookServer ().Register ("/convert" , conversion .NewWebhookHandler (mgr .GetScheme ()))
155+ mgr .GetWebhookServer ().Register ("/convert" , conversion .NewWebhookHandler (mgr .GetScheme (), conversion . NewRegistry () ))
171156
172157 return nil
173158}
174159
175- var _ admission.CustomValidator = (* hostedClusterValidator )(nil )
176-
177160type hostedClusterValidator struct {}
178161
179- func (v hostedClusterValidator ) ValidateCreate (ctx context.Context , obj runtime.Object ) (admission.Warnings , error ) {
180- hc , ok := obj .(* hyperv1.HostedCluster )
181- if ! ok {
182- return nil , fmt .Errorf ("wrong type %T for validation, instead of HostedCluster" , obj )
183- }
184-
162+ func (v hostedClusterValidator ) ValidateCreate (ctx context.Context , hc * hyperv1.HostedCluster ) (admission.Warnings , error ) {
185163 switch hc .Spec .Platform .Type {
186164 case hyperv1 .KubevirtPlatform :
187165 return v .validateCreateKubevirtHostedCluster (ctx , hc )
@@ -190,17 +168,7 @@ func (v hostedClusterValidator) ValidateCreate(ctx context.Context, obj runtime.
190168 }
191169}
192170
193- func (v hostedClusterValidator ) ValidateUpdate (ctx context.Context , oldHC , newHC runtime.Object ) (admission.Warnings , error ) {
194- hc , ok := newHC .(* hyperv1.HostedCluster )
195- if ! ok {
196- return nil , fmt .Errorf ("wrong type %T for validation, instead of HostedCluster" , newHC )
197- }
198-
199- hcOld , ok := oldHC .(* hyperv1.HostedCluster )
200- if ! ok {
201- return nil , fmt .Errorf ("wrong type %T for validation, instead of HostedCluster" , oldHC )
202- }
203-
171+ func (v hostedClusterValidator ) ValidateUpdate (ctx context.Context , hcOld , hc * hyperv1.HostedCluster ) (admission.Warnings , error ) {
204172 switch hc .Spec .Platform .Type {
205173 case hyperv1 .KubevirtPlatform :
206174 err := v .validateUpdateKubevirtHostedCluster (ctx , hcOld , hc )
@@ -211,7 +179,7 @@ func (v hostedClusterValidator) ValidateUpdate(ctx context.Context, oldHC, newHC
211179 return nil , nil
212180}
213181
214- func (v hostedClusterValidator ) ValidateDelete (_ context.Context , _ runtime. Object ) (admission.Warnings , error ) {
182+ func (v hostedClusterValidator ) ValidateDelete (_ context.Context , _ * hyperv1. HostedCluster ) (admission.Warnings , error ) {
215183 return nil , nil
216184}
217185
@@ -243,12 +211,7 @@ func newNodePoolValidator(logger logr.Logger) *nodePoolValidator {
243211 }
244212}
245213
246- func (v nodePoolValidator ) ValidateCreate (ctx context.Context , obj runtime.Object ) (admission.Warnings , error ) {
247- np , ok := obj .(* hyperv1.NodePool )
248- if ! ok {
249- return nil , fmt .Errorf ("wrong type %T for validation, instead of NodePool" , obj )
250- }
251-
214+ func (v nodePoolValidator ) ValidateCreate (ctx context.Context , np * hyperv1.NodePool ) (admission.Warnings , error ) {
252215 switch np .Spec .Platform .Type {
253216 case hyperv1 .KubevirtPlatform :
254217 return v .validateCreateKubevirtNodePool (ctx , np )
@@ -257,17 +220,7 @@ func (v nodePoolValidator) ValidateCreate(ctx context.Context, obj runtime.Objec
257220 }
258221}
259222
260- func (v nodePoolValidator ) ValidateUpdate (ctx context.Context , oldNP , newNP runtime.Object ) (admission.Warnings , error ) {
261- npNew , ok := newNP .(* hyperv1.NodePool )
262- if ! ok {
263- return nil , fmt .Errorf ("wrong type %T for validation, instead of NodePool" , newNP )
264- }
265-
266- npOld , ok := oldNP .(* hyperv1.NodePool )
267- if ! ok {
268- return nil , fmt .Errorf ("wrong type %T for validation, instead of NodePool" , npOld )
269- }
270-
223+ func (v nodePoolValidator ) ValidateUpdate (ctx context.Context , npOld , npNew * hyperv1.NodePool ) (admission.Warnings , error ) {
271224 switch npNew .Spec .Platform .Type {
272225 case hyperv1 .KubevirtPlatform :
273226 err := v .validateUpdateKubevirtNodePool (ctx , npOld , npNew )
@@ -279,7 +232,7 @@ func (v nodePoolValidator) ValidateUpdate(ctx context.Context, oldNP, newNP runt
279232 return nil , nil
280233}
281234
282- func (v nodePoolValidator ) ValidateDelete (_ context.Context , _ runtime. Object ) (admission.Warnings , error ) {
235+ func (v nodePoolValidator ) ValidateDelete (_ context.Context , _ * hyperv1. NodePool ) (admission.Warnings , error ) {
283236 return nil , nil
284237}
285238
0 commit comments