From 99830dcada5b628ef6734325d1270267170f3c90 Mon Sep 17 00:00:00 2001 From: Nico Braun Date: Tue, 31 Jan 2023 00:54:42 +0100 Subject: [PATCH] feat: add support for batch v1 cronjob Signed-off-by: Nico Braun --- internal/k8sinternal/client_test.go | 1 + .../fixtures/all_resources/cronjob-v1.yml | 23 +++++++++++++++++++ pkg/k8s/helpers.go | 2 ++ pkg/k8s/resources.go | 18 +++++++++++++++ pkg/k8s/types.go | 9 ++++++++ 5 files changed, 53 insertions(+) create mode 100644 internal/test/fixtures/all_resources/cronjob-v1.yml diff --git a/internal/k8sinternal/client_test.go b/internal/k8sinternal/client_test.go index 97a4f93b..b8d08f7e 100644 --- a/internal/k8sinternal/client_test.go +++ b/internal/k8sinternal/client_test.go @@ -86,6 +86,7 @@ func TestGetAllResources(t *testing.T) { k8s.NewStatefulSet(), k8s.NewPodTemplate(), k8s.NewCronJob(), + k8s.NewCronJobV1(), k8s.NewServiceAccount(), k8s.NewService(), k8s.NewJob(), diff --git a/internal/test/fixtures/all_resources/cronjob-v1.yml b/internal/test/fixtures/all_resources/cronjob-v1.yml new file mode 100644 index 00000000..6092e004 --- /dev/null +++ b/internal/test/fixtures/all_resources/cronjob-v1.yml @@ -0,0 +1,23 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: cronjob-v1 +--- +apiVersion: batch/v1 +kind: CronJob +metadata: + name: cronjob-v1 + namespace: cronjob-v1 +spec: + schedule: "*/1 * * * *" + jobTemplate: + spec: + template: + spec: + restartPolicy: OnFailure + hostPID: true + hostIPC: true + hostNetwork: true + containers: + - name: container + image: scratch diff --git a/pkg/k8s/helpers.go b/pkg/k8s/helpers.go index a20f650a..3b947035 100644 --- a/pkg/k8s/helpers.go +++ b/pkg/k8s/helpers.go @@ -111,6 +111,8 @@ func GetPodTemplateSpec(resource Resource) *PodTemplateSpecV1 { switch kubeType := resource.(type) { case *CronJobV1Beta1: return &kubeType.Spec.JobTemplate.Spec.Template + case *CronJobV1: + return &kubeType.Spec.JobTemplate.Spec.Template case *DaemonSetV1: return &kubeType.Spec.Template case *DeploymentV1: diff --git a/pkg/k8s/resources.go b/pkg/k8s/resources.go index 7a34e479..607cfa52 100644 --- a/pkg/k8s/resources.go +++ b/pkg/k8s/resources.go @@ -127,6 +127,24 @@ func NewCronJob() *CronJobV1Beta1 { } } +// NewCronJobV1 creates a new CronJob resource +func NewCronJobV1() *CronJobV1 { + return &CronJobV1{ + TypeMeta: TypeMetaV1{ + Kind: "CronJob", + APIVersion: "batch/v1", + }, + ObjectMeta: ObjectMetaV1{}, + Spec: CronJobSpecV1{ + JobTemplate: JobTemplateSpecV1{ + Spec: JobSpecV1{ + Template: podTemplateSpec, + }, + }, + }, + } +} + // NewServiceAccount creates a new ServiceAccount resource func NewServiceAccount() *ServiceAccountV1 { return &ServiceAccountV1{ diff --git a/pkg/k8s/types.go b/pkg/k8s/types.go index 76579aab..4b7755d2 100644 --- a/pkg/k8s/types.go +++ b/pkg/k8s/types.go @@ -25,6 +25,12 @@ type CronJobV1Beta1 = batchv1beta1.CronJob // CronJobSpecV1Beta1 is a type alias for the v1beta1 version of the k8s batch API. type CronJobSpecV1Beta1 = batchv1beta1.CronJobSpec +// CronJobV1 is a type alias for the v1 version of the k8s batch API. +type CronJobV1 = batchv1.CronJob + +// CronJobSpecV1 is a type alias for the v1 version of the k8s batch API. +type CronJobSpecV1 = batchv1.CronJobSpec + // DaemonSetSpecV1 is a type alias for the v1 version of the k8s apps API. type DaemonSetSpecV1 = appsv1.DaemonSetSpec @@ -40,6 +46,9 @@ type DeploymentV1 = appsv1.Deployment // JobTemplateSpecV1Beta1 is a type alias for the v1beta1 version of the k8s batch API. type JobTemplateSpecV1Beta1 = batchv1beta1.JobTemplateSpec +// JobTemplateSpecV1 is a type alias for the v1 version of the k8s batch API. +type JobTemplateSpecV1 = batchv1.JobTemplateSpec + // JobSpecV1 is a type alias for the v1 version of the k8s batch API. type JobSpecV1 = batchv1.JobSpec