1
16
17 package cronjob
18
19 import (
20 "context"
21 "sync"
22
23 batchv1 "k8s.io/api/batch/v1"
24 "k8s.io/apimachinery/pkg/api/errors"
25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26 "k8s.io/apimachinery/pkg/runtime/schema"
27 clientset "k8s.io/client-go/kubernetes"
28 "k8s.io/client-go/tools/record"
29 )
30
31
32
33 type cjControlInterface interface {
34 UpdateStatus(ctx context.Context, cj *batchv1.CronJob) (*batchv1.CronJob, error)
35
36 GetCronJob(ctx context.Context, namespace, name string) (*batchv1.CronJob, error)
37 }
38
39
40 type realCJControl struct {
41 KubeClient clientset.Interface
42 }
43
44 func (c *realCJControl) GetCronJob(ctx context.Context, namespace, name string) (*batchv1.CronJob, error) {
45 return c.KubeClient.BatchV1().CronJobs(namespace).Get(ctx, name, metav1.GetOptions{})
46 }
47
48 var _ cjControlInterface = &realCJControl{}
49
50 func (c *realCJControl) UpdateStatus(ctx context.Context, cj *batchv1.CronJob) (*batchv1.CronJob, error) {
51 return c.KubeClient.BatchV1().CronJobs(cj.Namespace).UpdateStatus(ctx, cj, metav1.UpdateOptions{})
52 }
53
54
55 type fakeCJControl struct {
56 CronJob *batchv1.CronJob
57 Updates []batchv1.CronJob
58 }
59
60 func (c *fakeCJControl) GetCronJob(ctx context.Context, namespace, name string) (*batchv1.CronJob, error) {
61 if name == c.CronJob.Name && namespace == c.CronJob.Namespace {
62 return c.CronJob, nil
63 }
64 return nil, errors.NewNotFound(schema.GroupResource{
65 Group: "v1beta1",
66 Resource: "cronjobs",
67 }, name)
68 }
69
70 var _ cjControlInterface = &fakeCJControl{}
71
72 func (c *fakeCJControl) UpdateStatus(ctx context.Context, cj *batchv1.CronJob) (*batchv1.CronJob, error) {
73 c.Updates = append(c.Updates, *cj)
74 return cj, nil
75 }
76
77
78
79
80
81 type jobControlInterface interface {
82
83 GetJob(namespace, name string) (*batchv1.Job, error)
84
85 CreateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error)
86
87
88 DeleteJob(namespace string, name string) error
89 }
90
91
92 type realJobControl struct {
93 KubeClient clientset.Interface
94 Recorder record.EventRecorder
95 }
96
97 var _ jobControlInterface = &realJobControl{}
98
99 func (r realJobControl) GetJob(namespace, name string) (*batchv1.Job, error) {
100 return r.KubeClient.BatchV1().Jobs(namespace).Get(context.TODO(), name, metav1.GetOptions{})
101 }
102
103 func (r realJobControl) CreateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error) {
104 return r.KubeClient.BatchV1().Jobs(namespace).Create(context.TODO(), job, metav1.CreateOptions{})
105 }
106
107 func (r realJobControl) DeleteJob(namespace string, name string) error {
108 background := metav1.DeletePropagationBackground
109 return r.KubeClient.BatchV1().Jobs(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{PropagationPolicy: &background})
110 }
111
112 type fakeJobControl struct {
113 sync.Mutex
114 Job *batchv1.Job
115 Jobs []batchv1.Job
116 DeleteJobName []string
117 Err error
118 CreateErr error
119 UpdateJobName []string
120 PatchJobName []string
121 Patches [][]byte
122 }
123
124 var _ jobControlInterface = &fakeJobControl{}
125
126 func (f *fakeJobControl) CreateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error) {
127 f.Lock()
128 defer f.Unlock()
129 if f.CreateErr != nil {
130 return nil, f.CreateErr
131 }
132 f.Jobs = append(f.Jobs, *job)
133 job.UID = "test-uid"
134 return job, nil
135 }
136
137 func (f *fakeJobControl) GetJob(namespace, name string) (*batchv1.Job, error) {
138 f.Lock()
139 defer f.Unlock()
140 if f.Err != nil {
141 return nil, f.Err
142 }
143 return f.Job, nil
144 }
145
146 func (f *fakeJobControl) DeleteJob(namespace string, name string) error {
147 f.Lock()
148 defer f.Unlock()
149 if f.Err != nil {
150 return f.Err
151 }
152 f.DeleteJobName = append(f.DeleteJobName, name)
153 return nil
154 }
155
View as plain text