-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcron.go
137 lines (118 loc) · 3.58 KB
/
cron.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2023 Cover Whale Insurance Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kopts
import (
"fmt"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// CronSchedule is a cronjob schedule string
type CronSchedule string
const (
Daily CronSchedule = "0 0 * * *"
Hourly CronSchedule = "0 * * * *"
Minute CronSchedule = "* * * * *"
Weekly CronSchedule = "0 0 1 * *"
Monthly CronSchedule = "0 0 1 1/1 *"
Yearly CronSchedule = "0 0 1 1 *"
Every15Minutes CronSchedule = "*/15 * * * *"
Every5Minutes CronSchedule = "*/5 * * * *"
Every10Minutes CronSchedule = "*/10 * * *"
EveryHalfHour CronSchedule = "*/30 * * *"
)
// CronJob is a Kubernetes cron job
type CronJob struct {
batchv1.CronJob
}
type CronJobOpt func(*CronJob)
// NewCronJob returns a cron job with the given name and options
func NewCronJob(name string, opts ...CronJobOpt) *CronJob {
c := &CronJob{
CronJob: batchv1.CronJob{
TypeMeta: metav1.TypeMeta{
Kind: "CronJob",
APIVersion: "batch/v1",
},
ObjectMeta: newObjectMeta(name),
Spec: batchv1.CronJobSpec{
JobTemplate: batchv1.JobTemplateSpec{
Spec: batchv1.JobSpec{},
},
},
},
}
for _, v := range opts {
v(c)
}
return c
}
// CronJobNamespace sets the namespace for the cronjob
func CronJobNamespace(n string) CronJobOpt {
return func(c *CronJob) {
setNamespace(n, &c.ObjectMeta)
}
}
// CronJobRestartPolicy sets the restart policy for the cronjob
func CronJobRestartPolicy(r corev1.RestartPolicy) CronJobOpt {
return func(c *CronJob) {
c.Spec.JobTemplate.Spec.Template.Spec.RestartPolicy = r
}
}
// CronJobParallelism sets the parallelism for the cronjob
func CronJobParallelism(i int) CronJobOpt {
parallel := int32(i)
return func(c *CronJob) {
c.Spec.JobTemplate.Spec.Parallelism = ¶llel
}
}
// CronJobCompletions sets the completions for the cronjob
func CronJobCompletions(i int) CronJobOpt {
completions := int32(i)
return func(c *CronJob) {
c.Spec.JobTemplate.Spec.Completions = &completions
}
}
// CronJobActiveDeadlineSeconds sets the active deadline seconds for the cronjob
func CronJobActiveDeadlineSeconds(i int) CronJobOpt {
seconds := int64(i)
return func(c *CronJob) {
c.Spec.JobTemplate.Spec.ActiveDeadlineSeconds = &seconds
}
}
// CronJobBackoffLimit sets the backoff limit for the cronjob
func CronJobBackoffLimit(i int) CronJobOpt {
limit := int32(i)
return func(c *CronJob) {
c.Spec.JobTemplate.Spec.BackoffLimit = &limit
}
}
// CronJobPodSpec sets the pod spec for the cronjob
func CronJobPodSpec(p PodSpec) CronJobOpt {
return func(c *CronJob) {
c.Spec.JobTemplate.Spec.Template = p.Spec
}
}
// CronJobSchedule sets the schedule for the cronjob
func CronJobSchedule(cs CronSchedule) CronJobOpt {
return func(c *CronJob) {
c.Spec.Schedule = fmt.Sprintf("%s", string(cs))
}
}
// CronJobConcurrency sets the concurrency for the cronjob
func CronJobConcurrency(p batchv1.ConcurrencyPolicy) CronJobOpt {
return func(c *CronJob) {
c.Spec.ConcurrencyPolicy = p
}
}