Skip to content

Commit dbacd7a

Browse files
author
Per Goncalves da Silva
committed
add regression test for custom labels no longer getting removed from the cluster role
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent 0b9d7fb commit dbacd7a

File tree

2 files changed

+124
-12
lines changed

2 files changed

+124
-12
lines changed

pkg/controller/operators/olm/operator_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4724,6 +4724,116 @@ func TestSyncOperatorGroups(t *testing.T) {
47244724
},
47254725
}},
47264726
},
4727+
{
4728+
// ensure that ownership labels are fixed but user labels are preserved
4729+
name: "MatchingNamespace/NoCSVs/ClusterRoleOwnershipLabels",
4730+
expectedEqual: true,
4731+
initial: initial{
4732+
operatorGroup: &operatorsv1.OperatorGroup{
4733+
ObjectMeta: metav1.ObjectMeta{
4734+
Name: "operator-group-1",
4735+
Namespace: operatorNamespace},
4736+
Spec: operatorsv1.OperatorGroupSpec{
4737+
Selector: &metav1.LabelSelector{
4738+
MatchLabels: map[string]string{"app": "app-a"},
4739+
},
4740+
},
4741+
},
4742+
k8sObjs: []runtime.Object{
4743+
&corev1.Namespace{
4744+
ObjectMeta: metav1.ObjectMeta{
4745+
Name: operatorNamespace,
4746+
},
4747+
},
4748+
&corev1.Namespace{
4749+
ObjectMeta: metav1.ObjectMeta{
4750+
Name: targetNamespace,
4751+
Labels: map[string]string{"app": "app-a"},
4752+
},
4753+
},
4754+
&rbacv1.ClusterRole{
4755+
ObjectMeta: metav1.ObjectMeta{
4756+
ResourceVersion: "",
4757+
Name: "olm.og.operator-group-1.admin-8rdAjL0E35JMMAkOqYmoorzjpIIihfnj3DcgDU",
4758+
Labels: map[string]string{
4759+
"olm.owner": "operator-group-1",
4760+
"olm.owner.namespace": "operator-ns-bob",
4761+
"olm.owner.kind": "OperatorGroup",
4762+
"not.an.olm.label": "true",
4763+
},
4764+
},
4765+
},
4766+
&rbacv1.ClusterRole{
4767+
ObjectMeta: metav1.ObjectMeta{
4768+
ResourceVersion: "",
4769+
Name: "olm.og.operator-group-1.view-1l6ymczPK5SceF4d0DCtAnWZuvmKn6s8oBUxHr",
4770+
Labels: map[string]string{
4771+
"olm.owner": "operator-group-5",
4772+
"olm.owner.namespace": "operator-ns",
4773+
"olm.owner.kind": "OperatorGroup",
4774+
"not.an.olm.label": "false",
4775+
"another.olm.label": "or maybe not",
4776+
},
4777+
},
4778+
},
4779+
&rbacv1.ClusterRole{
4780+
ObjectMeta: metav1.ObjectMeta{
4781+
ResourceVersion: "",
4782+
Name: "olm.og.operator-group-1.edit-9lBEUxqAYE7CX7wZfFEPYutTfQTo43WarB08od",
4783+
Labels: map[string]string{
4784+
"olm.owner": "operator-group-1",
4785+
"olm.owner.namespace": "operator-ns",
4786+
"olm.owner.kind": "OperatorGroupKind",
4787+
},
4788+
},
4789+
},
4790+
},
4791+
},
4792+
expectedStatus: operatorsv1.OperatorGroupStatus{
4793+
Namespaces: []string{targetNamespace},
4794+
LastUpdated: &now,
4795+
},
4796+
final: final{objects: map[string][]runtime.Object{
4797+
"": {
4798+
&rbacv1.ClusterRole{
4799+
ObjectMeta: metav1.ObjectMeta{
4800+
ResourceVersion: "",
4801+
Name: "olm.og.operator-group-1.admin-8rdAjL0E35JMMAkOqYmoorzjpIIihfnj3DcgDU",
4802+
Labels: map[string]string{
4803+
"olm.owner": "operator-group-1",
4804+
"olm.owner.namespace": "operator-ns",
4805+
"olm.owner.kind": "OperatorGroup",
4806+
"not.an.olm.label": "true",
4807+
},
4808+
},
4809+
},
4810+
&rbacv1.ClusterRole{
4811+
ObjectMeta: metav1.ObjectMeta{
4812+
ResourceVersion: "",
4813+
Name: "olm.og.operator-group-1.edit-9lBEUxqAYE7CX7wZfFEPYutTfQTo43WarB08od",
4814+
Labels: map[string]string{
4815+
"olm.owner": "operator-group-1",
4816+
"olm.owner.namespace": "operator-ns",
4817+
"olm.owner.kind": "OperatorGroup",
4818+
},
4819+
},
4820+
},
4821+
&rbacv1.ClusterRole{
4822+
ObjectMeta: metav1.ObjectMeta{
4823+
ResourceVersion: "",
4824+
Name: "olm.og.operator-group-1.view-1l6ymczPK5SceF4d0DCtAnWZuvmKn6s8oBUxHr",
4825+
Labels: map[string]string{
4826+
"olm.owner": "operator-group-1",
4827+
"olm.owner.namespace": "operator-ns",
4828+
"olm.owner.kind": "OperatorGroup",
4829+
"not.an.olm.label": "false",
4830+
"another.olm.label": "or maybe not",
4831+
},
4832+
},
4833+
},
4834+
},
4835+
}},
4836+
},
47274837
{
47284838
// if a cluster role exists with the correct name, use that
47294839
name: "MatchingNamespace/NoCSVs/DoesNotUpdateClusterRoles",

pkg/controller/operators/olm/operatorgroup.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,17 +1016,6 @@ func (a *Operator) ensureOpGroupClusterRole(op *operatorsv1.OperatorGroup, suffi
10161016
return err
10171017
}
10181018

1019-
clusterRole = &rbacv1.ClusterRole{
1020-
ObjectMeta: metav1.ObjectMeta{
1021-
Name: clusterRoleName,
1022-
},
1023-
AggregationRule: aggregationRule,
1024-
}
1025-
1026-
if err := ownerutil.AddOwnerLabels(clusterRole, op); err != nil {
1027-
return err
1028-
}
1029-
10301019
// get existing cluster role for this level (suffix: admin, edit, view))
10311020
existingRole, err := a.lister.RbacV1().ClusterRoleLister().Get(clusterRoleName)
10321021
if err != nil && !apierrors.IsNotFound(err) {
@@ -1044,12 +1033,25 @@ func (a *Operator) ensureOpGroupClusterRole(op *operatorsv1.OperatorGroup, suffi
10441033
if labels.Equals(existingRole.Labels, cp.Labels) && reflect.DeepEqual(existingRole.AggregationRule, aggregationRule) {
10451034
return nil
10461035
}
1047-
if _, err := a.opClient.UpdateClusterRole(clusterRole); err != nil {
1036+
1037+
cp.AggregationRule = aggregationRule
1038+
if _, err := a.opClient.UpdateClusterRole(cp); err != nil {
10481039
a.logger.WithError(err).Errorf("update existing cluster role failed: %v", clusterRole)
10491040
}
10501041
return err
10511042
}
10521043

1044+
clusterRole = &rbacv1.ClusterRole{
1045+
ObjectMeta: metav1.ObjectMeta{
1046+
Name: clusterRoleName,
1047+
},
1048+
AggregationRule: aggregationRule,
1049+
}
1050+
1051+
if err := ownerutil.AddOwnerLabels(clusterRole, op); err != nil {
1052+
return err
1053+
}
1054+
10531055
a.logger.Infof("creating cluster role: %s owned by operator group: %s/%s", clusterRole.GetName(), op.GetNamespace(), op.GetName())
10541056
_, err = a.opClient.KubernetesInterface().RbacV1().ClusterRoles().Create(context.TODO(), clusterRole, metav1.CreateOptions{})
10551057
return err

0 commit comments

Comments
 (0)