Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions api/v1alpha7/openstackcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

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

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

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

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

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

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *OpenStackCluster) ValidateDelete() error {
return nil
func (r *OpenStackCluster) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}
8 changes: 6 additions & 2 deletions api/v1alpha7/openstackcluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,14 @@ func TestOpenStackCluster_ValidateUpdate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.newTemplate.ValidateUpdate(tt.oldTemplate)
warn, err := tt.newTemplate.ValidateUpdate(tt.oldTemplate)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
// Nothing emits warnings yet
g.Expect(warn).To(BeEmpty())
})
}
}
Expand Down Expand Up @@ -345,12 +347,14 @@ func TestOpenStackCluster_ValidateCreate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.template.ValidateCreate()
warn, err := tt.template.ValidateCreate()
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
// Nothing emits warnings yet
g.Expect(warn).To(BeEmpty())
})
}
}
11 changes: 6 additions & 5 deletions api/v1alpha7/openstackclustertemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

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

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

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

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

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

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *OpenStackClusterTemplate) ValidateDelete() error {
return nil
func (r *OpenStackClusterTemplate) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}
13 changes: 7 additions & 6 deletions api/v1alpha7/openstackmachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

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

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

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

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *OpenStackMachine) ValidateUpdate(old runtime.Object) error {
func (r *OpenStackMachine) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
newOpenStackMachine, err := runtime.DefaultUnstructuredConverter.ToUnstructured(r)
if err != nil {
return apierrors.NewInvalid(GroupVersion.WithKind("OpenStackMachine").GroupKind(), r.Name, field.ErrorList{
return nil, apierrors.NewInvalid(GroupVersion.WithKind("OpenStackMachine").GroupKind(), r.Name, field.ErrorList{
field.InternalError(nil, fmt.Errorf("failed to convert new OpenStackMachine to unstructured object: %w", err)),
})
}
oldOpenStackMachine, err := runtime.DefaultUnstructuredConverter.ToUnstructured(old)
if err != nil {
return apierrors.NewInvalid(GroupVersion.WithKind("OpenStackMachine").GroupKind(), r.Name, field.ErrorList{
return nil, apierrors.NewInvalid(GroupVersion.WithKind("OpenStackMachine").GroupKind(), r.Name, field.ErrorList{
field.InternalError(nil, fmt.Errorf("failed to convert old OpenStackMachine to unstructured object: %w", err)),
})
}
Expand Down Expand Up @@ -108,6 +109,6 @@ func (r *OpenStackMachine) ValidateUpdate(old runtime.Object) error {
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *OpenStackMachine) ValidateDelete() error {
return nil
func (r *OpenStackMachine) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}
16 changes: 8 additions & 8 deletions api/v1alpha7/openstackmachinetemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func (r *OpenStackMachineTemplateWebhook) SetupWebhookWithManager(mgr manager.Ma
var _ webhook.CustomValidator = &OpenStackMachineTemplateWebhook{}

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

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

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

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

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

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

// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
func (r *OpenStackMachineTemplateWebhook) ValidateDelete(_ context.Context, _ runtime.Object) error {
return nil
func (r *OpenStackMachineTemplateWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
return nil, nil
}
4 changes: 3 additions & 1 deletion api/v1alpha7/openstackmachinetemplate_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,14 @@ func TestOpenStackMachineTemplate_ValidateUpdate(t *testing.T) {
webhook := &OpenStackMachineTemplateWebhook{}
ctx := admission.NewContextWithRequest(context.Background(), *tt.req)

err := webhook.ValidateUpdate(ctx, tt.oldTemplate, tt.newTemplate)
warn, err := webhook.ValidateUpdate(ctx, tt.oldTemplate, tt.newTemplate)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
// Nothing emits warnings yet
g.Expect(warn).To(BeEmpty())
})
}
}
7 changes: 4 additions & 3 deletions api/v1alpha7/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

func aggregateObjErrors(gk schema.GroupKind, name string, allErrs field.ErrorList) error {
func aggregateObjErrors(gk schema.GroupKind, name string, allErrs field.ErrorList) (admission.Warnings, error) {
if len(allErrs) == 0 {
return nil
return nil, nil
}

return apierrors.NewInvalid(
return nil, apierrors.NewInvalid(
gk,
name,
allErrs,
Expand Down
7 changes: 3 additions & 4 deletions controllers/openstackcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/compute"
Expand Down Expand Up @@ -596,9 +595,9 @@ func (r *OpenStackClusterReconciler) SetupWithManager(ctx context.Context, mgr c
),
).
Watches(
&source.Kind{Type: &clusterv1.Cluster{}},
handler.EnqueueRequestsFromMapFunc(func(o client.Object) []reconcile.Request {
requests := clusterToInfraFn(o)
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) []reconcile.Request {
requests := clusterToInfraFn(ctx, o)
if len(requests) < 1 {
return nil
}
Expand Down
11 changes: 5 additions & 6 deletions controllers/openstackmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/compute"
Expand Down Expand Up @@ -203,16 +202,16 @@ func (r *OpenStackMachineReconciler) SetupWithManager(ctx context.Context, mgr c
),
).
Watches(
&source.Kind{Type: &clusterv1.Machine{}},
&clusterv1.Machine{},
handler.EnqueueRequestsFromMapFunc(util.MachineToInfrastructureMapFunc(infrav1.GroupVersion.WithKind("OpenStackMachine"))),
).
Watches(
&source.Kind{Type: &infrav1.OpenStackCluster{}},
&infrav1.OpenStackCluster{},
handler.EnqueueRequestsFromMapFunc(r.OpenStackClusterToOpenStackMachines(ctx)),
).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Watches(
&source.Kind{Type: &clusterv1.Cluster{}},
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(r.requeueOpenStackMachinesForUnpausedCluster(ctx)),
builder.WithPredicates(predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx))),
).
Expand Down Expand Up @@ -521,7 +520,7 @@ func (r *OpenStackMachineReconciler) reconcileLoadBalancerMember(scope scope.Sco
// of OpenStackMachines.
func (r *OpenStackMachineReconciler) OpenStackClusterToOpenStackMachines(ctx context.Context) handler.MapFunc {
log := ctrl.LoggerFrom(ctx)
return func(o client.Object) []ctrl.Request {
return func(ctx context.Context, o client.Object) []ctrl.Request {
c, ok := o.(*infrav1.OpenStackCluster)
if !ok {
panic(fmt.Sprintf("Expected a OpenStackCluster but got a %T", o))
Expand Down Expand Up @@ -570,7 +569,7 @@ func (r *OpenStackMachineReconciler) getBootstrapData(ctx context.Context, machi

func (r *OpenStackMachineReconciler) requeueOpenStackMachinesForUnpausedCluster(ctx context.Context) handler.MapFunc {
log := ctrl.LoggerFrom(ctx)
return func(o client.Object) []ctrl.Request {
return func(ctx context.Context, o client.Object) []ctrl.Request {
c, ok := o.(*clusterv1.Cluster)
if !ok {
panic(fmt.Sprintf("Expected a Cluster but got a %T", o))
Expand Down
Loading