/* Copyright 2024. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package controller import ( "context" "fmt" "time" zitadelv1alpha1 "github.com/HaimKortovich/zitadel-resources-operator/api/v1alpha1" "github.com/HaimKortovich/zitadel-resources-operator/pkg/builder" condition "github.com/HaimKortovich/zitadel-resources-operator/pkg/condition" "github.com/HaimKortovich/zitadel-resources-operator/pkg/controller/core" clientv2 "github.com/zitadel/zitadel-go/v3/pkg/client" "k8s.io/client-go/util/workqueue" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/reconcile" ) // ConnectionReconciler reconciles a Connection object type ConnectionReconciler struct { client.Client RefResolver *zitadelv1alpha1.RefResolver ConditionReady *condition.Ready RequeueInterval time.Duration Builder *builder.Builder } func NewConnectionReconciler(client client.Client, refResolver *zitadelv1alpha1.RefResolver, builder *builder.Builder, conditionReady *condition.Ready, requeueInterval time.Duration) *ConnectionReconciler { return &ConnectionReconciler{ Client: client, RefResolver: refResolver, ConditionReady: conditionReady, RequeueInterval: requeueInterval, Builder: builder, } } //+kubebuilder:rbac:groups=zitadel.github.com,resources=connections,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=zitadel.github.com,resources=connections/status,verbs=get;update;patch //+kubebuilder:rbac:groups=zitadel.github.com,resources=connections/finalizers,verbs=update // +kubebuilder:rbac:groups="",resources=secrets,verbs=list;watch;create;patch // Reconcile is part of the main kubernetes reconciliation loop which aims to // move the current state of the cluster closer to the desired state. func (r *ConnectionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { var Connection zitadelv1alpha1.Connection if err := r.Get(ctx, req.NamespacedName, &Connection); err != nil { return ctrl.Result{}, client.IgnoreNotFound(err) } wr := newWrappedConnectionReconciler(r.Client, r.RefResolver, r.Builder, &Connection) wf := newWrappedConnectionFinalizer(r.Client, &Connection, r.RefResolver) tf := core.NewCoreFinalizer(r.Client, wf) tr := core.NewCoreReconciler(r.Client, r.ConditionReady, wr, tf, r.RequeueInterval) result, err := tr.Reconcile(ctx, &Connection) if err != nil { return result, fmt.Errorf("error reconciling in ConnectionReconciler: %v", err) } return result, nil } type wrappedConnectionReconciler struct { client.Client refResolver *zitadelv1alpha1.RefResolver Connection *zitadelv1alpha1.Connection Builder *builder.Builder } func newWrappedConnectionReconciler(client client.Client, refResolver *zitadelv1alpha1.RefResolver, builder *builder.Builder, Connection *zitadelv1alpha1.Connection) core.WrappedCoreReconciler { return &wrappedConnectionReconciler{ Client: client, refResolver: refResolver, Connection: Connection, Builder: builder, } } func (wr *wrappedConnectionReconciler) Reconcile(ctx context.Context, ztdClient *clientv2.Client) error { return nil } func (wr *wrappedConnectionReconciler) PatchStatus(ctx context.Context, patcher condition.Patcher) error { patch := client.MergeFrom(wr.Connection.DeepCopy()) patcher(&wr.Connection.Status) if err := wr.Client.Status().Patch(ctx, wr.Connection, patch); err != nil { return fmt.Errorf("error patching Connection status: %v", err) } return nil } // SetupWithManager sets up the controller with the Manager. func (r *ConnectionReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&zitadelv1alpha1.Connection{}). WithOptions(controller.Options{RateLimiter: workqueue.NewTypedItemExponentialFailureRateLimiter[reconcile.Request](time.Millisecond*500, time.Minute*3)}). Complete(r) }