diff --git a/pkg/controller/operators/olm/operatorgroup.go b/pkg/controller/operators/olm/operatorgroup.go index 8cfbc8f780..da9d96f697 100644 --- a/pkg/controller/operators/olm/operatorgroup.go +++ b/pkg/controller/operators/olm/operatorgroup.go @@ -783,14 +783,31 @@ func (a *Operator) ensureCSVsInNamespaces(csv *v1alpha1.ClusterServiceVersion, o return nil } -// copyableCSVHash returns a hash of the parts of the given CSV that -// are relevant to copied CSV projection. +// copyableCSVHash returns hashes of the parts of a CSV that OLM manages in +// copied CSV projections. Unmanaged metadata is excluded because admission +// may inject or normalize it without changing the projected CSV. func copyableCSVHash(original *v1alpha1.ClusterServiceVersion) (string, string, error) { + labels := make(map[string]string, 2) + if value, ok := original.Labels[v1alpha1.CopiedLabelKey]; ok { + labels[v1alpha1.CopiedLabelKey] = value + } + if value, ok := original.Labels[install.OLMManagedLabelKey]; ok { + labels[install.OLMManagedLabelKey] = value + } + + annotations := make(map[string]string, 2) + if value, ok := original.Annotations[operatorsv1.OperatorGroupAnnotationKey]; ok { + annotations[operatorsv1.OperatorGroupAnnotationKey] = value + } + if value, ok := original.Annotations[operatorsv1.OperatorGroupNamespaceAnnotationKey]; ok { + annotations[operatorsv1.OperatorGroupNamespaceAnnotationKey] = value + } + shallow := v1alpha1.ClusterServiceVersion{ ObjectMeta: metav1.ObjectMeta{ Name: original.Name, - Labels: original.Labels, - Annotations: original.Annotations, + Labels: labels, + Annotations: annotations, }, Spec: original.Spec, } diff --git a/pkg/controller/operators/olm/operatorgroup_test.go b/pkg/controller/operators/olm/operatorgroup_test.go index 89e9c3a210..24273fda50 100644 --- a/pkg/controller/operators/olm/operatorgroup_test.go +++ b/pkg/controller/operators/olm/operatorgroup_test.go @@ -13,9 +13,11 @@ import ( "k8s.io/apimachinery/pkg/labels" ktesting "k8s.io/client-go/testing" + operatorsv1 "github.com/operator-framework/api/pkg/operators/v1" "github.com/operator-framework/api/pkg/operators/v1alpha1" "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/fake" listersv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1" + "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install" "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/cache" "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister/operatorlisterfakes" "github.com/operator-framework/operator-registry/pkg/registry" @@ -409,6 +411,44 @@ func TestCSVCopyPrototype(t *testing.T) { }, dst) } +func TestCopyableCSVHashIgnoresUnmanagedMetadata(t *testing.T) { + base := &v1alpha1.ClusterServiceVersion{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Labels: map[string]string{ + v1alpha1.CopiedLabelKey: "operator-namespace", + install.OLMManagedLabelKey: install.OLMManagedLabelValue, + }, + Annotations: map[string]string{ + operatorsv1.OperatorGroupAnnotationKey: "operator-group", + operatorsv1.OperatorGroupNamespaceAnnotationKey: "operator-namespace", + }, + }, + Spec: v1alpha1.ClusterServiceVersionSpec{ + Replaces: "replacee", + }, + Status: v1alpha1.ClusterServiceVersionStatus{ + Phase: "succeeded", + }, + } + baseHash, baseStatusHash, err := copyableCSVHash(base) + require.NoError(t, err) + + withInjectedMetadata := base.DeepCopy() + withInjectedMetadata.Labels["example.com/injected"] = "label" + withInjectedMetadata.Annotations["example.com/injected"] = "annotation" + injectedHash, injectedStatusHash, err := copyableCSVHash(withInjectedMetadata) + require.NoError(t, err) + assert.Equal(t, baseHash, injectedHash) + assert.Equal(t, baseStatusHash, injectedStatusHash) + + withManagedMetadataChange := base.DeepCopy() + withManagedMetadataChange.Annotations[operatorsv1.OperatorGroupAnnotationKey] = "other-operator-group" + changedHash, _, err := copyableCSVHash(withManagedMetadataChange) + require.NoError(t, err) + assert.NotEqual(t, baseHash, changedHash) +} + func TestOperator_getClusterRoleAggregationRule(t *testing.T) { tests := []struct { name string