Skip to content

Commit

Permalink
Add global environment variable config (#1406)
Browse files Browse the repository at this point in the history
* add global environment variable

* add tests for global env

* update comments

* update formatting, logging, config docs

* update CRD

* remove global env log statement
  • Loading branch information
swang392 authored and mftoure committed Oct 3, 2024
1 parent 88a3229 commit 7c02985
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 3 deletions.
6 changes: 6 additions & 0 deletions api/datadoghq/v2alpha1/datadogagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,12 @@ type GlobalConfig struct {
// +listType=set
Tags []string `json:"tags,omitempty"`

//Env contains a list of environment variables that are set for all Agents.
// +optional
// +listType=map
// +listMapKey=name
Env []corev1.EnvVar `json:"env,omitempty"`

// OriginDetectionUnified defines the origin detection unified mechanism behavior.
// +optional
OriginDetectionUnified *OriginDetectionUnified `json:"originDetectionUnified,omitempty"`
Expand Down
6 changes: 6 additions & 0 deletions api/datadoghq/v2alpha1/test/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ func (builder *DatadogAgentBuilder) WithName(name string) *DatadogAgentBuilder {
return builder
}

// Global environment variable
func (builder *DatadogAgentBuilder) WithEnvVars(envs []corev1.EnvVar) *DatadogAgentBuilder {
builder.datadogAgent.Spec.Global.Env = envs
return builder
}

// Dogstatsd
func (builder *DatadogAgentBuilder) initDogstatsd() {
if builder.datadogAgent.Spec.Features.Dogstatsd == nil {
Expand Down
7 changes: 7 additions & 0 deletions api/datadoghq/v2alpha1/zz_generated.deepcopy.go

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

106 changes: 106 additions & 0 deletions config/crd/bases/v1/datadoghq.com_datadogagents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,112 @@ spec:
description: URL defines the endpoint URL.
type: string
type: object
env:
description: Env contains a list of environment variables that are set for all Agents.
items:
description: EnvVar represents an environment variable present in a Container.
properties:
name:
description: Name of the environment variable. Must be a C_IDENTIFIER.
type: string
value:
description: |-
Variable references $(VAR_NAME) are expanded
using the previously defined environment variables in the container and
any service environment variables. If a variable cannot be resolved,
the reference in the input string will be unchanged. Double $$ are reduced
to a single $, which allows for escaping the $(VAR_NAME) syntax: i.e.
"$$(VAR_NAME)" will produce the string literal "$(VAR_NAME)".
Escaped references will never be expanded, regardless of whether the variable
exists or not.
Defaults to "".
type: string
valueFrom:
description: Source for the environment variable's value. Cannot be used if value is not empty.
properties:
configMapKeyRef:
description: Selects a key of a ConfigMap.
properties:
key:
description: The key to select.
type: string
name:
description: |-
Name of the referent.
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?
type: string
optional:
description: Specify whether the ConfigMap or its key must be defined
type: boolean
required:
- key
type: object
x-kubernetes-map-type: atomic
fieldRef:
description: |-
Selects a field of the pod: supports metadata.name, metadata.namespace, `metadata.labels['<KEY>']`, `metadata.annotations['<KEY>']`,
spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP, status.podIPs.
properties:
apiVersion:
description: Version of the schema the FieldPath is written in terms of, defaults to "v1".
type: string
fieldPath:
description: Path of the field to select in the specified API version.
type: string
required:
- fieldPath
type: object
x-kubernetes-map-type: atomic
resourceFieldRef:
description: |-
Selects a resource of the container: only resources limits and requests
(limits.cpu, limits.memory, limits.ephemeral-storage, requests.cpu, requests.memory and requests.ephemeral-storage) are currently supported.
properties:
containerName:
description: 'Container name: required for volumes, optional for env vars'
type: string
divisor:
anyOf:
- type: integer
- type: string
description: Specifies the output format of the exposed resources, defaults to "1"
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
resource:
description: 'Required: resource to select'
type: string
required:
- resource
type: object
x-kubernetes-map-type: atomic
secretKeyRef:
description: Selects a key of a secret in the pod's namespace
properties:
key:
description: The key of the secret to select from. Must be a valid secret key.
type: string
name:
description: |-
Name of the referent.
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?
type: string
optional:
description: Specify whether the Secret or its key must be defined
type: boolean
required:
- key
type: object
x-kubernetes-map-type: atomic
type: object
required:
- name
type: object
type: array
x-kubernetes-list-map-keys:
- name
x-kubernetes-list-type: map
fips:
description: FIPS contains configuration used to customize the FIPS proxy sidecar.
properties:
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.v2alpha1.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ spec:
| global.endpoint.credentials.appSecret.keyName | KeyName is the key of the secret to use. |
| global.endpoint.credentials.appSecret.secretName | SecretName is the name of the secret. |
| global.endpoint.url | URL defines the endpoint URL. |
| global.env | Env contains a list of environment variables that are set for all Agents. |
| global.fips.customFIPSConfig.configData | ConfigData corresponds to the configuration file content. |
| global.fips.customFIPSConfig.configMap.items | Items maps a ConfigMap data `key` to a file `path` mount. |
| global.fips.customFIPSConfig.configMap.name | Name is the name of the ConfigMap. |
Expand Down
7 changes: 7 additions & 0 deletions internal/controller/datadogagent/override/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ func applyGlobalSettings(logger logr.Logger, manager feature.PodTemplateManagers
}
}

// Env is a list of custom global variables that are set across all agents.
if config.Env != nil {
for _, envVar := range config.Env {
manager.EnvVar().AddEnvVar(&envVar)
}
}

if config.OriginDetectionUnified != nil && config.OriginDetectionUnified.Enabled != nil {
manager.EnvVar().AddEnvVar(&corev1.EnvVar{
Name: apicommon.DDOriginDetectionUnified,
Expand Down
29 changes: 29 additions & 0 deletions internal/controller/datadogagent/override/global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,35 @@ func TestNodeAgentComponenGlobalSettings(t *testing.T) {
wantVolumes: emptyVolumes,
want: assertAll,
},
{
name: "Global environment variable configured",
singleContainerStrategyEnabled: false,
dda: v2alpha1test.NewDatadogAgentBuilder().
WithEnvVars([]corev1.EnvVar{
{
Name: "envA",
Value: "valueA",
},
{
Name: "envB",
Value: "valueB",
},
}).
BuildWithDefaults(),
wantEnvVars: getExpectedEnvVars([]*corev1.EnvVar{
{
Name: "envA",
Value: "valueA",
},
{
Name: "envB",
Value: "valueB",
},
}...),
wantVolumeMounts: emptyVolumeMounts,
wantVolumes: emptyVolumes,
want: assertAll,
},
}

for _, tt := range tests {
Expand Down
16 changes: 13 additions & 3 deletions internal/controller/testutils/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,19 @@ func NewDatadogAgentWithGlobalConfigSettings(namespace string, name string) v2al
AppKey: apiutils.NewStringPointer("my-app-key"),
},
},
Registry: apiutils.NewStringPointer("my-custom-registry"),
LogLevel: apiutils.NewStringPointer("INFO"),
Tags: []string{"tagA:valA", "tagB:valB"},
Registry: apiutils.NewStringPointer("my-custom-registry"),
LogLevel: apiutils.NewStringPointer("INFO"),
Tags: []string{"tagA:valA", "tagB:valB"},
Env: []v1.EnvVar{
{
Name: "some-envA",
Value: "some-valA",
},
{
Name: "some-envB",
Value: "some-valB",
},
},
PodLabelsAsTags: map[string]string{"some-label": "some-tag"},
PodAnnotationsAsTags: map[string]string{"some-annotation": "some-tag"},
NodeLabelsAsTags: map[string]string{"some-label": "some-tag"},
Expand Down

0 comments on commit 7c02985

Please sign in to comment.