Skip to content

Commit ffea150

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

24 files changed

+652
-324
lines changed

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
}

internal/manifests/collector/annotations_test.go

+30-14
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,25 @@ import (
2020
"github.com/stretchr/testify/assert"
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222

23-
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
23+
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2"
2424
)
2525

2626
func TestDefaultAnnotations(t *testing.T) {
2727
// prepare
28-
otelcol := v1alpha1.OpenTelemetryCollector{
28+
otelcol := v1alpha2.OpenTelemetryCollector{
2929
ObjectMeta: metav1.ObjectMeta{
3030
Name: "my-instance",
3131
Namespace: "my-ns",
3232
},
33-
Spec: v1alpha1.OpenTelemetryCollectorSpec{
34-
Config: "test",
33+
Spec: v1alpha2.OpenTelemetryCollectorSpec{
34+
Config: v1alpha2.Config{
35+
Service: v1alpha2.Service{
36+
Extensions: func() *[]string {
37+
res := []string{"test"}
38+
return &res
39+
}(),
40+
},
41+
},
3542
},
3643
}
3744

@@ -43,17 +50,17 @@ func TestDefaultAnnotations(t *testing.T) {
4350
assert.Equal(t, "true", annotations["prometheus.io/scrape"])
4451
assert.Equal(t, "8888", annotations["prometheus.io/port"])
4552
assert.Equal(t, "/metrics", annotations["prometheus.io/path"])
46-
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", annotations["opentelemetry-operator-config/sha256"])
53+
assert.Equal(t, "5b3b62aa5e0a3c7250084c2b49190e30b72fc2ad352ffbaa699224e1aa900834", annotations["opentelemetry-operator-config/sha256"])
4754
//verify propagation from metadata.annotations to spec.template.spec.metadata.annotations
4855
assert.Equal(t, "true", podAnnotations["prometheus.io/scrape"])
4956
assert.Equal(t, "8888", podAnnotations["prometheus.io/port"])
5057
assert.Equal(t, "/metrics", podAnnotations["prometheus.io/path"])
51-
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", podAnnotations["opentelemetry-operator-config/sha256"])
58+
assert.Equal(t, "5b3b62aa5e0a3c7250084c2b49190e30b72fc2ad352ffbaa699224e1aa900834", podAnnotations["opentelemetry-operator-config/sha256"])
5259
}
5360

5461
func TestUserAnnotations(t *testing.T) {
5562
// prepare
56-
otelcol := v1alpha1.OpenTelemetryCollector{
63+
otelcol := v1alpha2.OpenTelemetryCollector{
5764
ObjectMeta: metav1.ObjectMeta{
5865
Name: "my-instance",
5966
Namespace: "my-ns",
@@ -63,8 +70,15 @@ func TestUserAnnotations(t *testing.T) {
6370
"opentelemetry-operator-config/sha256": "shouldBeOverwritten",
6471
},
6572
},
66-
Spec: v1alpha1.OpenTelemetryCollectorSpec{
67-
Config: "test",
73+
Spec: v1alpha2.OpenTelemetryCollectorSpec{
74+
Config: v1alpha2.Config{
75+
Service: v1alpha2.Service{
76+
Extensions: func() *[]string {
77+
res := []string{"test2"}
78+
return &res
79+
}(),
80+
},
81+
},
6882
},
6983
}
7084

@@ -76,18 +90,20 @@ func TestUserAnnotations(t *testing.T) {
7690
assert.Equal(t, "false", annotations["prometheus.io/scrape"])
7791
assert.Equal(t, "1234", annotations["prometheus.io/port"])
7892
assert.Equal(t, "/test", annotations["prometheus.io/path"])
79-
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", annotations["opentelemetry-operator-config/sha256"])
80-
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", podAnnotations["opentelemetry-operator-config/sha256"])
93+
assert.Equal(t, "29cb15a4b87f8c6284e7c3377f6b6c5c74519f5aee8ca39a90b3cf3ca2043c4d", annotations["opentelemetry-operator-config/sha256"])
94+
assert.Equal(t, "29cb15a4b87f8c6284e7c3377f6b6c5c74519f5aee8ca39a90b3cf3ca2043c4d", podAnnotations["opentelemetry-operator-config/sha256"])
8195
}
8296

8397
func TestAnnotationsPropagateDown(t *testing.T) {
8498
// prepare
85-
otelcol := v1alpha1.OpenTelemetryCollector{
99+
otelcol := v1alpha2.OpenTelemetryCollector{
86100
ObjectMeta: metav1.ObjectMeta{
87101
Annotations: map[string]string{"myapp": "mycomponent"},
88102
},
89-
Spec: v1alpha1.OpenTelemetryCollectorSpec{
90-
PodAnnotations: map[string]string{"pod_annotation": "pod_annotation_value"},
103+
Spec: v1alpha2.OpenTelemetryCollectorSpec{
104+
OpenTelemetryCommonFields: v1alpha2.OpenTelemetryCommonFields{
105+
PodAnnotations: map[string]string{"pod_annotation": "pod_annotation_value"},
106+
},
91107
},
92108
}
93109

0 commit comments

Comments
 (0)