Skip to content

Commit

Permalink
feat: add support for batch v1 cronjob
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Braun <[email protected]>
  • Loading branch information
bluebrown committed Feb 1, 2023
1 parent d3ecd6b commit 99830dc
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/k8sinternal/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestGetAllResources(t *testing.T) {
k8s.NewStatefulSet(),
k8s.NewPodTemplate(),
k8s.NewCronJob(),
k8s.NewCronJobV1(),
k8s.NewServiceAccount(),
k8s.NewService(),
k8s.NewJob(),
Expand Down
23 changes: 23 additions & 0 deletions internal/test/fixtures/all_resources/cronjob-v1.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions pkg/k8s/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions pkg/k8s/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
9 changes: 9 additions & 0 deletions pkg/k8s/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 99830dc

Please sign in to comment.