Skip to content

Commit

Permalink
Make Dynakube args and env vars override default operator values (#2023)
Browse files Browse the repository at this point in the history
* adds a test for overriding env vars

* adds a test for argument ordering

* reordered argument processing to give customer args the highest precedence

* reordered env var processing to give customer env vars the highest precedence

* fixes linting errors
  • Loading branch information
wepudt authored Aug 8, 2023
1 parent 863e55f commit b05974d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ func (statefulSetBuilder Builder) buildCommonEnvs() []corev1.EnvVar {
},
}},
}
envs = append(envs, statefulSetBuilder.capability.Properties().Env...)

if statefulSetBuilder.capability.Properties().Group != "" {
envs = append(envs, corev1.EnvVar{Name: consts.EnvDtGroup, Value: statefulSetBuilder.capability.Properties().Group})
Expand All @@ -212,6 +211,8 @@ func (statefulSetBuilder Builder) buildCommonEnvs() []corev1.EnvVar {
envs = append(envs, corev1.EnvVar{Name: consts.EnvDtHttpPort, Value: strconv.Itoa(consts.HttpContainerPort)})
}

envs = append(envs, statefulSetBuilder.capability.Properties().Env...)

return envs
}

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/dynakube/oneagent/daemonset/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
func (dsInfo *builderInfo) arguments() []string {
args := make([]string, 0)

args = dsInfo.appendHostInjectArgs(args)
args = dsInfo.appendProxyArg(args)
args = dsInfo.appendNetworkZoneArg(args)
args = appendOperatorVersionArg(args)
args = appendImmutableImageArgs(args)
args = dsInfo.appendHostInjectArgs(args)

return args
}
Expand Down
12 changes: 12 additions & 0 deletions src/controllers/dynakube/oneagent/daemonset/arguments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ func TestArguments(t *testing.T) {
assert.NotEmpty(t, podSpecs.Containers)
assert.Contains(t, podSpecs.Containers[0].Args, testValue)
})
t.Run("when injected arguments are provided then they are appended at the end of the arguments", func(t *testing.T) {
args := []string{testValue}
builder := builderInfo{
dynakube: &dynatracev1beta1.DynaKube{},
hostInjectSpec: &dynatracev1beta1.HostInjectSpec{Args: args},
}

arguments := builder.arguments()
expectedDefaultArguments := appendImmutableImageArgs(appendOperatorVersionArg([]string{}))
expectedDefaultArguments = append(expectedDefaultArguments, testValue)
assert.Equal(t, expectedDefaultArguments, arguments)
})
}

func TestPodSpec_Arguments(t *testing.T) {
Expand Down
30 changes: 30 additions & 0 deletions src/controllers/dynakube/oneagent/daemonset/env_vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,36 @@ func TestEnvironmentVariables(t *testing.T) {
assertProxyEnv(t, envVars, dynakube)
assertReadOnlyEnv(t, envVars)
})
t.Run("when injected envvars are provided then they will not be overridden", func(t *testing.T) {
potentiallyOverriddenEnvVars := []corev1.EnvVar{
{Name: dtNodeName, Value: testValue},
{Name: dtClusterId, Value: testValue},
{Name: deploymentmetadata.EnvDtDeploymentMetadata, Value: testValue},
{Name: deploymentmetadata.EnvDtOperatorVersion, Value: testValue},
{Name: connectioninfo.EnvDtTenant, Value: testValue},
{Name: proxy, Value: testValue},
{Name: oneagentReadOnlyMode, Value: testValue},
}
builder := builderInfo{
dynakube: &dynatracev1beta1.DynaKube{},
hostInjectSpec: &dynatracev1beta1.HostInjectSpec{Env: potentiallyOverriddenEnvVars},
}
envVars := builder.environmentVariables()

assertEnvVarNameAndValue(t, envVars, dtNodeName, testValue)
assertEnvVarNameAndValue(t, envVars, dtClusterId, testValue)
assertEnvVarNameAndValue(t, envVars, deploymentmetadata.EnvDtDeploymentMetadata, testValue)
assertEnvVarNameAndValue(t, envVars, deploymentmetadata.EnvDtOperatorVersion, testValue)
assertEnvVarNameAndValue(t, envVars, connectioninfo.EnvDtTenant, testValue)
assertEnvVarNameAndValue(t, envVars, proxy, testValue)
assertEnvVarNameAndValue(t, envVars, oneagentReadOnlyMode, testValue)
})
}

func assertEnvVarNameAndValue(t *testing.T, envVars []corev1.EnvVar, name, value string) {
env := kubeobjects.FindEnvVar(envVars, name)
assert.Equal(t, name, env.Name)
assert.Equal(t, value, env.Value)
}

func TestAddNodeNameEnv(t *testing.T) {
Expand Down

0 comments on commit b05974d

Please sign in to comment.