From c2e81d8c83297561e4539a2377dbd90ad9dab887 Mon Sep 17 00:00:00 2001 From: FloGro3 <93530403+FloGro3@users.noreply.github.com> Date: Mon, 18 Mar 2024 13:56:51 +0100 Subject: [PATCH] feat: differentiate between Runner and Initializer initContainers (#379) * feat: differentiate between Runner and Initializer initContainers * feat: added example for usage * fix: readded pointer * fix: fixed linting issue --- .../k6_v1alpha1_k6_with_initContainers.yaml | 19 +++++++++++++++++++ pkg/resources/jobs/helpers.go | 8 ++++---- pkg/resources/jobs/initializer.go | 2 +- pkg/resources/jobs/runner.go | 2 +- 4 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 config/samples/k6_v1alpha1_k6_with_initContainers.yaml diff --git a/config/samples/k6_v1alpha1_k6_with_initContainers.yaml b/config/samples/k6_v1alpha1_k6_with_initContainers.yaml new file mode 100644 index 00000000..bc5dc18c --- /dev/null +++ b/config/samples/k6_v1alpha1_k6_with_initContainers.yaml @@ -0,0 +1,19 @@ +--- +apiVersion: k6.io/v1alpha1 +kind: K6 +metadata: + name: k6-sample +spec: + parallelism: 4 + script: + configMap: + name: k6-test + file: test.js + initializer: + initContainers: + - image: busybox:latest + command: ["sh", "-c", "echo 'is part of initializer pod'"] + runner: + initContainers: + - image: busybox:latest + command: ["sh", "-c", "echo 'is part of all 4 testrun pods'"] diff --git a/pkg/resources/jobs/helpers.go b/pkg/resources/jobs/helpers.go index ec25e815..633f8db5 100644 --- a/pkg/resources/jobs/helpers.go +++ b/pkg/resources/jobs/helpers.go @@ -117,10 +117,10 @@ func newIstioEnvVar(istio v1alpha1.K6Scuttle, istioEnabled bool) []corev1.EnvVar } // TODO: Envoy variables are not passed to init containers -func getInitContainers(k6Spec *v1alpha1.TestRunSpec, script *types.Script) []corev1.Container { +func getInitContainers(pod *v1alpha1.Pod, script *types.Script) []corev1.Container { var initContainers []corev1.Container - for i, k6InitContainer := range k6Spec.Runner.InitContainers { + for i, k6InitContainer := range pod.InitContainers { name := fmt.Sprintf("k6-init-%d", i) if k6InitContainer.Name != "" { @@ -138,8 +138,8 @@ func getInitContainers(k6Spec *v1alpha1.TestRunSpec, script *types.Script) []cor EnvFrom: k6InitContainer.EnvFrom, Env: k6InitContainer.Env, VolumeMounts: volumeMounts, - ImagePullPolicy: k6Spec.Runner.ImagePullPolicy, - SecurityContext: &k6Spec.Runner.ContainerSecurityContext, + ImagePullPolicy: pod.ImagePullPolicy, + SecurityContext: &pod.ContainerSecurityContext, } initContainers = append(initContainers, initContainer) } diff --git a/pkg/resources/jobs/initializer.go b/pkg/resources/jobs/initializer.go index 3e2e3ffb..64bb2de4 100644 --- a/pkg/resources/jobs/initializer.go +++ b/pkg/resources/jobs/initializer.go @@ -113,7 +113,7 @@ func NewInitializerJob(k6 v1alpha1.TestRunI, argLine string) (*batchv1.Job, erro SecurityContext: &k6.GetSpec().Initializer.SecurityContext, RestartPolicy: corev1.RestartPolicyNever, ImagePullSecrets: k6.GetSpec().Initializer.ImagePullSecrets, - InitContainers: getInitContainers(k6.GetSpec(), script), + InitContainers: getInitContainers(k6.GetSpec().Initializer, script), Containers: []corev1.Container{ { Image: image, diff --git a/pkg/resources/jobs/runner.go b/pkg/resources/jobs/runner.go index a9a204f0..54105401 100644 --- a/pkg/resources/jobs/runner.go +++ b/pkg/resources/jobs/runner.go @@ -174,7 +174,7 @@ func NewRunnerJob(k6 v1alpha1.TestRunI, index int, token string) (*batchv1.Job, TopologySpreadConstraints: k6.GetSpec().Runner.TopologySpreadConstraints, SecurityContext: &k6.GetSpec().Runner.SecurityContext, ImagePullSecrets: k6.GetSpec().Runner.ImagePullSecrets, - InitContainers: getInitContainers(k6.GetSpec(), script), + InitContainers: getInitContainers(&k6.GetSpec().Runner, script), Containers: []corev1.Container{{ Image: image, ImagePullPolicy: k6.GetSpec().Runner.ImagePullPolicy,