utils.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. "encoding/json"
  16. "fmt"
  17. "hash/adler32"
  18. "time"
  19. "github.com/golang/glog"
  20. "github.com/robfig/cron"
  21. "k8s.io/kubernetes/pkg/api"
  22. "k8s.io/kubernetes/pkg/api/unversioned"
  23. "k8s.io/kubernetes/pkg/apis/batch"
  24. "k8s.io/kubernetes/pkg/runtime"
  25. "k8s.io/kubernetes/pkg/types"
  26. hashutil "k8s.io/kubernetes/pkg/util/hash"
  27. )
  28. // Utilities for dealing with Jobs and ScheduledJobs and time.
  29. const (
  30. CreatedByAnnotation = "kubernetes.io/created-by"
  31. )
  32. func inActiveList(sj batch.ScheduledJob, uid types.UID) bool {
  33. for _, j := range sj.Status.Active {
  34. if j.UID == uid {
  35. return true
  36. }
  37. }
  38. return false
  39. }
  40. func deleteFromActiveList(sj *batch.ScheduledJob, uid types.UID) {
  41. if sj == nil {
  42. return
  43. }
  44. newActive := []api.ObjectReference{}
  45. for _, j := range sj.Status.Active {
  46. if j.UID != uid {
  47. newActive = append(newActive, j)
  48. }
  49. }
  50. sj.Status.Active = newActive
  51. }
  52. // getParentUIDFromJob extracts UID of job's parent and whether it was found
  53. func getParentUIDFromJob(j batch.Job) (types.UID, bool) {
  54. creatorRefJson, found := j.ObjectMeta.Annotations[CreatedByAnnotation]
  55. if !found {
  56. glog.V(4).Infof("Job with no created-by annotation, name %s namespace %s", j.Name, j.Namespace)
  57. return types.UID(""), false
  58. }
  59. var sr api.SerializedReference
  60. err := json.Unmarshal([]byte(creatorRefJson), &sr)
  61. if err != nil {
  62. glog.V(4).Infof("Job with unparsable created-by annotation, name %s namespace %s: %v", j.Name, j.Namespace, err)
  63. return types.UID(""), false
  64. }
  65. if sr.Reference.Kind != "ScheduledJob" {
  66. glog.V(4).Infof("Job with non-ScheduledJob parent, name %s namespace %s", j.Name, j.Namespace)
  67. return types.UID(""), false
  68. }
  69. // Don't believe a job that claims to have a parent in a different namespace.
  70. if sr.Reference.Namespace != j.Namespace {
  71. glog.V(4).Infof("Alleged scheduledJob parent in different namespace (%s) from Job name %s namespace %s", sr.Reference.Namespace, j.Name, j.Namespace)
  72. return types.UID(""), false
  73. }
  74. return sr.Reference.UID, true
  75. }
  76. // groupJobsByParent groups jobs into a map keyed by the job parent UID (e.g. scheduledJob).
  77. // It has no receiver, to facilitate testing.
  78. func groupJobsByParent(sjs []batch.ScheduledJob, js []batch.Job) map[types.UID][]batch.Job {
  79. jobsBySj := make(map[types.UID][]batch.Job)
  80. for _, job := range js {
  81. parentUID, found := getParentUIDFromJob(job)
  82. if !found {
  83. glog.Errorf("Unable to get uid from job %s in namespace %s", job.Name, job.Namespace)
  84. continue
  85. }
  86. jobsBySj[parentUID] = append(jobsBySj[parentUID], job)
  87. }
  88. return jobsBySj
  89. }
  90. // getNextStartTimeAfter gets the latest scheduled start time that is less than "now", or an error.
  91. func getNextStartTimeAfter(schedule string, now time.Time) (time.Time, error) {
  92. // Using robfig/cron for cron scheduled parsing and next runtime
  93. // computation. Not using the entire library because:
  94. // - I want to detect when we missed a runtime due to being down.
  95. // - How do I set the time such that I can detect the last known runtime?
  96. // - I guess the functions could launch a go-routine to start the job and
  97. // then return.
  98. // How to handle concurrency control.
  99. // How to detect changes to schedules or deleted schedules and then
  100. // update the jobs?
  101. tmpSched := addSeconds(schedule)
  102. sched, err := cron.Parse(tmpSched)
  103. if err != nil {
  104. return time.Unix(0, 0), fmt.Errorf("Unparseable schedule: %s : %s", schedule, err)
  105. }
  106. return sched.Next(now), nil
  107. }
  108. // getRecentUnmetScheduleTimes gets a slice of times (from oldest to latest) that have passed when a Job should have started but did not.
  109. //
  110. // If there are too many (>100) unstarted times, just give up and return an empty slice.
  111. // If there were missed times prior to the last known start time, then those are not returned.
  112. func getRecentUnmetScheduleTimes(sj batch.ScheduledJob, now time.Time) ([]time.Time, error) {
  113. starts := []time.Time{}
  114. tmpSched := addSeconds(sj.Spec.Schedule)
  115. sched, err := cron.Parse(tmpSched)
  116. if err != nil {
  117. return starts, fmt.Errorf("Unparseable schedule: %s : %s", sj.Spec.Schedule, err)
  118. }
  119. var earliestTime time.Time
  120. if sj.Status.LastScheduleTime != nil {
  121. earliestTime = sj.Status.LastScheduleTime.Time
  122. } else {
  123. // If none found, then this is either a recently created scheduledJob,
  124. // or the active/completed info was somehow lost (contract for status
  125. // in kubernetes says it may need to be recreated), or that we have
  126. // started a job, but have not noticed it yet (distributed systems can
  127. // have arbitrary delays). In any case, use the creation time of the
  128. // ScheduledJob as last known start time.
  129. earliestTime = sj.ObjectMeta.CreationTimestamp.Time
  130. }
  131. if earliestTime.After(now) {
  132. return []time.Time{}, nil
  133. }
  134. for t := sched.Next(earliestTime); !t.After(now); t = sched.Next(t) {
  135. starts = append(starts, t)
  136. // An object might miss several starts. For example, if
  137. // controller gets wedged on friday at 5:01pm when everyone has
  138. // gone home, and someone comes in on tuesday AM and discovers
  139. // the problem and restarts the controller, then all the hourly
  140. // jobs, more than 80 of them for one hourly scheduledJob, should
  141. // all start running with no further intervention (if the scheduledJob
  142. // allows concurrency and late starts).
  143. //
  144. // However, if there is a bug somewhere, or incorrect clock
  145. // on controller's server or apiservers (for setting creationTimestamp)
  146. // then there could be so many missed start times (it could be off
  147. // by decades or more), that it would eat up all the CPU and memory
  148. // of this controller. In that case, we want to not try to list
  149. // all the misseded start times.
  150. //
  151. // I've somewhat arbitrarily picked 100, as more than 80, but
  152. // but less than "lots".
  153. if len(starts) > 100 {
  154. // We can't get the most recent times so just return an empty slice
  155. return []time.Time{}, fmt.Errorf("Too many missed start times to list")
  156. }
  157. }
  158. return starts, nil
  159. }
  160. // TODO soltysh: this should be removed when https://github.com/robfig/cron/issues/58 is fixed
  161. func addSeconds(schedule string) string {
  162. tmpSched := schedule
  163. if len(schedule) > 0 && schedule[0] != '@' {
  164. tmpSched = "0 " + schedule
  165. }
  166. return tmpSched
  167. }
  168. // XXX unit test this
  169. // getJobFromTemplate makes a Job from a ScheduledJob
  170. func getJobFromTemplate(sj *batch.ScheduledJob, scheduledTime time.Time) (*batch.Job, error) {
  171. // TODO: consider adding the following labels:
  172. // nominal-start-time=$RFC_3339_DATE_OF_INTENDED_START -- for user convenience
  173. // scheduled-job-name=$SJ_NAME -- for user convenience
  174. labels := copyLabels(&sj.Spec.JobTemplate)
  175. annotations := copyAnnotations(&sj.Spec.JobTemplate)
  176. createdByRefJson, err := makeCreatedByRefJson(sj)
  177. if err != nil {
  178. return nil, err
  179. }
  180. annotations[CreatedByAnnotation] = string(createdByRefJson)
  181. // We want job names for a given nominal start time to have a deterministic name to avoid the same job being created twice
  182. name := fmt.Sprintf("%s-%d", sj.Name, getTimeHash(scheduledTime))
  183. job := &batch.Job{
  184. ObjectMeta: api.ObjectMeta{
  185. Labels: labels,
  186. Annotations: annotations,
  187. Name: name,
  188. },
  189. }
  190. if err := api.Scheme.Convert(&sj.Spec.JobTemplate.Spec, &job.Spec, nil); err != nil {
  191. return nil, fmt.Errorf("unable to convert job template: %v", err)
  192. }
  193. return job, nil
  194. }
  195. func getTimeHash(scheduledTime time.Time) uint32 {
  196. timeHasher := adler32.New()
  197. hashutil.DeepHashObject(timeHasher, scheduledTime)
  198. return timeHasher.Sum32()
  199. }
  200. // makeCreatedByRefJson makes a json string with an object reference for use in "created-by" annotation value
  201. func makeCreatedByRefJson(object runtime.Object) (string, error) {
  202. createdByRef, err := api.GetReference(object)
  203. if err != nil {
  204. return "", fmt.Errorf("unable to get controller reference: %v", err)
  205. }
  206. // TODO: this code was not safe previously - as soon as new code came along that switched to v2, old clients
  207. // would be broken upon reading it. This is explicitly hardcoded to v1 to guarantee predictable deployment.
  208. // We need to consistently handle this case of annotation versioning.
  209. codec := api.Codecs.LegacyCodec(unversioned.GroupVersion{Group: api.GroupName, Version: "v1"})
  210. createdByRefJson, err := runtime.Encode(codec, &api.SerializedReference{
  211. Reference: *createdByRef,
  212. })
  213. if err != nil {
  214. return "", fmt.Errorf("unable to serialize controller reference: %v", err)
  215. }
  216. return string(createdByRefJson), nil
  217. }