Skip to content

Commit 409ec70

Browse files
operators/v1alpha1: expose CSV copied logic (#289)
We want to use a partial object metadata query for CSVs to reduce resource utilization. We still want to ask questions about whether these CSVs are copied, so we need to refactor this method to take only object metadata. While it's not a perfect port of the previous logic, it defies explanation how an object would have all the other markings of being copied but the wrong status. Signed-off-by: Steve Kuznetsov <[email protected]>
1 parent 92e4341 commit 409ec70

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

pkg/operators/v1alpha1/clusterserviceversion.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,19 @@ func (c *ClusterServiceVersion) IsObsolete() bool {
120120

121121
// IsCopied returns true if the CSV has been copied and false otherwise.
122122
func (c *ClusterServiceVersion) IsCopied() bool {
123-
operatorNamespace, ok := c.GetAnnotations()[OperatorGroupNamespaceAnnotationKey]
124-
if c.Status.Reason == CSVReasonCopied || ok && c.GetNamespace() != operatorNamespace {
125-
return true
123+
return c.Status.Reason == CSVReasonCopied || IsCopied(c)
124+
}
125+
126+
func IsCopied(o metav1.Object) bool {
127+
annotations := o.GetAnnotations()
128+
if annotations != nil {
129+
operatorNamespace, ok := annotations[OperatorGroupNamespaceAnnotationKey]
130+
if ok && o.GetNamespace() != operatorNamespace {
131+
return true
132+
}
126133
}
127134

128-
if labels := c.GetLabels(); labels != nil {
135+
if labels := o.GetLabels(); labels != nil {
129136
if _, ok := labels[CopiedLabelKey]; ok {
130137
return true
131138
}

pkg/operators/v1alpha1/clusterserviceversion_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,68 @@ func helperNewConditions(count int) []ClusterServiceVersionCondition {
430430

431431
return conditions
432432
}
433+
434+
func TestIsCopied(t *testing.T) {
435+
var testCases = []struct {
436+
name string
437+
input metav1.Object
438+
expected bool
439+
}{
440+
{
441+
name: "no labels or annotations",
442+
input: &metav1.ObjectMeta{},
443+
expected: false,
444+
},
445+
{
446+
name: "no labels, has annotations but missing operatorgroup namespace annotation",
447+
input: &metav1.ObjectMeta{
448+
Annotations: map[string]string{},
449+
},
450+
expected: false,
451+
},
452+
{
453+
name: "no labels, has operatorgroup namespace annotation matching self",
454+
input: &metav1.ObjectMeta{
455+
Namespace: "whatever",
456+
Annotations: map[string]string{
457+
"olm.operatorNamespace": "whatever",
458+
},
459+
},
460+
expected: false,
461+
},
462+
{
463+
name: "no labels, has operatorgroup namespace annotation not matching self",
464+
input: &metav1.ObjectMeta{
465+
Namespace: "whatever",
466+
Annotations: map[string]string{
467+
"olm.operatorNamespace": "other",
468+
},
469+
},
470+
expected: true,
471+
},
472+
{
473+
name: "no annotations, labels missing copied key",
474+
input: &metav1.ObjectMeta{
475+
Labels: map[string]string{},
476+
},
477+
expected: false,
478+
},
479+
{
480+
name: "no annotations, labels has copied key",
481+
input: &metav1.ObjectMeta{
482+
Labels: map[string]string{
483+
"olm.copiedFrom": "whatever",
484+
},
485+
},
486+
expected: true,
487+
},
488+
}
489+
490+
for _, testCase := range testCases {
491+
t.Run(testCase.name, func(t *testing.T) {
492+
if got, expected := IsCopied(testCase.input), testCase.expected; got != expected {
493+
t.Errorf("got %v, expected %v", got, expected)
494+
}
495+
})
496+
}
497+
}

0 commit comments

Comments
 (0)