move everything to src
All checks were successful
Build and Publish / build-release (push) Successful in 8m29s
All checks were successful
Build and Publish / build-release (push) Successful in 8m29s
This commit is contained in:
192
internal/controller/action_controller.gold
Normal file
192
internal/controller/action_controller.gold
Normal file
@@ -0,0 +1,192 @@
|
||||
/*
|
||||
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"
|
||||
durationpb "google.golang.org/protobuf/types/known/durationpb"
|
||||
"k8s.io/client-go/util/workqueue"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
ctrlClient "sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
)
|
||||
|
||||
// ActionReconciler reconciles a Action object
|
||||
type ActionReconciler struct {
|
||||
client.Client
|
||||
RefResolver *zitadelv1alpha1.RefResolver
|
||||
ConditionReady *condition.Ready
|
||||
RequeueInterval time.Duration
|
||||
Builder *builder.Builder
|
||||
}
|
||||
|
||||
func NewActionReconciler(client client.Client, refResolver *zitadelv1alpha1.RefResolver, builder *builder.Builder, conditionReady *condition.Ready,
|
||||
requeueInterval time.Duration) *ActionReconciler {
|
||||
return &ActionReconciler{
|
||||
Client: client,
|
||||
RefResolver: refResolver,
|
||||
ConditionReady: conditionReady,
|
||||
RequeueInterval: requeueInterval,
|
||||
Builder: builder,
|
||||
}
|
||||
}
|
||||
|
||||
//+kubebuilder:rbac:groups=zitadel.topmanage.com,resources=actions,verbs=get;list;watch;create;update;patch;delete
|
||||
//+kubebuilder:rbac:groups=zitadel.topmanage.com,resources=actions/status,verbs=get;update;patch
|
||||
//+kubebuilder:rbac:groups=zitadel.topmanage.com,resources=actions/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 *ActionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||||
var Action zitadelv1alpha1.Action
|
||||
if err := r.Get(ctx, req.NamespacedName, &Action); err != nil {
|
||||
return ctrl.Result{}, client.IgnoreNotFound(err)
|
||||
}
|
||||
wr := newWrappedActionReconciler(r.Client, r.RefResolver, r.Builder, &Action)
|
||||
wf := newWrappedActionFinalizer(r.Client, &Action, r.RefResolver)
|
||||
tf := zitadel.NewZitadelFinalizer(r.Client, wf)
|
||||
tr := zitadel.NewZitadelReconciler(r.Client, r.ConditionReady, wr, tf, r.RequeueInterval)
|
||||
|
||||
result, err := tr.Reconcile(ctx, &Action)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("error reconciling in ActionReconciler: %v", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type wrappedActionReconciler struct {
|
||||
client.Client
|
||||
refResolver *zitadelv1alpha1.RefResolver
|
||||
Action *zitadelv1alpha1.Action
|
||||
Builder *builder.Builder
|
||||
}
|
||||
|
||||
func newWrappedActionReconciler(client client.Client, refResolver *zitadelv1alpha1.RefResolver, builder *builder.Builder,
|
||||
Action *zitadelv1alpha1.Action) zitadel.WrappedReconciler {
|
||||
return &wrappedActionReconciler{
|
||||
Client: client,
|
||||
refResolver: refResolver,
|
||||
Action: Action,
|
||||
Builder: builder,
|
||||
}
|
||||
}
|
||||
|
||||
type actionReoncilePhase struct {
|
||||
Name string
|
||||
Reconcile func(context.Context, *management.Client) error
|
||||
}
|
||||
|
||||
func (wr *wrappedActionReconciler) Reconcile(ctx context.Context, ztdClient *management.Client) error {
|
||||
phases := []actionReoncilePhase{
|
||||
{
|
||||
Name: "action",
|
||||
Reconcile: wr.reconcileAction,
|
||||
},
|
||||
}
|
||||
for _, p := range phases {
|
||||
err := p.Reconcile(ctx, ztdClient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (wr *wrappedActionReconciler) reconcileAction(ctx context.Context, ztdClient *management.Client) error {
|
||||
org, err := wr.refResolver.OrganizationRef(ctx, &wr.Action.Spec.OrganizationRef, wr.Action.Namespace)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx = middleware.SetOrgID(ctx, org.Status.OrgId)
|
||||
|
||||
if wr.Action.Status.ActionId != "" {
|
||||
p, err := ztdClient.GetAction(ctx, &pb.GetActionRequest{Id: wr.Action.Status.ActionId})
|
||||
if p != nil {
|
||||
_, err := ztdClient.UpdateAction(ctx,
|
||||
&pb.UpdateActionRequest{
|
||||
Id: p.Action.Id,
|
||||
Name: wr.Action.Name,
|
||||
Script: wr.Action.Spec.Script,
|
||||
Timeout: durationpb.New(wr.Action.Spec.Timeout.Duration),
|
||||
AllowedToFail: wr.Action.Spec.AllowedToFail,
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
if !strings.Contains(err.Error(), "No changes") {
|
||||
return fmt.Errorf("Error updating Action: %v", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
if !strings.Contains(err.Error(), "not found") {
|
||||
return fmt.Errorf("Error getting Action: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := ztdClient.CreateAction(ctx,
|
||||
&pb.CreateActionRequest{
|
||||
Name: wr.Action.Name,
|
||||
Script: wr.Action.Spec.Script,
|
||||
Timeout: durationpb.New(wr.Action.Spec.Timeout.Duration),
|
||||
AllowedToFail: wr.Action.Spec.AllowedToFail,
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "AlreadyExists") {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("error creating action in Zitadel: %v", err)
|
||||
}
|
||||
patch := ctrlClient.MergeFrom(wr.Action.DeepCopy())
|
||||
wr.Action.Status.ActionId = resp.Id
|
||||
return wr.Client.Status().Patch(ctx, wr.Action, patch)
|
||||
}
|
||||
|
||||
func (wr *wrappedActionReconciler) PatchStatus(ctx context.Context, patcher condition.Patcher) error {
|
||||
patch := client.MergeFrom(wr.Action.DeepCopy())
|
||||
patcher(&wr.Action.Status)
|
||||
|
||||
if err := wr.Client.Status().Patch(ctx, wr.Action, patch); err != nil {
|
||||
return fmt.Errorf("error patching Action status: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
func (r *ActionReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&zitadelv1alpha1.Action{}).
|
||||
WithOptions(controller.Options{RateLimiter: workqueue.NewTypedItemExponentialFailureRateLimiter[reconcile.Request](time.Millisecond*500, time.Minute*3)}).
|
||||
Complete(r)
|
||||
}
|
||||
Reference in New Issue
Block a user