Skip to content

Commit 4aff56b

Browse files
committed
feat(hypershift-operator): migrate webhooks to controller-runtime v0.24 generic API
Update webhook registrations to use the typed generic builder introduced in controller-runtime v0.23: - NewWebhookManagedBy(mgr).For(&obj{}) → NewWebhookManagedBy(mgr, &obj{}) - Validator/Defaulter methods now use concrete types instead of runtime.Object (e.g. *hyperv1.HostedCluster, *hyperv1.NodePool) - Add conversion.NewRegistry() parameter to NewWebhookHandler - Remove unused apierrors and runtime imports Signed-off-by: Bryan Cox <brcox@redhat.com> Commit-Message-Assisted-by: Claude (via Claude Code)
1 parent 3b40174 commit 4aff56b

6 files changed

Lines changed: 143 additions & 72 deletions

File tree

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ require (
305305
kubevirt.io/controller-lifecycle-operator-sdk/api v0.2.4 // indirect
306306
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.34.0 // indirect
307307
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 // indirect
308-
sigs.k8s.io/kube-storage-version-migrator v0.0.6-0.20230721195810-5c8923c5ff96 // indirect
309308
sigs.k8s.io/kustomize/api v0.21.1 // indirect
310309
sigs.k8s.io/kustomize/kyaml v0.21.1 // indirect
311310
sigs.k8s.io/randfill v1.0.0 // indirect

go.sum

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ cyphar.com/go-pathrs v0.2.1 h1:9nx1vOgwVvX1mNBWDu93+vaceedpbsDqo+XuBGL40b8=
1111
cyphar.com/go-pathrs v0.2.1/go.mod h1:y8f1EMG7r+hCuFf/rXsKqMJrJAUoADZGNh5/vZPKcGc=
1212
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
1313
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
14-
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.21.1 h1:jHb/wfvRikGdxMXYV3QG/SzUOPYN9KEUUuC0Yd0/vC0=
15-
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.21.1/go.mod h1:pzBXCYn05zvYIrwLgtK8Ap8QcjRg+0i76tMQdWN6wOk=
14+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.22.0 h1:aokoqcHvaGjiM3VpjKDfMMnF/8epJ+Q1HLJ7CudztqE=
15+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.22.0/go.mod h1:/WYEx9pcM9Y+Dd/APJaNlSvVSvzl54rrMdZT5+Oi2LM=
1616
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.13.1 h1:Hk5QBxZQC1jb2Fwj6mpzme37xbCDdNTxU7O9eb5+LB4=
1717
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.13.1/go.mod h1:IYus9qsFobWIc2YVwe/WPjcnyCkPKtnHAqUYeebc8z0=
1818
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.2 h1:yz1bePFlP5Vws5+8ez6T3HWXPmwOK7Yvq8QxDBD3SKY=
@@ -215,6 +215,8 @@ github.com/coreos/go-systemd/v22 v22.7.0 h1:LAEzFkke61DFROc7zNLX/WA2i5J8gYqe0rSj
215215
github.com/coreos/go-systemd/v22 v22.7.0/go.mod h1:xNUYtjHu2EDXbsxz1i41wouACIwT7Ybq9o0BQhMwD0w=
216216
github.com/coreos/ignition/v2 v2.25.1 h1:mrXOVwb4ZPlLG1tIki5em604Yiz5oRFlWkISrEYpHds=
217217
github.com/coreos/ignition/v2 v2.25.1/go.mod h1:Px9MZK4oLhMUM3QMzzRhKbHowc5Hkf+VUu67i0gmsNw=
218+
github.com/coreos/stream-metadata-go v0.4.11 h1:sQQOpI+v/eTyYO76r7jJeCmqZcm1oQYKdwirFJ7f0M0=
219+
github.com/coreos/stream-metadata-go v0.4.11/go.mod h1:dTE8UEFgyUcrbdUg7vGT3uIP7S8a1IwUlmWLKlOp8G8=
218220
github.com/coreos/vcontext v0.0.0-20231102161604-685dc7299dc5 h1:sMZSC2BW5LKCdvNbfN12SbKrNvtLBUNjfHZmMvI2ItY=
219221
github.com/coreos/vcontext v0.0.0-20231102161604-685dc7299dc5/go.mod h1:Salmysdw7DAVuobBW/LwsKKgpyCPHUhjyJoMJD+ZJiI=
220222
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
@@ -825,8 +827,8 @@ golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su
825827
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
826828
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
827829
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
828-
golang.org/x/net v0.54.0 h1:2zJIZAxAHV/OHCDTCOHAYehQzLfSXuf/5SoL/Dv6w/w=
829-
golang.org/x/net v0.54.0/go.mod h1:Sj4oj8jK6XmHpBZU/zWHw3BV3abl4Kvi+Ut7cQcY+cQ=
830+
golang.org/x/net v0.55.0 h1:bcvxaJn3e1U6InsFWt1JUq1aSjnRxLzT2rtD2KfkDF8=
831+
golang.org/x/net v0.55.0/go.mod h1:L5U2KuzuOe1lY7Z+aWVIKK6qEeJXnXV9yzGA+WCHJww=
830832
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
831833
golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs=
832834
golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q=
@@ -877,8 +879,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
877879
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
878880
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
879881
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
880-
golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ=
881-
golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
882+
golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=
883+
golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
882884
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
883885
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
884886
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=

hypershift-operator/controllers/hostedcluster/hostedcluster_webhook.go

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import (
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.
133121
func 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-
177160
type 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

vendor/modules.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ cyphar.com/go-pathrs
3131
cyphar.com/go-pathrs/internal/fdutils
3232
cyphar.com/go-pathrs/internal/libpathrs
3333
cyphar.com/go-pathrs/procfs
34-
# github.com/Azure/azure-sdk-for-go/sdk/azcore v1.21.1
34+
# github.com/Azure/azure-sdk-for-go/sdk/azcore v1.22.0
3535
## explicit; go 1.25.0
3636
github.com/Azure/azure-sdk-for-go/sdk/azcore
3737
github.com/Azure/azure-sdk-for-go/sdk/azcore/arm
@@ -543,6 +543,10 @@ github.com/coreos/ignition/v2/config/shared/parse
543543
github.com/coreos/ignition/v2/config/shared/validations
544544
github.com/coreos/ignition/v2/config/util
545545
github.com/coreos/ignition/v2/config/v3_2/types
546+
# github.com/coreos/stream-metadata-go v0.4.11
547+
## explicit; go 1.18
548+
github.com/coreos/stream-metadata-go/stream
549+
github.com/coreos/stream-metadata-go/stream/rhcos
546550
# github.com/coreos/vcontext v0.0.0-20231102161604-685dc7299dc5
547551
## explicit; go 1.18
548552
github.com/coreos/vcontext/path
@@ -1160,6 +1164,7 @@ github.com/openshift/library-go/pkg/network
11601164
github.com/openshift/library-go/pkg/operator/apiserver/audit
11611165
github.com/openshift/library-go/pkg/operator/certrotation
11621166
github.com/openshift/library-go/pkg/operator/condition
1167+
github.com/openshift/library-go/pkg/operator/encryption/controllers/migrators
11631168
github.com/openshift/library-go/pkg/operator/events
11641169
github.com/openshift/library-go/pkg/operator/management
11651170
github.com/openshift/library-go/pkg/operator/resource/resourceapply
@@ -1467,7 +1472,7 @@ golang.org/x/exp/slices
14671472
# golang.org/x/mod v0.35.0
14681473
## explicit; go 1.25.0
14691474
golang.org/x/mod/semver
1470-
# golang.org/x/net v0.54.0
1475+
# golang.org/x/net v0.55.0
14711476
## explicit; go 1.25.0
14721477
golang.org/x/net/context
14731478
golang.org/x/net/html
@@ -1501,7 +1506,7 @@ golang.org/x/oauth2/jwt
15011506
## explicit; go 1.25.0
15021507
golang.org/x/sync/errgroup
15031508
golang.org/x/sync/singleflight
1504-
# golang.org/x/sys v0.44.0
1509+
# golang.org/x/sys v0.45.0
15051510
## explicit; go 1.25.0
15061511
golang.org/x/sys/cpu
15071512
golang.org/x/sys/plan9
@@ -2619,6 +2624,7 @@ sigs.k8s.io/cluster-api-provider-aws/v2/pkg/hash
26192624
# sigs.k8s.io/cluster-api-provider-azure v1.23.2
26202625
## explicit; go 1.25.0
26212626
sigs.k8s.io/cluster-api-provider-azure/api/v1beta1
2627+
sigs.k8s.io/cluster-api-provider-azure/util/azure
26222628
# sigs.k8s.io/cluster-api-provider-gcp v1.12.0
26232629
## explicit; go 1.24.0
26242630
sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1
@@ -2713,6 +2719,11 @@ sigs.k8s.io/kube-storage-version-migrator/pkg/apis/migration/v1alpha1
27132719
sigs.k8s.io/kube-storage-version-migrator/pkg/clients/clientset
27142720
sigs.k8s.io/kube-storage-version-migrator/pkg/clients/clientset/scheme
27152721
sigs.k8s.io/kube-storage-version-migrator/pkg/clients/clientset/typed/migration/v1alpha1
2722+
sigs.k8s.io/kube-storage-version-migrator/pkg/clients/informer
2723+
sigs.k8s.io/kube-storage-version-migrator/pkg/clients/informer/internalinterfaces
2724+
sigs.k8s.io/kube-storage-version-migrator/pkg/clients/informer/migration
2725+
sigs.k8s.io/kube-storage-version-migrator/pkg/clients/informer/migration/v1alpha1
2726+
sigs.k8s.io/kube-storage-version-migrator/pkg/clients/lister/migration/v1alpha1
27162727
# sigs.k8s.io/kustomize/api v0.21.1
27172728
## explicit; go 1.24.0
27182729
sigs.k8s.io/kustomize/api/filters/annotations

vendor/sigs.k8s.io/cluster-api-provider-azure/util/azure/azure.go

Lines changed: 109 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/sigs.k8s.io/controller-runtime/.gomodcheck.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)