Skip to content

Commit 9e06dc2

Browse files
authored
Merge pull request #1600 from shiftstack/capi-1.5.0
✨ Update to CAPI 1.5
2 parents d6ffef3 + 538ef15 commit 9e06dc2

17 files changed

+194
-178
lines changed

api/v1alpha7/openstackcluster_webhook.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
logf "sigs.k8s.io/controller-runtime/pkg/log"
2929
"sigs.k8s.io/controller-runtime/pkg/manager"
3030
"sigs.k8s.io/controller-runtime/pkg/webhook"
31+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3132
)
3233

3334
// log is for logging in this package.
@@ -55,7 +56,7 @@ func (r *OpenStackCluster) Default() {
5556
}
5657

5758
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
58-
func (r *OpenStackCluster) ValidateCreate() error {
59+
func (r *OpenStackCluster) ValidateCreate() (admission.Warnings, error) {
5960
var allErrs field.ErrorList
6061

6162
if r.Spec.IdentityRef != nil && r.Spec.IdentityRef.Kind != defaultIdentityRefKind {
@@ -66,11 +67,11 @@ func (r *OpenStackCluster) ValidateCreate() error {
6667
}
6768

6869
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
69-
func (r *OpenStackCluster) ValidateUpdate(oldRaw runtime.Object) error {
70+
func (r *OpenStackCluster) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings, error) {
7071
var allErrs field.ErrorList
7172
old, ok := oldRaw.(*OpenStackCluster)
7273
if !ok {
73-
return apierrors.NewBadRequest(fmt.Sprintf("expected an OpenStackCluster but got a %T", oldRaw))
74+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an OpenStackCluster but got a %T", oldRaw))
7475
}
7576

7677
if r.Spec.IdentityRef != nil && r.Spec.IdentityRef.Kind != defaultIdentityRefKind {
@@ -137,6 +138,6 @@ func (r *OpenStackCluster) ValidateUpdate(oldRaw runtime.Object) error {
137138
}
138139

139140
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
140-
func (r *OpenStackCluster) ValidateDelete() error {
141-
return nil
141+
func (r *OpenStackCluster) ValidateDelete() (admission.Warnings, error) {
142+
return nil, nil
142143
}

api/v1alpha7/openstackcluster_webhook_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,14 @@ func TestOpenStackCluster_ValidateUpdate(t *testing.T) {
298298
}
299299
for _, tt := range tests {
300300
t.Run(tt.name, func(t *testing.T) {
301-
err := tt.newTemplate.ValidateUpdate(tt.oldTemplate)
301+
warn, err := tt.newTemplate.ValidateUpdate(tt.oldTemplate)
302302
if tt.wantErr {
303303
g.Expect(err).To(HaveOccurred())
304304
} else {
305305
g.Expect(err).NotTo(HaveOccurred())
306306
}
307+
// Nothing emits warnings yet
308+
g.Expect(warn).To(BeEmpty())
307309
})
308310
}
309311
}
@@ -345,12 +347,14 @@ func TestOpenStackCluster_ValidateCreate(t *testing.T) {
345347
}
346348
for _, tt := range tests {
347349
t.Run(tt.name, func(t *testing.T) {
348-
err := tt.template.ValidateCreate()
350+
warn, err := tt.template.ValidateCreate()
349351
if tt.wantErr {
350352
g.Expect(err).To(HaveOccurred())
351353
} else {
352354
g.Expect(err).NotTo(HaveOccurred())
353355
}
356+
// Nothing emits warnings yet
357+
g.Expect(warn).To(BeEmpty())
354358
})
355359
}
356360
}

api/v1alpha7/openstackclustertemplate_webhook.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"k8s.io/apimachinery/pkg/util/validation/field"
2626
ctrl "sigs.k8s.io/controller-runtime"
2727
"sigs.k8s.io/controller-runtime/pkg/webhook"
28+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2829
)
2930

3031
const openStackClusterTemplateImmutableMsg = "OpenStackClusterTemplate spec.template.spec field is immutable. Please create new resource instead."
@@ -51,7 +52,7 @@ func (r *OpenStackClusterTemplate) Default() {
5152
}
5253

5354
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
54-
func (r *OpenStackClusterTemplate) ValidateCreate() error {
55+
func (r *OpenStackClusterTemplate) ValidateCreate() (admission.Warnings, error) {
5556
var allErrs field.ErrorList
5657

5758
if r.Spec.Template.Spec.IdentityRef != nil && r.Spec.Template.Spec.IdentityRef.Kind != defaultIdentityRefKind {
@@ -62,11 +63,11 @@ func (r *OpenStackClusterTemplate) ValidateCreate() error {
6263
}
6364

6465
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
65-
func (r *OpenStackClusterTemplate) ValidateUpdate(oldRaw runtime.Object) error {
66+
func (r *OpenStackClusterTemplate) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings, error) {
6667
var allErrs field.ErrorList
6768
old, ok := oldRaw.(*OpenStackClusterTemplate)
6869
if !ok {
69-
return apierrors.NewBadRequest(fmt.Sprintf("expected an OpenStackClusterTemplate but got a %T", oldRaw))
70+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an OpenStackClusterTemplate but got a %T", oldRaw))
7071
}
7172

7273
if !reflect.DeepEqual(r.Spec.Template.Spec, old.Spec.Template.Spec) {
@@ -79,6 +80,6 @@ func (r *OpenStackClusterTemplate) ValidateUpdate(oldRaw runtime.Object) error {
7980
}
8081

8182
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
82-
func (r *OpenStackClusterTemplate) ValidateDelete() error {
83-
return nil
83+
func (r *OpenStackClusterTemplate) ValidateDelete() (admission.Warnings, error) {
84+
return nil, nil
8485
}

api/v1alpha7/openstackmachine_webhook.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
logf "sigs.k8s.io/controller-runtime/pkg/log"
2828
"sigs.k8s.io/controller-runtime/pkg/manager"
2929
"sigs.k8s.io/controller-runtime/pkg/webhook"
30+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3031
)
3132

3233
// log is for logging in this package.
@@ -54,7 +55,7 @@ func (r *OpenStackMachine) Default() {
5455
}
5556

5657
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
57-
func (r *OpenStackMachine) ValidateCreate() error {
58+
func (r *OpenStackMachine) ValidateCreate() (admission.Warnings, error) {
5859
var allErrs field.ErrorList
5960

6061
if r.Spec.IdentityRef != nil && r.Spec.IdentityRef.Kind != defaultIdentityRefKind {
@@ -65,16 +66,16 @@ func (r *OpenStackMachine) ValidateCreate() error {
6566
}
6667

6768
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
68-
func (r *OpenStackMachine) ValidateUpdate(old runtime.Object) error {
69+
func (r *OpenStackMachine) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
6970
newOpenStackMachine, err := runtime.DefaultUnstructuredConverter.ToUnstructured(r)
7071
if err != nil {
71-
return apierrors.NewInvalid(GroupVersion.WithKind("OpenStackMachine").GroupKind(), r.Name, field.ErrorList{
72+
return nil, apierrors.NewInvalid(GroupVersion.WithKind("OpenStackMachine").GroupKind(), r.Name, field.ErrorList{
7273
field.InternalError(nil, fmt.Errorf("failed to convert new OpenStackMachine to unstructured object: %w", err)),
7374
})
7475
}
7576
oldOpenStackMachine, err := runtime.DefaultUnstructuredConverter.ToUnstructured(old)
7677
if err != nil {
77-
return apierrors.NewInvalid(GroupVersion.WithKind("OpenStackMachine").GroupKind(), r.Name, field.ErrorList{
78+
return nil, apierrors.NewInvalid(GroupVersion.WithKind("OpenStackMachine").GroupKind(), r.Name, field.ErrorList{
7879
field.InternalError(nil, fmt.Errorf("failed to convert old OpenStackMachine to unstructured object: %w", err)),
7980
})
8081
}
@@ -108,6 +109,6 @@ func (r *OpenStackMachine) ValidateUpdate(old runtime.Object) error {
108109
}
109110

110111
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
111-
func (r *OpenStackMachine) ValidateDelete() error {
112-
return nil
112+
func (r *OpenStackMachine) ValidateDelete() (admission.Warnings, error) {
113+
return nil, nil
113114
}

api/v1alpha7/openstackmachinetemplate_webhook.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ func (r *OpenStackMachineTemplateWebhook) SetupWebhookWithManager(mgr manager.Ma
4949
var _ webhook.CustomValidator = &OpenStackMachineTemplateWebhook{}
5050

5151
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type.
52-
func (r *OpenStackMachineTemplateWebhook) ValidateCreate(_ context.Context, obj runtime.Object) error {
52+
func (r *OpenStackMachineTemplateWebhook) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
5353
openStackMachineTemplate, ok := obj.(*OpenStackMachineTemplate)
5454
if !ok {
55-
return apierrors.NewBadRequest(fmt.Sprintf("expected an OpenStackMachineTemplate but got a %T", obj))
55+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an OpenStackMachineTemplate but got a %T", obj))
5656
}
5757

5858
var allErrs field.ErrorList
@@ -65,21 +65,21 @@ func (r *OpenStackMachineTemplateWebhook) ValidateCreate(_ context.Context, obj
6565
}
6666

6767
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type.
68-
func (r *OpenStackMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldRaw runtime.Object, newRaw runtime.Object) error {
68+
func (r *OpenStackMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldRaw runtime.Object, newRaw runtime.Object) (admission.Warnings, error) {
6969
var allErrs field.ErrorList
7070
old, ok := oldRaw.(*OpenStackMachineTemplate)
7171
if !ok {
72-
return apierrors.NewBadRequest(fmt.Sprintf("expected an OpenStackMachineTemplate but got a %T", oldRaw))
72+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an OpenStackMachineTemplate but got a %T", oldRaw))
7373
}
7474

7575
newObj, ok := newRaw.(*OpenStackMachineTemplate)
7676
if !ok {
77-
return apierrors.NewBadRequest(fmt.Sprintf("expected an OpenStackMachineTemplate but got a %T", oldRaw))
77+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an OpenStackMachineTemplate but got a %T", oldRaw))
7878
}
7979

8080
req, err := admission.RequestFromContext(ctx)
8181
if err != nil {
82-
return apierrors.NewBadRequest(fmt.Sprintf("expected a admission.Request inside context: %v", err))
82+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a admission.Request inside context: %v", err))
8383
}
8484

8585
if !topology.ShouldSkipImmutabilityChecks(req, newObj) &&
@@ -93,6 +93,6 @@ func (r *OpenStackMachineTemplateWebhook) ValidateUpdate(ctx context.Context, ol
9393
}
9494

9595
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
96-
func (r *OpenStackMachineTemplateWebhook) ValidateDelete(_ context.Context, _ runtime.Object) error {
97-
return nil
96+
func (r *OpenStackMachineTemplateWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
97+
return nil, nil
9898
}

api/v1alpha7/openstackmachinetemplate_webhook_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,14 @@ func TestOpenStackMachineTemplate_ValidateUpdate(t *testing.T) {
157157
webhook := &OpenStackMachineTemplateWebhook{}
158158
ctx := admission.NewContextWithRequest(context.Background(), *tt.req)
159159

160-
err := webhook.ValidateUpdate(ctx, tt.oldTemplate, tt.newTemplate)
160+
warn, err := webhook.ValidateUpdate(ctx, tt.oldTemplate, tt.newTemplate)
161161
if tt.wantErr {
162162
g.Expect(err).To(HaveOccurred())
163163
} else {
164164
g.Expect(err).NotTo(HaveOccurred())
165165
}
166+
// Nothing emits warnings yet
167+
g.Expect(warn).To(BeEmpty())
166168
})
167169
}
168170
}

api/v1alpha7/webhooks.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ import (
2020
apierrors "k8s.io/apimachinery/pkg/api/errors"
2121
"k8s.io/apimachinery/pkg/runtime/schema"
2222
"k8s.io/apimachinery/pkg/util/validation/field"
23+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2324
)
2425

25-
func aggregateObjErrors(gk schema.GroupKind, name string, allErrs field.ErrorList) error {
26+
func aggregateObjErrors(gk schema.GroupKind, name string, allErrs field.ErrorList) (admission.Warnings, error) {
2627
if len(allErrs) == 0 {
27-
return nil
28+
return nil, nil
2829
}
2930

30-
return apierrors.NewInvalid(
31+
return nil, apierrors.NewInvalid(
3132
gk,
3233
name,
3334
allErrs,

controllers/openstackcluster_controller.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import (
4343
"sigs.k8s.io/controller-runtime/pkg/handler"
4444
"sigs.k8s.io/controller-runtime/pkg/predicate"
4545
"sigs.k8s.io/controller-runtime/pkg/reconcile"
46-
"sigs.k8s.io/controller-runtime/pkg/source"
4746

4847
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7"
4948
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/compute"
@@ -596,9 +595,9 @@ func (r *OpenStackClusterReconciler) SetupWithManager(ctx context.Context, mgr c
596595
),
597596
).
598597
Watches(
599-
&source.Kind{Type: &clusterv1.Cluster{}},
600-
handler.EnqueueRequestsFromMapFunc(func(o client.Object) []reconcile.Request {
601-
requests := clusterToInfraFn(o)
598+
&clusterv1.Cluster{},
599+
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) []reconcile.Request {
600+
requests := clusterToInfraFn(ctx, o)
602601
if len(requests) < 1 {
603602
return nil
604603
}

controllers/openstackmachine_controller.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ import (
4747
"sigs.k8s.io/controller-runtime/pkg/handler"
4848
"sigs.k8s.io/controller-runtime/pkg/predicate"
4949
"sigs.k8s.io/controller-runtime/pkg/reconcile"
50-
"sigs.k8s.io/controller-runtime/pkg/source"
5150

5251
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7"
5352
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/compute"
@@ -203,16 +202,16 @@ func (r *OpenStackMachineReconciler) SetupWithManager(ctx context.Context, mgr c
203202
),
204203
).
205204
Watches(
206-
&source.Kind{Type: &clusterv1.Machine{}},
205+
&clusterv1.Machine{},
207206
handler.EnqueueRequestsFromMapFunc(util.MachineToInfrastructureMapFunc(infrav1.GroupVersion.WithKind("OpenStackMachine"))),
208207
).
209208
Watches(
210-
&source.Kind{Type: &infrav1.OpenStackCluster{}},
209+
&infrav1.OpenStackCluster{},
211210
handler.EnqueueRequestsFromMapFunc(r.OpenStackClusterToOpenStackMachines(ctx)),
212211
).
213212
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
214213
Watches(
215-
&source.Kind{Type: &clusterv1.Cluster{}},
214+
&clusterv1.Cluster{},
216215
handler.EnqueueRequestsFromMapFunc(r.requeueOpenStackMachinesForUnpausedCluster(ctx)),
217216
builder.WithPredicates(predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx))),
218217
).
@@ -521,7 +520,7 @@ func (r *OpenStackMachineReconciler) reconcileLoadBalancerMember(scope scope.Sco
521520
// of OpenStackMachines.
522521
func (r *OpenStackMachineReconciler) OpenStackClusterToOpenStackMachines(ctx context.Context) handler.MapFunc {
523522
log := ctrl.LoggerFrom(ctx)
524-
return func(o client.Object) []ctrl.Request {
523+
return func(ctx context.Context, o client.Object) []ctrl.Request {
525524
c, ok := o.(*infrav1.OpenStackCluster)
526525
if !ok {
527526
panic(fmt.Sprintf("Expected a OpenStackCluster but got a %T", o))
@@ -570,7 +569,7 @@ func (r *OpenStackMachineReconciler) getBootstrapData(ctx context.Context, machi
570569

571570
func (r *OpenStackMachineReconciler) requeueOpenStackMachinesForUnpausedCluster(ctx context.Context) handler.MapFunc {
572571
log := ctrl.LoggerFrom(ctx)
573-
return func(o client.Object) []ctrl.Request {
572+
return func(ctx context.Context, o client.Object) []ctrl.Request {
574573
c, ok := o.(*clusterv1.Cluster)
575574
if !ok {
576575
panic(fmt.Sprintf("Expected a Cluster but got a %T", o))

0 commit comments

Comments
 (0)