Skip to content

Commit

Permalink
fix: passing env variable KATIB_TRIAL_NAME in the webhook of pod.
Browse files Browse the repository at this point in the history
Signed-off-by: Electronic-Waste <2690692950@qq.com>
  • Loading branch information
Electronic-Waste committed Jul 12, 2024
1 parent c4317f6 commit 3811459
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/controller.v1beta1/consts/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ const (
// resources list which can be used as trial template
ConfigTrialResources = "trial-resources"

// EnvTrialName is the env variable of Trial name
EnvTrialName = "KATIB_TRIAL_NAME"

// LabelExperimentName is the label of experiment name.
LabelExperimentName = "katib.kubeflow.org/experiment"
// LabelSuggestionName is the label of suggestion name.
Expand Down
20 changes: 20 additions & 0 deletions pkg/webhook/v1beta1/pod/inject_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -140,6 +141,25 @@ func (s *SidecarInjector) Mutate(pod *v1.Pod, namespace string) (*v1.Pod, error)
// Add Katib Trial labels to the Pod metadata.
mutatePodMetadata(mutatedPod, trial)


// Pass env variable KATIB_TRIAL_NAME to training containers using fieldPath.
for idx := range mutatedPod.Spec.Containers {
if mutatedPod.Spec.Containers[idx].Env == nil {
mutatedPod.Spec.Containers[idx].Env = []v1.EnvVar{}
}
mutatedPod.Spec.Containers[idx].Env = append(
mutatedPod.Spec.Containers[idx].Env,
v1.EnvVar{
Name: consts.EnvTrialName,
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
FieldPath: fmt.Sprintf("metadata.labels['%s']", consts.LabelTrialName),
},
},
},
)
}

// Do the following mutation only for the Primary pod.
// If PrimaryPodLabel is not set we mutate all pods which are related to Trial job.
// Otherwise, mutate pod only with the appropriate labels.
Expand Down

0 comments on commit 3811459

Please sign in to comment.