From fb8cfb4ce76c88058ba97519fd68f7d29edf807b Mon Sep 17 00:00:00 2001 From: Alexandr Demicev Date: Tue, 10 Oct 2023 16:43:18 +0200 Subject: [PATCH] Fix breaking changes after updating dependecies Signed-off-by: Alexandr Demicev --- bootstrap/api/v1alpha1/rke2config_webhook.go | 17 +++++----- .../v1alpha1/rke2configtemplate_webhook.go | 13 ++++---- .../api/v1alpha1/rke2controlplane_webhook.go | 19 ++++++------ .../rke2controlplanetemplate_webhook.go | 13 ++++---- .../rke2controlplane_controller.go | 31 ++++++++++--------- controlplane/main.go | 11 ++++--- 6 files changed, 57 insertions(+), 47 deletions(-) diff --git a/bootstrap/api/v1alpha1/rke2config_webhook.go b/bootstrap/api/v1alpha1/rke2config_webhook.go index 3416e036..56e4ab7c 100644 --- a/bootstrap/api/v1alpha1/rke2config_webhook.go +++ b/bootstrap/api/v1alpha1/rke2config_webhook.go @@ -27,6 +27,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" ) var ( @@ -62,7 +63,7 @@ func DefaultRKE2ConfigSpec(spec *RKE2ConfigSpec) { var _ webhook.Validator = &RKE2Config{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. -func (r *RKE2Config) ValidateCreate() error { +func (r *RKE2Config) ValidateCreate() (admission.Warnings, error) { rke2configlog.Info("RKE2Config validate create", "rke2config", klog.KObj(r)) var allErrs field.ErrorList @@ -70,14 +71,14 @@ func (r *RKE2Config) ValidateCreate() error { allErrs = append(allErrs, ValidateRKE2ConfigSpec(r.Name, &r.Spec)...) if len(allErrs) == 0 { - return nil + return nil, nil } - return apierrors.NewInvalid(GroupVersion.WithKind("RKE2Config").GroupKind(), r.Name, allErrs) + return nil, apierrors.NewInvalid(GroupVersion.WithKind("RKE2Config").GroupKind(), r.Name, allErrs) } // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. -func (r *RKE2Config) ValidateUpdate(_ runtime.Object) error { +func (r *RKE2Config) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) { rke2configlog.Info("RKE2Config validate update", "rke2config", klog.KObj(r)) var allErrs field.ErrorList @@ -85,15 +86,15 @@ func (r *RKE2Config) ValidateUpdate(_ runtime.Object) error { allErrs = append(allErrs, ValidateRKE2ConfigSpec(r.Name, &r.Spec)...) if len(allErrs) == 0 { - return nil + return nil, nil } - return apierrors.NewInvalid(GroupVersion.WithKind("RKE2Config").GroupKind(), r.Name, allErrs) + return nil, apierrors.NewInvalid(GroupVersion.WithKind("RKE2Config").GroupKind(), r.Name, allErrs) } // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. -func (r *RKE2Config) ValidateDelete() error { - return nil +func (r *RKE2Config) ValidateDelete() (admission.Warnings, error) { + return nil, nil } // ValidateRKE2ConfigSpec validates the RKE2ConfigSpec. diff --git a/bootstrap/api/v1alpha1/rke2configtemplate_webhook.go b/bootstrap/api/v1alpha1/rke2configtemplate_webhook.go index 59a9dcb9..1143baf6 100644 --- a/bootstrap/api/v1alpha1/rke2configtemplate_webhook.go +++ b/bootstrap/api/v1alpha1/rke2configtemplate_webhook.go @@ -21,6 +21,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" ) // RKE2configtemplatelog is for logging in this package. @@ -47,22 +48,22 @@ func (r *RKE2ConfigTemplate) Default() { var _ webhook.Validator = &RKE2ConfigTemplate{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. -func (r *RKE2ConfigTemplate) ValidateCreate() error { +func (r *RKE2ConfigTemplate) ValidateCreate() (admission.Warnings, error) { RKE2configtemplatelog.Info("validate create", "name", r.Name) - return nil + return nil, nil } // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. -func (r *RKE2ConfigTemplate) ValidateUpdate(old runtime.Object) error { +func (r *RKE2ConfigTemplate) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { RKE2configtemplatelog.Info("validate update", "name", r.Name) - return nil + return nil, nil } // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. -func (r *RKE2ConfigTemplate) ValidateDelete() error { +func (r *RKE2ConfigTemplate) ValidateDelete() (admission.Warnings, error) { RKE2configtemplatelog.Info("validate delete", "name", r.Name) - return nil + return nil, nil } diff --git a/controlplane/api/v1alpha1/rke2controlplane_webhook.go b/controlplane/api/v1alpha1/rke2controlplane_webhook.go index 5d14777e..4a65616f 100644 --- a/controlplane/api/v1alpha1/rke2controlplane_webhook.go +++ b/controlplane/api/v1alpha1/rke2controlplane_webhook.go @@ -26,6 +26,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" bootstrapv1 "github.com/rancher-sandbox/cluster-api-provider-rke2/bootstrap/api/v1alpha1" ) @@ -54,7 +55,7 @@ func (r *RKE2ControlPlane) Default() { var _ webhook.Validator = &RKE2ControlPlane{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. -func (r *RKE2ControlPlane) ValidateCreate() error { +func (r *RKE2ControlPlane) ValidateCreate() (admission.Warnings, error) { rke2controlplanelog.Info("RKE2ControlPlane validate create", "control-plane", klog.KObj(r)) var allErrs field.ErrorList @@ -64,17 +65,17 @@ func (r *RKE2ControlPlane) ValidateCreate() error { allErrs = append(allErrs, r.validateRegistrationMethod()...) if len(allErrs) == 0 { - return nil + return nil, nil } - return apierrors.NewInvalid(GroupVersion.WithKind("RKE2ControlPlane").GroupKind(), r.Name, allErrs) + return nil, apierrors.NewInvalid(GroupVersion.WithKind("RKE2ControlPlane").GroupKind(), r.Name, allErrs) } // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. -func (r *RKE2ControlPlane) ValidateUpdate(old runtime.Object) error { +func (r *RKE2ControlPlane) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { oldControlplane, ok := old.(*RKE2ControlPlane) if !ok { - return apierrors.NewInvalid(GroupVersion.WithKind("RKE2ControlPlane").GroupKind(), r.Name, field.ErrorList{ + return nil, apierrors.NewInvalid(GroupVersion.WithKind("RKE2ControlPlane").GroupKind(), r.Name, field.ErrorList{ field.InternalError(nil, errors.New("failed to convert old RKE2ControlPlane to object")), }) } @@ -91,17 +92,17 @@ func (r *RKE2ControlPlane) ValidateUpdate(old runtime.Object) error { } if len(allErrs) == 0 { - return nil + return nil, nil } - return apierrors.NewInvalid(GroupVersion.WithKind("RKE2ControlPlane").GroupKind(), r.Name, allErrs) + return nil, apierrors.NewInvalid(GroupVersion.WithKind("RKE2ControlPlane").GroupKind(), r.Name, allErrs) } // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. -func (r *RKE2ControlPlane) ValidateDelete() error { +func (r *RKE2ControlPlane) ValidateDelete() (admission.Warnings, error) { rke2controlplanelog.Info("validate delete", "name", r.Name) - return nil + return nil, nil } func (r *RKE2ControlPlane) validateCNI() field.ErrorList { diff --git a/controlplane/api/v1alpha1/rke2controlplanetemplate_webhook.go b/controlplane/api/v1alpha1/rke2controlplanetemplate_webhook.go index 365f4150..1ee5a734 100644 --- a/controlplane/api/v1alpha1/rke2controlplanetemplate_webhook.go +++ b/controlplane/api/v1alpha1/rke2controlplanetemplate_webhook.go @@ -21,6 +21,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" ) // log is for logging in this package. @@ -47,22 +48,22 @@ func (r *RKE2ControlPlaneTemplate) Default() { var _ webhook.Validator = &RKE2ControlPlaneTemplate{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. -func (r *RKE2ControlPlaneTemplate) ValidateCreate() error { +func (r *RKE2ControlPlaneTemplate) ValidateCreate() (admission.Warnings, error) { rke2controlplanetemplatelog.Info("validate create", "name", r.Name) - return nil + return nil, nil } // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. -func (r *RKE2ControlPlaneTemplate) ValidateUpdate(old runtime.Object) error { +func (r *RKE2ControlPlaneTemplate) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { rke2controlplanetemplatelog.Info("validate update", "name", r.Name) - return nil + return nil, nil } // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. -func (r *RKE2ControlPlaneTemplate) ValidateDelete() error { +func (r *RKE2ControlPlaneTemplate) ValidateDelete() (admission.Warnings, error) { rke2controlplanetemplatelog.Info("validate delete", "name", r.Name) - return nil + return nil, nil } diff --git a/controlplane/internal/controllers/rke2controlplane_controller.go b/controlplane/internal/controllers/rke2controlplane_controller.go index b68eb4cf..c8bee858 100644 --- a/controlplane/internal/controllers/rke2controlplane_controller.go +++ b/controlplane/internal/controllers/rke2controlplane_controller.go @@ -214,7 +214,7 @@ func patchRKE2ControlPlane(ctx context.Context, patchHelper *patch.Helper, rcp * } // SetupWithManager sets up the controller with the Manager. -func (r *RKE2ControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *RKE2ControlPlaneReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { c, err := ctrl.NewControllerManagedBy(mgr). For(&controlplanev1.RKE2ControlPlane{}). Build(r) @@ -223,8 +223,8 @@ func (r *RKE2ControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager) error { } err = c.Watch( - &source.Kind{Type: &clusterv1.Cluster{}}, - handler.EnqueueRequestsFromMapFunc(r.ClusterToRKE2ControlPlane), + source.Kind(mgr.GetCache(), &clusterv1.Cluster{}), + handler.EnqueueRequestsFromMapFunc(r.ClusterToRKE2ControlPlane(ctx)), ) if err != nil { return errors.Wrap(err, "failed adding Watch for Clusters to controller manager") @@ -736,18 +736,21 @@ func (r *RKE2ControlPlaneReconciler) upgradeControlPlane( // ClusterToRKE2ControlPlane is a handler.ToRequestsFunc to be used to enqueue requests for reconciliation // for RKE2ControlPlane based on updates to a Cluster. -func (r *RKE2ControlPlaneReconciler) ClusterToRKE2ControlPlane(o client.Object) []ctrl.Request { - c, ok := o.(*clusterv1.Cluster) - if !ok { - r.Log.Error(nil, fmt.Sprintf("Expected a Cluster but got a %T", o)) +func (r *RKE2ControlPlaneReconciler) ClusterToRKE2ControlPlane(ctx context.Context) handler.MapFunc { + log := log.FromContext(ctx) - return nil - } + return func(_ context.Context, o client.Object) []ctrl.Request { + c, ok := o.(*clusterv1.Cluster) + if !ok { + log.Error(nil, fmt.Sprintf("Expected a Cluster but got a %T", o)) - controlPlaneRef := c.Spec.ControlPlaneRef - if controlPlaneRef != nil && controlPlaneRef.Kind == "RKE2ControlPlane" { - return []ctrl.Request{{NamespacedName: client.ObjectKey{Namespace: controlPlaneRef.Namespace, Name: controlPlaneRef.Name}}} - } + return nil + } - return nil + controlPlaneRef := c.Spec.ControlPlaneRef + if controlPlaneRef != nil && controlPlaneRef.Kind == "RKE2ControlPlane" { + return []ctrl.Request{{NamespacedName: client.ObjectKey{Namespace: controlPlaneRef.Namespace, Name: controlPlaneRef.Name}}} + } + return nil + } } diff --git a/controlplane/main.go b/controlplane/main.go index 5227cce5..ab2a7102 100644 --- a/controlplane/main.go +++ b/controlplane/main.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "context" "flag" "fmt" "net/http" @@ -149,14 +150,16 @@ func main() { os.Exit(1) } + ctx := ctrl.SetupSignalHandler() + setupChecks(mgr) - setupReconcilers(mgr) + setupReconcilers(ctx, mgr) setupWebhooks(mgr) //+kubebuilder:scaffold:builder setupLog.Info("starting manager") - if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { + if err := mgr.Start(ctx); err != nil { setupLog.Error(err, "problem running manager") os.Exit(1) } @@ -182,11 +185,11 @@ func setupChecks(mgr ctrl.Manager) { } } -func setupReconcilers(mgr ctrl.Manager) { +func setupReconcilers(ctx context.Context, mgr ctrl.Manager) { if err := (&controllers.RKE2ControlPlaneReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(ctx, mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "RKE2ControlPlane") os.Exit(1) }