image_manager.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 images
  14. import (
  15. "fmt"
  16. "github.com/golang/glog"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/client/record"
  19. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  20. "k8s.io/kubernetes/pkg/kubelet/events"
  21. "k8s.io/kubernetes/pkg/util/flowcontrol"
  22. )
  23. // imageManager provides the functionalities for image pulling.
  24. type imageManager struct {
  25. recorder record.EventRecorder
  26. runtime kubecontainer.Runtime
  27. backOff *flowcontrol.Backoff
  28. // It will check the presence of the image, and report the 'image pulling', image pulled' events correspondingly.
  29. puller imagePuller
  30. }
  31. var _ ImageManager = &imageManager{}
  32. func NewImageManager(recorder record.EventRecorder, runtime kubecontainer.Runtime, imageBackOff *flowcontrol.Backoff, serialized bool) ImageManager {
  33. var puller imagePuller
  34. if serialized {
  35. puller = newSerialImagePuller(runtime)
  36. } else {
  37. puller = newParallelImagePuller(runtime)
  38. }
  39. return &imageManager{
  40. recorder: recorder,
  41. runtime: runtime,
  42. backOff: imageBackOff,
  43. puller: puller,
  44. }
  45. }
  46. // shouldPullImage returns whether we should pull an image according to
  47. // the presence and pull policy of the image.
  48. func shouldPullImage(container *api.Container, imagePresent bool) bool {
  49. if container.ImagePullPolicy == api.PullNever {
  50. return false
  51. }
  52. if container.ImagePullPolicy == api.PullAlways ||
  53. (container.ImagePullPolicy == api.PullIfNotPresent && (!imagePresent)) {
  54. return true
  55. }
  56. return false
  57. }
  58. // records an event using ref, event msg. log to glog using prefix, msg, logFn
  59. func (m *imageManager) logIt(ref *api.ObjectReference, eventtype, event, prefix, msg string, logFn func(args ...interface{})) {
  60. if ref != nil {
  61. m.recorder.Event(ref, eventtype, event, msg)
  62. } else {
  63. logFn(fmt.Sprint(prefix, " ", msg))
  64. }
  65. }
  66. // EnsureImageExists pulls the image for the specified pod and container.
  67. func (m *imageManager) EnsureImageExists(pod *api.Pod, container *api.Container, pullSecrets []api.Secret) (error, string) {
  68. logPrefix := fmt.Sprintf("%s/%s", pod.Name, container.Image)
  69. ref, err := kubecontainer.GenerateContainerRef(pod, container)
  70. if err != nil {
  71. glog.Errorf("Couldn't make a ref to pod %v, container %v: '%v'", pod.Name, container.Name, err)
  72. }
  73. spec := kubecontainer.ImageSpec{Image: container.Image}
  74. present, err := m.runtime.IsImagePresent(spec)
  75. if err != nil {
  76. msg := fmt.Sprintf("Failed to inspect image %q: %v", container.Image, err)
  77. m.logIt(ref, api.EventTypeWarning, events.FailedToInspectImage, logPrefix, msg, glog.Warning)
  78. return ErrImageInspect, msg
  79. }
  80. if !shouldPullImage(container, present) {
  81. if present {
  82. msg := fmt.Sprintf("Container image %q already present on machine", container.Image)
  83. m.logIt(ref, api.EventTypeNormal, events.PulledImage, logPrefix, msg, glog.Info)
  84. return nil, ""
  85. } else {
  86. msg := fmt.Sprintf("Container image %q is not present with pull policy of Never", container.Image)
  87. m.logIt(ref, api.EventTypeWarning, events.ErrImageNeverPullPolicy, logPrefix, msg, glog.Warning)
  88. return ErrImageNeverPull, msg
  89. }
  90. }
  91. backOffKey := fmt.Sprintf("%s_%s", pod.UID, container.Image)
  92. if m.backOff.IsInBackOffSinceUpdate(backOffKey, m.backOff.Clock.Now()) {
  93. msg := fmt.Sprintf("Back-off pulling image %q", container.Image)
  94. m.logIt(ref, api.EventTypeNormal, events.BackOffPullImage, logPrefix, msg, glog.Info)
  95. return ErrImagePullBackOff, msg
  96. }
  97. m.logIt(ref, api.EventTypeNormal, events.PullingImage, logPrefix, fmt.Sprintf("pulling image %q", container.Image), glog.Info)
  98. errChan := make(chan error)
  99. m.puller.pullImage(spec, pullSecrets, errChan)
  100. if err := <-errChan; err != nil {
  101. m.logIt(ref, api.EventTypeWarning, events.FailedToPullImage, logPrefix, fmt.Sprintf("Failed to pull image %q: %v", container.Image, err), glog.Warning)
  102. m.backOff.Next(backOffKey, m.backOff.Clock.Now())
  103. if err == RegistryUnavailable {
  104. msg := fmt.Sprintf("image pull failed for %s because the registry is unavailable.", container.Image)
  105. return err, msg
  106. } else {
  107. return ErrImagePull, err.Error()
  108. }
  109. }
  110. m.logIt(ref, api.EventTypeNormal, events.PulledImage, logPrefix, fmt.Sprintf("Successfully pulled image %q", container.Image), glog.Info)
  111. m.backOff.GC()
  112. return nil, ""
  113. }