Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global environment variable config #1406

Merged
merged 6 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/datadoghq/v2alpha1/datadogagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,8 @@ type GlobalConfig struct {
// +listType=set
Tags []string `json:"tags,omitempty"`

Env []corev1.EnvVar `json:"env,omitempty"`
swang392 marked this conversation as resolved.
Show resolved Hide resolved

// 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
}

// TODO: add generic with global env vars, takes in the test environment variables and add to DatadogAgentBuilder
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.

102 changes: 102 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,108 @@ spec:
description: URL defines the endpoint URL.
type: string
type: object
env:
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
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 | |
swang392 marked this conversation as resolved.
Show resolved Hide resolved
| 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
8 changes: 8 additions & 0 deletions internal/controller/datadogagent/override/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ func applyGlobalSettings(logger logr.Logger, manager feature.PodTemplateManagers
}
}

if config.Env != nil {
// do i need to unmarshal? there is an umarshal function in corev1.Env
logger.Info(fmt.Sprintf("adding custom env var %s", config.Env))
swang392 marked this conversation as resolved.
Show resolved Hide resolved
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
8 changes: 5 additions & 3 deletions internal/controller/testutils/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,11 @@ 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"},
swang392 marked this conversation as resolved.
Show resolved Hide resolved
{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
Loading