Skip to content

Commit 0eec778

Browse files
committed
internal/manifests/collector: switch internally to v1alphav2
Signed-off-by: Benedikt Bongartz <[email protected]>
1 parent fbd27e5 commit 0eec778

25 files changed

+662
-334
lines changed

controllers/builder_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ service:
124124
"app.kubernetes.io/version": "latest",
125125
},
126126
Annotations: map[string]string{
127-
"opentelemetry-operator-config/sha256": "8188c85abd4aa5e9c798874b4f47d37f08d4778933154e3f7d60246510ed9dd2",
127+
"opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8",
128128
"prometheus.io/path": "/metrics",
129129
"prometheus.io/port": "8888",
130130
"prometheus.io/scrape": "true",
@@ -146,7 +146,7 @@ service:
146146
"app.kubernetes.io/version": "latest",
147147
},
148148
Annotations: map[string]string{
149-
"opentelemetry-operator-config/sha256": "8188c85abd4aa5e9c798874b4f47d37f08d4778933154e3f7d60246510ed9dd2",
149+
"opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8",
150150
"prometheus.io/path": "/metrics",
151151
"prometheus.io/port": "8888",
152152
"prometheus.io/scrape": "true",
@@ -367,7 +367,7 @@ service:
367367
"app.kubernetes.io/version": "latest",
368368
},
369369
Annotations: map[string]string{
370-
"opentelemetry-operator-config/sha256": "8188c85abd4aa5e9c798874b4f47d37f08d4778933154e3f7d60246510ed9dd2",
370+
"opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8",
371371
"prometheus.io/path": "/metrics",
372372
"prometheus.io/port": "8888",
373373
"prometheus.io/scrape": "true",
@@ -389,7 +389,7 @@ service:
389389
"app.kubernetes.io/version": "latest",
390390
},
391391
Annotations: map[string]string{
392-
"opentelemetry-operator-config/sha256": "8188c85abd4aa5e9c798874b4f47d37f08d4778933154e3f7d60246510ed9dd2",
392+
"opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8",
393393
"prometheus.io/path": "/metrics",
394394
"prometheus.io/port": "8888",
395395
"prometheus.io/scrape": "true",
@@ -643,7 +643,7 @@ service:
643643
"app.kubernetes.io/version": "latest",
644644
},
645645
Annotations: map[string]string{
646-
"opentelemetry-operator-config/sha256": "8188c85abd4aa5e9c798874b4f47d37f08d4778933154e3f7d60246510ed9dd2",
646+
"opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8",
647647
"prometheus.io/path": "/metrics",
648648
"prometheus.io/port": "8888",
649649
"prometheus.io/scrape": "true",
@@ -665,7 +665,7 @@ service:
665665
"app.kubernetes.io/version": "latest",
666666
},
667667
Annotations: map[string]string{
668-
"opentelemetry-operator-config/sha256": "8188c85abd4aa5e9c798874b4f47d37f08d4778933154e3f7d60246510ed9dd2",
668+
"opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8",
669669
"prometheus.io/path": "/metrics",
670670
"prometheus.io/port": "8888",
671671
"prometheus.io/scrape": "true",
@@ -1153,7 +1153,7 @@ service:
11531153
"app.kubernetes.io/version": "latest",
11541154
},
11551155
Annotations: map[string]string{
1156-
"opentelemetry-operator-config/sha256": "5fe4d7d7faf3247bd7ec88688c9f73618f9ab8e170362bd7203c54e7a2f5cec0",
1156+
"opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43",
11571157
"prometheus.io/path": "/metrics",
11581158
"prometheus.io/port": "8888",
11591159
"prometheus.io/scrape": "true",
@@ -1176,7 +1176,7 @@ service:
11761176
"app.kubernetes.io/version": "latest",
11771177
},
11781178
Annotations: map[string]string{
1179-
"opentelemetry-operator-config/sha256": "5fe4d7d7faf3247bd7ec88688c9f73618f9ab8e170362bd7203c54e7a2f5cec0",
1179+
"opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43",
11801180
"prometheus.io/path": "/metrics",
11811181
"prometheus.io/port": "8888",
11821182
"prometheus.io/scrape": "true",
@@ -1544,7 +1544,7 @@ label_selector:
15441544
"app.kubernetes.io/version": "latest",
15451545
},
15461546
Annotations: map[string]string{
1547-
"opentelemetry-operator-config/sha256": "5fe4d7d7faf3247bd7ec88688c9f73618f9ab8e170362bd7203c54e7a2f5cec0",
1547+
"opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43",
15481548
"prometheus.io/path": "/metrics",
15491549
"prometheus.io/port": "8888",
15501550
"prometheus.io/scrape": "true",
@@ -1567,7 +1567,7 @@ label_selector:
15671567
"app.kubernetes.io/version": "latest",
15681568
},
15691569
Annotations: map[string]string{
1570-
"opentelemetry-operator-config/sha256": "5fe4d7d7faf3247bd7ec88688c9f73618f9ab8e170362bd7203c54e7a2f5cec0",
1570+
"opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43",
15711571
"prometheus.io/path": "/metrics",
15721572
"prometheus.io/port": "8888",
15731573
"prometheus.io/scrape": "true",

internal/api/convert/v1alpha.go

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package convert
16+
17+
import (
18+
"encoding/json"
19+
"fmt"
20+
21+
appsv1 "k8s.io/api/apps/v1"
22+
"sigs.k8s.io/yaml"
23+
24+
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
25+
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2"
26+
)
27+
28+
func V1Alpha1to2(in v1alpha1.OpenTelemetryCollector) v1alpha2.OpenTelemetryCollector {
29+
copy := in.DeepCopy()
30+
out := v1alpha2.OpenTelemetryCollector{
31+
TypeMeta: copy.TypeMeta,
32+
ObjectMeta: copy.ObjectMeta,
33+
}
34+
35+
collectorJson, err := yaml.YAMLToJSON([]byte(in.Spec.Config))
36+
if err != nil {
37+
panic(fmt.Sprintf("could not convert v1alpha1 yaml config to json, err: %s", err))
38+
}
39+
40+
cfg := &v1alpha2.Config{}
41+
if err := json.Unmarshal(collectorJson, cfg); err != nil {
42+
panic(fmt.Sprintf("could not convert v1alpha1 config to v1alpha2, err: %s", err))
43+
}
44+
out.Spec.Config = *cfg
45+
46+
out.Spec.OpenTelemetryCommonFields.ManagementState = v1alpha2.ManagementStateType(copy.Spec.ManagementState)
47+
out.Spec.OpenTelemetryCommonFields.Resources = copy.Spec.Resources
48+
out.Spec.OpenTelemetryCommonFields.NodeSelector = copy.Spec.NodeSelector
49+
out.Spec.OpenTelemetryCommonFields.Args = copy.Spec.NodeSelector
50+
out.Spec.OpenTelemetryCommonFields.Replicas = copy.Spec.Replicas
51+
52+
if copy.Spec.Autoscaler != nil {
53+
metrics := make([]v1alpha2.MetricSpec, len(copy.Spec.Autoscaler.Metrics))
54+
for i, m := range copy.Spec.Autoscaler.Metrics {
55+
metrics[i] = v1alpha2.MetricSpec{
56+
Type: m.Type,
57+
Pods: m.Pods,
58+
}
59+
}
60+
out.Spec.OpenTelemetryCommonFields.Autoscaler = &v1alpha2.AutoscalerSpec{
61+
MinReplicas: copy.Spec.Autoscaler.MinReplicas,
62+
MaxReplicas: copy.Spec.Autoscaler.MaxReplicas,
63+
Behavior: copy.Spec.Autoscaler.Behavior,
64+
Metrics: metrics,
65+
TargetCPUUtilization: copy.Spec.Autoscaler.TargetCPUUtilization,
66+
TargetMemoryUtilization: copy.Spec.Autoscaler.TargetMemoryUtilization,
67+
}
68+
}
69+
70+
if copy.Spec.PodDisruptionBudget != nil {
71+
out.Spec.OpenTelemetryCommonFields.PodDisruptionBudget = &v1alpha2.PodDisruptionBudgetSpec{
72+
MinAvailable: copy.Spec.PodDisruptionBudget.MinAvailable,
73+
MaxUnavailable: copy.Spec.PodDisruptionBudget.MaxUnavailable,
74+
}
75+
}
76+
if copy.Spec.SecurityContext != nil {
77+
out.Spec.OpenTelemetryCommonFields.SecurityContext = copy.Spec.SecurityContext
78+
}
79+
if copy.Spec.PodSecurityContext != nil {
80+
out.Spec.OpenTelemetryCommonFields.PodSecurityContext = copy.Spec.PodSecurityContext
81+
}
82+
out.Spec.OpenTelemetryCommonFields.PodAnnotations = copy.Spec.PodAnnotations
83+
out.Spec.OpenTelemetryCommonFields.ServiceAccount = copy.Spec.ServiceAccount
84+
out.Spec.OpenTelemetryCommonFields.Image = copy.Spec.Image
85+
out.Spec.OpenTelemetryCommonFields.ImagePullPolicy = copy.Spec.ImagePullPolicy
86+
out.Spec.OpenTelemetryCommonFields.VolumeMounts = copy.Spec.VolumeMounts
87+
out.Spec.OpenTelemetryCommonFields.Ports = copy.Spec.Ports
88+
out.Spec.OpenTelemetryCommonFields.Env = copy.Spec.Env
89+
out.Spec.OpenTelemetryCommonFields.EnvFrom = copy.Spec.EnvFrom
90+
out.Spec.OpenTelemetryCommonFields.VolumeClaimTemplates = copy.Spec.VolumeClaimTemplates
91+
out.Spec.OpenTelemetryCommonFields.Tolerations = copy.Spec.Tolerations
92+
out.Spec.OpenTelemetryCommonFields.Volumes = copy.Spec.Volumes
93+
out.Spec.OpenTelemetryCommonFields.Affinity = copy.Spec.Affinity
94+
out.Spec.OpenTelemetryCommonFields.Lifecycle = copy.Spec.Lifecycle
95+
out.Spec.OpenTelemetryCommonFields.TerminationGracePeriodSeconds = copy.Spec.TerminationGracePeriodSeconds
96+
out.Spec.OpenTelemetryCommonFields.TopologySpreadConstraints = copy.Spec.TopologySpreadConstraints
97+
out.Spec.OpenTelemetryCommonFields.HostNetwork = copy.Spec.HostNetwork
98+
out.Spec.OpenTelemetryCommonFields.PriorityClassName = copy.Spec.PriorityClassName
99+
out.Spec.OpenTelemetryCommonFields.InitContainers = copy.Spec.InitContainers
100+
out.Spec.OpenTelemetryCommonFields.AdditionalContainers = copy.Spec.AdditionalContainers
101+
102+
// TODO: migrate Target Allocator fields
103+
// out.Spec.TargetAllocator = ...
104+
105+
out.Spec.Mode = v1alpha2.Mode(copy.Spec.Mode)
106+
out.Spec.UpgradeStrategy = v1alpha2.UpgradeStrategy(copy.Spec.UpgradeStrategy)
107+
out.Spec.Ingress.Type = v1alpha2.IngressType(copy.Spec.Ingress.Type)
108+
out.Spec.Ingress.Annotations = copy.Spec.Ingress.Annotations
109+
out.Spec.Ingress.TLS = copy.Spec.Ingress.TLS
110+
out.Spec.Ingress.IngressClassName = copy.Spec.Ingress.IngressClassName
111+
out.Spec.Ingress.Route.Termination = v1alpha2.TLSRouteTerminationType(copy.Spec.Ingress.Route.Termination)
112+
113+
if copy.Spec.LivenessProbe != nil {
114+
out.Spec.LivenessProbe = &v1alpha2.Probe{
115+
InitialDelaySeconds: copy.Spec.LivenessProbe.InitialDelaySeconds,
116+
TimeoutSeconds: copy.Spec.LivenessProbe.TimeoutSeconds,
117+
PeriodSeconds: copy.Spec.LivenessProbe.PeriodSeconds,
118+
SuccessThreshold: copy.Spec.LivenessProbe.SuccessThreshold,
119+
FailureThreshold: copy.Spec.LivenessProbe.FailureThreshold,
120+
TerminationGracePeriodSeconds: copy.Spec.LivenessProbe.TerminationGracePeriodSeconds,
121+
}
122+
}
123+
124+
out.Spec.Observability.Metrics.EnableMetrics = copy.Spec.Observability.Metrics.EnableMetrics
125+
126+
out.Spec.ConfigMaps = copy.Spec.ConfigMaps
127+
out.Spec.DaemonSetUpdateStrategy = appsv1.DaemonSetUpdateStrategy{} // NOTE: N/A in v1alpha1
128+
out.Spec.DeploymentUpdateStrategy.Type = copy.Spec.DeploymentUpdateStrategy.Type
129+
out.Spec.DeploymentUpdateStrategy.RollingUpdate = copy.Spec.DeploymentUpdateStrategy.RollingUpdate
130+
131+
return out
132+
}

internal/api/convert/v1alpha_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package convert
16+
17+
import (
18+
"encoding/json"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
"sigs.k8s.io/yaml"
23+
24+
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
25+
)
26+
27+
func Test_V1Alpha1to2_Config(t *testing.T) {
28+
config := `---
29+
receivers:
30+
otlp:
31+
protocols:
32+
grpc:
33+
processors:
34+
resourcedetection:
35+
detectors: [kubernetes]
36+
exporters:
37+
otlp:
38+
endpoint: "otlp:4317"
39+
service:
40+
pipelines:
41+
traces:
42+
receivers: [otlp]
43+
processors: [resourcedetection]
44+
exporters: [otlp]
45+
`
46+
cfgV1 := v1alpha1.OpenTelemetryCollector{
47+
Spec: v1alpha1.OpenTelemetryCollectorSpec{
48+
Config: config,
49+
},
50+
}
51+
52+
cfgV2 := V1Alpha1to2(cfgV1)
53+
assert.NotNil(t, cfgV2)
54+
55+
jsonCfg, err := json.Marshal(&cfgV2.Spec.Config)
56+
assert.Nil(t, err)
57+
yamlCfg, err := yaml.JSONToYAML(jsonCfg)
58+
assert.Nil(t, err)
59+
assert.YAMLEq(t, config, string(yamlCfg))
60+
}

internal/manifests/collector/annotations.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ package collector
1616

1717
import (
1818
"crypto/sha256"
19+
"encoding/json"
1920
"fmt"
2021

21-
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
22+
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2"
2223
)
2324

2425
// Annotations return the annotations for OpenTelemetryCollector pod.
25-
func Annotations(instance v1alpha1.OpenTelemetryCollector) map[string]string {
26+
func Annotations(instance v1alpha2.OpenTelemetryCollector) map[string]string {
2627
// new map every time, so that we don't touch the instance's annotations
2728
annotations := map[string]string{}
2829

@@ -37,14 +38,15 @@ func Annotations(instance v1alpha1.OpenTelemetryCollector) map[string]string {
3738
annotations[k] = v
3839
}
3940
}
41+
4042
// make sure sha256 for configMap is always calculated
4143
annotations["opentelemetry-operator-config/sha256"] = getConfigMapSHA(instance.Spec.Config)
4244

4345
return annotations
4446
}
4547

4648
// PodAnnotations return the spec annotations for OpenTelemetryCollector pod.
47-
func PodAnnotations(instance v1alpha1.OpenTelemetryCollector) map[string]string {
49+
func PodAnnotations(instance v1alpha2.OpenTelemetryCollector) map[string]string {
4850
// new map every time, so that we don't touch the instance's annotations
4951
podAnnotations := map[string]string{}
5052

@@ -66,7 +68,11 @@ func PodAnnotations(instance v1alpha1.OpenTelemetryCollector) map[string]string
6668
return podAnnotations
6769
}
6870

69-
func getConfigMapSHA(config string) string {
70-
h := sha256.Sum256([]byte(config))
71+
func getConfigMapSHA(config v1alpha2.Config) string {
72+
b, err := json.Marshal(&config)
73+
if err != nil {
74+
return "invalid"
75+
}
76+
h := sha256.Sum256(b)
7177
return fmt.Sprintf("%x", h)
7278
}

0 commit comments

Comments
 (0)