239 lines
8.0 KiB
Go
239 lines
8.0 KiB
Go
/*
|
|
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"
|
|
"golang.org/x/exp/maps"
|
|
"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,
|
|
}
|
|
}
|
|
|
|
type projectReconcilePhase struct {
|
|
Name string
|
|
Reconcile func(context.Context, *management.Client) error
|
|
}
|
|
|
|
func (wr *wrappedProjectReconciler) Reconcile(ctx context.Context, ztdClient *management.Client) error {
|
|
phases := []projectReconcilePhase{
|
|
{
|
|
Name: "project",
|
|
Reconcile: wr.reconcileProject,
|
|
},
|
|
{
|
|
Name: "roles",
|
|
Reconcile: wr.reconcileRoles,
|
|
},
|
|
}
|
|
for _, p := range phases {
|
|
err := p.Reconcile(ctx, ztdClient)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (wr *wrappedProjectReconciler) reconcileProject(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
|
|
}
|
|
if wr.project.Status.ProjectId != "" {
|
|
p, err := ztdClient.GetProjectByID(middleware.SetOrgID(ctx, org.Status.OrgId), &pb.GetProjectByIDRequest{Id: wr.project.Status.ProjectId})
|
|
if p != nil {
|
|
_, 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
|
|
}
|
|
if err != nil {
|
|
if !strings.Contains(err.Error(), "not found") {
|
|
return fmt.Errorf("Error getting project: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
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) reconcileRoles(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
|
|
}
|
|
|
|
resp, err := ztdClient.ListProjectRoles(middleware.SetOrgID(ctx, org.Status.OrgId), &pb.ListProjectRolesRequest{
|
|
ProjectId: wr.project.Status.ProjectId,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("Could not list project roles: %v", err)
|
|
}
|
|
roles := map[string]*pb.BulkAddProjectRolesRequest_Role{}
|
|
deleteRoles := []*pb.BulkAddProjectRolesRequest_Role{}
|
|
for _, role := range wr.project.Spec.Roles {
|
|
roles[role.Key] = &pb.BulkAddProjectRolesRequest_Role{
|
|
Key: role.Key,
|
|
DisplayName: role.DisplayName,
|
|
Group: role.Group,
|
|
}
|
|
}
|
|
|
|
for _, role := range resp.Result {
|
|
if r, ok := roles[role.Key]; ok {
|
|
if r.DisplayName != role.DisplayName || r.Group != role.Group {
|
|
deleteRoles = append(deleteRoles, r)
|
|
} else {
|
|
delete(roles, role.Key)
|
|
}
|
|
} else {
|
|
delete(roles, role.Key)
|
|
}
|
|
}
|
|
|
|
for _, dRole := range deleteRoles {
|
|
if _, err = ztdClient.RemoveProjectRole(middleware.SetOrgID(ctx, org.Status.OrgId), &pb.RemoveProjectRoleRequest{
|
|
ProjectId: wr.project.Status.ProjectId,
|
|
RoleKey: dRole.Key,
|
|
}); err != nil {
|
|
return fmt.Errorf("Error removing project role: %v", err)
|
|
}
|
|
}
|
|
if len(roles) > 0 {
|
|
_, err = ztdClient.BulkAddProjectRoles(middleware.SetOrgID(ctx, org.Status.OrgId), &pb.BulkAddProjectRolesRequest{
|
|
ProjectId: wr.project.Status.ProjectId,
|
|
Roles: maps.Values(roles)})
|
|
if err != nil {
|
|
return fmt.Errorf("Could not add roles to project: %v", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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)
|
|
}
|