Skip to content

Commit b201949

Browse files
committed
Add tests for the optional field for secret/configmap environment variables
- Update protobuf definitions and regenerate bindings - Fix backend driver to handle Optional field correctly - Update tests and ensure backward compatibility Signed-off-by: ddalvi <[email protected]>
1 parent be50c3c commit b201949

File tree

8 files changed

+755
-151
lines changed

8 files changed

+755
-151
lines changed

backend/src/v2/driver/k8s.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,19 @@ func extendPodSpecPatch(
309309
// Get secret env information
310310
for _, secretAsEnv := range kubernetesExecutorConfig.GetSecretAsEnv() {
311311
for _, keyToEnv := range secretAsEnv.GetKeyToEnv() {
312+
secretKeySelector := &k8score.SecretKeySelector{
313+
Key: keyToEnv.GetSecretKey(),
314+
}
315+
316+
// Set Optional field when explicitly provided (true or false), leave nil when not specified
317+
if secretAsEnv.Optional != nil {
318+
secretKeySelector.Optional = secretAsEnv.Optional
319+
}
320+
312321
secretEnvVar := k8score.EnvVar{
313322
Name: keyToEnv.GetEnvVar(),
314323
ValueFrom: &k8score.EnvVarSource{
315-
SecretKeyRef: &k8score.SecretKeySelector{
316-
Key: keyToEnv.GetSecretKey(),
317-
},
324+
SecretKeyRef: secretKeySelector,
318325
},
319326
}
320327

@@ -392,12 +399,19 @@ func extendPodSpecPatch(
392399
// Get config map env information
393400
for _, configMapAsEnv := range kubernetesExecutorConfig.GetConfigMapAsEnv() {
394401
for _, keyToEnv := range configMapAsEnv.GetKeyToEnv() {
402+
configMapKeySelector := &k8score.ConfigMapKeySelector{
403+
Key: keyToEnv.GetConfigMapKey(),
404+
}
405+
406+
// Set Optional field when explicitly provided (true or false), leave nil when not specified
407+
if configMapAsEnv.Optional != nil {
408+
configMapKeySelector.Optional = configMapAsEnv.Optional
409+
}
410+
395411
configMapEnvVar := k8score.EnvVar{
396412
Name: keyToEnv.GetEnvVar(),
397413
ValueFrom: &k8score.EnvVarSource{
398-
ConfigMapKeyRef: &k8score.ConfigMapKeySelector{
399-
Key: keyToEnv.GetConfigMapKey(),
400-
},
414+
ConfigMapKeyRef: configMapKeySelector,
401415
},
402416
}
403417

backend/src/v2/driver/k8s_test.go

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,94 @@ func Test_extendPodSpecPatch_Secret(t *testing.T) {
602602
"param_1": structpb.NewStringValue("secret-name"),
603603
},
604604
},
605+
{
606+
"Valid - secret as env with optional true",
607+
&kubernetesplatform.KubernetesExecutorConfig{
608+
SecretAsEnv: []*kubernetesplatform.SecretAsEnv{
609+
{
610+
SecretNameParameter: inputParamConstant("my-secret"),
611+
KeyToEnv: []*kubernetesplatform.SecretAsEnv_SecretKeyToEnvMap{
612+
{
613+
SecretKey: "password",
614+
EnvVar: "SECRET_VAR",
615+
},
616+
},
617+
Optional: &[]bool{true}[0],
618+
},
619+
},
620+
},
621+
&k8score.PodSpec{
622+
Containers: []k8score.Container{
623+
{
624+
Name: "main",
625+
},
626+
},
627+
},
628+
&k8score.PodSpec{
629+
Containers: []k8score.Container{
630+
{
631+
Name: "main",
632+
Env: []k8score.EnvVar{
633+
{
634+
Name: "SECRET_VAR",
635+
ValueFrom: &k8score.EnvVarSource{
636+
SecretKeyRef: &k8score.SecretKeySelector{
637+
LocalObjectReference: k8score.LocalObjectReference{Name: "my-secret"},
638+
Key: "password",
639+
Optional: &[]bool{true}[0],
640+
},
641+
},
642+
},
643+
},
644+
},
645+
},
646+
},
647+
nil,
648+
},
649+
{
650+
"Valid - secret as env with optional false",
651+
&kubernetesplatform.KubernetesExecutorConfig{
652+
SecretAsEnv: []*kubernetesplatform.SecretAsEnv{
653+
{
654+
SecretNameParameter: inputParamConstant("my-secret"),
655+
KeyToEnv: []*kubernetesplatform.SecretAsEnv_SecretKeyToEnvMap{
656+
{
657+
SecretKey: "password",
658+
EnvVar: "SECRET_VAR",
659+
},
660+
},
661+
Optional: &[]bool{false}[0],
662+
},
663+
},
664+
},
665+
&k8score.PodSpec{
666+
Containers: []k8score.Container{
667+
{
668+
Name: "main",
669+
},
670+
},
671+
},
672+
&k8score.PodSpec{
673+
Containers: []k8score.Container{
674+
{
675+
Name: "main",
676+
Env: []k8score.EnvVar{
677+
{
678+
Name: "SECRET_VAR",
679+
ValueFrom: &k8score.EnvVarSource{
680+
SecretKeyRef: &k8score.SecretKeySelector{
681+
LocalObjectReference: k8score.LocalObjectReference{Name: "my-secret"},
682+
Key: "password",
683+
Optional: &[]bool{false}[0],
684+
},
685+
},
686+
},
687+
},
688+
},
689+
},
690+
},
691+
nil,
692+
},
605693
}
606694
for _, tt := range tests {
607695
t.Run(tt.name, func(t *testing.T) {
@@ -1011,6 +1099,94 @@ func Test_extendPodSpecPatch_ConfigMap(t *testing.T) {
10111099
"param_1": structpb.NewStringValue("cm-name"),
10121100
},
10131101
},
1102+
{
1103+
"Valid - config map as env with optional true",
1104+
&kubernetesplatform.KubernetesExecutorConfig{
1105+
ConfigMapAsEnv: []*kubernetesplatform.ConfigMapAsEnv{
1106+
{
1107+
ConfigMapNameParameter: inputParamConstant("my-cm"),
1108+
KeyToEnv: []*kubernetesplatform.ConfigMapAsEnv_ConfigMapKeyToEnvMap{
1109+
{
1110+
ConfigMapKey: "foo",
1111+
EnvVar: "CONFIG_MAP_VAR",
1112+
},
1113+
},
1114+
Optional: &[]bool{true}[0],
1115+
},
1116+
},
1117+
},
1118+
&k8score.PodSpec{
1119+
Containers: []k8score.Container{
1120+
{
1121+
Name: "main",
1122+
},
1123+
},
1124+
},
1125+
&k8score.PodSpec{
1126+
Containers: []k8score.Container{
1127+
{
1128+
Name: "main",
1129+
Env: []k8score.EnvVar{
1130+
{
1131+
Name: "CONFIG_MAP_VAR",
1132+
ValueFrom: &k8score.EnvVarSource{
1133+
ConfigMapKeyRef: &k8score.ConfigMapKeySelector{
1134+
LocalObjectReference: k8score.LocalObjectReference{Name: "my-cm"},
1135+
Key: "foo",
1136+
Optional: &[]bool{true}[0],
1137+
},
1138+
},
1139+
},
1140+
},
1141+
},
1142+
},
1143+
},
1144+
nil,
1145+
},
1146+
{
1147+
"Valid - config map as env with optional false",
1148+
&kubernetesplatform.KubernetesExecutorConfig{
1149+
ConfigMapAsEnv: []*kubernetesplatform.ConfigMapAsEnv{
1150+
{
1151+
ConfigMapNameParameter: inputParamConstant("my-cm"),
1152+
KeyToEnv: []*kubernetesplatform.ConfigMapAsEnv_ConfigMapKeyToEnvMap{
1153+
{
1154+
ConfigMapKey: "foo",
1155+
EnvVar: "CONFIG_MAP_VAR",
1156+
},
1157+
},
1158+
Optional: &[]bool{false}[0],
1159+
},
1160+
},
1161+
},
1162+
&k8score.PodSpec{
1163+
Containers: []k8score.Container{
1164+
{
1165+
Name: "main",
1166+
},
1167+
},
1168+
},
1169+
&k8score.PodSpec{
1170+
Containers: []k8score.Container{
1171+
{
1172+
Name: "main",
1173+
Env: []k8score.EnvVar{
1174+
{
1175+
Name: "CONFIG_MAP_VAR",
1176+
ValueFrom: &k8score.EnvVarSource{
1177+
ConfigMapKeyRef: &k8score.ConfigMapKeySelector{
1178+
LocalObjectReference: k8score.LocalObjectReference{Name: "my-cm"},
1179+
Key: "foo",
1180+
Optional: &[]bool{false}[0],
1181+
},
1182+
},
1183+
},
1184+
},
1185+
},
1186+
},
1187+
},
1188+
nil,
1189+
},
10141190
}
10151191
for _, tt := range tests {
10161192
t.Run(tt.name, func(t *testing.T) {

kubernetes_platform/go/kubernetesplatform/kubernetes_executor_config.pb.go

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kubernetes_platform/proto/kubernetes_executor_config.proto

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ message ConfigMapAsEnv {
175175
}
176176

177177
repeated ConfigMapKeyToEnvMap key_to_env = 2;
178-
// An optional boolean value indicating whether the ConfigMap must be defined.
179-
optional bool optional = 3;
180178

181179
// Name of the ConfigMap.
182-
ml_pipelines.TaskInputsSpec.InputParameterSpec config_map_name_parameter = 4;
180+
ml_pipelines.TaskInputsSpec.InputParameterSpec config_map_name_parameter = 3;
181+
// An optional boolean value indicating whether the ConfigMap must be defined.
182+
optional bool optional = 4;
183183
}
184184

185185
message GenericEphemeralVolume {

0 commit comments

Comments
 (0)