From 63c86b7ebec741842b00189b2d4f92824ea96969 Mon Sep 17 00:00:00 2001 From: grzegorz-ciezkowski Date: Tue, 10 Dec 2024 12:19:32 +0100 Subject: [PATCH] feat(teams) Add members to team status (#723) --- cmd/greenhouse/controllers.go | 1 + pkg/apis/greenhouse/v1alpha1/team_types.go | 1 + pkg/controllers/team/team_controller.go | 78 ++++++++++++++++++++++ test/e2e/controllers.go | 1 + 4 files changed, 81 insertions(+) create mode 100644 pkg/controllers/team/team_controller.go diff --git a/cmd/greenhouse/controllers.go b/cmd/greenhouse/controllers.go index 9803b9ee7..c714c03e7 100644 --- a/cmd/greenhouse/controllers.go +++ b/cmd/greenhouse/controllers.go @@ -23,6 +23,7 @@ var knownControllers = map[string]func(controllerName string, mgr ctrl.Manager) "organizationController": startOrganizationReconciler, // Team controllers. + "teamController": (&teamcontrollers.TeamReconciler{}).SetupWithManager, "teamPropagation": (&teamcontrollers.TeamPropagationReconciler{}).SetupWithManager, // TeamMembership controllers. diff --git a/pkg/apis/greenhouse/v1alpha1/team_types.go b/pkg/apis/greenhouse/v1alpha1/team_types.go index 89182cbc6..2614d5ece 100644 --- a/pkg/apis/greenhouse/v1alpha1/team_types.go +++ b/pkg/apis/greenhouse/v1alpha1/team_types.go @@ -20,6 +20,7 @@ type TeamSpec struct { // TeamStatus defines the observed state of Team type TeamStatus struct { StatusConditions StatusConditions `json:"statusConditions"` + Members []User `json:"members"` } //+kubebuilder:object:root=true diff --git a/pkg/controllers/team/team_controller.go b/pkg/controllers/team/team_controller.go new file mode 100644 index 000000000..ab84d72fb --- /dev/null +++ b/pkg/controllers/team/team_controller.go @@ -0,0 +1,78 @@ +package team + +import ( + "context" + + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/tools/record" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + + greenhouseapisv1alpha1 "github.com/cloudoperators/greenhouse/pkg/apis/greenhouse/v1alpha1" + "github.com/cloudoperators/greenhouse/pkg/lifecycle" +) + +type TeamReconciler struct { + client.Client + recorder record.EventRecorder +} + +// SetupWithManager sets up the controller with the Manager. +func (r *TeamReconciler) SetupWithManager(name string, mgr ctrl.Manager) error { + r.Client = mgr.GetClient() + r.recorder = mgr.GetEventRecorderFor(name) + return ctrl.NewControllerManagedBy(mgr). + Named(name). + For(&greenhouseapisv1alpha1.Team{}). + Complete(r) +} + +func (r *TeamReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + return lifecycle.Reconcile(ctx, r.Client, req.NamespacedName, &greenhouseapisv1alpha1.Team{}, r, r.setStatus()) +} + +func (r *TeamReconciler) EnsureDeleted(_ context.Context, _ lifecycle.RuntimeObject) (ctrl.Result, lifecycle.ReconcileResult, error) { + return ctrl.Result{}, lifecycle.Success, nil +} + +func (r *TeamReconciler) EnsureCreated(ctx context.Context, object lifecycle.RuntimeObject) (ctrl.Result, lifecycle.ReconcileResult, error) { + return ctrl.Result{}, lifecycle.Success, nil +} + +func (r *TeamReconciler) setStatus() lifecycle.Conditioner { + return func(ctx context.Context, object lifecycle.RuntimeObject) { + team, ok := object.(*greenhouseapisv1alpha1.Team) + if !ok { + return + } + + var members []greenhouseapisv1alpha1.User + teamMembershipList := new(greenhouseapisv1alpha1.TeamMembershipList) + + err := r.List(ctx, teamMembershipList) + if err != nil { + ctrl.Log.Error(err, "Failed to list team memberships") + return + } + + for _, member := range teamMembershipList.Items { + if !hasOwnerReference(member.OwnerReferences, team.Kind, team.Name) { + continue + } + + members = append(members, member.Spec.Members...) + } + + team.Status.Members = members + } +} + +func hasOwnerReference(ownerReferences []v1.OwnerReference, kind, name string) bool { + for _, ownerReference := range ownerReferences { + if ownerReference.Kind == kind && ownerReference.Name == name { + return true + } + } + + return false +} diff --git a/test/e2e/controllers.go b/test/e2e/controllers.go index d7f9ec8c9..ab441099e 100644 --- a/test/e2e/controllers.go +++ b/test/e2e/controllers.go @@ -36,6 +36,7 @@ var knownControllers = map[string]func(controllerName string, mgr ctrl.Manager) "organizationTeamRoleSeeder": (&organizationcontrollers.TeamRoleSeederReconciler{}).SetupWithManager, // Team controllers. + "teamController": (&teamcontrollers.TeamReconciler{}).SetupWithManager, "teamPropagation": (&teamcontrollers.TeamPropagationReconciler{}).SetupWithManager, // TeamMembership controllers.