Skip to content
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
21 changes: 10 additions & 11 deletions internal/operator-controller/applier/boxcutter.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ func (r *SimpleRevisionGenerator) GenerateRevisionFromHelmRelease(
if err := yaml.Unmarshal([]byte(doc), &obj); err != nil {
return nil, err
}

existingLabels := obj.GetLabels()
labels := make(map[string]string, len(existingLabels)+len(objectLabels))
maps.Copy(labels, existingLabels)
maps.Copy(labels, objectLabels)
obj.SetLabels(labels)
obj.SetLabels(mergeLabelMaps(obj.GetLabels(), objectLabels))

// Memory optimization: strip large annotations
// Note: ApplyStripTransform never returns an error in practice
Expand Down Expand Up @@ -131,11 +126,7 @@ func (r *SimpleRevisionGenerator) GenerateRevision(
// objectLabels
objs := make([]ocv1.ClusterExtensionRevisionObject, 0, len(plain))
for _, obj := range plain {
existingLabels := obj.GetLabels()
labels := make(map[string]string, len(existingLabels)+len(objectLabels))
maps.Copy(labels, existingLabels)
maps.Copy(labels, objectLabels)
obj.SetLabels(labels)
obj.SetLabels(mergeLabelMaps(obj.GetLabels(), objectLabels))

gvk, err := apiutil.GVKForObject(obj, r.Scheme)
if err != nil {
Expand Down Expand Up @@ -219,6 +210,7 @@ func (r *SimpleRevisionGenerator) buildClusterExtensionRevision(
ObjectMeta: metav1.ObjectMeta{
Annotations: annotations,
Labels: map[string]string{
labels.OwnerKindKey: ocv1.ClusterExtensionKind,
labels.OwnerNameKey: ext.Name,
},
},
Expand Down Expand Up @@ -666,3 +658,10 @@ func revisionManagementPerms(rev *ocv1.ClusterExtensionRevision) func(user.Info)
}
}
}

func mergeLabelMaps(m1, m2 map[string]string) map[string]string {
mergedLabels := make(map[string]string, len(m1)+len(m2))
maps.Copy(mergedLabels, m1)
maps.Copy(mergedLabels, m2)
return mergedLabels
}
4 changes: 3 additions & 1 deletion internal/operator-controller/applier/boxcutter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func Test_SimpleRevisionGenerator_GenerateRevisionFromHelmRelease(t *testing.T)
"olm.operatorframework.io/service-account-namespace": "test-namespace",
},
Labels: map[string]string{
labels.OwnerKindKey: ocv1.ClusterExtensionKind,
labels.OwnerNameKey: "test-123",
},
},
Expand Down Expand Up @@ -207,8 +208,9 @@ func Test_SimpleRevisionGenerator_GenerateRevision(t *testing.T) {
rev, err := b.GenerateRevision(t.Context(), dummyBundle, ext, map[string]string{}, map[string]string{})
require.NoError(t, err)

t.Log("by checking the olm.operatorframework.io/owner-name label is set to the name of the ClusterExtension")
t.Log("by checking the olm.operatorframework.io/owner-name and owner-kind labels are set")
require.Equal(t, map[string]string{
labels.OwnerKindKey: ocv1.ClusterExtensionKind,
labels.OwnerNameKey: "test-extension",
}, rev.Labels)
t.Log("by checking the revision number is 0")
Expand Down
26 changes: 26 additions & 0 deletions test/e2e/features/install.feature
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,29 @@ Feature: Install ClusterExtension
"""
[{"type":"olm.test-property","value":"some-value"}]
"""

@BoxcutterRuntime
Scenario: ClusterExtensionRevision is labeled with owner information
When ClusterExtension is applied
"""
apiVersion: olm.operatorframework.io/v1
kind: ClusterExtension
metadata:
name: ${NAME}
spec:
namespace: ${TEST_NAMESPACE}
serviceAccount:
name: olm-sa
source:
sourceType: Catalog
catalog:
packageName: test
version: 1.2.0
selector:
matchLabels:
"olm.operatorframework.io/metadata.name": test-catalog
"""
Then ClusterExtension is rolled out
And ClusterExtension is available
And ClusterExtensionRevision "${NAME}-1" has label "olm.operatorframework.io/owner-kind" with value "ClusterExtension"
And ClusterExtensionRevision "${NAME}-1" has label "olm.operatorframework.io/owner-name" with value "${NAME}"
19 changes: 19 additions & 0 deletions test/e2e/steps/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func RegisterSteps(sc *godog.ScenarioContext) {
sc.Step(`^(?i)ClusterExtension reports ([[:alnum:]]+) transition between (\d+) and (\d+) minutes since its creation$`, ClusterExtensionReportsConditionTransitionTime)
sc.Step(`^(?i)ClusterExtensionRevision "([^"]+)" is archived$`, ClusterExtensionRevisionIsArchived)
sc.Step(`^(?i)ClusterExtensionRevision "([^"]+)" contains annotation "([^"]+)" with value$`, ClusterExtensionRevisionHasAnnotationWithValue)
sc.Step(`^(?i)ClusterExtensionRevision "([^"]+)" has label "([^"]+)" with value "([^"]+)"$`, ClusterExtensionRevisionHasLabelWithValue)
Comment on lines 76 to +77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two are really similar, yet have different verbs ("contains" vs "has"), can we ry to use the same verb?


sc.Step(`^(?i)resource "([^"]+)" is installed$`, ResourceAvailable)
sc.Step(`^(?i)resource "([^"]+)" is available$`, ResourceAvailable)
Expand Down Expand Up @@ -507,6 +508,24 @@ func ClusterExtensionRevisionHasAnnotationWithValue(ctx context.Context, revisio
return nil
}

func ClusterExtensionRevisionHasLabelWithValue(ctx context.Context, revisionName, labelKey, labelValue string) error {
sc := scenarioCtx(ctx)
revisionName = substituteScenarioVars(strings.TrimSpace(revisionName), sc)
labelValue = substituteScenarioVars(labelValue, sc)
waitFor(ctx, func() bool {
obj, err := getResource("clusterextensionrevision", revisionName, "")
if err != nil {
logger.V(1).Error(err, "failed to get clusterextensionrevision", "name", revisionName)
return false
}
if obj.GetLabels() == nil {
return false
}
return obj.GetLabels()[labelKey] == labelValue
})
return nil
}

func ResourceAvailable(ctx context.Context, resource string) error {
sc := scenarioCtx(ctx)
resource = substituteScenarioVars(resource, sc)
Expand Down
Loading