Skip to content

Commit

Permalink
feat(teams) Add members to team status (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
gciezkowski-acc committed Dec 17, 2024
1 parent 0f46faf commit 63c86b7
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
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
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
78 changes: 78 additions & 0 deletions pkg/controllers/team/team_controller.go
Original file line number Diff line number Diff line change
@@ -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
}
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

0 comments on commit 63c86b7

Please sign in to comment.