Skip to content

Commit

Permalink
Merge pull request #164 from lburgazzoli/controller-runtime
Browse files Browse the repository at this point in the history
chore: leverage  reconcile.ObjectReconcile introduced in the latest controller-runtime
  • Loading branch information
lburgazzoli authored Jan 18, 2024
2 parents b5b3f7f + ccf6bee commit 8a34917
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 35 deletions.
34 changes: 12 additions & 22 deletions controllers/designer/kaoto_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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{},
)))
Expand All @@ -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() {

Expand All @@ -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 {
Expand All @@ -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())
}
}

Expand Down Expand Up @@ -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
Expand Down
13 changes: 4 additions & 9 deletions controllers/designer/kaoto_controller_action_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
Expand All @@ -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().
Expand All @@ -114,6 +111,4 @@ func (a *deployAction) deployment(ctx context.Context, rr *ReconciliationRequest
corev1.ResourceMemory: KaotoStandaloneDefaultMemory,
corev1.ResourceCPU: KaotoStandaloneDefaultCPU,
}))))))

return resource, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 14 additions & 2 deletions controllers/designer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package designer

import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/types"

"github.com/kaotoIO/kaoto-operator/pkg/controller/client"

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8a34917

Please sign in to comment.