Skip to content

Commit 821bc69

Browse files
committed
fix(operatorgroup): No targetNamespaces matched namespace selector
If no namespaces are matched selector in operatorgroup, CSV should have a failure status to indicate OperatorGroup NoTargetNamspaces. Also, a warning message for no-matched namespaces is added for visibility. Signed-off-by: Vu Dinh <[email protected]>
1 parent fb76336 commit 821bc69

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

pkg/controller/operators/olm/operator.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,9 @@ func (a *Operator) operatorGroupForCSV(csv *v1alpha1.ClusterServiceVersion, logg
719719
logger.WithError(err).Warn("error adding operatorgroup annotations")
720720
return nil, err
721721
}
722+
if targetNamespaceList, err := a.getOperatorGroupTargets(operatorGroup); err == nil && len(targetNamespaceList) == 0 {
723+
csv.SetPhaseWithEventIfChanged(v1alpha1.CSVPhaseFailed, v1alpha1.CSVReasonNoTargetNamespaces, "no targetNamespaces are matched operatorgroups namespace selection", now, a.recorder)
724+
}
722725
return nil, nil
723726
}
724727
logger.Info("csv in operatorgroup")

pkg/controller/operators/olm/operator_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3345,6 +3345,23 @@ func TestSyncOperatorGroups(t *testing.T) {
33453345
},
33463346
}
33473347

3348+
// Failed CSV due to operatorgroup namespace selector doesn't any existing namespaces
3349+
operatorCSVFailedNoTargetNS := operatorCSV.DeepCopy()
3350+
operatorCSVFailedNoTargetNS.Status.Phase = v1alpha1.CSVPhaseFailed
3351+
operatorCSVFailedNoTargetNS.Status.Message = "no targetNamespaces are matched operatorgroups namespace selection"
3352+
operatorCSVFailedNoTargetNS.Status.Reason = v1alpha1.CSVReasonNoTargetNamespaces
3353+
operatorCSVFailedNoTargetNS.Status.LastUpdateTime = timeNow()
3354+
operatorCSVFailedNoTargetNS.Status.LastTransitionTime = timeNow()
3355+
operatorCSVFailedNoTargetNS.Status.Conditions = []v1alpha1.ClusterServiceVersionCondition{
3356+
{
3357+
Phase: v1alpha1.CSVPhaseFailed,
3358+
Reason: v1alpha1.CSVReasonNoTargetNamespaces,
3359+
Message: "no targetNamespaces are matched operatorgroups namespace selection",
3360+
LastUpdateTime: timeNow(),
3361+
LastTransitionTime: timeNow(),
3362+
},
3363+
}
3364+
33483365
targetCSV := operatorCSVFinal.DeepCopy()
33493366
targetCSV.SetNamespace(targetNamespace)
33503367
targetCSV.Status.Reason = v1alpha1.CSVReasonCopied
@@ -3459,6 +3476,47 @@ func TestSyncOperatorGroups(t *testing.T) {
34593476
},
34603477
expectedStatus: v1.OperatorGroupStatus{},
34613478
},
3479+
{
3480+
name: "NoMatchingNamespace/CSVPresent",
3481+
expectedEqual: true,
3482+
initial: initial{
3483+
operatorGroup: &v1.OperatorGroup{
3484+
ObjectMeta: metav1.ObjectMeta{
3485+
Name: "operator-group-1",
3486+
Namespace: operatorNamespace,
3487+
},
3488+
Spec: v1.OperatorGroupSpec{
3489+
Selector: &metav1.LabelSelector{
3490+
MatchLabels: map[string]string{"a": "app-a"},
3491+
},
3492+
},
3493+
},
3494+
clientObjs: []runtime.Object{operatorCSV},
3495+
k8sObjs: []runtime.Object{
3496+
&corev1.Namespace{
3497+
ObjectMeta: metav1.ObjectMeta{
3498+
Name: operatorNamespace,
3499+
},
3500+
},
3501+
&corev1.Namespace{
3502+
ObjectMeta: metav1.ObjectMeta{
3503+
Name: targetNamespace,
3504+
},
3505+
},
3506+
ownedDeployment,
3507+
serviceAccount,
3508+
role,
3509+
roleBinding,
3510+
},
3511+
crds: []runtime.Object{crd},
3512+
},
3513+
expectedStatus: v1.OperatorGroupStatus{},
3514+
final: final{objects: map[string][]runtime.Object{
3515+
operatorNamespace: {
3516+
withAnnotations(operatorCSVFailedNoTargetNS.DeepCopy(), map[string]string{v1.OperatorGroupAnnotationKey: "operator-group-1", v1.OperatorGroupNamespaceAnnotationKey: operatorNamespace}),
3517+
},
3518+
}},
3519+
},
34623520
{
34633521
name: "MatchingNamespace/NoCSVs",
34643522
expectedEqual: true,

pkg/controller/operators/olm/operatorgroup.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,8 @@ func (a *Operator) getOperatorGroupTargets(op *v1.OperatorGroup) (map[string]str
715715
matchedNamespaces, err := a.lister.CoreV1().NamespaceLister().List(selector)
716716
if err != nil {
717717
return nil, err
718+
} else if len(matchedNamespaces) == 0 {
719+
a.Log.Debugf("No matched TargetNamespaces are found for given selector: %#v\n", selector)
718720
}
719721

720722
for _, ns := range matchedNamespaces {

0 commit comments

Comments
 (0)