Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Comment thread
stevekuznetsov marked this conversation as resolved.
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)
}
})
}
}