active_deadline.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 kubelet
  14. import (
  15. "fmt"
  16. "time"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/client/record"
  19. "k8s.io/kubernetes/pkg/kubelet/lifecycle"
  20. "k8s.io/kubernetes/pkg/kubelet/status"
  21. "k8s.io/kubernetes/pkg/util/clock"
  22. )
  23. const (
  24. reason = "DeadlineExceeded"
  25. message = "Pod was active on the node longer than the specified deadline"
  26. )
  27. // activeDeadlineHandler knows how to enforce active deadlines on pods.
  28. type activeDeadlineHandler struct {
  29. // the clock to use for deadline enforcement
  30. clock clock.Clock
  31. // the provider of pod status
  32. podStatusProvider status.PodStatusProvider
  33. // the recorder to dispatch events when we identify a pod has exceeded active deadline
  34. recorder record.EventRecorder
  35. }
  36. // newActiveDeadlineHandler returns an active deadline handler that can enforce pod active deadline
  37. func newActiveDeadlineHandler(
  38. podStatusProvider status.PodStatusProvider,
  39. recorder record.EventRecorder,
  40. clock clock.Clock,
  41. ) (*activeDeadlineHandler, error) {
  42. // check for all required fields
  43. if clock == nil || podStatusProvider == nil || recorder == nil {
  44. return nil, fmt.Errorf("Required arguments must not be nil: %v, %v, %v", clock, podStatusProvider, recorder)
  45. }
  46. return &activeDeadlineHandler{
  47. clock: clock,
  48. podStatusProvider: podStatusProvider,
  49. recorder: recorder,
  50. }, nil
  51. }
  52. // ShouldSync returns true if the pod is past its active deadline.
  53. func (m *activeDeadlineHandler) ShouldSync(pod *api.Pod) bool {
  54. return m.pastActiveDeadline(pod)
  55. }
  56. // ShouldEvict returns true if the pod is past its active deadline.
  57. // It dispatches an event that the pod should be evicted if it is past its deadline.
  58. func (m *activeDeadlineHandler) ShouldEvict(pod *api.Pod) lifecycle.ShouldEvictResponse {
  59. if !m.pastActiveDeadline(pod) {
  60. return lifecycle.ShouldEvictResponse{Evict: false}
  61. }
  62. m.recorder.Eventf(pod, api.EventTypeNormal, reason, message)
  63. return lifecycle.ShouldEvictResponse{Evict: true, Reason: reason, Message: message}
  64. }
  65. // pastActiveDeadline returns true if the pod has been active for more than its ActiveDeadlineSeconds
  66. func (m *activeDeadlineHandler) pastActiveDeadline(pod *api.Pod) bool {
  67. // no active deadline was specified
  68. if pod.Spec.ActiveDeadlineSeconds == nil {
  69. return false
  70. }
  71. // get the latest status to determine if it was started
  72. podStatus, ok := m.podStatusProvider.GetPodStatus(pod.UID)
  73. if !ok {
  74. podStatus = pod.Status
  75. }
  76. // we have no start time so just return
  77. if podStatus.StartTime.IsZero() {
  78. return false
  79. }
  80. // determine if the deadline was exceeded
  81. start := podStatus.StartTime.Time
  82. duration := m.clock.Since(start)
  83. allowedDuration := time.Duration(*pod.Spec.ActiveDeadlineSeconds) * time.Second
  84. return duration >= allowedDuration
  85. }