Skip to content

Commit bfb97a4

Browse files
committed
Remove component adoption labels before initial CSV copy.
Operator component labels were not being stripped before initially creating a CSV copy (they were only being stripped on subsequent updates to copies). Signed-off-by: Ben Luddy <[email protected]>
1 parent 4168648 commit bfb97a4

File tree

2 files changed

+75
-13
lines changed

2 files changed

+75
-13
lines changed

pkg/controller/operators/olm/operatorgroup.go

+6
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,12 @@ func (a *Operator) copyToNamespace(csv *v1alpha1.ClusterServiceVersion, namespac
755755
newCSV.SetNamespace(namespace)
756756
newCSV.SetResourceVersion("")
757757
newCSV.SetLabels(utillabels.AddLabel(newCSV.GetLabels(), v1alpha1.CopiedLabelKey, csv.GetNamespace()))
758+
// remove Operator object component labels before copying so that copied CSVs do not show up in the component list
759+
for k := range newCSV.GetLabels() {
760+
if strings.HasPrefix(k, decorators.ComponentLabelKeyPrefix) {
761+
delete(newCSV.Labels, k)
762+
}
763+
}
758764

759765
logger.Debug("copying CSV to target")
760766
createdCSV, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(namespace).Create(context.TODO(), newCSV, metav1.CreateOptions{})

pkg/controller/operators/olm/operatorgroup_test.go

+69-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package olm
22

33
import (
4-
"io/ioutil"
54
"testing"
65
"time"
76

8-
"github.com/sirupsen/logrus"
7+
"github.com/sirupsen/logrus/hooks/test"
98
"github.com/stretchr/testify/require"
109

1110
"k8s.io/apimachinery/pkg/api/errors"
@@ -126,12 +125,12 @@ func TestCopyToNamespace(t *testing.T) {
126125
Namespace: "bar",
127126
Original: &v1alpha1.ClusterServiceVersion{
128127
ObjectMeta: metav1.ObjectMeta{
129-
Name: "name",
128+
Name: "name",
130129
Namespace: "foo",
131130
Labels: map[string]string{
132131
"operators.coreos.com/foo": "",
133132
"operators.coreos.com/bar": "",
134-
"untouched": "fine",
133+
"untouched": "fine",
135134
},
136135
},
137136
},
@@ -142,25 +141,25 @@ func TestCopyToNamespace(t *testing.T) {
142141
Labels: map[string]string{
143142
"operators.coreos.com/foo": "",
144143
"operators.coreos.com/bar": "",
145-
"untouched": "fine",
144+
"untouched": "fine",
146145
},
147146
},
148147
Status: v1alpha1.ClusterServiceVersionStatus{
149-
Message: "The operator is running in foo but is managing this namespace",
150-
Reason: v1alpha1.CSVReasonCopied},
148+
Message: "The operator is running in foo but is managing this namespace",
149+
Reason: v1alpha1.CSVReasonCopied},
151150
},
152151
ExpectedResult: &v1alpha1.ClusterServiceVersion{
153152
ObjectMeta: metav1.ObjectMeta{
154153
Name: "name",
155154
Namespace: "bar",
156155
Labels: map[string]string{
157-
"untouched": "fine",
156+
"untouched": "fine",
158157
"olm.copiedFrom": "foo",
159158
},
160159
},
161160
Status: v1alpha1.ClusterServiceVersionStatus{
162-
Message: "The operator is running in foo but is managing this namespace",
163-
Reason: v1alpha1.CSVReasonCopied,
161+
Message: "The operator is running in foo but is managing this namespace",
162+
Reason: v1alpha1.CSVReasonCopied,
164163
},
165164
},
166165
ExpectedActions: []ktesting.Action{
@@ -169,7 +168,63 @@ func TestCopyToNamespace(t *testing.T) {
169168
Name: "name",
170169
Namespace: "bar",
171170
Labels: map[string]string{
172-
"untouched": "fine",
171+
"untouched": "fine",
172+
"olm.copiedFrom": "foo",
173+
},
174+
},
175+
Status: v1alpha1.ClusterServiceVersionStatus{
176+
Message: "The operator is running in foo but is managing this namespace",
177+
Reason: v1alpha1.CSVReasonCopied,
178+
},
179+
}),
180+
},
181+
},
182+
{
183+
Name: "component labels are stripped before initial copy",
184+
Namespace: "bar",
185+
Original: &v1alpha1.ClusterServiceVersion{
186+
ObjectMeta: metav1.ObjectMeta{
187+
Name: "name",
188+
Namespace: "foo",
189+
Labels: map[string]string{
190+
"operators.coreos.com/foo": "",
191+
"operators.coreos.com/bar": "",
192+
"untouched": "fine",
193+
},
194+
},
195+
},
196+
ExistingCopy: nil,
197+
ExpectedResult: &v1alpha1.ClusterServiceVersion{
198+
ObjectMeta: metav1.ObjectMeta{
199+
Name: "name",
200+
Namespace: "bar",
201+
Labels: map[string]string{
202+
"untouched": "fine",
203+
"olm.copiedFrom": "foo",
204+
},
205+
},
206+
Status: v1alpha1.ClusterServiceVersionStatus{
207+
Message: "The operator is running in foo but is managing this namespace",
208+
Reason: v1alpha1.CSVReasonCopied,
209+
},
210+
},
211+
ExpectedActions: []ktesting.Action{
212+
ktesting.NewCreateAction(gvr, "bar", &v1alpha1.ClusterServiceVersion{
213+
ObjectMeta: metav1.ObjectMeta{
214+
Name: "name",
215+
Namespace: "bar",
216+
Labels: map[string]string{
217+
"untouched": "fine",
218+
"olm.copiedFrom": "foo",
219+
},
220+
},
221+
}),
222+
ktesting.NewUpdateSubresourceAction(gvr, "status", "bar", &v1alpha1.ClusterServiceVersion{
223+
ObjectMeta: metav1.ObjectMeta{
224+
Name: "name",
225+
Namespace: "bar",
226+
Labels: map[string]string{
227+
"untouched": "fine",
173228
"olm.copiedFrom": "foo",
174229
},
175230
},
@@ -190,10 +245,11 @@ func TestCopyToNamespace(t *testing.T) {
190245
if tc.ExistingCopy != nil {
191246
client = fake.NewSimpleClientset(tc.ExistingCopy)
192247
v1alpha1lister.ClusterServiceVersionListerReturns(FakeClusterServiceVersionLister{tc.ExistingCopy})
248+
} else {
249+
v1alpha1lister.ClusterServiceVersionListerReturns(FakeClusterServiceVersionLister(nil))
193250
}
194251

195-
logger := logrus.New()
196-
logger.SetOutput(ioutil.Discard)
252+
logger, _ := test.NewNullLogger()
197253

198254
o := &Operator{
199255
lister: lister,

0 commit comments

Comments
 (0)