Skip to content

Commit a373dde

Browse files
authored
Fix RBAC in multicluster tool (#99)
# Summary Our multicluster CLI tool didn't treat the "IsAlreadyExists" errors properly so some roles and clusterrolebindings were not updated when upgrading the operator. ## Proof of Work You can see that [clusterrolebindings](https://operator-e2e-artifacts.s3.amazonaws.com/logs/mongodb_kubernetes_e2e_multi_cluster_kind_e2e_meko_mck_upgrade_patch_3178929a901f7bf30090fae7f634043c07b0db65_681dd24a5a5bd00007b819da_25_05_09_10_00_43/0/kind-e2e-cluster-1_z_clusterrolebindings.txt) now are applied to the new service account, and the operator doesn't print any errors related to RBAC in the [logs](https://operator-e2e-artifacts.s3.amazonaws.com/logs/mongodb_kubernetes_e2e_multi_cluster_kind_e2e_meko_mck_upgrade_patch_3178929a901f7bf30090fae7f634043c07b0db65_681dd24a5a5bd00007b819da_25_05_09_10_00_43/0/mongodb-kubernetes-operator-multi-cluster-767f89dd9-9wt9n-mongodb-kubernetes-operator-multi-cluster.log). Here are the [clusterrolebindings](https://operator-e2e-artifacts.s3.amazonaws.com/logs/mongodb_kubernetes_e2e_multi_cluster_kind_e2e_meko_mck_upgrade_patch_3178929a901f7bf30090fae7f634043c07b0db65_681dc1aba422640007a240db_25_05_09_08_49_49/4/kind-e2e-cluster-1_z_clusterrolebindings.txt) and the operator [logs](https://operator-e2e-artifacts.s3.amazonaws.com/logs/mongodb_kubernetes_e2e_multi_cluster_kind_e2e_meko_mck_upgrade_patch_3178929a901f7bf30090fae7f634043c07b0db65_681dc1aba422640007a240db_25_05_09_08_49_49/4/mongodb-kubernetes-operator-multi-cluster-9f66cc7bc-n877n-mongodb-kubernetes-operator-multi-cluster.log) before. ## Checklist - [ ] Have you linked a jira ticket and/or is the ticket in the title? - [ ] Have you checked whether your jira ticket required DOCSP changes? - [ ] Have you checked for release_note changes?
1 parent fb38d83 commit a373dde

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

public/tools/multicluster/pkg/common/common.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,14 @@ func createRoles(ctx context.Context, c KubeClient, serviceAccountName, serviceA
575575
if telemetryClusterRoles {
576576
clusterRoleTelemetry := buildClusterRoleTelemetry()
577577
_, err = c.RbacV1().ClusterRoles().Create(ctx, &clusterRoleTelemetry, metav1.CreateOptions{})
578-
if !errors.IsAlreadyExists(err) && err != nil {
579-
return xerrors.Errorf("error creating cluster role: %w", err)
578+
if err != nil {
579+
if errors.IsAlreadyExists(err) {
580+
if _, err := c.RbacV1().ClusterRoles().Update(ctx, &clusterRoleTelemetry, metav1.UpdateOptions{}); err != nil {
581+
return xerrors.Errorf("error updating role: %w", err)
582+
}
583+
} else {
584+
return xerrors.Errorf("error creating cluster role: %w", err)
585+
}
580586
}
581587
fmt.Printf("created clusterrole: %s\n", clusterRoleTelemetry.Name)
582588
if err = createClusterRoleBinding(ctx, c, serviceAccountName, serviceAccountNamespace, DefaultOperatorName+"-multi-telemetry-cluster-role-binding", clusterRoleTelemetry); err != nil {
@@ -594,7 +600,7 @@ func createRoles(ctx context.Context, c KubeClient, serviceAccountName, serviceA
594600
}
595601

596602
_, err = c.RbacV1().Roles(namespace).Create(ctx, &role, metav1.CreateOptions{})
597-
if !errors.IsAlreadyExists(err) && err != nil {
603+
if err != nil {
598604
if errors.IsAlreadyExists(err) {
599605
if _, err := c.RbacV1().Roles(namespace).Update(ctx, &role, metav1.UpdateOptions{}); err != nil {
600606
return xerrors.Errorf("error updating role: %w", err)
@@ -641,8 +647,14 @@ func createRoles(ctx context.Context, c KubeClient, serviceAccountName, serviceA
641647
func createClusterRoleBinding(ctx context.Context, c KubeClient, serviceAccountName string, serviceAccountNamespace string, clusterRoleBindingName string, clusterRole rbacv1.ClusterRole) error {
642648
clusterRoleBinding := buildClusterRoleBinding(clusterRole, serviceAccountName, serviceAccountNamespace, clusterRoleBindingName)
643649
_, err := c.RbacV1().ClusterRoleBindings().Create(ctx, &clusterRoleBinding, metav1.CreateOptions{})
644-
if !errors.IsAlreadyExists(err) && err != nil {
645-
return xerrors.Errorf("error creating cluster role binding: %w", err)
650+
if err != nil {
651+
if errors.IsAlreadyExists(err) {
652+
if _, err := c.RbacV1().ClusterRoleBindings().Update(ctx, &clusterRoleBinding, metav1.UpdateOptions{}); err != nil {
653+
return xerrors.Errorf("error updating role: %w", err)
654+
}
655+
} else {
656+
return xerrors.Errorf("error creating cluster role binding: %w", err)
657+
}
646658
}
647659
fmt.Printf("created clusterrolebinding: %s\n", clusterRoleBinding.Name)
648660
return nil

scripts/evergreen/e2e/dump_diagnostic_information.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ dump_namespace() {
293293
dump_objects sts "StatefulSets" "${namespace}" describe > "logs/${prefix}z_statefulsets.txt"
294294
dump_objects sts "StatefulSets Yaml" "${namespace}" >> "logs/${prefix}z_statefulsets.txt"
295295
dump_objects serviceaccounts "ServiceAccounts" "${namespace}" > "logs/${prefix}z_service_accounts.txt"
296+
dump_objects clusterrolebindings "ClusterRoleBindings" "${namespace}" > "logs/${prefix}z_clusterrolebindings.txt"
297+
dump_objects clusterroles "ClusterRoles" "${namespace}" > "logs/${prefix}z_clusterroles.txt"
298+
dump_objects rolebindings "RoleBindings" "${namespace}" > "logs/${prefix}z_rolebindings.txt"
299+
dump_objects roles "Roles" "${namespace}" > "logs/${prefix}z_roles.txt"
296300
dump_objects validatingwebhookconfigurations "Validating Webhook Configurations" "${namespace}" > "logs/${prefix}z_validatingwebhookconfigurations.txt"
297301
dump_objects certificates.cert-manager.io "Cert-manager certificates" "${namespace}" 2> /dev/null > "logs/${prefix}z_certificates_certmanager.txt"
298302
dump_objects catalogsources "OLM CatalogSources" "${namespace}" 2> /dev/null > "logs/${prefix}z_olm_catalogsources.txt"

0 commit comments

Comments
 (0)