Add Flows and Actions
[ZITADOPER-4]
This commit is contained in:
164
src/internal/controller/flow_controller.go
Normal file
164
src/internal/controller/flow_controller.go
Normal file
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
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 "bitbucket.org/topmanage-software-engineering/zitadel-k8s-operator/src/api/v1alpha1"
|
||||
"bitbucket.org/topmanage-software-engineering/zitadel-k8s-operator/src/pkg/builder"
|
||||
condition "bitbucket.org/topmanage-software-engineering/zitadel-k8s-operator/src/pkg/condition"
|
||||
"bitbucket.org/topmanage-software-engineering/zitadel-k8s-operator/src/pkg/controller/zitadel"
|
||||
"github.com/zitadel/zitadel-go/v2/pkg/client/management"
|
||||
"github.com/zitadel/zitadel-go/v2/pkg/client/middleware"
|
||||
pb "github.com/zitadel/zitadel-go/v2/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"
|
||||
)
|
||||
|
||||
// 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 {
|
||||
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.NewItemExponentialFailureRateLimiter(time.Millisecond*500, time.Minute*3)}).
|
||||
Complete(r)
|
||||
}
|
||||
Reference in New Issue
Block a user