Skip to content
Open
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
40 changes: 40 additions & 0 deletions modules/k8s/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,43 @@ func IsJobSucceeded(job *batchv1.Job) bool {
}
return false
}

// CreateJobFromCronJob creates a Job from the specified CronJob in the given namespace and returns the created Job.
func CreateJobFromCronJob(t testing.TestingT, options *KubectlOptions, cronJobName, newJobName string) *batchv1.Job {
job, err := CreateJobFromCronJobE(t, options, cronJobName, newJobName)
require.NoError(t, err)
return job
}

// CreateJobFromCronJobE creates a Job from the specified CronJob in the given namespace and returns the created Job.
// This function is similar to running `kubectl create job --from=cronjob/<cron-job-name> <new-job-name>`.
func CreateJobFromCronJobE(t testing.TestingT, options *KubectlOptions, cronJobName, newJobName string) (*batchv1.Job, error) {
clientset, err := GetKubernetesClientFromOptionsE(t, options)
if err != nil {
return nil, err
}

cronJob, err := clientset.BatchV1().CronJobs(options.Namespace).Get(context.Background(), cronJobName, metav1.GetOptions{})
if err != nil {
return nil, err
}

annotations := make(map[string]string)
for k, v := range cronJob.Spec.JobTemplate.Annotations {
annotations[k] = v
}

job := &batchv1.Job{
TypeMeta: metav1.TypeMeta{APIVersion: batchv1.SchemeGroupVersion.String(), Kind: "Job"},
ObjectMeta: metav1.ObjectMeta{
Name: newJobName,
Namespace: options.Namespace,
Labels: cronJob.Spec.JobTemplate.Labels,
Annotations: annotations,
},
Spec: cronJob.Spec.JobTemplate.Spec,
}

createdJob, err := clientset.BatchV1().Jobs(options.Namespace).Create(context.Background(), job, metav1.CreateOptions{})
return createdJob, err
}
51 changes: 51 additions & 0 deletions modules/k8s/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -129,6 +130,29 @@ func TestIsJobSucceeded(t *testing.T) {
}
}

func TestCreateJobFromCronJobReturnsCreatedJob(t *testing.T) {
t.Parallel()

uniqueID := strings.ToLower(random.UniqueId())
options := NewKubectlOptions("", "", uniqueID)
configData := fmt.Sprintf(EXAMPLE_CRON_JOB_YAML_TEMPLATE, uniqueID, uniqueID)
defer KubectlDeleteFromString(t, options, configData)
KubectlApplyFromString(t, options, configData)

newJobName := "pi-copied-job"
job := CreateJobFromCronJob(t, options, "pi-cronjob", newJobName)
require.NotNil(t, job)
assert.Equal(t, job.Namespace, uniqueID)
assert.Equal(t, job.Name, newJobName)
}

func TestCreateJobFromCronJobEReturnsErrorForNonExistentCronJob(t *testing.T) {
t.Parallel()
options := NewKubectlOptions("", "", "default")
_, err := CreateJobFromCronJobE(t, options, "non-existent-cronjob", "new-job-name")
require.Error(t, err)
}

const EXAMPLE_JOB_YAML_TEMPLATE = `---
apiVersion: v1
kind: Namespace
Expand All @@ -150,3 +174,30 @@ spec:
restartPolicy: Never
backoffLimit: 4
`

const EXAMPLE_CRON_JOB_YAML_TEMPLATE = `---
apiVersion: v1
kind: Namespace
metadata:
name: %s
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: pi-cronjob
namespace: %s
spec:
schedule: "* 1 * * *"
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
jobTemplate:
spec:
backoffLimit: 4
template:
spec:
containers:
- name: pi
image: "perl:5.34.1"
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
`