Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(teams): add members to team status #794

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/greenhouse/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 14 additions & 1 deletion docs/reference/api/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3404,6 +3404,18 @@ <h3 id="greenhouse.sap/v1alpha1.TeamStatus">TeamStatus
<td>
</td>
</tr>
<tr>
<td>
<code>members</code><br>
<em>
<a href="#greenhouse.sap/v1alpha1.User">
[]User
</a>
</em>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</div>
Expand Down Expand Up @@ -3468,7 +3480,8 @@ <h3 id="greenhouse.sap/v1alpha1.User">User
</h3>
<p>
(<em>Appears on:</em>
<a href="#greenhouse.sap/v1alpha1.TeamMembershipSpec">TeamMembershipSpec</a>)
<a href="#greenhouse.sap/v1alpha1.TeamMembershipSpec">TeamMembershipSpec</a>,
<a href="#greenhouse.sap/v1alpha1.TeamStatus">TeamStatus</a>)
</p>
<p>User specifies a human person.</p>
<div class="md-typeset__scrollwrap">
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/greenhouse/v1alpha1/team_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 81 additions & 0 deletions pkg/controllers/team/team_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
// SPDX-License-Identifier: Apache-2.0

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
}
Comment on lines +82 to +90
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be replaced with

// GetOwnerReference returns the OwnerReference if found else nil.
func GetOwnerReference(obj metav1.Object, kind string) *metav1.OwnerReference {
for _, ref := range obj.GetOwnerReferences() {
if ref.Kind == kind {
return &ref
}
}
return nil
}

1 change: 1 addition & 0 deletions test/e2e/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading