Skip to content

Commit 98e3110

Browse files
committed
remove component labels when copying CSVs
Signed-off-by: Evan Cordell <[email protected]>
1 parent 5b2df84 commit 98e3110

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

pkg/controller/operators/olm/operatorgroup.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/operator-framework/api/pkg/operators/v1alpha1"
2020
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
21+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/decorators"
2122
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver"
2223
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
2324
opregistry "github.com/operator-framework/operator-registry/pkg/registry"
@@ -708,15 +709,23 @@ func (a *Operator) copyToNamespace(csv *v1alpha1.ClusterServiceVersion, namespac
708709
delete(newCSV.Annotations, v1.OperatorGroupTargetsAnnotationKey)
709710

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

716-
if !reflect.DeepEqual(a.copyOperatorGroupAnnotations(&fetchedCSV.ObjectMeta), a.copyOperatorGroupAnnotations(&newCSV.ObjectMeta)) {
716+
if !reflect.DeepEqual(a.copyOperatorGroupAnnotations(&fetchedCSV.ObjectMeta), a.copyOperatorGroupAnnotations(&newCSV.ObjectMeta)) ||
717+
len(decorators.OperatorNames(fetchedCSV.GetLabels())) > 0 {
717718
// TODO: only copy over the opgroup annotations, not _all_ annotations
718719
fetchedCSV.Annotations = newCSV.Annotations
719720
fetchedCSV.SetLabels(utillabels.AddLabel(fetchedCSV.GetLabels(), v1alpha1.CopiedLabelKey, csv.GetNamespace()))
721+
722+
// remove Operator object component labels before copying so that copied CSVs do not show up in the component list
723+
for k := range fetchedCSV.GetLabels() {
724+
if strings.HasPrefix(k, decorators.ComponentLabelKeyPrefix) {
725+
delete(fetchedCSV.Labels, k)
726+
}
727+
}
728+
720729
// CRs don't support strategic merge patching, but in the future if they do this should be updated to patch
721730
logger.Debug("updating target CSV")
722731
if fetchedCSV, err = a.client.OperatorsV1alpha1().ClusterServiceVersions(namespace).Update(context.TODO(), fetchedCSV, metav1.UpdateOptions{}); err != nil {

pkg/controller/operators/olm/operatorgroup_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,65 @@ func TestCopyToNamespace(t *testing.T) {
121121
},
122122
},
123123
},
124+
{
125+
Name: "component labels are stripped before copy",
126+
Namespace: "bar",
127+
Original: &v1alpha1.ClusterServiceVersion{
128+
ObjectMeta: metav1.ObjectMeta{
129+
Name: "name",
130+
Namespace: "foo",
131+
Labels: map[string]string{
132+
"operators.coreos.com/foo": "",
133+
"operators.coreos.com/bar": "",
134+
"untouched": "fine",
135+
},
136+
},
137+
},
138+
ExistingCopy: &v1alpha1.ClusterServiceVersion{
139+
ObjectMeta: metav1.ObjectMeta{
140+
Name: "name",
141+
Namespace: "bar",
142+
Labels: map[string]string{
143+
"operators.coreos.com/foo": "",
144+
"operators.coreos.com/bar": "",
145+
"untouched": "fine",
146+
},
147+
},
148+
Status: v1alpha1.ClusterServiceVersionStatus{
149+
Message: "The operator is running in foo but is managing this namespace",
150+
Reason: v1alpha1.CSVReasonCopied},
151+
},
152+
ExpectedResult: &v1alpha1.ClusterServiceVersion{
153+
ObjectMeta: metav1.ObjectMeta{
154+
Name: "name",
155+
Namespace: "bar",
156+
Labels: map[string]string{
157+
"untouched": "fine",
158+
"olm.copiedFrom": "foo",
159+
},
160+
},
161+
Status: v1alpha1.ClusterServiceVersionStatus{
162+
Message: "The operator is running in foo but is managing this namespace",
163+
Reason: v1alpha1.CSVReasonCopied,
164+
},
165+
},
166+
ExpectedActions: []ktesting.Action{
167+
ktesting.NewUpdateAction(gvr, "bar", &v1alpha1.ClusterServiceVersion{
168+
ObjectMeta: metav1.ObjectMeta{
169+
Name: "name",
170+
Namespace: "bar",
171+
Labels: map[string]string{
172+
"untouched": "fine",
173+
"olm.copiedFrom": "foo",
174+
},
175+
},
176+
Status: v1alpha1.ClusterServiceVersionStatus{
177+
Message: "The operator is running in foo but is managing this namespace",
178+
Reason: v1alpha1.CSVReasonCopied,
179+
},
180+
}),
181+
},
182+
},
124183
} {
125184
t.Run(tc.Name, func(t *testing.T) {
126185
lister := &operatorlisterfakes.FakeOperatorLister{}

0 commit comments

Comments
 (0)