Skip to content

Add DD_AGENT_IPC_* env vars #1604

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

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions api/datadoghq/v2alpha1/envvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ const (
DDSystemProbeExternal = "DD_SYSTEM_PROBE_EXTERNAL"
DDSystemProbeSocket = "DD_SYSPROBE_SOCKET"
DDTags = "DD_TAGS"
DDAgentIpcPort = "DD_AGENT_IPC_PORT"
DDAgentIpcConfigRefreshInterval = "DD_AGENT_IPC_CONFIG_REFRESH_INTERVAL"

// otelcollector core agent configs
DDOtelCollectorCoreConfigEnabled = "DD_OTELCOLLECTOR_ENABLED"
DDOtelCollectorCoreConfigExtensionURL = "DD_OTELCOLLECTOR_EXTENSION_URL"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,25 @@ func (o *otelCollectorFeature) ManageNodeAgent(managers feature.PodTemplateManag
managers.Port().AddPortToContainer(apicommon.OtelAgent, port)
}

// (todo: mackjmr): remove this once IPC port is enabled by default. Enabling this port is required to fetch the API key from
// core agent when secrets backend is used.
agentIpcPortEnvVar := &corev1.EnvVar{
Name: v2alpha1.DDAgentIpcPort,
Value: "5009",
}
agentIpcConfigRefreshIntervalEnvVar := &corev1.EnvVar{
Name: v2alpha1.DDAgentIpcConfigRefreshInterval,
Value: "60",
}
// don't set env var if it was already set by user.
mergeFunc := func(current, newEnv *corev1.EnvVar) (*corev1.EnvVar, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this can be tested by the current test framework, as the env vars added via WithEnvVars helper don't make it into container env vars.

e.g.

		{
			Name: "otel agent user provided ipc port",
			DDA: testutils.NewDatadogAgentBuilder().
				WithOTelCollectorEnabled(true).
				WithEnvVars([]corev1.EnvVar{
					{
						Name:  v2alpha1.DDAgentIpcPort,
						Value: "3333",
					},
				}).
				Build(),
			WantConfigure:        true,
			WantDependenciesFunc: testExpectedDepsCreatedCM,
			Agent: testExpectedAgent(apicommon.OtelAgent, defaultExpectedPorts, defaultLocalObjectReferenceName,
				expectedEnvVars{
					agent_ipc_port: expectedEnvVar{
						present: true,
						value:   "3333",
					},
					agent_ipc_refresh: expectedEnvVar{
						present: true,
						value:   "60",
					},
				},
			),
		},

will fail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WithEnvVars adds env variables to global handled in

if config.Env != nil {
for _, envVar := range config.Env {
manager.EnvVar().AddEnvVar(&envVar)
}
}
which isn't executed by feature unit tests.

controlelr_v2_test.go could be used for this if you want to test outcome of whole reconcile.

return current, nil
Copy link
Contributor

@levan-m levan-m Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want to add the two env vars if current doesn't contain them?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding based on code is that the mergeFunc is only run if the container already contains the env var we are trying to add. In which case, I want to prioritize the one already set, which is why I return current.

If the env var is not present in the container, then the merge func is not run, and the env vars are added.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, that's right. Thanks for clarifying.

}
for _, container := range []apicommon.AgentContainerName{apicommon.CoreAgentContainerName, apicommon.OtelAgent} {
managers.EnvVar().AddEnvVarToContainerWithMergeFunc(container, agentIpcPortEnvVar, mergeFunc)
managers.EnvVar().AddEnvVarToContainerWithMergeFunc(container, agentIpcConfigRefreshIntervalEnvVar, mergeFunc)
}

var enableEnvVar *corev1.EnvVar
if o.coreAgentConfig.enabled != nil {
if *o.coreAgentConfig.enabled {
Expand Down Expand Up @@ -238,6 +257,7 @@ func (o *otelCollectorFeature) ManageNodeAgent(managers feature.PodTemplateManag
Value: *o.coreAgentConfig.extension_url,
})
}

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type expectedPorts struct {
}

type expectedEnvVars struct {
agent_ipc_port expectedEnvVar
agent_ipc_refresh expectedEnvVar
enabled expectedEnvVar
extension_timeout expectedEnvVar
extension_url expectedEnvVar
Expand All @@ -43,13 +45,32 @@ var (
}
defaultLocalObjectReferenceName = "-otel-agent-config"
defaultExpectedEnvVars = expectedEnvVars{
agent_ipc_port: expectedEnvVar{
present: true,
value: "5009",
},
agent_ipc_refresh: expectedEnvVar{
present: true,
value: "60",
},
enabled: expectedEnvVar{
present: true,
value: "true",
},
extension_timeout: expectedEnvVar{},
extension_url: expectedEnvVar{},
}

onlyIpcEnvVars = expectedEnvVars{
agent_ipc_port: expectedEnvVar{
present: true,
value: "5009",
},
agent_ipc_refresh: expectedEnvVar{
present: true,
value: "60",
},
}
)

var defaultAnnotations = map[string]string{"checksum/otel_agent-custom-config": "07f4530ba2b36a9279f070daa769454e"}
Expand Down Expand Up @@ -138,7 +159,7 @@ func Test_otelCollectorFeature_Configure(t *testing.T) {
Build(),
WantConfigure: true,
WantDependenciesFunc: testExpectedDepsCreatedCM,
Agent: testExpectedAgent(apicommon.OtelAgent, defaultExpectedPorts, defaultLocalObjectReferenceName, expectedEnvVars{}, defaultAnnotations),
Agent: testExpectedAgent(apicommon.OtelAgent, defaultExpectedPorts, defaultLocalObjectReferenceName, onlyIpcEnvVars, defaultAnnotations),
},
{
Name: "otel agent coreconfig extensionTimeout",
Expand All @@ -150,6 +171,14 @@ func Test_otelCollectorFeature_Configure(t *testing.T) {
WantConfigure: true,
WantDependenciesFunc: testExpectedDepsCreatedCM,
Agent: testExpectedAgent(apicommon.OtelAgent, defaultExpectedPorts, defaultLocalObjectReferenceName, expectedEnvVars{
agent_ipc_port: expectedEnvVar{
present: true,
value: "5009",
},
agent_ipc_refresh: expectedEnvVar{
present: true,
value: "60",
},
extension_timeout: expectedEnvVar{
present: true,
value: "13",
Expand All @@ -167,6 +196,14 @@ func Test_otelCollectorFeature_Configure(t *testing.T) {
WantConfigure: true,
WantDependenciesFunc: testExpectedDepsCreatedCM,
Agent: testExpectedAgent(apicommon.OtelAgent, defaultExpectedPorts, defaultLocalObjectReferenceName, expectedEnvVars{
agent_ipc_port: expectedEnvVar{
present: true,
value: "5009",
},
agent_ipc_refresh: expectedEnvVar{
present: true,
value: "60",
},
extension_url: expectedEnvVar{
present: true,
value: "https://localhost:1234",
Expand All @@ -185,6 +222,14 @@ func Test_otelCollectorFeature_Configure(t *testing.T) {
WantConfigure: true,
WantDependenciesFunc: testExpectedDepsCreatedCM,
Agent: testExpectedAgent(apicommon.OtelAgent, defaultExpectedPorts, defaultLocalObjectReferenceName, expectedEnvVars{
agent_ipc_port: expectedEnvVar{
present: true,
value: "5009",
},
agent_ipc_refresh: expectedEnvVar{
present: true,
value: "60",
},
extension_url: expectedEnvVar{
present: true,
value: "https://localhost:1234",
Expand Down Expand Up @@ -260,6 +305,20 @@ func testExpectedAgent(agentContainerName apicommon.AgentContainerName, expected
// check env vars
wantEnvVars := []*corev1.EnvVar{}

if expectedEnvVars.agent_ipc_port.present {
wantEnvVars = append(wantEnvVars, &corev1.EnvVar{
Name: v2alpha1.DDAgentIpcPort,
Value: expectedEnvVars.agent_ipc_port.value,
})
}

if expectedEnvVars.agent_ipc_refresh.present {
wantEnvVars = append(wantEnvVars, &corev1.EnvVar{
Name: v2alpha1.DDAgentIpcConfigRefreshInterval,
Value: expectedEnvVars.agent_ipc_refresh.value,
})
}

if expectedEnvVars.enabled.present {
wantEnvVars = append(wantEnvVars, &corev1.EnvVar{
Name: v2alpha1.DDOtelCollectorCoreConfigEnabled,
Expand Down
Loading