injection.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cronjob
  14. import (
  15. "fmt"
  16. "sync"
  17. batchv1 "k8s.io/api/batch/v1"
  18. batchv1beta1 "k8s.io/api/batch/v1beta1"
  19. "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/labels"
  22. "k8s.io/apimachinery/pkg/types"
  23. clientset "k8s.io/client-go/kubernetes"
  24. "k8s.io/client-go/tools/record"
  25. )
  26. // sjControlInterface is an interface that knows how to update CronJob status
  27. // created as an interface to allow testing.
  28. type sjControlInterface interface {
  29. UpdateStatus(sj *batchv1beta1.CronJob) (*batchv1beta1.CronJob, error)
  30. }
  31. // realSJControl is the default implementation of sjControlInterface.
  32. type realSJControl struct {
  33. KubeClient clientset.Interface
  34. }
  35. var _ sjControlInterface = &realSJControl{}
  36. func (c *realSJControl) UpdateStatus(sj *batchv1beta1.CronJob) (*batchv1beta1.CronJob, error) {
  37. return c.KubeClient.BatchV1beta1().CronJobs(sj.Namespace).UpdateStatus(sj)
  38. }
  39. // fakeSJControl is the default implementation of sjControlInterface.
  40. type fakeSJControl struct {
  41. Updates []batchv1beta1.CronJob
  42. }
  43. var _ sjControlInterface = &fakeSJControl{}
  44. func (c *fakeSJControl) UpdateStatus(sj *batchv1beta1.CronJob) (*batchv1beta1.CronJob, error) {
  45. c.Updates = append(c.Updates, *sj)
  46. return sj, nil
  47. }
  48. // ------------------------------------------------------------------ //
  49. // jobControlInterface is an interface that knows how to add or delete jobs
  50. // created as an interface to allow testing.
  51. type jobControlInterface interface {
  52. // GetJob retrieves a Job.
  53. GetJob(namespace, name string) (*batchv1.Job, error)
  54. // CreateJob creates new Jobs according to the spec.
  55. CreateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error)
  56. // UpdateJob updates a Job.
  57. UpdateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error)
  58. // PatchJob patches a Job.
  59. PatchJob(namespace string, name string, pt types.PatchType, data []byte, subresources ...string) (*batchv1.Job, error)
  60. // DeleteJob deletes the Job identified by name.
  61. // TODO: delete by UID?
  62. DeleteJob(namespace string, name string) error
  63. }
  64. // realJobControl is the default implementation of jobControlInterface.
  65. type realJobControl struct {
  66. KubeClient clientset.Interface
  67. Recorder record.EventRecorder
  68. }
  69. var _ jobControlInterface = &realJobControl{}
  70. func copyLabels(template *batchv1beta1.JobTemplateSpec) labels.Set {
  71. l := make(labels.Set)
  72. for k, v := range template.Labels {
  73. l[k] = v
  74. }
  75. return l
  76. }
  77. func copyAnnotations(template *batchv1beta1.JobTemplateSpec) labels.Set {
  78. a := make(labels.Set)
  79. for k, v := range template.Annotations {
  80. a[k] = v
  81. }
  82. return a
  83. }
  84. func (r realJobControl) GetJob(namespace, name string) (*batchv1.Job, error) {
  85. return r.KubeClient.BatchV1().Jobs(namespace).Get(name, metav1.GetOptions{})
  86. }
  87. func (r realJobControl) UpdateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error) {
  88. return r.KubeClient.BatchV1().Jobs(namespace).Update(job)
  89. }
  90. func (r realJobControl) PatchJob(namespace string, name string, pt types.PatchType, data []byte, subresources ...string) (*batchv1.Job, error) {
  91. return r.KubeClient.BatchV1().Jobs(namespace).Patch(name, pt, data, subresources...)
  92. }
  93. func (r realJobControl) CreateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error) {
  94. return r.KubeClient.BatchV1().Jobs(namespace).Create(job)
  95. }
  96. func (r realJobControl) DeleteJob(namespace string, name string) error {
  97. background := metav1.DeletePropagationBackground
  98. return r.KubeClient.BatchV1().Jobs(namespace).Delete(name, &metav1.DeleteOptions{PropagationPolicy: &background})
  99. }
  100. type fakeJobControl struct {
  101. sync.Mutex
  102. Job *batchv1.Job
  103. Jobs []batchv1.Job
  104. DeleteJobName []string
  105. Err error
  106. UpdateJobName []string
  107. PatchJobName []string
  108. Patches [][]byte
  109. }
  110. var _ jobControlInterface = &fakeJobControl{}
  111. func (f *fakeJobControl) CreateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error) {
  112. f.Lock()
  113. defer f.Unlock()
  114. if f.Err != nil {
  115. return nil, f.Err
  116. }
  117. job.SelfLink = fmt.Sprintf("/api/batch/v1/namespaces/%s/jobs/%s", namespace, job.Name)
  118. f.Jobs = append(f.Jobs, *job)
  119. job.UID = "test-uid"
  120. return job, nil
  121. }
  122. func (f *fakeJobControl) GetJob(namespace, name string) (*batchv1.Job, error) {
  123. f.Lock()
  124. defer f.Unlock()
  125. if f.Err != nil {
  126. return nil, f.Err
  127. }
  128. return f.Job, nil
  129. }
  130. func (f *fakeJobControl) UpdateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error) {
  131. f.Lock()
  132. defer f.Unlock()
  133. if f.Err != nil {
  134. return nil, f.Err
  135. }
  136. f.UpdateJobName = append(f.UpdateJobName, job.Name)
  137. return job, nil
  138. }
  139. func (f *fakeJobControl) PatchJob(namespace string, name string, pt types.PatchType, data []byte, subresources ...string) (*batchv1.Job, error) {
  140. f.Lock()
  141. defer f.Unlock()
  142. if f.Err != nil {
  143. return nil, f.Err
  144. }
  145. f.PatchJobName = append(f.PatchJobName, name)
  146. f.Patches = append(f.Patches, data)
  147. // We don't have anything to return. Just return something non-nil.
  148. return &batchv1.Job{}, nil
  149. }
  150. func (f *fakeJobControl) DeleteJob(namespace string, name string) error {
  151. f.Lock()
  152. defer f.Unlock()
  153. if f.Err != nil {
  154. return f.Err
  155. }
  156. f.DeleteJobName = append(f.DeleteJobName, name)
  157. return nil
  158. }
  159. func (f *fakeJobControl) Clear() {
  160. f.Lock()
  161. defer f.Unlock()
  162. f.DeleteJobName = []string{}
  163. f.Jobs = []batchv1.Job{}
  164. f.Err = nil
  165. }
  166. // ------------------------------------------------------------------ //
  167. // podControlInterface is an interface that knows how to list or delete pods
  168. // created as an interface to allow testing.
  169. type podControlInterface interface {
  170. // ListPods list pods
  171. ListPods(namespace string, opts metav1.ListOptions) (*v1.PodList, error)
  172. // DeleteJob deletes the pod identified by name.
  173. // TODO: delete by UID?
  174. DeletePod(namespace string, name string) error
  175. }
  176. // realPodControl is the default implementation of podControlInterface.
  177. type realPodControl struct {
  178. KubeClient clientset.Interface
  179. Recorder record.EventRecorder
  180. }
  181. var _ podControlInterface = &realPodControl{}
  182. func (r realPodControl) ListPods(namespace string, opts metav1.ListOptions) (*v1.PodList, error) {
  183. return r.KubeClient.CoreV1().Pods(namespace).List(opts)
  184. }
  185. func (r realPodControl) DeletePod(namespace string, name string) error {
  186. return r.KubeClient.CoreV1().Pods(namespace).Delete(name, nil)
  187. }
  188. type fakePodControl struct {
  189. sync.Mutex
  190. Pods []v1.Pod
  191. DeletePodName []string
  192. Err error
  193. }
  194. var _ podControlInterface = &fakePodControl{}
  195. func (f *fakePodControl) ListPods(namespace string, opts metav1.ListOptions) (*v1.PodList, error) {
  196. f.Lock()
  197. defer f.Unlock()
  198. if f.Err != nil {
  199. return nil, f.Err
  200. }
  201. return &v1.PodList{Items: f.Pods}, nil
  202. }
  203. func (f *fakePodControl) DeletePod(namespace string, name string) error {
  204. f.Lock()
  205. defer f.Unlock()
  206. if f.Err != nil {
  207. return f.Err
  208. }
  209. f.DeletePodName = append(f.DeletePodName, name)
  210. return nil
  211. }