From 76a92e25a77596766954952ec5b03d177176a185 Mon Sep 17 00:00:00 2001 From: Romain Manni-Bucau Date: Wed, 16 Jul 2025 09:38:40 +0200 Subject: [PATCH] Fixes #604, default TerminationGracePeriodSeconds to 30s or default if pod spec is not defined --- api/v1alpha1/testrun_types.go | 2 ++ api/v1alpha1/zz_generated.deepcopy.go | 5 +++ config/crd/bases/k6.io_testruns.yaml | 12 ++++++++ pkg/resources/jobs/initializer.go | 21 +++++++------ pkg/resources/jobs/runner.go | 7 ++--- pkg/resources/jobs/runner_test.go | 44 +++++++++++++-------------- pkg/resources/jobs/starter.go | 19 ++++++------ 7 files changed, 64 insertions(+), 46 deletions(-) diff --git a/api/v1alpha1/testrun_types.go b/api/v1alpha1/testrun_types.go index c7879e1d..8fe56243 100644 --- a/api/v1alpha1/testrun_types.go +++ b/api/v1alpha1/testrun_types.go @@ -52,6 +52,8 @@ type Pod struct { InitContainers []InitContainer `json:"initContainers,omitempty"` Volumes []corev1.Volume `json:"volumes,omitempty"` VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty"` + // +kubebuilder:default=30 + TerminationGracePeriodSeconds *int64 `json:"terminationGracePeriodSeconds,omitempty"` } type InitContainer struct { diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 10d98b81..1fc484e4 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -247,6 +247,11 @@ func (in *Pod) DeepCopyInto(out *Pod) { (*in)[i].DeepCopyInto(&(*out)[i]) } } + if in.TerminationGracePeriodSeconds != nil { + in, out := &in.TerminationGracePeriodSeconds, &out.TerminationGracePeriodSeconds + *out = new(int64) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Pod. diff --git a/config/crd/bases/k6.io_testruns.yaml b/config/crd/bases/k6.io_testruns.yaml index fc09b851..e9c75681 100644 --- a/config/crd/bases/k6.io_testruns.yaml +++ b/config/crd/bases/k6.io_testruns.yaml @@ -1087,6 +1087,10 @@ spec: type: object serviceAccountName: type: string + terminationGracePeriodSeconds: + default: 30 + format: int64 + type: integer tolerations: items: properties: @@ -3032,6 +3036,10 @@ spec: type: object serviceAccountName: type: string + terminationGracePeriodSeconds: + default: 30 + format: int64 + type: integer tolerations: items: properties: @@ -5000,6 +5008,10 @@ spec: type: object serviceAccountName: type: string + terminationGracePeriodSeconds: + default: 30 + format: int64 + type: integer tolerations: items: properties: diff --git a/pkg/resources/jobs/initializer.go b/pkg/resources/jobs/initializer.go index 2d9b8ec9..8eeac18f 100644 --- a/pkg/resources/jobs/initializer.go +++ b/pkg/resources/jobs/initializer.go @@ -104,16 +104,17 @@ func NewInitializerJob(k6 *v1alpha1.TestRun, argLine string) (*batchv1.Job, erro Annotations: annotations, }, Spec: corev1.PodSpec{ - AutomountServiceAccountToken: &automountServiceAccountToken, - ServiceAccountName: serviceAccountName, - Affinity: k6.GetSpec().Initializer.Affinity, - NodeSelector: k6.GetSpec().Initializer.NodeSelector, - Tolerations: k6.GetSpec().Initializer.Tolerations, - TopologySpreadConstraints: k6.GetSpec().Initializer.TopologySpreadConstraints, - SecurityContext: &k6.GetSpec().Initializer.SecurityContext, - RestartPolicy: corev1.RestartPolicyNever, - ImagePullSecrets: k6.GetSpec().Initializer.ImagePullSecrets, - InitContainers: getInitContainers(k6.GetSpec().Initializer, script), + AutomountServiceAccountToken: &automountServiceAccountToken, + ServiceAccountName: serviceAccountName, + Affinity: k6.GetSpec().Initializer.Affinity, + NodeSelector: k6.GetSpec().Initializer.NodeSelector, + Tolerations: k6.GetSpec().Initializer.Tolerations, + TopologySpreadConstraints: k6.GetSpec().Initializer.TopologySpreadConstraints, + SecurityContext: &k6.GetSpec().Initializer.SecurityContext, + RestartPolicy: corev1.RestartPolicyNever, + ImagePullSecrets: k6.GetSpec().Initializer.ImagePullSecrets, + InitContainers: getInitContainers(k6.GetSpec().Initializer, script), + TerminationGracePeriodSeconds: k6.GetSpec().Initializer.TerminationGracePeriodSeconds, Containers: []corev1.Container{ { Image: image, diff --git a/pkg/resources/jobs/runner.go b/pkg/resources/jobs/runner.go index 8c13b1ff..69d7d25f 100644 --- a/pkg/resources/jobs/runner.go +++ b/pkg/resources/jobs/runner.go @@ -79,10 +79,7 @@ func NewRunnerJob(k6 *v1alpha1.TestRun, index int, token string) (*batchv1.Job, command = script.UpdateCommand(command) - var ( - zero int64 = 0 - zero32 int32 = 0 - ) + var zero32 int32 = 0 image := "ghcr.io/grafana/k6-operator:latest-runner" if k6.GetSpec().Runner.Image != "" { @@ -189,7 +186,7 @@ func NewRunnerJob(k6 *v1alpha1.TestRun, index int, token string) (*batchv1.Job, ReadinessProbe: generateProbe(k6.GetSpec().Runner.ReadinessProbe), SecurityContext: &k6.GetSpec().Runner.ContainerSecurityContext, }}, - TerminationGracePeriodSeconds: &zero, + TerminationGracePeriodSeconds: k6.GetSpec().Runner.TerminationGracePeriodSeconds, Volumes: volumes, }, }, diff --git a/pkg/resources/jobs/runner_test.go b/pkg/resources/jobs/runner_test.go index 6a7ad1a6..829a0f49 100644 --- a/pkg/resources/jobs/runner_test.go +++ b/pkg/resources/jobs/runner_test.go @@ -279,7 +279,7 @@ func TestNewRunnerJob(t *testing.T) { Type: "ConfigMap", } - var zero int64 = 0 + var zero *int64 = nil automountServiceAccountToken := true expectedLabels := map[string]string{ @@ -355,7 +355,7 @@ func TestNewRunnerJob(t *testing.T) { }, SecurityContext: &corev1.SecurityContext{}, }}, - TerminationGracePeriodSeconds: &zero, + TerminationGracePeriodSeconds: zero, Volumes: script.Volume(), }, }, @@ -412,7 +412,7 @@ func TestNewRunnerJobNoisy(t *testing.T) { Type: "ConfigMap", } - var zero int64 = 0 + var zero *int64 = nil automountServiceAccountToken := true expectedLabels := map[string]string{ @@ -479,7 +479,7 @@ func TestNewRunnerJobNoisy(t *testing.T) { }, SecurityContext: &corev1.SecurityContext{}, }}, - TerminationGracePeriodSeconds: &zero, + TerminationGracePeriodSeconds: zero, Volumes: script.Volume(), }, }, @@ -527,7 +527,7 @@ func TestNewRunnerJobUnpaused(t *testing.T) { Type: "ConfigMap", } - var zero int64 = 0 + var zero *int64 = nil automountServiceAccountToken := true expectedLabels := map[string]string{ @@ -594,7 +594,7 @@ func TestNewRunnerJobUnpaused(t *testing.T) { }, SecurityContext: &corev1.SecurityContext{}, }}, - TerminationGracePeriodSeconds: &zero, + TerminationGracePeriodSeconds: zero, Volumes: script.Volume(), }, }, @@ -642,7 +642,7 @@ func TestNewRunnerJobArguments(t *testing.T) { Type: "ConfigMap", } - var zero int64 = 0 + var zero *int64 = nil automountServiceAccountToken := true expectedLabels := map[string]string{ @@ -709,7 +709,7 @@ func TestNewRunnerJobArguments(t *testing.T) { }, SecurityContext: &corev1.SecurityContext{}, }}, - TerminationGracePeriodSeconds: &zero, + TerminationGracePeriodSeconds: zero, Volumes: script.Volume(), }, }, @@ -758,7 +758,7 @@ func TestNewRunnerJobServiceAccount(t *testing.T) { Type: "ConfigMap", } - var zero int64 = 0 + var zero *int64 = nil automountServiceAccountToken := true expectedLabels := map[string]string{ @@ -825,7 +825,7 @@ func TestNewRunnerJobServiceAccount(t *testing.T) { }, SecurityContext: &corev1.SecurityContext{}, }}, - TerminationGracePeriodSeconds: &zero, + TerminationGracePeriodSeconds: zero, Volumes: script.Volume(), }, }, @@ -875,7 +875,7 @@ func TestNewRunnerJobIstio(t *testing.T) { Type: "ConfigMap", } - var zero int64 = 0 + var zero *int64 = nil automountServiceAccountToken := true expectedLabels := map[string]string{ @@ -955,7 +955,7 @@ func TestNewRunnerJobIstio(t *testing.T) { }, SecurityContext: &corev1.SecurityContext{}, }}, - TerminationGracePeriodSeconds: &zero, + TerminationGracePeriodSeconds: zero, Volumes: script.Volume(), }, }, @@ -1005,7 +1005,7 @@ func TestNewRunnerJobCloud(t *testing.T) { Type: "ConfigMap", } - var zero int64 = 0 + var zero *int64 = nil automountServiceAccountToken := true expectedLabels := map[string]string{ @@ -1080,7 +1080,7 @@ func TestNewRunnerJobCloud(t *testing.T) { }, SecurityContext: &corev1.SecurityContext{}, }}, - TerminationGracePeriodSeconds: &zero, + TerminationGracePeriodSeconds: zero, Volumes: script.Volume(), }, }, @@ -1131,7 +1131,7 @@ func TestNewRunnerJobLocalFile(t *testing.T) { Type: "LocalFile", } - var zero int64 = 0 + var zero *int64 = nil automountServiceAccountToken := true expectedLabels := map[string]string{ @@ -1197,7 +1197,7 @@ func TestNewRunnerJobLocalFile(t *testing.T) { }, SecurityContext: &corev1.SecurityContext{}, }}, - TerminationGracePeriodSeconds: &zero, + TerminationGracePeriodSeconds: zero, Volumes: script.Volume(), }, }, @@ -1244,7 +1244,7 @@ func TestNewRunnerJobWithInitContainer(t *testing.T) { Type: "ConfigMap", } - var zero int64 = 0 + var zero *int64 = nil automountServiceAccountToken := true expectedLabels := map[string]string{ @@ -1339,7 +1339,7 @@ func TestNewRunnerJobWithInitContainer(t *testing.T) { }, SecurityContext: &corev1.SecurityContext{}, }}, - TerminationGracePeriodSeconds: &zero, + TerminationGracePeriodSeconds: zero, Volumes: script.Volume(), }, }, @@ -1411,7 +1411,7 @@ func TestNewRunnerJobWithVolume(t *testing.T) { Type: "ConfigMap", } - var zero int64 = 0 + var zero *int64 = nil automountServiceAccountToken := true expectedLabels := map[string]string{ @@ -1518,7 +1518,7 @@ func TestNewRunnerJobWithVolume(t *testing.T) { }, SecurityContext: &corev1.SecurityContext{}, }}, - TerminationGracePeriodSeconds: &zero, + TerminationGracePeriodSeconds: zero, Volumes: expectedVolumes, }, }, @@ -1612,7 +1612,7 @@ func TestNewRunnerJobPLZTestRun(t *testing.T) { Type: "ConfigMap", } - var zero int64 = 0 + var zero *int64 = nil automountServiceAccountToken := true expectedLabels := map[string]string{ @@ -1688,7 +1688,7 @@ func TestNewRunnerJobPLZTestRun(t *testing.T) { }, SecurityContext: &corev1.SecurityContext{}, }}, - TerminationGracePeriodSeconds: &zero, + TerminationGracePeriodSeconds: zero, Volumes: script.Volume(), }, }, diff --git a/pkg/resources/jobs/starter.go b/pkg/resources/jobs/starter.go index 73ed75fa..cce17e27 100644 --- a/pkg/resources/jobs/starter.go +++ b/pkg/resources/jobs/starter.go @@ -76,15 +76,16 @@ func NewStarterJob(k6 *v1alpha1.TestRun, hostname []string) *batchv1.Job { Annotations: starterAnnotations, }, Spec: corev1.PodSpec{ - AutomountServiceAccountToken: &automountServiceAccountToken, - ServiceAccountName: serviceAccountName, - Affinity: k6.GetSpec().Starter.Affinity, - NodeSelector: k6.GetSpec().Starter.NodeSelector, - Tolerations: k6.GetSpec().Starter.Tolerations, - TopologySpreadConstraints: k6.GetSpec().Starter.TopologySpreadConstraints, - RestartPolicy: corev1.RestartPolicyNever, - SecurityContext: &k6.GetSpec().Starter.SecurityContext, - ImagePullSecrets: k6.GetSpec().Starter.ImagePullSecrets, + AutomountServiceAccountToken: &automountServiceAccountToken, + ServiceAccountName: serviceAccountName, + Affinity: k6.GetSpec().Starter.Affinity, + NodeSelector: k6.GetSpec().Starter.NodeSelector, + Tolerations: k6.GetSpec().Starter.Tolerations, + TopologySpreadConstraints: k6.GetSpec().Starter.TopologySpreadConstraints, + RestartPolicy: corev1.RestartPolicyNever, + SecurityContext: &k6.GetSpec().Starter.SecurityContext, + ImagePullSecrets: k6.GetSpec().Starter.ImagePullSecrets, + TerminationGracePeriodSeconds: k6.GetSpec().Runner.TerminationGracePeriodSeconds, Containers: []corev1.Container{ containers.NewStartContainer( hostname,