Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCPBUGS-17157: operators/v1alpha1: expose CSV copied logic #289

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 @@

// 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)

Check warning on line 123 in pkg/operators/v1alpha1/clusterserviceversion.go

View check run for this annotation

Codecov / codecov/patch

pkg/operators/v1alpha1/clusterserviceversion.go#L123

Added line #L123 was not covered by tests
}

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 {
stevekuznetsov marked this conversation as resolved.
Show resolved Hide 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)
}
})
}
}
Loading