diff --git a/pkg/controller.v1beta1/consts/const.go b/pkg/controller.v1beta1/consts/const.go index 2cffe30cde3..350a74eded0 100644 --- a/pkg/controller.v1beta1/consts/const.go +++ b/pkg/controller.v1beta1/consts/const.go @@ -62,6 +62,8 @@ const ( // EnvTrialName is the env variable of Trial name EnvTrialName = "KATIB_TRIAL_NAME" + // EnvExperimentName is the env variable of Experiment name + EnvExperimentName = "KATIB_EXPERIMENT_NAME" // LabelExperimentName is the label of experiment name. LabelExperimentName = "katib.kubeflow.org/experiment" diff --git a/pkg/webhook/v1beta1/pod/inject_webhook_test.go b/pkg/webhook/v1beta1/pod/inject_webhook_test.go index 8350264cfaa..f39aff5ddbd 100644 --- a/pkg/webhook/v1beta1/pod/inject_webhook_test.go +++ b/pkg/webhook/v1beta1/pod/inject_webhook_test.go @@ -1094,6 +1094,14 @@ func TestMutatePodEnv(t *testing.T) { }, }, }, + { + Name: consts.EnvExperimentName, + ValueFrom: &v1.EnvVarSource{ + FieldRef: &v1.ObjectFieldSelector{ + FieldPath: fmt.Sprintf("metadata.labels['%s']", consts.LabelExperimentName), + }, + }, + }, }, }, }, diff --git a/pkg/webhook/v1beta1/pod/utils.go b/pkg/webhook/v1beta1/pod/utils.go index a3dc66e1cc8..d8c2b01bf07 100644 --- a/pkg/webhook/v1beta1/pod/utils.go +++ b/pkg/webhook/v1beta1/pod/utils.go @@ -291,7 +291,7 @@ func mutatePodEnv(pod *v1.Pod, trial *trialsv1beta1.Trial) error { pod.Spec.Containers[index].Env = []v1.EnvVar{} } - // Pass env variable KATIB_TRIAL_NAME to the primary container using fieldPath + // Pass env variable KATIB_TRIAL_NAME and KATIB_EXPERIMENT_NAME to the primary container using fieldPath pod.Spec.Containers[index].Env = append( pod.Spec.Containers[index].Env, v1.EnvVar{ @@ -302,6 +302,14 @@ func mutatePodEnv(pod *v1.Pod, trial *trialsv1beta1.Trial) error { }, }, }, + v1.EnvVar{ + Name: consts.EnvExperimentName, + ValueFrom: &v1.EnvVarSource{ + FieldRef: &v1.ObjectFieldSelector{ + FieldPath: fmt.Sprintf("metadata.labels['%s']", consts.LabelExperimentName), + }, + }, + }, ) return nil } else {