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

JobSink: Inject a KNATIVE_EXECUTION_MODE environment variable with value batch #8346

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 cmd/jobsink/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

js = js.DeepCopy() // Do not modify informer copy.
js.SetDefaults(ctx)

job := js.Spec.Job.DeepCopy()
job.Name = jobName
if job.Labels == nil {
Expand Down
41 changes: 41 additions & 0 deletions pkg/apis/sinks/v1alpha1/job_sink_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,48 @@ package v1alpha1

import (
"context"

batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
)

func (sink *JobSink) SetDefaults(ctx context.Context) {
if sink.Spec.Job != nil {
setBatchJobDefaults(sink.Spec.Job)
}
}

func setBatchJobDefaults(job *batchv1.Job) {
for i := range job.Spec.Template.Spec.Containers {
executionModeFound := false
for j := range job.Spec.Template.Spec.Containers[i].Env {
if job.Spec.Template.Spec.Containers[i].Env[j].Name == ExecutionModeEnvVar {
executionModeFound = true
break
}
}
if executionModeFound {
continue
}
job.Spec.Template.Spec.Containers[i].Env = append(job.Spec.Template.Spec.Containers[i].Env, corev1.EnvVar{
Name: ExecutionModeEnvVar,
Value: string(ExecutionModeBatch),
})
}
for i := range job.Spec.Template.Spec.InitContainers {
executionModeFound := false
for j := range job.Spec.Template.Spec.InitContainers[i].Env {
if job.Spec.Template.Spec.InitContainers[i].Env[j].Name == ExecutionModeEnvVar {
executionModeFound = true
break
}
}
if executionModeFound {
continue
}
job.Spec.Template.Spec.InitContainers[i].Env = append(job.Spec.Template.Spec.InitContainers[i].Env, corev1.EnvVar{
Name: ExecutionModeEnvVar,
Value: string(ExecutionModeBatch),
})
}
}
100 changes: 99 additions & 1 deletion pkg/apis/sinks/v1alpha1/job_sink_defaults_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check failure on line 1 in pkg/apis/sinks/v1alpha1/job_sink_defaults_test.go

View workflow job for this annotation

GitHub Actions / style / Golang / Auto-format and Check

Please run goimports. diff --git a/pkg/apis/sinks/v1alpha1/job_sink_defaults_test.go b/pkg/apis/sinks/v1alpha1/job_sink_defaults_test.go index 766ab45..5f693c0 100644 --- a/pkg/apis/sinks/v1alpha1/job_sink_defaults_test.go +++ b/pkg/apis/sinks/v1alpha1/job_sink_defaults_test.go @@ -34,21 +34,21 @@ func TestSetDefaults(t *testing.T) { initial: JobSink{ Spec: JobSinkSpec{ Job: &batchv1.Job{ - Spec: batchv1.JobSpec{ + Spec: batchv1.JobSpec{ Template: corev1.PodTemplateSpec{ - Spec: corev1.PodSpec{ + Spec: corev1.PodSpec{ Containers: []corev1.Container{ { - Name: "cnt", - Image: "img", + Name: "cnt", + Image: "img", }, { - Name: "cnt2", - Image: "img2", + Name: "cnt2", + Image: "img2", }, { - Name: "cnt3", - Image: "img3", + Name: "cnt3", + Image: "img3", Env: []corev1.EnvVar{ {Name: "KNATIVE_EXECUTION_MODE", Value: "something"}, }, @@ -56,12 +56,12 @@ func TestSetDefaults(t *testing.T) { }, InitContainers: []corev1.Container{ { - Name: "cnt", - Image: "img", + Name: "cnt", + Image: "img", }, { - Name: "cnt-ini2", - Image: "img-ini2", + Name: "cnt-ini2", + Image: "img-ini2", Env: []corev1.EnvVar{ {Name: "KNATIVE_EXECUTION_MODE", Value: "something"}, }, @@ -76,20 +76,20 @@ func TestSetDefaults(t *testing.T) { expected: JobSink{ Spec: JobSinkSpec{ Job: &batchv1.Job{ - Spec: batchv1.JobSpec{ + Spec: batchv1.JobSpec{ Template: corev1.PodTemplateSpec{ - Spec: corev1.PodSpec{ + Spec: corev1.PodSpec{ InitContainers: []corev1.Container{ { - Name: "cnt", - Image: "img", + Name: "cnt", + Image: "img", Env: []corev1.EnvVar{ {Name: "KNATIVE_EXECUTION_MODE", Value: "batch"}, }, }, { - Name: "cnt-ini2", - Image: "img-ini2", + Name: "cnt-ini2", + Image: "img-ini2", Env: []corev1.EnvVar{ {Name: "KNATIVE_EXECUTION_MODE", Value: "something"}, }, @@ -97,22 +97,22 @@ func TestSetDefaults(t *testing.T) { }, Containers: []corev1.Container{ { - Name: "cnt", - Image: "img", + Name: "cnt", + Image: "img", Env: []corev1.EnvVar{ {Name: "KNATIVE_EXECUTION_MODE", Value: "batch"}, }, }, { - Name: "cnt2", - Image: "img2", + Name: "cnt2", + Image: "img2", Env: []corev1.EnvVar{ {Name: "KNATIVE_EXECUTION_MODE", Value: "batch"}, }, }, { - Name: "cnt3", - Image: "img3", + Name: "cnt3", + Image: "img3", Env: []corev1.EnvVar{ {Name: "KNATIVE_EXECUTION_MODE", Value: "something"}, },
Copyright 2024 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,13 +21,111 @@
"testing"

"github.com/google/go-cmp/cmp"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
)

func TestSetDefaults(t *testing.T) {
testCases := map[string]struct {
initial JobSink
expected JobSink
}{}
}{
"execution mode": {
initial: JobSink{
Spec: JobSinkSpec{
Job: &batchv1.Job{
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "cnt",
Image: "img",
},
{
Name: "cnt2",
Image: "img2",
},
{
Name: "cnt3",
Image: "img3",
Env: []corev1.EnvVar{
{Name: "KNATIVE_EXECUTION_MODE", Value: "something"},
},
},
},
InitContainers: []corev1.Container{
{
Name: "cnt",
Image: "img",
},
{
Name: "cnt-ini2",
Image: "img-ini2",
Env: []corev1.EnvVar{
{Name: "KNATIVE_EXECUTION_MODE", Value: "something"},
},
},
},
},
},
},
},
},
},
expected: JobSink{
Spec: JobSinkSpec{
Job: &batchv1.Job{
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{
{
Name: "cnt",
Image: "img",
Env: []corev1.EnvVar{
{Name: "KNATIVE_EXECUTION_MODE", Value: "batch"},
},
},
{
Name: "cnt-ini2",
Image: "img-ini2",
Env: []corev1.EnvVar{
{Name: "KNATIVE_EXECUTION_MODE", Value: "something"},
},
},
},
Containers: []corev1.Container{
{
Name: "cnt",
Image: "img",
Env: []corev1.EnvVar{
{Name: "KNATIVE_EXECUTION_MODE", Value: "batch"},
},
},
{
Name: "cnt2",
Image: "img2",
Env: []corev1.EnvVar{
{Name: "KNATIVE_EXECUTION_MODE", Value: "batch"},
},
},
{
Name: "cnt3",
Image: "img3",
Env: []corev1.EnvVar{
{Name: "KNATIVE_EXECUTION_MODE", Value: "something"},
},
},
},
},
},
},
},
},
},
},
}
for n, tc := range testCases {
t.Run(n, func(t *testing.T) {
tc.initial.SetDefaults(context.TODO())
Expand Down
13 changes: 12 additions & 1 deletion pkg/apis/sinks/v1alpha1/job_sink_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
eventingduckv1 "knative.dev/eventing/pkg/apis/duck/v1"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/kmeta"

eventingduckv1 "knative.dev/eventing/pkg/apis/duck/v1"
)

const (
ExecutionModeEnvVar = "KNATIVE_EXECUTION_MODE"
)

type ExecutionMode string

const (
ExecutionModeBatch ExecutionMode = "batch"
)

// +genclient
Expand Down
3 changes: 3 additions & 0 deletions pkg/reconciler/jobsink/jobsink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ func testJob(name string) *batchv1.Job {
Containers: []corev1.Container{
{
Name: "test-container",
Env: []corev1.EnvVar{
{Name: "KNATIVE_EXECUTION_MODE", Value: "batch"},
},
},
},
},
Expand Down
Loading