Skip to content

Commit

Permalink
operators/v1alpha1: expose CSV copied logic (#289)
Browse files Browse the repository at this point in the history
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 <skuznets@redhat.com>
  • Loading branch information
stevekuznetsov committed Aug 3, 2023
1 parent 92e4341 commit 409ec70
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pkg/operators/v1alpha1/clusterserviceversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,19 @@ func (c *ClusterServiceVersion) IsObsolete() bool {

// IsCopied returns true if the CSV has been copied and false otherwise.
func (c *ClusterServiceVersion) IsCopied() bool {
operatorNamespace, ok := c.GetAnnotations()[OperatorGroupNamespaceAnnotationKey]
if c.Status.Reason == CSVReasonCopied || ok && c.GetNamespace() != operatorNamespace {
return true
return c.Status.Reason == CSVReasonCopied || IsCopied(c)
}

func IsCopied(o metav1.Object) bool {
annotations := o.GetAnnotations()
if annotations != nil {
operatorNamespace, ok := annotations[OperatorGroupNamespaceAnnotationKey]
if ok && o.GetNamespace() != operatorNamespace {
return true
}
}

if labels := c.GetLabels(); labels != nil {
if labels := o.GetLabels(); labels != nil {
if _, ok := labels[CopiedLabelKey]; ok {
return true
}
Expand Down
65 changes: 65 additions & 0 deletions pkg/operators/v1alpha1/clusterserviceversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,68 @@ func helperNewConditions(count int) []ClusterServiceVersionCondition {

return conditions
}

func TestIsCopied(t *testing.T) {
var testCases = []struct {
name string
input metav1.Object
expected bool
}{
{
name: "no labels or annotations",
input: &metav1.ObjectMeta{},
expected: false,
},
{
name: "no labels, has annotations but missing operatorgroup namespace annotation",
input: &metav1.ObjectMeta{
Annotations: map[string]string{},
},
expected: false,
},
{
name: "no labels, has operatorgroup namespace annotation matching self",
input: &metav1.ObjectMeta{
Namespace: "whatever",
Annotations: map[string]string{
"olm.operatorNamespace": "whatever",
},
},
expected: false,
},
{
name: "no labels, has operatorgroup namespace annotation not matching self",
input: &metav1.ObjectMeta{
Namespace: "whatever",
Annotations: map[string]string{
"olm.operatorNamespace": "other",
},
},
expected: true,
},
{
name: "no annotations, labels missing copied key",
input: &metav1.ObjectMeta{
Labels: map[string]string{},
},
expected: false,
},
{
name: "no annotations, labels has copied key",
input: &metav1.ObjectMeta{
Labels: map[string]string{
"olm.copiedFrom": "whatever",
},
},
expected: true,
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
if got, expected := IsCopied(testCase.input), testCase.expected; got != expected {
t.Errorf("got %v, expected %v", got, expected)
}
})
}
}

0 comments on commit 409ec70

Please sign in to comment.