fix unit test bug

This commit is contained in:
Daniel Vega-Myhre 2023-07-13 22:38:21 +00:00
parent a1a5f49bb9
commit 037091284e
2 changed files with 18 additions and 4 deletions

View File

@ -26,8 +26,10 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apiserver/pkg/storage/names"
"k8s.io/apiserver/pkg/util/feature"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/features"
)
const (
@ -294,11 +296,17 @@ func addCompletionIndexEnvVariable(container *v1.Container) {
return
}
}
var fieldPath string
if feature.DefaultFeatureGate.Enabled(features.PodIndexLabel) {
fieldPath = fmt.Sprintf("metadata.labels['%s']", batch.JobCompletionIndexAnnotation)
} else {
fieldPath = fmt.Sprintf("metadata.annotations['%s']", batch.JobCompletionIndexAnnotation)
}
container.Env = append(container.Env, v1.EnvVar{
Name: completionIndexEnvName,
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
FieldPath: fmt.Sprintf("metadata.labels['%s']", batch.JobCompletionIndexAnnotation),
FieldPath: fieldPath,
},
},
})

View File

@ -973,7 +973,7 @@ func checkIndexedJobPods(t *testing.T, control *controller.FakePodControl, wantI
t.Helper()
gotIndexes := sets.New[int]()
for _, p := range control.Templates {
checkJobCompletionEnvVariable(t, &p.Spec)
checkJobCompletionEnvVariable(t, &p.Spec, podIndexLabelDisabled)
if !podIndexLabelDisabled {
checkJobCompletionLabel(t, &p)
}
@ -4418,14 +4418,20 @@ func checkJobCompletionLabel(t *testing.T, p *v1.PodTemplateSpec) {
}
}
func checkJobCompletionEnvVariable(t *testing.T, spec *v1.PodSpec) {
func checkJobCompletionEnvVariable(t *testing.T, spec *v1.PodSpec, podIndexLabelDisabled bool) {
t.Helper()
var fieldPath string
if podIndexLabelDisabled {
fieldPath = fmt.Sprintf("metadata.annotations['%s']", batch.JobCompletionIndexAnnotation)
} else {
fieldPath = fmt.Sprintf("metadata.labels['%s']", batch.JobCompletionIndexAnnotation)
}
want := []v1.EnvVar{
{
Name: "JOB_COMPLETION_INDEX",
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
FieldPath: fmt.Sprintf("metadata.annotations['%s']", batch.JobCompletionIndexAnnotation),
FieldPath: fieldPath,
},
},
},