Skip to content
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
16 changes: 14 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,15 @@ func customizeAffinityArchitectures(affinity *corev1.Affinity, affinityConfig co
}
}

func GetEnvVarValue(envs []corev1.EnvVar, name string, defaultValue string) (string, bool) {
for _, env := range envs {
if env.Name == name {
return env.Value, true
}
}
return defaultValue, false
}

// CustomizePodSpec ...
func CustomizePodSpec(pts *corev1.PodTemplateSpec, ba common.BaseComponent) {
obj := ba.(metav1.Object)
Expand Down Expand Up @@ -692,7 +701,10 @@ func CustomizePodSpec(pts *corev1.PodTemplateSpec, ba common.BaseComponent) {
if ba.GetManageTLS() == nil || *ba.GetManageTLS() || ba.GetService().GetCertificateSecretRef() != nil {

secretName := ba.GetStatus().GetReferences()[common.StatusReferenceCertSecretName]
appContainer.Env = append(appContainer.Env, corev1.EnvVar{Name: "TLS_DIR", Value: "/etc/x509/certs"})
tlsDir, found := GetEnvVarValue(appContainer.Env, "TLS_DIR", "/etc/x509/certs")
if !found {
appContainer.Env = append(appContainer.Env, corev1.EnvVar{Name: "TLS_DIR", Value: tlsDir})
}
pts.Spec.Volumes = append(pts.Spec.Volumes, corev1.Volume{
Name: "svc-certificate",
VolumeSource: corev1.VolumeSource{
Expand All @@ -703,7 +715,7 @@ func CustomizePodSpec(pts *corev1.PodTemplateSpec, ba common.BaseComponent) {
})
appContainer.VolumeMounts = append(appContainer.VolumeMounts, corev1.VolumeMount{
Name: "svc-certificate",
MountPath: "/etc/x509/certs",
MountPath: tlsDir,
ReadOnly: true,
})
}
Expand Down
26 changes: 26 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,32 @@ func TestShouldDeleteRoute(t *testing.T) {
verifyTests(testCR, t)
}

func TestGetEnvVarValue(t *testing.T) {
logger := zap.New()
logf.SetLogger(logger)

envs := []corev1.EnvVar{
{Name: "NONEMPTY_VAR", Value: "/custom/path"},
{Name: "EMPTY_VAR", Value: ""},
}

// Helper to run func for Test struct
val1, found1 := GetEnvVarValue(envs, "NONEMPTY_VAR", "/default/path")
val2, found2 := GetEnvVarValue(envs, "EMPTY_VAR", "/default/path")
val3, found3 := GetEnvVarValue(envs, "UNSET_VAR", "/default/path")

testGEVV := []Test{
{"Retrieve existing variable set to non-empty value", "/custom/path", val1},
{"Retrieve found status of existing variable set to non-empty value", true, found1},
{"Retrieve existing variable set to empty value", "", val2},
{"Retrieve found status of existing variable set to empty value", true, found2},
{"Handle value for an unset variable", "/default/path", val3},
{"Retrieve found status for an unset variable", false, found3},
}

verifyTests(testGEVV, t)
}

// Helper Functions
// Unconditionally set the proper tags for an enabled runtime omponent
func createAppDefinitionTags(app *appstacksv1.RuntimeComponent) (map[string]string, map[string]string) {
Expand Down