Skip to content

Commit 0c63cec

Browse files
author
Kristina Pathak
committed
opamp bridge: use manifestutils.Labels() logic to set version label
1 parent 92b4e2a commit 0c63cec

File tree

2 files changed

+70
-53
lines changed

2 files changed

+70
-53
lines changed

internal/manifests/opampbridge/configmap.go

-9
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
corev1 "k8s.io/api/core/v1"
1919
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2020

21-
"strings"
22-
2321
"gopkg.in/yaml.v2"
2422

2523
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
@@ -33,15 +31,8 @@ const (
3331

3432
func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) {
3533
name := naming.OpAMPBridgeConfigMap(params.OpAMPBridge.Name)
36-
version := strings.Split(params.OpAMPBridge.Spec.Image, ":")
3734
labels := manifestutils.Labels(params.OpAMPBridge.ObjectMeta, name, params.OpAMPBridge.Spec.Image, ComponentOpAMPBridge, []string{})
3835

39-
if len(version) > 1 {
40-
labels["app.kubernetes.io/version"] = version[len(version)-1]
41-
} else {
42-
labels["app.kubernetes.io/version"] = "latest"
43-
}
44-
4536
config := make(map[interface{}]interface{})
4637

4738
if len(params.OpAMPBridge.Spec.Endpoint) > 0 {

internal/manifests/opampbridge/configmap_test.go

+70-44
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ import (
2626
"github.com/stretchr/testify/assert"
2727
)
2828

29-
func TestDesiredConfigMap(t *testing.T) {
30-
expectedLables := map[string]string{
29+
func expectedLabels() map[string]string {
30+
return map[string]string{
3131
"app.kubernetes.io/managed-by": "opentelemetry-operator",
3232
"app.kubernetes.io/instance": "my-namespace.my-instance",
3333
"app.kubernetes.io/part-of": "opentelemetry",
3434
"app.kubernetes.io/version": "0.69.0",
35+
"app.kubernetes.io/component": "opentelemetry-opamp-bridge",
36+
"app.kubernetes.io/name": "my-instance-opamp-bridge",
3537
}
38+
}
3639

37-
t.Run("should return expected opamp-bridge config map", func(t *testing.T) {
38-
expectedLables["app.kubernetes.io/component"] = "opentelemetry-opamp-bridge"
39-
expectedLables["app.kubernetes.io/name"] = "my-instance-opamp-bridge"
40-
41-
expectedData := map[string]string{
42-
"remoteconfiguration.yaml": `capabilities:
40+
func TestDesiredConfigMap(t *testing.T) {
41+
data := map[string]string{
42+
"remoteconfiguration.yaml": `capabilities:
4343
AcceptsOpAMPConnectionSettings: true
4444
AcceptsOtherConnectionSettings: true
4545
AcceptsRemoteConfig: true
@@ -62,46 +62,72 @@ endpoint: ws://opamp-server:4320/v1/opamp
6262
headers:
6363
authorization: access-12345-token
6464
`}
65-
66-
opampBridge := v1alpha1.OpAMPBridge{
67-
ObjectMeta: metav1.ObjectMeta{
68-
Name: "my-instance",
69-
Namespace: "my-namespace",
65+
tests := []struct {
66+
description string
67+
image string
68+
expectedLabels func() map[string]string
69+
expectedData map[string]string
70+
}{
71+
{
72+
description: "should return expected opamp-bridge config map",
73+
image: "ghcr.io/open-telemetry/opentelemetry-operator/operator-opamp-bridge:0.69.0",
74+
expectedLabels: expectedLabels,
75+
expectedData: data,
76+
},
77+
{
78+
description: "should return expected opamp-bridge config map, sha256 image",
79+
image: "ghcr.io/open-telemetry/opentelemetry-operator/operator-opamp-bridge:main@sha256:00738c3a6bca8f143995c9c89fd0c1976784d9785ea394fcdfe580fb18754e1e",
80+
expectedLabels: func() map[string]string {
81+
ls := expectedLabels()
82+
ls["app.kubernetes.io/version"] = "main"
83+
return ls
7084
},
71-
Spec: v1alpha1.OpAMPBridgeSpec{
72-
Image: "ghcr.io/open-telemetry/opentelemetry-operator/operator-opamp-bridge:0.69.0",
73-
Endpoint: "ws://opamp-server:4320/v1/opamp",
74-
Headers: map[string]string{"authorization": "access-12345-token"},
75-
Capabilities: map[v1alpha1.OpAMPBridgeCapability]bool{
76-
v1alpha1.OpAMPBridgeCapabilityReportsStatus: true,
77-
v1alpha1.OpAMPBridgeCapabilityAcceptsRemoteConfig: true,
78-
v1alpha1.OpAMPBridgeCapabilityReportsEffectiveConfig: true,
79-
v1alpha1.OpAMPBridgeCapabilityReportsOwnTraces: true,
80-
v1alpha1.OpAMPBridgeCapabilityReportsOwnMetrics: true,
81-
v1alpha1.OpAMPBridgeCapabilityReportsOwnLogs: true,
82-
v1alpha1.OpAMPBridgeCapabilityAcceptsOpAMPConnectionSettings: true,
83-
v1alpha1.OpAMPBridgeCapabilityAcceptsOtherConnectionSettings: true,
84-
v1alpha1.OpAMPBridgeCapabilityAcceptsRestartCommand: true,
85-
v1alpha1.OpAMPBridgeCapabilityReportsHealth: true,
86-
v1alpha1.OpAMPBridgeCapabilityReportsRemoteConfig: true,
85+
expectedData: data,
86+
},
87+
}
88+
89+
for _, tc := range tests {
90+
t.Run(tc.description, func(t *testing.T) {
91+
opampBridge := v1alpha1.OpAMPBridge{
92+
ObjectMeta: metav1.ObjectMeta{
93+
Name: "my-instance",
94+
Namespace: "my-namespace",
8795
},
88-
ComponentsAllowed: map[string][]string{"receivers": {"otlp"}, "processors": {"memory_limiter"}, "exporters": {"logging"}},
89-
},
90-
}
96+
Spec: v1alpha1.OpAMPBridgeSpec{
97+
Image: tc.image,
98+
Endpoint: "ws://opamp-server:4320/v1/opamp",
99+
Headers: map[string]string{"authorization": "access-12345-token"},
100+
Capabilities: map[v1alpha1.OpAMPBridgeCapability]bool{
101+
v1alpha1.OpAMPBridgeCapabilityReportsStatus: true,
102+
v1alpha1.OpAMPBridgeCapabilityAcceptsRemoteConfig: true,
103+
v1alpha1.OpAMPBridgeCapabilityReportsEffectiveConfig: true,
104+
v1alpha1.OpAMPBridgeCapabilityReportsOwnTraces: true,
105+
v1alpha1.OpAMPBridgeCapabilityReportsOwnMetrics: true,
106+
v1alpha1.OpAMPBridgeCapabilityReportsOwnLogs: true,
107+
v1alpha1.OpAMPBridgeCapabilityAcceptsOpAMPConnectionSettings: true,
108+
v1alpha1.OpAMPBridgeCapabilityAcceptsOtherConnectionSettings: true,
109+
v1alpha1.OpAMPBridgeCapabilityAcceptsRestartCommand: true,
110+
v1alpha1.OpAMPBridgeCapabilityReportsHealth: true,
111+
v1alpha1.OpAMPBridgeCapabilityReportsRemoteConfig: true,
112+
},
113+
ComponentsAllowed: map[string][]string{"receivers": {"otlp"}, "processors": {"memory_limiter"}, "exporters": {"logging"}},
114+
},
115+
}
91116

92-
cfg := config.New()
117+
cfg := config.New()
93118

94-
params := manifests.Params{
95-
Config: cfg,
96-
OpAMPBridge: opampBridge,
97-
Log: logger,
98-
}
119+
params := manifests.Params{
120+
Config: cfg,
121+
OpAMPBridge: opampBridge,
122+
Log: logger,
123+
}
99124

100-
actual, err := ConfigMap(params)
101-
assert.NoError(t, err)
125+
actual, err := ConfigMap(params)
126+
assert.NoError(t, err)
102127

103-
assert.Equal(t, "my-instance-opamp-bridge", actual.Name)
104-
assert.Equal(t, expectedLables, actual.Labels)
105-
assert.Equal(t, expectedData, actual.Data)
106-
})
128+
assert.Equal(t, "my-instance-opamp-bridge", actual.Name)
129+
assert.Equal(t, tc.expectedLabels(), actual.Labels)
130+
assert.Equal(t, tc.expectedData, actual.Data)
131+
})
132+
}
107133
}

0 commit comments

Comments
 (0)