@@ -19,18 +19,20 @@ import (
19
19
"time"
20
20
21
21
"github.com/stretchr/testify/assert"
22
+ "github.com/stretchr/testify/require"
22
23
"gopkg.in/yaml.v3"
24
+ appsv1 "k8s.io/api/apps/v1"
25
+ autoscalingv2 "k8s.io/api/autoscaling/v2"
23
26
v1 "k8s.io/api/core/v1"
27
+ networkingv1 "k8s.io/api/networking/v1"
24
28
"k8s.io/apimachinery/pkg/api/resource"
25
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
30
"k8s.io/apimachinery/pkg/util/intstr"
27
31
28
32
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
29
33
)
30
34
31
- func Test_V1Alpha1to2 (t * testing.T ) {
32
- t .Run ("valid config" , func (t * testing.T ) {
33
- config := `---
35
+ const collectorCfg = `---
34
36
receivers:
35
37
otlp:
36
38
protocols:
@@ -48,9 +50,12 @@ service:
48
50
processors: [resourcedetection]
49
51
exporters: [otlp]
50
52
`
53
+
54
+ func Test_tov1beta1_config (t * testing.T ) {
55
+ t .Run ("valid config" , func (t * testing.T ) {
51
56
cfgV1 := OpenTelemetryCollector {
52
57
Spec : OpenTelemetryCollectorSpec {
53
- Config : config ,
58
+ Config : collectorCfg ,
54
59
Args : map [string ]string {
55
60
"test" : "something" ,
56
61
},
@@ -64,7 +69,7 @@ service:
64
69
65
70
yamlCfg , err := yaml .Marshal (& cfgV2 .Spec .Config )
66
71
assert .Nil (t , err )
67
- assert .YAMLEq (t , config , string (yamlCfg ))
72
+ assert .YAMLEq (t , collectorCfg , string (yamlCfg ))
68
73
})
69
74
t .Run ("invalid config" , func (t * testing.T ) {
70
75
config := `!!!`
@@ -79,13 +84,252 @@ service:
79
84
})
80
85
}
81
86
82
- func Test_TargetAllocator (t * testing.T ) {
87
+ func Test_tov1alpha1_config (t * testing.T ) {
88
+ cfg := v1beta1.Config {}
89
+ err := yaml .Unmarshal ([]byte (collectorCfg ), & cfg )
90
+ require .NoError (t , err )
91
+
92
+ beta1Col := v1beta1.OpenTelemetryCollector {
93
+ Spec : v1beta1.OpenTelemetryCollectorSpec {
94
+ Config : cfg ,
95
+ },
96
+ }
97
+ alpha1Col , err := tov1alpha1 (beta1Col )
98
+ require .NoError (t , err )
99
+ assert .YAMLEq (t , collectorCfg , alpha1Col .Spec .Config )
100
+ }
101
+
102
+ func Test_tov1beta1AndBack (t * testing.T ) {
103
+ one := int32 (1 )
104
+ two := int64 (2 )
105
+ intstrAAA := intstr .FromString ("aaa" )
106
+ boolTrue := true
107
+ ingressClass := "someClass"
108
+ colalpha1 := & OpenTelemetryCollector {
109
+ ObjectMeta : metav1.ObjectMeta {
110
+ Name : "otel" ,
111
+ Namespace : "observability" ,
112
+ Labels : map [string ]string {"foo" : "bar" },
113
+ Annotations : map [string ]string {"bax" : "foo" },
114
+ },
115
+ Spec : OpenTelemetryCollectorSpec {
116
+ ManagementState : ManagementStateManaged ,
117
+ Resources : v1.ResourceRequirements {
118
+ Limits : v1.ResourceList {
119
+ v1 .ResourceCPU : resource .MustParse ("500m" ),
120
+ v1 .ResourceMemory : resource .MustParse ("128Mi" ),
121
+ },
122
+ Requests : v1.ResourceList {
123
+ v1 .ResourceCPU : resource .MustParse ("500m" ),
124
+ v1 .ResourceMemory : resource .MustParse ("128Mi" ),
125
+ },
126
+ },
127
+ NodeSelector : map [string ]string {"aaa" : "ccc" },
128
+ Args : map [string ]string {"foo" : "bar" },
129
+ Replicas : & one ,
130
+ Autoscaler : & AutoscalerSpec {
131
+ MinReplicas : & one ,
132
+ MaxReplicas : & one ,
133
+ Behavior : & autoscalingv2.HorizontalPodAutoscalerBehavior {
134
+ ScaleUp : & autoscalingv2.HPAScalingRules {
135
+ Policies : []autoscalingv2.HPAScalingPolicy {
136
+ {
137
+ Type : "aaa" ,
138
+ Value : 2 ,
139
+ PeriodSeconds : 4 ,
140
+ },
141
+ },
142
+ },
143
+ },
144
+ Metrics : []MetricSpec {
145
+ {
146
+ Type : autoscalingv2 .ContainerResourceMetricSourceType ,
147
+ Pods : & autoscalingv2.PodsMetricSource {
148
+ Metric : autoscalingv2.MetricIdentifier {
149
+ Name : "rrrrt" ,
150
+ },
151
+ },
152
+ },
153
+ },
154
+ TargetCPUUtilization : & one ,
155
+ TargetMemoryUtilization : & one ,
156
+ },
157
+ PodDisruptionBudget : & PodDisruptionBudgetSpec {
158
+ MinAvailable : & intstrAAA ,
159
+ MaxUnavailable : & intstrAAA ,
160
+ },
161
+ SecurityContext : & v1.SecurityContext {
162
+ RunAsUser : & two ,
163
+ },
164
+ PodSecurityContext : & v1.PodSecurityContext {
165
+ RunAsNonRoot : & boolTrue ,
166
+ },
167
+ PodAnnotations : map [string ]string {"foo" : "bar" },
168
+ TargetAllocator : createTA (),
169
+ Mode : ModeDeployment ,
170
+ ServiceAccount : "foo" ,
171
+ Image : "baz/bar:1.0" ,
172
+ UpgradeStrategy : UpgradeStrategyAutomatic ,
173
+ ImagePullPolicy : v1 .PullAlways ,
174
+ Config : collectorCfg ,
175
+ VolumeMounts : []v1.VolumeMount {
176
+ {
177
+ Name : "aaa" ,
178
+ },
179
+ },
180
+ Ports : []v1.ServicePort {
181
+ {
182
+ Name : "otlp" ,
183
+ },
184
+ },
185
+ Env : []v1.EnvVar {
186
+ {
187
+ Name : "foo" ,
188
+ Value : "bar" ,
189
+ ValueFrom : & v1.EnvVarSource {
190
+ ResourceFieldRef : & v1.ResourceFieldSelector {
191
+ ContainerName : "bbb" ,
192
+ Resource : "aaa" ,
193
+ Divisor : resource.Quantity {},
194
+ },
195
+ },
196
+ },
197
+ },
198
+ EnvFrom : []v1.EnvFromSource {
199
+ {
200
+ Prefix : "aa" ,
201
+ ConfigMapRef : & v1.ConfigMapEnvSource {
202
+ LocalObjectReference : v1.LocalObjectReference {
203
+ Name : "bbb" ,
204
+ },
205
+ },
206
+ },
207
+ },
208
+ VolumeClaimTemplates : []v1.PersistentVolumeClaim {
209
+ {
210
+ TypeMeta : metav1.TypeMeta {},
211
+ ObjectMeta : metav1.ObjectMeta {},
212
+ Spec : v1.PersistentVolumeClaimSpec {
213
+ VolumeName : "aaaa" ,
214
+ },
215
+ },
216
+ },
217
+ Tolerations : []v1.Toleration {
218
+ {
219
+ Key : "11" ,
220
+ Operator : "33" ,
221
+ Value : "44" ,
222
+ Effect : "55" ,
223
+ },
224
+ },
225
+ Volumes : []v1.Volume {
226
+ {
227
+ Name : "cfg" ,
228
+ VolumeSource : v1.VolumeSource {},
229
+ },
230
+ },
231
+ Ingress : Ingress {
232
+ Type : IngressTypeRoute ,
233
+ RuleType : IngressRuleTypePath ,
234
+ Hostname : "foo.com" ,
235
+ Annotations : map [string ]string {"aa" : "bb" },
236
+ TLS : []networkingv1.IngressTLS {
237
+ {
238
+ Hosts : []string {"foo" },
239
+ SecretName : "bar" ,
240
+ },
241
+ },
242
+ IngressClassName : & ingressClass ,
243
+ Route : OpenShiftRoute {
244
+ Termination : TLSRouteTerminationTypeEdge ,
245
+ },
246
+ },
247
+ HostNetwork : true ,
248
+ ShareProcessNamespace : true ,
249
+ PriorityClassName : "foobar" ,
250
+ Affinity : & v1.Affinity {
251
+ NodeAffinity : & v1.NodeAffinity {
252
+ PreferredDuringSchedulingIgnoredDuringExecution : []v1.PreferredSchedulingTerm {{
253
+ Weight : 444 ,
254
+ }},
255
+ },
256
+ },
257
+ Lifecycle : & v1.Lifecycle {
258
+ PostStart : & v1.LifecycleHandler {
259
+ Exec : & v1.ExecAction {
260
+ Command : []string {"/bin" },
261
+ },
262
+ },
263
+ },
264
+ TerminationGracePeriodSeconds : & two ,
265
+ LivenessProbe : & Probe {
266
+ PeriodSeconds : & one ,
267
+ },
268
+ InitContainers : []v1.Container {
269
+ {
270
+ Name : "init" ,
271
+ },
272
+ },
273
+ AdditionalContainers : []v1.Container {
274
+ {
275
+ Name : "some" ,
276
+ },
277
+ },
278
+ Observability : ObservabilitySpec {
279
+ Metrics : MetricsConfigSpec {
280
+ EnableMetrics : true ,
281
+ DisablePrometheusAnnotations : true ,
282
+ },
283
+ },
284
+ TopologySpreadConstraints : []v1.TopologySpreadConstraint {
285
+ {
286
+ TopologyKey : "key" ,
287
+ },
288
+ },
289
+ ConfigMaps : []ConfigMapsSpec {
290
+ {
291
+ Name : "aaa" ,
292
+ MountPath : "bbb" ,
293
+ },
294
+ },
295
+ UpdateStrategy : appsv1.DaemonSetUpdateStrategy {
296
+ Type : appsv1 .RollingUpdateDaemonSetStrategyType ,
297
+ },
298
+ DeploymentUpdateStrategy : appsv1.DeploymentStrategy {
299
+ Type : appsv1 .RecreateDeploymentStrategyType ,
300
+ },
301
+ },
302
+ Status : OpenTelemetryCollectorStatus {
303
+ Scale : ScaleSubresourceStatus {
304
+ Selector : "bar" ,
305
+ Replicas : 1 ,
306
+ StatusReplicas : "foo" ,
307
+ },
308
+ Version : "1.0" ,
309
+ Image : "foo/bar:1.0" ,
310
+ },
311
+ }
312
+
313
+ colbeta1 , err := Tov1beta1 (* colalpha1 )
314
+ require .NoError (t , err )
315
+ colalpha1Converted , err := tov1alpha1 (colbeta1 )
316
+ require .NoError (t , err )
317
+
318
+ assert .YAMLEq (t , colalpha1 .Spec .Config , colalpha1Converted .Spec .Config )
319
+
320
+ // empty the config to enable assertion on the entire objects
321
+ colalpha1 .Spec .Config = ""
322
+ colalpha1Converted .Spec .Config = ""
323
+ assert .Equal (t , colalpha1 , colalpha1Converted )
324
+ }
325
+
326
+ func createTA () OpenTelemetryTargetAllocator {
83
327
replicas := int32 (2 )
84
328
runAsNonRoot := true
85
329
privileged := true
86
330
runAsUser := int64 (1337 )
87
331
runasGroup := int64 (1338 )
88
- input := OpenTelemetryTargetAllocator {
332
+ return OpenTelemetryTargetAllocator {
89
333
Replicas : & replicas ,
90
334
NodeSelector : map [string ]string {"key" : "value" },
91
335
Resources : v1.ResourceRequirements {
@@ -176,43 +420,4 @@ func Test_TargetAllocator(t *testing.T) {
176
420
},
177
421
},
178
422
}
179
-
180
- expected := v1beta1.TargetAllocatorEmbedded {
181
- Replicas : input .Replicas ,
182
- NodeSelector : input .NodeSelector ,
183
- Resources : input .Resources ,
184
- AllocationStrategy : v1beta1 .TargetAllocatorAllocationStrategyConsistentHashing ,
185
- FilterStrategy : v1beta1 .TargetAllocatorFilterStrategyRelabelConfig ,
186
- ServiceAccount : input .ServiceAccount ,
187
- Image : input .Image ,
188
- Enabled : input .Enabled ,
189
- Affinity : input .Affinity ,
190
- PrometheusCR : v1beta1.TargetAllocatorPrometheusCR {
191
- Enabled : input .PrometheusCR .Enabled ,
192
- ScrapeInterval : input .PrometheusCR .ScrapeInterval ,
193
- PodMonitorSelector : & metav1.LabelSelector {
194
- MatchLabels : input .PrometheusCR .PodMonitorSelector ,
195
- },
196
- ServiceMonitorSelector : & metav1.LabelSelector {
197
- MatchLabels : input .PrometheusCR .ServiceMonitorSelector ,
198
- },
199
- },
200
- SecurityContext : input .SecurityContext ,
201
- PodSecurityContext : input .PodSecurityContext ,
202
- TopologySpreadConstraints : input .TopologySpreadConstraints ,
203
- Tolerations : input .Tolerations ,
204
- Env : input .Env ,
205
- Observability : v1beta1.ObservabilitySpec {
206
- Metrics : v1beta1.MetricsConfigSpec {
207
- EnableMetrics : input .Observability .Metrics .EnableMetrics ,
208
- DisablePrometheusAnnotations : input .Observability .Metrics .DisablePrometheusAnnotations ,
209
- },
210
- },
211
- PodDisruptionBudget : & v1beta1.PodDisruptionBudgetSpec {
212
- MinAvailable : input .PodDisruptionBudget .MinAvailable ,
213
- MaxUnavailable : input .PodDisruptionBudget .MaxUnavailable ,
214
- },
215
- }
216
-
217
- assert .Equal (t , expected , TargetAllocatorEmbedded (input ))
218
423
}
0 commit comments