Skip to content
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

Fixed Labels with sha256 image ref #2238

Merged
merged 7 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions .chloggen/bug-fix-labeling-process.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
component: Operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fixed the labeling process which was broken at the moment to capture the current image tag when the users set the sha256 reference.

# One or more tracking issues related to the change
issues: [1982]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: ""
13 changes: 11 additions & 2 deletions internal/manifests/collector/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func isFilteredLabel(label string, filterLabels []string) bool {

// Labels return the common labels to all objects that are part of a managed OpenTelemetryCollector.
func Labels(instance v1alpha1.OpenTelemetryCollector, name string, filterLabels []string) map[string]string {
var versionLabel string
// new map every time, so that we don't touch the instance's label
base := map[string]string{}
if nil != instance.Labels {
Expand All @@ -48,9 +49,17 @@ func Labels(instance v1alpha1.OpenTelemetryCollector, name string, filterLabels
}

version := strings.Split(instance.Spec.Image, ":")
if len(version) > 1 {
for _, v := range version {
if strings.HasSuffix(v, "@sha256") {
versionLabel = strings.TrimSuffix(v, "@sha256")
}
}
switch lenVersion := len(version); lenVersion {
case 3:
base["app.kubernetes.io/version"] = versionLabel
case 2:
base["app.kubernetes.io/version"] = version[len(version)-1]
} else {
default:
base["app.kubernetes.io/version"] = "latest"
}

Expand Down
19 changes: 19 additions & 0 deletions internal/manifests/collector/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,26 @@ func TestLabelsCommonSet(t *testing.T) {
assert.Equal(t, "opentelemetry", labels["app.kubernetes.io/part-of"])
assert.Equal(t, "opentelemetry-collector", labels["app.kubernetes.io/component"])
}
func TestLabelsSha256Set(t *testing.T) {
// prepare
otelcol := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: collectorName,
Namespace: collectorNamespace,
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
Image: "ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator:0.81.0@sha256:c6671841470b83007e0553cdadbc9d05f6cfe17b3ebe9733728dc4a579a5b532",
Copy link
Member

Choose a reason for hiding this comment

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

Could you please add a test case for otel/opentelemetry-collector-contrib@sha256:c6671841470b83007e0553cdadbc9d05f6cfe17b3ebe9733728dc4a579a5b532 - it was reported in the issue this PR resolves

},
}

// test
labels := Labels(otelcol, collectorName, []string{})
assert.Equal(t, "opentelemetry-operator", labels["app.kubernetes.io/managed-by"])
assert.Equal(t, "my-ns.my-instance", labels["app.kubernetes.io/instance"])
assert.Equal(t, "0.81.0", labels["app.kubernetes.io/version"])
assert.Equal(t, "opentelemetry", labels["app.kubernetes.io/part-of"])
assert.Equal(t, "opentelemetry-collector", labels["app.kubernetes.io/component"])
}
func TestLabelsTagUnset(t *testing.T) {
// prepare
otelcol := v1alpha1.OpenTelemetryCollector{
Expand Down