Skip to content

Commit 23dce89

Browse files
authored
Fix rbac bug on trident installation through operator and helm
1 parent 6d3c8a5 commit 23dce89

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

operator/controllers/orchestrator/installer/k8s_client.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,13 @@ func (k *K8sClient) GetClusterRoleInformation(clusterRoleName string, allowedClu
392392
"Deleted unlabeled Trident cluster role; replacing it with a labeled Trident cluster role.")
393393
}
394394
} else if shouldUpdate {
395-
unwantedClusterRoles = clusterRoles
395+
// During updates, only delete the specific cluster role being processed, not siblings with same label
396+
for _, clusterRole := range clusterRoles {
397+
if clusterRole.Name == clusterRoleName {
398+
// This is the one we're updating - mark for deletion and recreation
399+
unwantedClusterRoles = append(unwantedClusterRoles, clusterRole)
400+
}
401+
}
396402
} else {
397403
// Processing cluster roles with selective deletion logic:
398404
// 1. Keep cluster roles that match names in allowedClusterRoleNames (e.g., trident-controller,
@@ -540,6 +546,13 @@ func (k *K8sClient) PutClusterRole(currentClusterRole *rbacv1.ClusterRole, creat
540546

541547
if currentClusterRole != nil {
542548
clusterRoleName = currentClusterRole.Name
549+
} else {
550+
// get cluster role from newClusterRoleYAML
551+
var clusterRoleFromYAML rbacv1.ClusterRole
552+
if err := yaml.Unmarshal([]byte(newClusterRoleYAML), &clusterRoleFromYAML); err != nil {
553+
return fmt.Errorf("could not unmarshal new cluster role YAML; %v", err)
554+
}
555+
clusterRoleName = clusterRoleFromYAML.Name
543556
}
544557

545558
logFields := LogFields{
@@ -566,7 +579,7 @@ func (k *K8sClient) PutClusterRole(currentClusterRole *rbacv1.ClusterRole, creat
566579

567580
// Apply the patch to the current Cluster Role
568581
patchType := types.MergePatchType
569-
if err = k.PatchClusterRoleByLabel(appLabel, patchBytes, patchType); err != nil {
582+
if err = k.PatchClusterRoleByLabelAndName(appLabel, currentClusterRole.Name, patchBytes, patchType); err != nil {
570583
return fmt.Errorf("could not patch Trident Cluster role; %v", err)
571584
}
572585

@@ -701,7 +714,13 @@ func (k *K8sClient) GetClusterRoleBindingInformation(clusterRoleBindingName stri
701714
"Deleted unlabeled Trident cluster role binding; replacing it with a labeled Trident cluster role binding.")
702715
}
703716
} else if shouldUpdate {
704-
unwantedClusterRoleBindings = clusterRoleBindings
717+
// During updates, only delete the specific cluster role binding being processed, not siblings with same label
718+
for _, clusterRoleBinding := range clusterRoleBindings {
719+
if clusterRoleBinding.Name == clusterRoleBindingName {
720+
// This is the one we're updating - mark for deletion and recreation
721+
unwantedClusterRoleBindings = append(unwantedClusterRoleBindings, clusterRoleBinding)
722+
}
723+
}
705724
} else {
706725
// Processing cluster role bindings with selective deletion logic:
707726
// 1. Keep cluster role bindings that match names in allowedClusterRoleBindingNames (e.g., trident-controller,
@@ -743,6 +762,13 @@ func (k *K8sClient) PutClusterRoleBinding(currentClusterRoleBinding *rbacv1.Clus
743762

744763
if currentClusterRoleBinding != nil {
745764
clusterRoleBindingName = currentClusterRoleBinding.Name
765+
} else {
766+
// get cluster role from newClusterRoleYAML
767+
var clusterRoleBindingYAML rbacv1.ClusterRole
768+
if err := yaml.Unmarshal([]byte(newClusterRoleBindingYAML), &clusterRoleBindingYAML); err != nil {
769+
return fmt.Errorf("could not unmarshal new cluster role YAML; %v", err)
770+
}
771+
clusterRoleBindingName = clusterRoleBindingYAML.Name
746772
}
747773

748774
logFields := LogFields{
@@ -769,7 +795,8 @@ func (k *K8sClient) PutClusterRoleBinding(currentClusterRoleBinding *rbacv1.Clus
769795

770796
// Apply the patch to the current Cluster Role Binding
771797
patchType := types.MergePatchType
772-
if err = k.PatchClusterRoleBindingByLabel(appLabel, patchBytes, patchType); err != nil {
798+
if err = k.PatchClusterRoleBindingByLabelAndName(appLabel, currentClusterRoleBinding.Name, patchBytes,
799+
patchType); err != nil {
773800
return fmt.Errorf("could not patch cluster role binding; %v", err)
774801
}
775802

operator/controllers/orchestrator/installer/k8s_client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ func TestPutClusterRole(t *testing.T) {
13121312
patchBytes, _ := args[2].([]byte)
13131313
patchType, _ := args[3].(types.PatchType)
13141314
patchBytesMatcher := &JSONMatcher{patchBytes}
1315-
mockKubeClient.EXPECT().PatchClusterRoleByLabel(appLabel, patchBytesMatcher,
1315+
mockKubeClient.EXPECT().PatchClusterRoleByLabelAndName(appLabel, clusterRoleName, patchBytesMatcher,
13161316
patchType).Return(k8sClientErr)
13171317
},
13181318
},
@@ -1330,7 +1330,7 @@ func TestPutClusterRole(t *testing.T) {
13301330
patchBytes, _ := args[2].([]byte)
13311331
patchType, _ := args[3].(types.PatchType)
13321332
patchBytesMatcher := &JSONMatcher{patchBytes}
1333-
mockKubeClient.EXPECT().PatchClusterRoleByLabel(appLabel, patchBytesMatcher,
1333+
mockKubeClient.EXPECT().PatchClusterRoleByLabelAndName(appLabel, clusterRoleName, patchBytesMatcher,
13341334
patchType).Return(nil)
13351335
},
13361336
},
@@ -1853,7 +1853,7 @@ func TestPutClusterRoleBinding(t *testing.T) {
18531853
patchBytes, _ := args[2].([]byte)
18541854
patchType, _ := args[3].(types.PatchType)
18551855
patchBytesMatcher := &JSONMatcher{patchBytes}
1856-
mockKubeClient.EXPECT().PatchClusterRoleBindingByLabel(appLabel, patchBytesMatcher,
1856+
mockKubeClient.EXPECT().PatchClusterRoleBindingByLabelAndName(appLabel, clusterRoleBindingName, patchBytesMatcher,
18571857
patchType).Return(k8sClientErr)
18581858
},
18591859
},
@@ -1871,7 +1871,7 @@ func TestPutClusterRoleBinding(t *testing.T) {
18711871
patchBytes, _ := args[2].([]byte)
18721872
patchType, _ := args[3].(types.PatchType)
18731873
patchBytesMatcher := &JSONMatcher{patchBytes}
1874-
mockKubeClient.EXPECT().PatchClusterRoleBindingByLabel(appLabel, patchBytesMatcher,
1874+
mockKubeClient.EXPECT().PatchClusterRoleBindingByLabelAndName(appLabel, clusterRoleBindingName, patchBytesMatcher,
18751875
patchType).Return(nil)
18761876
},
18771877
},

0 commit comments

Comments
 (0)