Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Adding trial name as env for metrics collectors #2165

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions pkg/webhook/v1beta1/pod/inject_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ func (s *SidecarInjector) Mutate(pod *v1.Pod, namespace string) (*v1.Pod, error)
return nil, err
}

// Add KATIB_TRIAL_NAME env name to all containers
axel7083 marked this conversation as resolved.
Show resolved Hide resolved
mutatePodContainersEnv(mutatedPod, trial)
axel7083 marked this conversation as resolved.
Show resolved Hide resolved

// Add Katib Trial labels to the Pod metadata.
mutatePodMetadata(mutatedPod, trial)

Expand Down
65 changes: 65 additions & 0 deletions pkg/webhook/v1beta1/pod/inject_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1065,3 +1065,68 @@ func TestMutatePodMetadata(t *testing.T) {
}
}
}

func TestMutatePodContainersEnv(t *testing.T) {
testTrialName := "hello-trial"

testCases := []struct {
pod *v1.Pod
trial *trialsv1beta1.Trial
mutatedPod *v1.Pod
testDescription string
}{
{
pod: &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "container-1",
Env: []v1.EnvVar{},
},
{
Name: "container-2",
Env: []v1.EnvVar{},
},
},
},
},
trial: &trialsv1beta1.Trial{
ObjectMeta: metav1.ObjectMeta{
Name: testTrialName,
},
},
mutatedPod: &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "container-1",
Env: []v1.EnvVar{
{
Name: "KATIB_TRIAL_NAME",
Value: testTrialName,
},
},
},
{
Name: "container-2",
Env: []v1.EnvVar{
{
Name: "KATIB_TRIAL_NAME",
Value: testTrialName,
},
},
},
},
},
},
testDescription: "todo",
},
}

for _, tc := range testCases {
mutatePodContainersEnv(tc.pod, tc.trial)
if !reflect.DeepEqual(tc.mutatedPod, tc.pod) {
t.Errorf("Case %v. Expected Pod %v, got %v", tc.testDescription, tc.mutatedPod, tc.pod)
}
}
}
10 changes: 10 additions & 0 deletions pkg/webhook/v1beta1/pod/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ func mutateMetricsCollectorVolume(pod *v1.Pod, mountPath, sidecarContainerName,
return nil
}

func mutatePodContainersEnv(pod *v1.Pod, trial *trialsv1beta1.Trial) {
for i := range pod.Spec.Containers {
container := &pod.Spec.Containers[i]
container.Env = append(container.Env, v1.EnvVar{
Name: "KATIB_TRIAL_NAME",
axel7083 marked this conversation as resolved.
Show resolved Hide resolved
Value: trial.Name,
})
}
}

func mutatePodMetadata(pod *v1.Pod, trial *trialsv1beta1.Trial) {
podLabels := map[string]string{}

Expand Down