Skip to content

Commit af5f907

Browse files
authored
Merge pull request #185 from kube-logging/feat/support-custom-image
feat: support setting custom otelcol image
2 parents 4c477b2 + aeefedc commit af5f907

File tree

2 files changed

+75
-40
lines changed

2 files changed

+75
-40
lines changed

pkg/resources/manager/collector_manager.go

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package manager
1717
import (
1818
"context"
1919
"fmt"
20+
"maps"
2021
"math"
2122
"path/filepath"
2223
"strings"
@@ -472,47 +473,39 @@ func setOtelCommonFieldsDefaults(otelCommonFields *otelv1beta1.OpenTelemetryComm
472473
otelCommonFields = &otelv1beta1.OpenTelemetryCommonFields{}
473474
}
474475

475-
otelCommonFields.Image = axoflowOtelCollectorImageRef
476+
if otelCommonFields.Image == "" {
477+
otelCommonFields.Image = axoflowOtelCollectorImageRef
478+
}
479+
476480
otelCommonFields.ServiceAccount = saName
477481

478482
if otelCommonFields.Args == nil {
479483
otelCommonFields.Args = make(map[string]string)
480484
}
481-
for key, value := range additionalArgs {
482-
otelCommonFields.Args[key] = value
483-
}
484-
485-
volumeMounts := []corev1.VolumeMount{
486-
{
487-
Name: "varlog",
488-
ReadOnly: true,
489-
MountPath: "/var/log",
490-
},
491-
{
492-
Name: "varlibdockercontainers",
493-
ReadOnly: true,
494-
MountPath: "/var/lib/docker/containers",
495-
},
496-
}
497-
otelCommonFields.VolumeMounts = append(otelCommonFields.VolumeMounts, volumeMounts...)
498-
499-
volumes := []corev1.Volume{
500-
{
501-
Name: "varlog",
502-
VolumeSource: corev1.VolumeSource{
503-
HostPath: &corev1.HostPathVolumeSource{
504-
Path: "/var/log",
485+
maps.Copy(otelCommonFields.Args, additionalArgs)
486+
487+
volumeConfigs := []struct {
488+
name string
489+
path string
490+
}{
491+
{name: "varlog", path: "/var/log"},
492+
{name: "varlibdockercontainers", path: "/var/lib/docker/containers"},
493+
}
494+
for _, config := range volumeConfigs {
495+
if !volumeExists(otelCommonFields.Volumes, config.name) {
496+
otelCommonFields.Volumes = append(otelCommonFields.Volumes, corev1.Volume{
497+
Name: config.name,
498+
VolumeSource: corev1.VolumeSource{
499+
HostPath: &corev1.HostPathVolumeSource{
500+
Path: config.path,
501+
},
505502
},
506-
},
507-
},
508-
{
509-
Name: "varlibdockercontainers",
510-
VolumeSource: corev1.VolumeSource{
511-
HostPath: &corev1.HostPathVolumeSource{
512-
Path: "/var/lib/docker/containers",
513-
},
514-
},
515-
},
503+
})
504+
otelCommonFields.VolumeMounts = append(otelCommonFields.VolumeMounts, corev1.VolumeMount{
505+
Name: config.name,
506+
ReadOnly: true,
507+
MountPath: config.path,
508+
})
509+
}
516510
}
517-
otelCommonFields.Volumes = append(otelCommonFields.Volumes, volumes...)
518511
}

pkg/resources/manager/collector_manager_test.go

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ func TestSetOtelCommonFieldsDefaults(t *testing.T) {
3232
expectedError error
3333
}{
3434
{
35-
name: "Basic Initialization",
36-
initialCommonFields: &otelv1beta1.OpenTelemetryCommonFields{
37-
Args: map[string]string{},
38-
},
35+
name: "Basic Initialization",
36+
initialCommonFields: &otelv1beta1.OpenTelemetryCommonFields{},
3937
additionalArgs: map[string]string{
4038
"key1": "value1",
4139
},
@@ -139,6 +137,50 @@ func TestSetOtelCommonFieldsDefaults(t *testing.T) {
139137
expectedResult: nil,
140138
expectedError: nil,
141139
},
140+
{
141+
name: "custom image",
142+
initialCommonFields: &otelv1beta1.OpenTelemetryCommonFields{
143+
Image: "custom-image:latest",
144+
},
145+
additionalArgs: map[string]string{},
146+
saName: "test-sa",
147+
expectedResult: &otelv1beta1.OpenTelemetryCommonFields{
148+
Image: "custom-image:latest",
149+
ServiceAccount: "test-sa",
150+
Args: map[string]string{},
151+
VolumeMounts: []corev1.VolumeMount{
152+
{
153+
Name: "varlog",
154+
ReadOnly: true,
155+
MountPath: "/var/log",
156+
},
157+
{
158+
Name: "varlibdockercontainers",
159+
ReadOnly: true,
160+
MountPath: "/var/lib/docker/containers",
161+
},
162+
},
163+
Volumes: []corev1.Volume{
164+
{
165+
Name: "varlog",
166+
VolumeSource: corev1.VolumeSource{
167+
HostPath: &corev1.HostPathVolumeSource{
168+
Path: "/var/log",
169+
},
170+
},
171+
},
172+
{
173+
Name: "varlibdockercontainers",
174+
VolumeSource: corev1.VolumeSource{
175+
HostPath: &corev1.HostPathVolumeSource{
176+
Path: "/var/lib/docker/containers",
177+
},
178+
},
179+
},
180+
},
181+
},
182+
expectedError: nil,
183+
},
142184
}
143185

144186
for _, tt := range tests {

0 commit comments

Comments
 (0)