Skip to content

Commit 579c7dd

Browse files
committed
pkg/controller: Fix panic when creating cluster-scoped RBAC in OG controller
Fixes [#2091](#2091). This is a follow-up to [#2309](#2309) that attempted to fix the original issue. When checking whether the ClusterRole/ClusterRoleBinding resources already exist, we're also checking whether the existing labels are owned by the CSV we're currently handling. When accessing the "cr" or "crb" variables that the Create calls output, a panic is produced as we're attempting to access the meta.Labels key from those resources, except those resources themselves are nil. Update the check to verify that the cr/crb variables are not nil before attempting to access those object's labels. The testing fake client may need to be updated in the future to handle returning these resources properly. Signed-off-by: timflannagan <[email protected]>
1 parent 11f1d0c commit 579c7dd

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

pkg/controller/operators/olm/operatorgroup.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,12 @@ func (a *Operator) ensureSingletonRBAC(operatorNamespace string, csv *v1alpha1.C
532532
}
533533
// TODO: this should do something smarter if the cluster role already exists
534534
if cr, err := a.opClient.CreateClusterRole(clusterRole); err != nil {
535+
if !k8serrors.IsAlreadyExists(err) {
536+
return err
537+
}
535538
// if the CR already exists, but the label is correct, the cache is just behind
536-
if k8serrors.IsAlreadyExists(err) && ownerutil.IsOwnedByLabel(cr, csv) {
539+
if cr != nil && ownerutil.IsOwnedByLabel(cr, csv) {
537540
continue
538-
} else {
539-
return err
540541
}
541542
}
542543
a.logger.Debug("created cluster role")
@@ -572,12 +573,14 @@ func (a *Operator) ensureSingletonRBAC(operatorNamespace string, csv *v1alpha1.C
572573
}
573574
// TODO: this should do something smarter if the cluster role binding already exists
574575
if crb, err := a.opClient.CreateClusterRoleBinding(clusterRoleBinding); err != nil {
575-
// if the CR already exists, but the label is correct, the cache is just behind
576-
if k8serrors.IsAlreadyExists(err) && ownerutil.IsOwnedByLabel(crb, csv) {
577-
continue
578-
} else {
576+
if !k8serrors.IsAlreadyExists(err) {
579577
return err
580578
}
579+
// if the CRB already exists, but the label is correct, the cache is just behind
580+
if crb != nil && ownerutil.IsOwnedByLabel(crb, csv) {
581+
continue
582+
}
583+
return err
581584
}
582585
}
583586
}

0 commit comments

Comments
 (0)