utils.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. "time"
  17. "github.com/robfig/cron"
  18. "k8s.io/klog"
  19. batchv1 "k8s.io/api/batch/v1"
  20. batchv1beta1 "k8s.io/api/batch/v1beta1"
  21. "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/types"
  24. "k8s.io/kubernetes/pkg/api/legacyscheme"
  25. )
  26. // Utilities for dealing with Jobs and CronJobs and time.
  27. func inActiveList(sj batchv1beta1.CronJob, uid types.UID) bool {
  28. for _, j := range sj.Status.Active {
  29. if j.UID == uid {
  30. return true
  31. }
  32. }
  33. return false
  34. }
  35. func deleteFromActiveList(sj *batchv1beta1.CronJob, uid types.UID) {
  36. if sj == nil {
  37. return
  38. }
  39. newActive := []v1.ObjectReference{}
  40. for _, j := range sj.Status.Active {
  41. if j.UID != uid {
  42. newActive = append(newActive, j)
  43. }
  44. }
  45. sj.Status.Active = newActive
  46. }
  47. // getParentUIDFromJob extracts UID of job's parent and whether it was found
  48. func getParentUIDFromJob(j batchv1.Job) (types.UID, bool) {
  49. controllerRef := metav1.GetControllerOf(&j)
  50. if controllerRef == nil {
  51. return types.UID(""), false
  52. }
  53. if controllerRef.Kind != "CronJob" {
  54. klog.V(4).Infof("Job with non-CronJob parent, name %s namespace %s", j.Name, j.Namespace)
  55. return types.UID(""), false
  56. }
  57. return controllerRef.UID, true
  58. }
  59. // groupJobsByParent groups jobs into a map keyed by the job parent UID (e.g. scheduledJob).
  60. // It has no receiver, to facilitate testing.
  61. func groupJobsByParent(js []batchv1.Job) map[types.UID][]batchv1.Job {
  62. jobsBySj := make(map[types.UID][]batchv1.Job)
  63. for _, job := range js {
  64. parentUID, found := getParentUIDFromJob(job)
  65. if !found {
  66. klog.V(4).Infof("Unable to get parent uid from job %s in namespace %s", job.Name, job.Namespace)
  67. continue
  68. }
  69. jobsBySj[parentUID] = append(jobsBySj[parentUID], job)
  70. }
  71. return jobsBySj
  72. }
  73. // getRecentUnmetScheduleTimes gets a slice of times (from oldest to latest) that have passed when a Job should have started but did not.
  74. //
  75. // If there are too many (>100) unstarted times, just give up and return an empty slice.
  76. // If there were missed times prior to the last known start time, then those are not returned.
  77. func getRecentUnmetScheduleTimes(sj batchv1beta1.CronJob, now time.Time) ([]time.Time, error) {
  78. starts := []time.Time{}
  79. sched, err := cron.ParseStandard(sj.Spec.Schedule)
  80. if err != nil {
  81. return starts, fmt.Errorf("Unparseable schedule: %s : %s", sj.Spec.Schedule, err)
  82. }
  83. var earliestTime time.Time
  84. if sj.Status.LastScheduleTime != nil {
  85. earliestTime = sj.Status.LastScheduleTime.Time
  86. } else {
  87. // If none found, then this is either a recently created scheduledJob,
  88. // or the active/completed info was somehow lost (contract for status
  89. // in kubernetes says it may need to be recreated), or that we have
  90. // started a job, but have not noticed it yet (distributed systems can
  91. // have arbitrary delays). In any case, use the creation time of the
  92. // CronJob as last known start time.
  93. earliestTime = sj.ObjectMeta.CreationTimestamp.Time
  94. }
  95. if sj.Spec.StartingDeadlineSeconds != nil {
  96. // Controller is not going to schedule anything below this point
  97. schedulingDeadline := now.Add(-time.Second * time.Duration(*sj.Spec.StartingDeadlineSeconds))
  98. if schedulingDeadline.After(earliestTime) {
  99. earliestTime = schedulingDeadline
  100. }
  101. }
  102. if earliestTime.After(now) {
  103. return []time.Time{}, nil
  104. }
  105. for t := sched.Next(earliestTime); !t.After(now); t = sched.Next(t) {
  106. starts = append(starts, t)
  107. // An object might miss several starts. For example, if
  108. // controller gets wedged on friday at 5:01pm when everyone has
  109. // gone home, and someone comes in on tuesday AM and discovers
  110. // the problem and restarts the controller, then all the hourly
  111. // jobs, more than 80 of them for one hourly scheduledJob, should
  112. // all start running with no further intervention (if the scheduledJob
  113. // allows concurrency and late starts).
  114. //
  115. // However, if there is a bug somewhere, or incorrect clock
  116. // on controller's server or apiservers (for setting creationTimestamp)
  117. // then there could be so many missed start times (it could be off
  118. // by decades or more), that it would eat up all the CPU and memory
  119. // of this controller. In that case, we want to not try to list
  120. // all the missed start times.
  121. //
  122. // I've somewhat arbitrarily picked 100, as more than 80,
  123. // but less than "lots".
  124. if len(starts) > 100 {
  125. // We can't get the most recent times so just return an empty slice
  126. return []time.Time{}, fmt.Errorf("too many missed start time (> 100). Set or decrease .spec.startingDeadlineSeconds or check clock skew")
  127. }
  128. }
  129. return starts, nil
  130. }
  131. // getJobFromTemplate makes a Job from a CronJob
  132. func getJobFromTemplate(sj *batchv1beta1.CronJob, scheduledTime time.Time) (*batchv1.Job, error) {
  133. labels := copyLabels(&sj.Spec.JobTemplate)
  134. annotations := copyAnnotations(&sj.Spec.JobTemplate)
  135. // We want job names for a given nominal start time to have a deterministic name to avoid the same job being created twice
  136. name := fmt.Sprintf("%s-%d", sj.Name, getTimeHash(scheduledTime))
  137. job := &batchv1.Job{
  138. ObjectMeta: metav1.ObjectMeta{
  139. Labels: labels,
  140. Annotations: annotations,
  141. Name: name,
  142. OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(sj, controllerKind)},
  143. },
  144. }
  145. if err := legacyscheme.Scheme.Convert(&sj.Spec.JobTemplate.Spec, &job.Spec, nil); err != nil {
  146. return nil, fmt.Errorf("unable to convert job template: %v", err)
  147. }
  148. return job, nil
  149. }
  150. // getTimeHash returns Unix Epoch Time
  151. func getTimeHash(scheduledTime time.Time) int64 {
  152. return scheduledTime.Unix()
  153. }
  154. func getFinishedStatus(j *batchv1.Job) (bool, batchv1.JobConditionType) {
  155. for _, c := range j.Status.Conditions {
  156. if (c.Type == batchv1.JobComplete || c.Type == batchv1.JobFailed) && c.Status == v1.ConditionTrue {
  157. return true, c.Type
  158. }
  159. }
  160. return false, ""
  161. }
  162. // IsJobFinished returns whether or not a job has completed successfully or failed.
  163. func IsJobFinished(j *batchv1.Job) bool {
  164. isFinished, _ := getFinishedStatus(j)
  165. return isFinished
  166. }
  167. // byJobStartTime sorts a list of jobs by start timestamp, using their names as a tie breaker.
  168. type byJobStartTime []batchv1.Job
  169. func (o byJobStartTime) Len() int { return len(o) }
  170. func (o byJobStartTime) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
  171. func (o byJobStartTime) Less(i, j int) bool {
  172. if o[i].Status.StartTime == nil && o[j].Status.StartTime != nil {
  173. return false
  174. }
  175. if o[i].Status.StartTime != nil && o[j].Status.StartTime == nil {
  176. return true
  177. }
  178. if o[i].Status.StartTime.Equal(o[j].Status.StartTime) {
  179. return o[i].Name < o[j].Name
  180. }
  181. return o[i].Status.StartTime.Before(o[j].Status.StartTime)
  182. }