Skip to content

Remove component adoption labels before initial CSV copy. #2164

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

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
11 changes: 8 additions & 3 deletions pkg/controller/operators/olm/operatorgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,12 +704,11 @@ func (a *Operator) copyToNamespace(csv *v1alpha1.ClusterServiceVersion, namespac
return nil, fmt.Errorf("bug: can not copy to active namespace %v", csv.GetNamespace())
}

logger := a.logger.WithField("operator-ns", csv.GetNamespace()).WithField("target-ns", namespace)
logger := a.logger.WithField("operator-ns", csv.GetNamespace()).WithField("target-ns", namespace).WithField("csv", csv.GetName())
newCSV := csv.DeepCopy()
delete(newCSV.Annotations, v1.OperatorGroupTargetsAnnotationKey)

fetchedCSV, err := a.lister.OperatorsV1alpha1().ClusterServiceVersionLister().ClusterServiceVersions(namespace).Get(newCSV.GetName())
logger = logger.WithField("csv", csv.GetName())
if fetchedCSV != nil {
logger.Debug("checking annotations")

Expand Down Expand Up @@ -755,11 +754,17 @@ func (a *Operator) copyToNamespace(csv *v1alpha1.ClusterServiceVersion, namespac
newCSV.SetNamespace(namespace)
newCSV.SetResourceVersion("")
newCSV.SetLabels(utillabels.AddLabel(newCSV.GetLabels(), v1alpha1.CopiedLabelKey, csv.GetNamespace()))
// remove Operator object component labels before copying so that copied CSVs do not show up in the component list
for k := range newCSV.GetLabels() {
if strings.HasPrefix(k, decorators.ComponentLabelKeyPrefix) {
delete(newCSV.Labels, k)
}
}

logger.Debug("copying CSV to target")
createdCSV, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(namespace).Create(context.TODO(), newCSV, metav1.CreateOptions{})
if err != nil {
a.logger.Errorf("Create for new CSV failed: %v", err)
logger.Errorf("Create for new CSV failed: %v", err)
return nil, err
}
createdCSV.Status.Reason = v1alpha1.CSVReasonCopied
Expand Down
82 changes: 69 additions & 13 deletions pkg/controller/operators/olm/operatorgroup_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package olm

import (
"io/ioutil"
"testing"
"time"

"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/require"

"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -126,12 +125,12 @@ func TestCopyToNamespace(t *testing.T) {
Namespace: "bar",
Original: &v1alpha1.ClusterServiceVersion{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Name: "name",
Namespace: "foo",
Labels: map[string]string{
"operators.coreos.com/foo": "",
"operators.coreos.com/bar": "",
"untouched": "fine",
"untouched": "fine",
},
},
},
Expand All @@ -142,25 +141,25 @@ func TestCopyToNamespace(t *testing.T) {
Labels: map[string]string{
"operators.coreos.com/foo": "",
"operators.coreos.com/bar": "",
"untouched": "fine",
"untouched": "fine",
},
},
Status: v1alpha1.ClusterServiceVersionStatus{
Message: "The operator is running in foo but is managing this namespace",
Reason: v1alpha1.CSVReasonCopied},
Message: "The operator is running in foo but is managing this namespace",
Reason: v1alpha1.CSVReasonCopied},
},
ExpectedResult: &v1alpha1.ClusterServiceVersion{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Namespace: "bar",
Labels: map[string]string{
"untouched": "fine",
"untouched": "fine",
"olm.copiedFrom": "foo",
},
},
Status: v1alpha1.ClusterServiceVersionStatus{
Message: "The operator is running in foo but is managing this namespace",
Reason: v1alpha1.CSVReasonCopied,
Message: "The operator is running in foo but is managing this namespace",
Reason: v1alpha1.CSVReasonCopied,
},
},
ExpectedActions: []ktesting.Action{
Expand All @@ -169,7 +168,63 @@ func TestCopyToNamespace(t *testing.T) {
Name: "name",
Namespace: "bar",
Labels: map[string]string{
"untouched": "fine",
"untouched": "fine",
"olm.copiedFrom": "foo",
},
},
Status: v1alpha1.ClusterServiceVersionStatus{
Message: "The operator is running in foo but is managing this namespace",
Reason: v1alpha1.CSVReasonCopied,
},
}),
},
},
{
Name: "component labels are stripped before initial copy",
Namespace: "bar",
Original: &v1alpha1.ClusterServiceVersion{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Namespace: "foo",
Labels: map[string]string{
"operators.coreos.com/foo": "",
"operators.coreos.com/bar": "",
"untouched": "fine",
},
},
},
ExistingCopy: nil,
ExpectedResult: &v1alpha1.ClusterServiceVersion{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Namespace: "bar",
Labels: map[string]string{
"untouched": "fine",
"olm.copiedFrom": "foo",
},
},
Status: v1alpha1.ClusterServiceVersionStatus{
Message: "The operator is running in foo but is managing this namespace",
Reason: v1alpha1.CSVReasonCopied,
},
},
ExpectedActions: []ktesting.Action{
ktesting.NewCreateAction(gvr, "bar", &v1alpha1.ClusterServiceVersion{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Namespace: "bar",
Labels: map[string]string{
"untouched": "fine",
"olm.copiedFrom": "foo",
},
},
}),
ktesting.NewUpdateSubresourceAction(gvr, "status", "bar", &v1alpha1.ClusterServiceVersion{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Namespace: "bar",
Labels: map[string]string{
"untouched": "fine",
"olm.copiedFrom": "foo",
},
},
Expand All @@ -190,10 +245,11 @@ func TestCopyToNamespace(t *testing.T) {
if tc.ExistingCopy != nil {
client = fake.NewSimpleClientset(tc.ExistingCopy)
v1alpha1lister.ClusterServiceVersionListerReturns(FakeClusterServiceVersionLister{tc.ExistingCopy})
} else {
v1alpha1lister.ClusterServiceVersionListerReturns(FakeClusterServiceVersionLister(nil))
}

logger := logrus.New()
logger.SetOutput(ioutil.Discard)
logger, _ := test.NewNullLogger()

o := &Operator{
lister: lister,
Expand Down