/* 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 "bitbucket.org/topmanage-software-engineering/zitadel-k8s-operator/src/api/v1alpha1" 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" ctrlClient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" ) // ProjectReconciler reconciles a Project object type ProjectReconciler struct { client.Client RefResolver *zitadelv1alpha1.RefResolver ConditionReady *condition.Ready RequeueInterval time.Duration } func NewProjectReconciler(client client.Client, refResolver *zitadelv1alpha1.RefResolver, conditionReady *condition.Ready, requeueInterval time.Duration) *ProjectReconciler { return &ProjectReconciler{ Client: client, RefResolver: refResolver, ConditionReady: conditionReady, RequeueInterval: requeueInterval, } } //+kubebuilder:rbac:groups=zitadel.topmanage.com,resources=projects,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=zitadel.topmanage.com,resources=projects/status,verbs=get;update;patch //+kubebuilder:rbac:groups=zitadel.topmanage.com,resources=projects/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 *ProjectReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { var project zitadelv1alpha1.Project if err := r.Get(ctx, req.NamespacedName, &project); err != nil { return ctrl.Result{}, client.IgnoreNotFound(err) } wr := newWrappedProjectReconciler(r.Client, r.RefResolver, &project) wf := newWrappedProjectFinalizer(r.Client, &project, r.RefResolver) tf := zitadel.NewZitadelFinalizer(r.Client, wf) tr := zitadel.NewZitadelReconciler(r.Client, r.ConditionReady, wr, tf, r.RequeueInterval) result, err := tr.Reconcile(ctx, &project) if err != nil { return result, fmt.Errorf("error reconciling in ProjectReconciler: %v", err) } return result, nil } type wrappedProjectReconciler struct { client.Client refResolver *zitadelv1alpha1.RefResolver project *zitadelv1alpha1.Project } func newWrappedProjectReconciler(client client.Client, refResolver *zitadelv1alpha1.RefResolver, project *zitadelv1alpha1.Project) zitadel.WrappedReconciler { return &wrappedProjectReconciler{ Client: client, refResolver: refResolver, project: project, } } func (wr *wrappedProjectReconciler) Reconcile(ctx context.Context, ztdClient *management.Client) error { org, err := wr.refResolver.OrganizationRef(ctx, &wr.project.Spec.OrganizationRef, wr.project.Namespace) if err != nil { return err } { _, err := ztdClient.GetProjectByID(middleware.SetOrgID(ctx, org.Status.OrgId), &pb.GetProjectByIDRequest{Id: wr.project.Status.ProjectId}) if err != nil { if !strings.Contains(err.Error(), "not found") { return fmt.Errorf("Error getting Project: %v", err) } } else { _, err := ztdClient.UpdateProject(middleware.SetOrgID(ctx, org.Status.OrgId), &pb.UpdateProjectRequest{ Id: wr.project.Status.ProjectId, Name: wr.project.Name, ProjectRoleAssertion: wr.project.Spec.ProjectRoleAssertion, ProjectRoleCheck: wr.project.Spec.ProjectRoleAssertion, HasProjectCheck: wr.project.Spec.HasProjectCheck}, ) if err != nil { if !strings.Contains(err.Error(), "No changes") { return fmt.Errorf("Error updating Project: %v", err) } } return nil } } resp, err := ztdClient.AddProject(middleware.SetOrgID(ctx, org.Status.OrgId), &pb.AddProjectRequest{ Name: wr.project.Name, ProjectRoleAssertion: wr.project.Spec.ProjectRoleAssertion, ProjectRoleCheck: wr.project.Spec.ProjectRoleAssertion, HasProjectCheck: wr.project.Spec.HasProjectCheck}, ) if err != nil { if strings.Contains(err.Error(), "AlreadyExists") { return nil } return fmt.Errorf("error creating project in Zitadel: %v", err) } patch := ctrlClient.MergeFrom(wr.project.DeepCopy()) wr.project.Status.ProjectId = resp.Id return wr.Client.Status().Patch(ctx, wr.project, patch) } func (wr *wrappedProjectReconciler) PatchStatus(ctx context.Context, patcher condition.Patcher) error { patch := client.MergeFrom(wr.project.DeepCopy()) patcher(&wr.project.Status) if err := wr.Client.Status().Patch(ctx, wr.project, patch); err != nil { return fmt.Errorf("error patching Project status: %v", err) } return nil } // SetupWithManager sets up the controller with the Manager. func (r *ProjectReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&zitadelv1alpha1.Project{}). WithOptions(controller.Options{RateLimiter: workqueue.NewItemExponentialFailureRateLimiter(time.Millisecond*500, time.Minute*3)}). Complete(r) }