Skip to content

Commit 3b9d943

Browse files
Merge pull request #851 from dinhxuanvu/op-ns
fix(operatorgroup): No targetNamespaces matched namespace selector
2 parents 839248b + 821bc69 commit 3b9d943

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
@@ -3375,6 +3375,23 @@ func TestSyncOperatorGroups(t *testing.T) {
33753375
},
33763376
}
33773377

3378+
// Failed CSV due to operatorgroup namespace selector doesn't any existing namespaces
3379+
operatorCSVFailedNoTargetNS := operatorCSV.DeepCopy()
3380+
operatorCSVFailedNoTargetNS.Status.Phase = v1alpha1.CSVPhaseFailed
3381+
operatorCSVFailedNoTargetNS.Status.Message = "no targetNamespaces are matched operatorgroups namespace selection"
3382+
operatorCSVFailedNoTargetNS.Status.Reason = v1alpha1.CSVReasonNoTargetNamespaces
3383+
operatorCSVFailedNoTargetNS.Status.LastUpdateTime = timeNow()
3384+
operatorCSVFailedNoTargetNS.Status.LastTransitionTime = timeNow()
3385+
operatorCSVFailedNoTargetNS.Status.Conditions = []v1alpha1.ClusterServiceVersionCondition{
3386+
{
3387+
Phase: v1alpha1.CSVPhaseFailed,
3388+
Reason: v1alpha1.CSVReasonNoTargetNamespaces,
3389+
Message: "no targetNamespaces are matched operatorgroups namespace selection",
3390+
LastUpdateTime: timeNow(),
3391+
LastTransitionTime: timeNow(),
3392+
},
3393+
}
3394+
33783395
targetCSV := operatorCSVFinal.DeepCopy()
33793396
targetCSV.SetNamespace(targetNamespace)
33803397
targetCSV.Status.Reason = v1alpha1.CSVReasonCopied
@@ -3489,6 +3506,47 @@ func TestSyncOperatorGroups(t *testing.T) {
34893506
},
34903507
expectedStatus: v1.OperatorGroupStatus{},
34913508
},
3509+
{
3510+
name: "NoMatchingNamespace/CSVPresent",
3511+
expectedEqual: true,
3512+
initial: initial{
3513+
operatorGroup: &v1.OperatorGroup{
3514+
ObjectMeta: metav1.ObjectMeta{
3515+
Name: "operator-group-1",
3516+
Namespace: operatorNamespace,
3517+
},
3518+
Spec: v1.OperatorGroupSpec{
3519+
Selector: &metav1.LabelSelector{
3520+
MatchLabels: map[string]string{"a": "app-a"},
3521+
},
3522+
},
3523+
},
3524+
clientObjs: []runtime.Object{operatorCSV},
3525+
k8sObjs: []runtime.Object{
3526+
&corev1.Namespace{
3527+
ObjectMeta: metav1.ObjectMeta{
3528+
Name: operatorNamespace,
3529+
},
3530+
},
3531+
&corev1.Namespace{
3532+
ObjectMeta: metav1.ObjectMeta{
3533+
Name: targetNamespace,
3534+
},
3535+
},
3536+
ownedDeployment,
3537+
serviceAccount,
3538+
role,
3539+
roleBinding,
3540+
},
3541+
crds: []runtime.Object{crd},
3542+
},
3543+
expectedStatus: v1.OperatorGroupStatus{},
3544+
final: final{objects: map[string][]runtime.Object{
3545+
operatorNamespace: {
3546+
withAnnotations(operatorCSVFailedNoTargetNS.DeepCopy(), map[string]string{v1.OperatorGroupAnnotationKey: "operator-group-1", v1.OperatorGroupNamespaceAnnotationKey: operatorNamespace}),
3547+
},
3548+
}},
3549+
},
34923550
{
34933551
name: "MatchingNamespace/NoCSVs",
34943552
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)