Skip to content

Commit 8fba31d

Browse files
operators/v1alpha1: expose CSV copied logic (openshift#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]> Upstream-repository: api Upstream-commit: 409ec70c6c889945acb0447397305aa5c2150ea2 Signed-off-by: Steve Kuznetsov <[email protected]>
1 parent 61341bb commit 8fba31d

File tree

4 files changed

+89
-10
lines changed

4 files changed

+89
-10
lines changed

pkg/manifests/csv.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
name: packageserver
66
namespace: openshift-operator-lifecycle-manager
77
labels:
8-
olm.version: 0.0.0-7b1fd73aa35a79a5766541029762ec59ef9eab29
8+
olm.version: 0.0.0-6e63ad61683d27bae6a357eb473fb3067a4c73e6
99
olm.clusteroperator.name: operator-lifecycle-manager-packageserver
1010
annotations:
1111
include.release.openshift.io/self-managed-high-availability: "true"
@@ -159,7 +159,7 @@ spec:
159159
- packageserver
160160
topologyKey: "kubernetes.io/hostname"
161161
maturity: alpha
162-
version: 0.0.0-7b1fd73aa35a79a5766541029762ec59ef9eab29
162+
version: 0.0.0-6e63ad61683d27bae6a357eb473fb3067a4c73e6
163163
apiservicedefinitions:
164164
owned:
165165
- group: packages.operators.coreos.com

staging/api/pkg/operators/v1alpha1/clusterserviceversion.go

Lines changed: 11 additions & 4 deletions
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
}

staging/api/pkg/operators/v1alpha1/clusterserviceversion_test.go

Lines changed: 65 additions & 0 deletions
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+
}

vendor/github.com/operator-framework/api/pkg/operators/v1alpha1/clusterserviceversion.go

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)