injection.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 scheduledjob
  14. import (
  15. "sync"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/apis/batch"
  18. "k8s.io/kubernetes/pkg/client/record"
  19. client "k8s.io/kubernetes/pkg/client/unversioned"
  20. "k8s.io/kubernetes/pkg/labels"
  21. )
  22. // sjControlInterface is an interface that knows how to update ScheduledJob status
  23. // created as an interface to allow testing.
  24. type sjControlInterface interface {
  25. UpdateStatus(sj *batch.ScheduledJob) (*batch.ScheduledJob, error)
  26. }
  27. // realSJControl is the default implementation of sjControlInterface.
  28. type realSJControl struct {
  29. KubeClient *client.Client
  30. }
  31. var _ sjControlInterface = &realSJControl{}
  32. func (c *realSJControl) UpdateStatus(sj *batch.ScheduledJob) (*batch.ScheduledJob, error) {
  33. return c.KubeClient.Batch().ScheduledJobs(sj.Namespace).UpdateStatus(sj)
  34. }
  35. // fakeSJControl is the default implementation of sjControlInterface.
  36. type fakeSJControl struct {
  37. Updates []batch.ScheduledJob
  38. }
  39. var _ sjControlInterface = &fakeSJControl{}
  40. func (c *fakeSJControl) UpdateStatus(sj *batch.ScheduledJob) (*batch.ScheduledJob, error) {
  41. c.Updates = append(c.Updates, *sj)
  42. return sj, nil
  43. }
  44. // ------------------------------------------------------------------ //
  45. // jobControlInterface is an interface that knows how to add or delete jobs
  46. // created as an interface to allow testing.
  47. type jobControlInterface interface {
  48. // GetJob retrieves a job
  49. GetJob(namespace, name string) (*batch.Job, error)
  50. // CreateJob creates new jobs according to the spec
  51. CreateJob(namespace string, job *batch.Job) (*batch.Job, error)
  52. // UpdateJob updates a job
  53. UpdateJob(namespace string, job *batch.Job) (*batch.Job, error)
  54. // DeleteJob deletes the job identified by name.
  55. // TODO: delete by UID?
  56. DeleteJob(namespace string, name string) error
  57. }
  58. // realJobControl is the default implementation of jobControlInterface.
  59. type realJobControl struct {
  60. KubeClient *client.Client
  61. Recorder record.EventRecorder
  62. }
  63. var _ jobControlInterface = &realJobControl{}
  64. func copyLabels(template *batch.JobTemplateSpec) labels.Set {
  65. l := make(labels.Set)
  66. for k, v := range template.Labels {
  67. l[k] = v
  68. }
  69. return l
  70. }
  71. func copyAnnotations(template *batch.JobTemplateSpec) labels.Set {
  72. a := make(labels.Set)
  73. for k, v := range template.Annotations {
  74. a[k] = v
  75. }
  76. return a
  77. }
  78. func (r realJobControl) GetJob(namespace, name string) (*batch.Job, error) {
  79. return r.KubeClient.Batch().Jobs(namespace).Get(name)
  80. }
  81. func (r realJobControl) UpdateJob(namespace string, job *batch.Job) (*batch.Job, error) {
  82. return r.KubeClient.Batch().Jobs(namespace).Update(job)
  83. }
  84. func (r realJobControl) CreateJob(namespace string, job *batch.Job) (*batch.Job, error) {
  85. return r.KubeClient.Batch().Jobs(namespace).Create(job)
  86. }
  87. func (r realJobControl) DeleteJob(namespace string, name string) error {
  88. return r.KubeClient.Batch().Jobs(namespace).Delete(name, nil)
  89. }
  90. type fakeJobControl struct {
  91. sync.Mutex
  92. Job *batch.Job
  93. Jobs []batch.Job
  94. DeleteJobName []string
  95. Err error
  96. }
  97. var _ jobControlInterface = &fakeJobControl{}
  98. func (f *fakeJobControl) CreateJob(namespace string, job *batch.Job) (*batch.Job, error) {
  99. f.Lock()
  100. defer f.Unlock()
  101. if f.Err != nil {
  102. return nil, f.Err
  103. }
  104. f.Jobs = append(f.Jobs, *job)
  105. job.UID = "test-uid"
  106. return job, nil
  107. }
  108. func (f *fakeJobControl) GetJob(namespace, name string) (*batch.Job, error) {
  109. f.Lock()
  110. defer f.Unlock()
  111. if f.Err != nil {
  112. return nil, f.Err
  113. }
  114. return f.Job, nil
  115. }
  116. func (f *fakeJobControl) UpdateJob(namespace string, job *batch.Job) (*batch.Job, error) {
  117. f.Lock()
  118. defer f.Unlock()
  119. if f.Err != nil {
  120. return nil, f.Err
  121. }
  122. return job, nil
  123. }
  124. func (f *fakeJobControl) DeleteJob(namespace string, name string) error {
  125. f.Lock()
  126. defer f.Unlock()
  127. if f.Err != nil {
  128. return f.Err
  129. }
  130. f.DeleteJobName = append(f.DeleteJobName, name)
  131. return nil
  132. }
  133. func (f *fakeJobControl) Clear() {
  134. f.Lock()
  135. defer f.Unlock()
  136. f.DeleteJobName = []string{}
  137. f.Jobs = []batch.Job{}
  138. f.Err = nil
  139. }
  140. // ------------------------------------------------------------------ //
  141. // podControlInterface is an interface that knows how to list or delete pods
  142. // created as an interface to allow testing.
  143. type podControlInterface interface {
  144. // ListPods list pods
  145. ListPods(namespace string, opts api.ListOptions) (*api.PodList, error)
  146. // DeleteJob deletes the pod identified by name.
  147. // TODO: delete by UID?
  148. DeletePod(namespace string, name string) error
  149. }
  150. // realPodControl is the default implementation of podControlInterface.
  151. type realPodControl struct {
  152. KubeClient *client.Client
  153. Recorder record.EventRecorder
  154. }
  155. var _ podControlInterface = &realPodControl{}
  156. func (r realPodControl) ListPods(namespace string, opts api.ListOptions) (*api.PodList, error) {
  157. return r.KubeClient.Pods(namespace).List(opts)
  158. }
  159. func (r realPodControl) DeletePod(namespace string, name string) error {
  160. return r.KubeClient.Pods(namespace).Delete(name, nil)
  161. }
  162. type fakePodControl struct {
  163. sync.Mutex
  164. Pods []api.Pod
  165. DeletePodName []string
  166. Err error
  167. }
  168. var _ podControlInterface = &fakePodControl{}
  169. func (f *fakePodControl) ListPods(namespace string, opts api.ListOptions) (*api.PodList, error) {
  170. f.Lock()
  171. defer f.Unlock()
  172. return &api.PodList{Items: f.Pods}, nil
  173. }
  174. func (f *fakePodControl) DeletePod(namespace string, name string) error {
  175. f.Lock()
  176. defer f.Unlock()
  177. if f.Err != nil {
  178. return f.Err
  179. }
  180. f.DeletePodName = append(f.DeletePodName, name)
  181. return nil
  182. }