diff --git a/controllers/designer/kaoto_controller.go b/controllers/designer/kaoto_controller.go index 08af3d5..e587852 100644 --- a/controllers/designer/kaoto_controller.go +++ b/controllers/designer/kaoto_controller.go @@ -20,6 +20,8 @@ import ( "context" "sort" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + "github.com/kaotoIO/kaoto-operator/pkg/controller/client" "github.com/go-logr/logr" @@ -29,14 +31,13 @@ import ( "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" - kaotoiov1alpha1 "github.com/kaotoIO/kaoto-operator/apis/designer/v1alpha1" + kaotoApi "github.com/kaotoIO/kaoto-operator/apis/designer/v1alpha1" "github.com/kaotoIO/kaoto-operator/pkg/defaults" k8serrors "k8s.io/apimachinery/pkg/api/errors" ) @@ -96,7 +97,7 @@ type KaotoReconciler struct { func (r *KaotoReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { c := ctrl.NewControllerManagedBy(mgr) - c = c.For(&kaotoiov1alpha1.Kaoto{}, builder.WithPredicates( + c = c.For(&kaotoApi.Kaoto{}, builder.WithPredicates( predicate.Or( predicate.GenerationChangedPredicate{}, ))) @@ -110,31 +111,20 @@ func (r *KaotoReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager c = b } - return c.Complete(r) + return c.Complete(reconcile.AsReconciler[*kaotoApi.Kaoto](mgr.GetClient(), r)) } -func (r *KaotoReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { +func (r *KaotoReconciler) Reconcile(ctx context.Context, res *kaotoApi.Kaoto) (ctrl.Result, error) { l := log.FromContext(ctx) - l.Info("Reconciling", "resource", req.NamespacedName.String()) rr := ReconciliationRequest{ - Client: r.Client, - NamespacedName: types.NamespacedName{ - Name: req.Name, - Namespace: req.Namespace, - }, + Client: r.Client, ClusterType: r.ClusterType, // safety copy - Kaoto: &kaotoiov1alpha1.Kaoto{}, + Kaoto: res.DeepCopy(), } - err := r.Get(ctx, req.NamespacedName, rr.Kaoto) - if err != nil { - if k8serrors.IsNotFound(err) { - // no CR found - return ctrl.Result{}, nil - } - } + l.Info("Reconciling", "resource", rr.String()) if rr.Kaoto.ObjectMeta.DeletionTimestamp.IsZero() { @@ -148,7 +138,7 @@ func (r *KaotoReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl return ctrl.Result{}, err } - return ctrl.Result{}, errors.Wrapf(err, "failure adding finalizer to connector cluster %s", req.NamespacedName) + return ctrl.Result{}, errors.Wrapf(err, "failure adding finalizer to connector cluster %s", rr.String()) } } } else { @@ -173,7 +163,7 @@ func (r *KaotoReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl return ctrl.Result{}, err } - return ctrl.Result{}, errors.Wrapf(err, "failure removing finalizer from connector cluster %s", req.NamespacedName) + return ctrl.Result{}, errors.Wrapf(err, "failure removing finalizer from connector cluster %s", rr.String()) } } @@ -221,7 +211,7 @@ func (r *KaotoReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl // Update status // - err = r.Status().Update(ctx, rr.Kaoto) + err := r.Status().Update(ctx, rr.Kaoto) if err != nil && k8serrors.IsConflict(err) { l.Info(err.Error()) return ctrl.Result{Requeue: true}, nil diff --git a/controllers/designer/kaoto_controller_action_deploy.go b/controllers/designer/kaoto_controller_action_deploy.go index 2480a3f..2d22fcb 100644 --- a/controllers/designer/kaoto_controller_action_deploy.go +++ b/controllers/designer/kaoto_controller_action_deploy.go @@ -65,12 +65,9 @@ func (a *deployAction) Apply(ctx context.Context, rr *ReconciliationRequest) err func (a *deployAction) deploy(ctx context.Context, rr *ReconciliationRequest) error { - d, err := a.deployment(ctx, rr) - if err != nil { - return err - } + d := a.deployment(rr) - _, err = rr.Client.AppsV1().Deployments(rr.Kaoto.Namespace).Apply( + _, err := rr.Client.AppsV1().Deployments(rr.Kaoto.Namespace).Apply( ctx, d, metav1.ApplyOptions{ @@ -82,7 +79,7 @@ func (a *deployAction) deploy(ctx context.Context, rr *ReconciliationRequest) er return err } -func (a *deployAction) deployment(ctx context.Context, rr *ReconciliationRequest) (*appsv1ac.DeploymentApplyConfiguration, error) { +func (a *deployAction) deployment(rr *ReconciliationRequest) *appsv1ac.DeploymentApplyConfiguration { image := rr.Kaoto.Spec.Image if image == "" { image = defaults.KaotoAppImage @@ -93,7 +90,7 @@ func (a *deployAction) deployment(ctx context.Context, rr *ReconciliationRequest envs := make([]*corev1ac.EnvVarApplyConfiguration, 0) envs = append(envs, apply.WithEnvFromField("NAMESPACE", "metadata.namespace")) - resource := appsv1ac.Deployment(rr.Kaoto.Name, rr.Kaoto.Namespace). + return appsv1ac.Deployment(rr.Kaoto.Name, rr.Kaoto.Namespace). WithOwnerReferences(apply.WithOwnerReference(rr.Kaoto)). WithLabels(Labels(rr.Kaoto)). WithSpec(appsv1ac.DeploymentSpec(). @@ -114,6 +111,4 @@ func (a *deployAction) deployment(ctx context.Context, rr *ReconciliationRequest corev1.ResourceMemory: KaotoStandaloneDefaultMemory, corev1.ResourceCPU: KaotoStandaloneDefaultCPU, })))))) - - return resource, nil } diff --git a/controllers/designer/kaoto_controller_action_expose_ingress.go b/controllers/designer/kaoto_controller_action_expose_ingress.go index 8ba2e5a..abf0b71 100644 --- a/controllers/designer/kaoto_controller_action_expose_ingress.go +++ b/controllers/designer/kaoto_controller_action_expose_ingress.go @@ -76,7 +76,7 @@ func (a *ingressAction) Apply(ctx context.Context, rr *ReconciliationRequest) er var in netv1.Ingress - if err := rr.Get(ctx, rr.NamespacedName, &in); err != nil && !k8serrors.IsNotFound(err) { + if err := rr.Get(ctx, rr.Key(), &in); err != nil && !k8serrors.IsNotFound(err) { ingressCondition.Status = metav1.ConditionFalse ingressCondition.Reason = "Failure" ingressCondition.Message = err.Error() diff --git a/controllers/designer/kaoto_controller_action_expose_route.go b/controllers/designer/kaoto_controller_action_expose_route.go index 0db566f..1fe22f8 100644 --- a/controllers/designer/kaoto_controller_action_expose_route.go +++ b/controllers/designer/kaoto_controller_action_expose_route.go @@ -82,7 +82,7 @@ func (a *routeAction) Apply(ctx context.Context, rr *ReconciliationRequest) erro var in routev1.Route - if err := rr.Get(ctx, rr.NamespacedName, &in); err != nil && !k8serrors.IsNotFound(err) { + if err := rr.Get(ctx, rr.Key(), &in); err != nil && !k8serrors.IsNotFound(err) { ingressCondition.Status = metav1.ConditionFalse ingressCondition.Reason = "Failure" ingressCondition.Message = err.Error() diff --git a/controllers/designer/types.go b/controllers/designer/types.go index 4ad6491..d198d09 100644 --- a/controllers/designer/types.go +++ b/controllers/designer/types.go @@ -2,6 +2,9 @@ package designer import ( "context" + "fmt" + + "k8s.io/apimachinery/pkg/types" "github.com/kaotoIO/kaoto-operator/pkg/controller/client" @@ -10,7 +13,6 @@ import ( "k8s.io/apimachinery/pkg/api/resource" kaotoiov1alpha1 "github.com/kaotoIO/kaoto-operator/apis/designer/v1alpha1" - "k8s.io/apimachinery/pkg/types" ) type ClusterType string @@ -43,12 +45,22 @@ const ( type ReconciliationRequest struct { *client.Client - types.NamespacedName ClusterType ClusterType Kaoto *kaotoiov1alpha1.Kaoto } +func (rr *ReconciliationRequest) Key() types.NamespacedName { + return types.NamespacedName{ + Namespace: rr.Kaoto.Namespace, + Name: rr.Kaoto.Name, + } +} + +func (rr *ReconciliationRequest) String() string { + return fmt.Sprintf("%s/%s", rr.Kaoto.Namespace, rr.Kaoto.Name) +} + type Action interface { Configure(context.Context, *client.Client, *builder.Builder) (*builder.Builder, error) Apply(context.Context, *ReconciliationRequest) error