All checks were successful
Build and Publish / build-release (push) Successful in 8m29s
169 lines
5.6 KiB
Plaintext
169 lines
5.6 KiB
Plaintext
/*
|
|
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"
|
|
"strings"
|
|
"time"
|
|
|
|
zitadelv1alpha1 "github.com/HaimKortovich/zitadel-k8s-operator/api/v1alpha1"
|
|
"github.com/HaimKortovich/zitadel-k8s-operator/pkg/builder"
|
|
condition "github.com/HaimKortovich/zitadel-k8s-operator/pkg/condition"
|
|
"github.com/HaimKortovich/zitadel-k8s-operator/pkg/controller/zitadel"
|
|
"github.com/zitadel/zitadel-go/v3/pkg/client/management"
|
|
"github.com/zitadel/zitadel-go/v3/pkg/client/middleware"
|
|
pb "github.com/zitadel/zitadel-go/v3/pkg/client/zitadel/management"
|
|
"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"
|
|
)
|
|
|
|
// FlowReconciler reconciles a Flow object
|
|
type FlowReconciler struct {
|
|
client.Client
|
|
RefResolver *zitadelv1alpha1.RefResolver
|
|
ConditionReady *condition.Ready
|
|
RequeueInterval time.Duration
|
|
Builder *builder.Builder
|
|
}
|
|
|
|
func NewFlowReconciler(client client.Client, refResolver *zitadelv1alpha1.RefResolver, builder *builder.Builder, conditionReady *condition.Ready,
|
|
requeueInterval time.Duration) *FlowReconciler {
|
|
return &FlowReconciler{
|
|
Client: client,
|
|
RefResolver: refResolver,
|
|
ConditionReady: conditionReady,
|
|
RequeueInterval: requeueInterval,
|
|
Builder: builder,
|
|
}
|
|
}
|
|
|
|
//+kubebuilder:rbac:groups=zitadel.topmanage.com,resources=flows,verbs=get;list;watch;create;update;patch;delete
|
|
//+kubebuilder:rbac:groups=zitadel.topmanage.com,resources=flows/status,verbs=get;update;patch
|
|
//+kubebuilder:rbac:groups=zitadel.topmanage.com,resources=flows/finalizers,verbs=update
|
|
|
|
// 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 *FlowReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
|
var Flow zitadelv1alpha1.Flow
|
|
if err := r.Get(ctx, req.NamespacedName, &Flow); err != nil {
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
}
|
|
wr := newWrappedFlowReconciler(r.Client, r.RefResolver, r.Builder, &Flow)
|
|
wf := newWrappedFlowFinalizer(r.Client, &Flow, r.RefResolver)
|
|
tf := zitadel.NewZitadelFinalizer(r.Client, wf)
|
|
tr := zitadel.NewZitadelReconciler(r.Client, r.ConditionReady, wr, tf, r.RequeueInterval)
|
|
|
|
result, err := tr.Reconcile(ctx, &Flow)
|
|
if err != nil {
|
|
return result, fmt.Errorf("error reconciling in FlowReconciler: %v", err)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
type wrappedFlowReconciler struct {
|
|
client.Client
|
|
refResolver *zitadelv1alpha1.RefResolver
|
|
Flow *zitadelv1alpha1.Flow
|
|
Builder *builder.Builder
|
|
}
|
|
|
|
func newWrappedFlowReconciler(client client.Client, refResolver *zitadelv1alpha1.RefResolver, builder *builder.Builder,
|
|
Flow *zitadelv1alpha1.Flow) zitadel.WrappedReconciler {
|
|
return &wrappedFlowReconciler{
|
|
Client: client,
|
|
refResolver: refResolver,
|
|
Flow: Flow,
|
|
Builder: builder,
|
|
}
|
|
}
|
|
|
|
type flowReoncilePhase struct {
|
|
Name string
|
|
Reconcile func(context.Context, *management.Client) error
|
|
}
|
|
|
|
func (wr *wrappedFlowReconciler) Reconcile(ctx context.Context, ztdClient *management.Client) error {
|
|
phases := []flowReoncilePhase{
|
|
{
|
|
Name: "flow",
|
|
Reconcile: wr.reconcileFlow,
|
|
},
|
|
}
|
|
for _, p := range phases {
|
|
err := p.Reconcile(ctx, ztdClient)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (wr *wrappedFlowReconciler) reconcileFlow(ctx context.Context, ztdClient *management.Client) error {
|
|
org, err := wr.refResolver.OrganizationRef(ctx, &wr.Flow.Spec.OrganizationRef, wr.Flow.Namespace)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctx = middleware.SetOrgID(ctx, org.Status.OrgId)
|
|
|
|
actionIds := []string{}
|
|
for _, actionRef := range wr.Flow.Spec.ActionRefs {
|
|
action, err := wr.refResolver.ActionRef(ctx, &actionRef, wr.Flow.Namespace)
|
|
if err != nil {
|
|
return fmt.Errorf("Error resolving action reference: %v", err)
|
|
}
|
|
if action.Status.ActionId == "" {
|
|
return fmt.Errorf("Action with name: %s not ready for trigger", action.Name)
|
|
}
|
|
actionIds = append(actionIds, action.Status.ActionId)
|
|
}
|
|
|
|
_, err = ztdClient.SetTriggerActions(ctx, &pb.SetTriggerActionsRequest{
|
|
FlowType: wr.Flow.Spec.FlowType,
|
|
TriggerType: wr.Flow.Spec.TriggerType,
|
|
ActionIds: actionIds,
|
|
})
|
|
if err != nil {
|
|
if !strings.Contains(err.Error(), "No Changes") {
|
|
return fmt.Errorf("Error triggering action flow: %v", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (wr *wrappedFlowReconciler) PatchStatus(ctx context.Context, patcher condition.Patcher) error {
|
|
patch := client.MergeFrom(wr.Flow.DeepCopy())
|
|
patcher(&wr.Flow.Status)
|
|
|
|
if err := wr.Client.Status().Patch(ctx, wr.Flow, patch); err != nil {
|
|
return fmt.Errorf("error patching Flow status: %v", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetupWithManager sets up the controller with the Manager.
|
|
func (r *FlowReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
|
return ctrl.NewControllerManagedBy(mgr).
|
|
For(&zitadelv1alpha1.Flow{}).
|
|
WithOptions(controller.Options{RateLimiter: workqueue.NewTypedItemExponentialFailureRateLimiter[reconcile.Request](time.Millisecond*500, time.Minute*3)}).
|
|
Complete(r)
|
|
}
|