Skip to content

Commit

Permalink
Enabling collector to mount multiple configMaps (open-telemetry#1989)
Browse files Browse the repository at this point in the history
* Enabling collector to mount multiple configMaps

Signed-off-by: Yuri Sa <[email protected]>

* Fixed lints

Signed-off-by: Yuri Sa <[email protected]>

* Fixed new copies

Signed-off-by: Yuri Sa <[email protected]>

* Removed unnecessary kustomization

Signed-off-by: Yuri Sa <[email protected]>

* Removed unnecessary kustomization

Signed-off-by: Yuri Sa <[email protected]>

* Implemented path on ConfigMapsSpec

Signed-off-by: Yuri Sa <[email protected]>

* Implemented path on ConfigMapsSpec

Signed-off-by: Yuri Sa <[email protected]>

* Removed the name's append on VolumeMount

Signed-off-by: Yuri Sa <[email protected]>

* Fix format

Signed-off-by: Yuri Sa <[email protected]>

* Fix bundle

Signed-off-by: Yuri Sa <[email protected]>

* Added e2e tests

Signed-off-by: Yuri Sa <[email protected]>

* Added e2e tests

Signed-off-by: Yuri Sa <[email protected]>

* Fixed comments regarding validations

* Fixed comments regarding validations

---------

Signed-off-by: Yuri Sa <[email protected]>
  • Loading branch information
yuriolisa authored Sep 18, 2023
1 parent afa52f4 commit 69d0747
Show file tree
Hide file tree
Showing 14 changed files with 299 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .chloggen/add-more-configmaps.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'enhancement'

# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
component: operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Allow the collector CRD to specify a list of configmaps to mount

# One or more tracking issues related to the change
issues: [1819]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
11 changes: 11 additions & 0 deletions apis/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ type OpenTelemetryCollectorSpec struct {
// This is only relevant to statefulset, and deployment mode
// +optional
TopologySpreadConstraints []v1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`

// ConfigMaps is a list of ConfigMaps in the same namespace as the OpenTelemetryCollector
// object, which shall be mounted into the Collector Pods.
// Each ConfigMap will be added to the Collector's Deployments as a volume named `configmap-<configmap-name>`.
ConfigMaps []ConfigMapsSpec `json:"configmaps,omitempty"`
}

// OpenTelemetryTargetAllocator defines the configurations for the Prometheus target allocator.
Expand Down Expand Up @@ -506,6 +511,12 @@ type MetricSpec struct {
Pods *autoscalingv2.PodsMetricSource `json:"pods,omitempty"`
}

type ConfigMapsSpec struct {
// Configmap defines name and path where the configMaps should be mounted.
Name string `json:"name"`
MountPath string `json:"mountpath"`
}

func init() {
SchemeBuilder.Register(&OpenTelemetryCollector{}, &OpenTelemetryCollectorList{})
}
20 changes: 20 additions & 0 deletions apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ metadata:
categories: Logging & Tracing,Monitoring
certified: "false"
containerImage: ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator
createdAt: "2023-09-07T09:45:03Z"
createdAt: "2023-09-18T13:46:11Z"
description: Provides the OpenTelemetry components, including the Collector
operators.operatorframework.io/builder: operator-sdk-v1.29.0
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
Expand Down
17 changes: 17 additions & 0 deletions bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,23 @@ spec:
configuration. Refer to the OpenTelemetry Collector documentation
for details.
type: string
configmaps:
description: ConfigMaps is a list of ConfigMaps in the same namespace
as the OpenTelemetryCollector object, which shall be mounted into
the Collector Pods.
items:
properties:
mountpath:
type: string
name:
description: Configmap defines name and path where the configMaps
should be mounted.
type: string
required:
- mountpath
- name
type: object
type: array
env:
description: ENV vars to set on the OpenTelemetry Collector's Pods.
These can then in certain cases be consumed in the config file for
Expand Down
17 changes: 17 additions & 0 deletions config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,23 @@ spec:
configuration. Refer to the OpenTelemetry Collector documentation
for details.
type: string
configmaps:
description: ConfigMaps is a list of ConfigMaps in the same namespace
as the OpenTelemetryCollector object, which shall be mounted into
the Collector Pods.
items:
properties:
mountpath:
type: string
name:
description: Configmap defines name and path where the configMaps
should be mounted.
type: string
required:
- mountpath
- name
type: object
type: array
env:
description: ENV vars to set on the OpenTelemetry Collector's Pods.
These can then in certain cases be consumed in the config file for
Expand Down
41 changes: 41 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3688,6 +3688,13 @@ OpenTelemetryCollectorSpec defines the desired state of OpenTelemetryCollector.
Config is the raw JSON to be used as the collector's configuration. Refer to the OpenTelemetry Collector documentation for details.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b><a href="#opentelemetrycollectorspecconfigmapsindex">configmaps</a></b></td>
<td>[]object</td>
<td>
ConfigMaps is a list of ConfigMaps in the same namespace as the OpenTelemetryCollector object, which shall be mounted into the Collector Pods.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b><a href="#opentelemetrycollectorspecenvindex">env</a></b></td>
<td>[]object</td>
Expand Down Expand Up @@ -8082,6 +8089,40 @@ target specifies the target value for the given metric
</table>


### OpenTelemetryCollector.spec.configmaps[index]
<sup><sup>[↩ Parent](#opentelemetrycollectorspec)</sup></sup>





<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
<th>Required</th>
</tr>
</thead>
<tbody><tr>
<td><b>mountpath</b></td>
<td>string</td>
<td>
<br/>
</td>
<td>true</td>
</tr><tr>
<td><b>name</b></td>
<td>string</td>
<td>
Configmap defines name and path where the configMaps should be mounted.<br/>
</td>
<td>true</td>
</tr></tbody>
</table>


### OpenTelemetryCollector.spec.env[index]
<sup><sup>[↩ Parent](#opentelemetrycollectorspec)</sup></sup>

Expand Down
10 changes: 10 additions & 0 deletions internal/manifests/collector/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package collector

import (
"fmt"
"path"
"sort"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -104,6 +105,15 @@ func Container(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelem
},
})

if len(otelcol.Spec.ConfigMaps) > 0 {
for keyCfgMap := range otelcol.Spec.ConfigMaps {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: naming.ConfigMapExtra(otelcol.Spec.ConfigMaps[keyCfgMap].Name),
MountPath: path.Join("/var/conf", otelcol.Spec.ConfigMaps[keyCfgMap].MountPath, naming.ConfigMapExtra(otelcol.Spec.ConfigMaps[keyCfgMap].Name)),
})
}
}

if otelcol.Spec.TargetAllocator.Enabled {
// We need to add a SHARD here so the collector is able to keep targets after the hashmod operation which is
// added by default by the Prometheus operator's config generator.
Expand Down
26 changes: 26 additions & 0 deletions internal/manifests/collector/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,32 @@ func TestContainerCustomVolumes(t *testing.T) {
assert.Equal(t, "custom-volume-mount", c.VolumeMounts[1].Name)
}

func TestContainerCustomConfigMapsVolumes(t *testing.T) {
// prepare
otelcol := v1alpha1.OpenTelemetryCollector{
Spec: v1alpha1.OpenTelemetryCollectorSpec{
ConfigMaps: []v1alpha1.ConfigMapsSpec{{
Name: "test",
MountPath: "/",
}, {
Name: "test2",
MountPath: "/dir",
}},
},
}
cfg := config.New()

// test
c := Container(cfg, logger, otelcol, true)

// verify
assert.Len(t, c.VolumeMounts, 3)
assert.Equal(t, "configmap-test", c.VolumeMounts[1].Name)
assert.Equal(t, "/var/conf/configmap-test", c.VolumeMounts[1].MountPath)
assert.Equal(t, "configmap-test2", c.VolumeMounts[2].Name)
assert.Equal(t, "/var/conf/dir/configmap-test2", c.VolumeMounts[2].MountPath)
}

func TestContainerCustomSecurityContext(t *testing.T) {
// default config without security context
c1 := Container(config.New(), logger, v1alpha1.OpenTelemetryCollector{Spec: v1alpha1.OpenTelemetryCollectorSpec{}}, true)
Expand Down
15 changes: 15 additions & 0 deletions internal/manifests/collector/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,20 @@ func Volumes(cfg config.Config, otelcol v1alpha1.OpenTelemetryCollector) []corev
volumes = append(volumes, otelcol.Spec.Volumes...)
}

if len(otelcol.Spec.ConfigMaps) > 0 {
for keyCfgMap := range otelcol.Spec.ConfigMaps {
volumes = append(volumes, corev1.Volume{
Name: naming.ConfigMapExtra(otelcol.Spec.ConfigMaps[keyCfgMap].Name),
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: otelcol.Spec.ConfigMaps[keyCfgMap].Name,
},
},
},
})
}
}

return volumes
}
26 changes: 26 additions & 0 deletions internal/manifests/collector/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,29 @@ func TestVolumeAllowsMoreToBeAdded(t *testing.T) {
// check that it's the otc-internal volume, with the config map
assert.Equal(t, "my-volume", volumes[1].Name)
}

func TestVolumeWithMoreConfigMaps(t *testing.T) {
// prepare
otelcol := v1alpha1.OpenTelemetryCollector{
Spec: v1alpha1.OpenTelemetryCollectorSpec{
ConfigMaps: []v1alpha1.ConfigMapsSpec{{
Name: "configmap-test",
MountPath: "/",
}, {
Name: "configmap-test2",
MountPath: "/dir",
}},
},
}
cfg := config.New()

// test
volumes := Volumes(cfg, otelcol)

// verify
assert.Len(t, volumes, 3)

// check if the volume with the configmap prefix is mounted after defining the config map.
assert.Equal(t, "configmap-configmap-test", volumes[1].Name)
assert.Equal(t, "configmap-configmap-test2", volumes[2].Name)
}
5 changes: 5 additions & 0 deletions internal/naming/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func ConfigMapVolume() string {
return "otc-internal"
}

// ConfigMapExtra returns the prefix to use for the extras mounted configmaps in the pod.
func ConfigMapExtra(extraConfigMapName string) string {
return DNSName(Truncate("configmap-%s", 63, extraConfigMapName))
}

// TAConfigMapVolume returns the name to use for the config map's volume in the TargetAllocator pod.
func TAConfigMapVolume() string {
return "ta-internal"
Expand Down
51 changes: 51 additions & 0 deletions tests/e2e/multiple-configmaps/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: mount-test1
data:
export.ini: |
- EXPORT=true
---
apiVersion: v1
kind: ConfigMap
metadata:
name: mount-test2
data:
otel.ini: |
- OTEL_ENDPOINT=localhost
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: simplest-with-configmaps-collector
spec:
template:
spec:
volumes:
- name: otc-internal
configMap:
name: simplest-with-configmaps-collector
items:
- key: collector.yaml
path: collector.yaml
defaultMode: 420
- name: configmap-mount-test1
configMap:
name: mount-test1
defaultMode: 420
- name: configmap-mount-test2
configMap:
name: mount-test2
defaultMode: 420
containers:
- name: otc-container
volumeMounts:
- name: otc-internal
mountPath: /conf
- name: configmap-mount-test1
mountPath: /var/conf/configmap-mount-test1
- name: configmap-mount-test2
mountPath: /var/conf/dir/configmap-mount-test2
status:
readyReplicas: 1
Loading

0 comments on commit 69d0747

Please sign in to comment.