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(teamrolebindings): teamrbac supports single users #927

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

gciezkowski-acc
Copy link
Contributor

@gciezkowski-acc gciezkowski-acc commented Feb 21, 2025

Description

What type of PR is this? (check all applicable)

  • πŸ• Feature
  • πŸ› Bug Fix
  • πŸ“ Documentation Update
  • 🎨 Style
  • πŸ§‘β€πŸ’» Code Refactor
  • πŸ”₯ Performance Improvements
  • βœ… Test
  • πŸ€– Build
  • πŸ” CI
  • πŸ“¦ Chore (Release)
  • ⏩ Revert

Related Tickets & Documents

Added tests?

  • πŸ‘ yes
  • πŸ™… no, because they aren't needed
  • πŸ™‹ no, because I need help
  • Separate ticket for tests # (issue/pr)

Added to documentation?

  • πŸ“œ README.md
  • 🀝 Documentation pages updated
  • πŸ™… no documentation needed
  • (if applicable) generated OpenAPI docs for CRD changes

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • New and existing unit tests pass locally with my changes

@github-actions github-actions bot added feature core-apis helm-charts documentation Improvements or additions to documentation labels Feb 21, 2025
@gciezkowski-acc gciezkowski-acc changed the title Feat/920 teamrbac supports single users feat(teamrolebindings): teamrbac supports single users Feb 21, 2025
@gciezkowski-acc gciezkowski-acc force-pushed the feat/920_teamrbac_supports_single_users branch from 00cc0ac to d24741b Compare February 21, 2025 16:06
@gciezkowski-acc gciezkowski-acc marked this pull request as ready for review February 24, 2025 11:55
@gciezkowski-acc gciezkowski-acc requested a review from a team as a code owner February 24, 2025 11:55
@@ -17,13 +17,18 @@ type TeamRoleBindingSpec struct {
TeamRoleRef string `json:"teamRoleRef,omitempty"`
// TeamRef references a Greenhouse Team by name
TeamRef string `json:"teamRef,omitempty"`
// Usernames define list of users with team role
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Usernames define list of users with team role
// Usernames defines list of users to add to the (Cluster-)RoleBindings

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 805 to 824
for _, nampespaceName := range teamRoleBinding.Spec.Namespaces {
namespace := new(corev1.Namespace)
err := r.Client.Get(ctx, types.NamespacedName{Name: nampespaceName}, namespace)
if err == nil {
continue
}
if apierrors.IsNotFound(err) {
err := r.Client.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: nampespaceName}})
if err != nil {
return err
}
} else {
return err
}
}

return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

This creates the namespaces on the Greenhouse cluster. The namespaces should be created on the remote cluster.
It should return a not found error here, when trying to create a RoleBinding in a Namespace that does not exist. It should be fine to create the namespace if there is a NotFound error and then return the original error.
This way the controller will try again and now the namespace exists. This way there is no need for an additional retry logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

By("checking that the Namespace is created")
namespace := &corev1.Namespace{}
Eventually(func(g Gomega) {
err := test.K8sClient.Get(test.Ctx, types.NamespacedName{Name: "non-existing-namespace"}, namespace)
Copy link
Contributor

Choose a reason for hiding this comment

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

The used client is for the greenhouse cluster, this test should verify that the namespace was created on the remote cluster.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

}
}

func joinUsernamesWithDefault(usernames []string, mappedIDPGroup string) []rbacv1.Subject {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func joinUsernamesWithDefault(usernames []string, mappedIDPGroup string) []rbacv1.Subject {
// generateSubjects returns a list of subjects with mappedIDPGroup as a rbacv1.GroupKind, and any usernames as rbacv1.UserKind
func generateSubjects(usernames []string, mappedIDPGroup string) []rbacv1.Subject {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@gciezkowski-acc gciezkowski-acc force-pushed the feat/920_teamrbac_supports_single_users branch from c0b8307 to 5018f56 Compare February 25, 2025 09:08
@gciezkowski-acc gciezkowski-acc force-pushed the feat/920_teamrbac_supports_single_users branch 3 times, most recently from 34ddcfb to 80657c3 Compare February 26, 2025 12:17

err = createNamespaces(ctx, trb, cl)
Copy link
Contributor

Choose a reason for hiding this comment

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

The reconcileClusterRoleBinding is called only when there are no namespaces set on the ClusterRoleBindings. The ClusterRoleBindings that are created by this method are cluster scoped resources. It will never be the case that the namespace of the TeamRoleBinding will be filled.

Copy link
Contributor

Choose a reason for hiding this comment

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

This logic will also not work, as the namespace needs to be created before the rolebinding can be created.
It would require something like

if err != nil{
 if apierrors.IsNotFound(err){
  // create the namespace for the RoleBinding that is currently reconciled
}
return err
}

@gciezkowski-acc gciezkowski-acc force-pushed the feat/920_teamrbac_supports_single_users branch 2 times, most recently from 1d9011b to 16e5449 Compare February 27, 2025 11:15
Comment on lines 236 to 240
err = createNamespaces(ctx, trb, remoteRestClient)
if err != nil {
trb.SetPropagationStatus(cluster.GetName(), metav1.ConditionFalse, greenhousev1alpha1.CreateNamespacesFailed, "Failed to create namespaces: "+err.Error())
continue
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This will try to create the namespaces on every reconciliation of the TeamRoleBinding. This will result in at least one get request per namespace.
Please move the creation of the individual namespaces into this part of the creation of the rolebinding on the remote cluster

if err != nil {
return err
}

The create of a RoleBinding will fail with an NotFound error if the namespace does not exists.
That error can be caught as previously suggested with apierrors.IsNotFound(..).

In case it is such an error the namespace can be created. It should still return an error so that the TeamRoleBinding is reconciled once more. In the second reconciliation the create should succeed since the namespace was created.

something like

if err != nil{
 if apierrors.IsNotFound(err){
  // create the namespace for the RoleBinding that is currently reconciled
}
return err
}

@gciezkowski-acc gciezkowski-acc force-pushed the feat/920_teamrbac_supports_single_users branch from 16e5449 to 5a25362 Compare February 28, 2025 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-apis documentation Improvements or additions to documentation feature helm-charts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEAT] - TeamRBAC to support single users
3 participants