2015-08-10 17:28:39 +00:00
|
|
|
/*
|
2016-07-14 01:05:18 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2015-08-10 17:28:39 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-07-14 01:05:18 +00:00
|
|
|
package images
|
2015-08-10 17:28:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-09-03 21:40:58 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/record"
|
2016-07-14 22:54:02 +00:00
|
|
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
2016-07-13 00:32:24 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/events"
|
2016-03-09 02:58:24 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/flowcontrol"
|
2015-08-10 17:28:39 +00:00
|
|
|
)
|
|
|
|
|
2016-07-15 21:54:06 +00:00
|
|
|
// imageManager provides the functionalities for image pulling.
|
|
|
|
type imageManager struct {
|
2016-09-21 21:26:17 +00:00
|
|
|
recorder record.EventRecorder
|
|
|
|
imageService kubecontainer.ImageService
|
|
|
|
backOff *flowcontrol.Backoff
|
2016-07-15 21:54:06 +00:00
|
|
|
// It will check the presence of the image, and report the 'image pulling', image pulled' events correspondingly.
|
|
|
|
puller imagePuller
|
2015-08-10 17:28:39 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 21:54:06 +00:00
|
|
|
var _ ImageManager = &imageManager{}
|
2015-10-20 21:49:44 +00:00
|
|
|
|
2016-09-21 21:26:17 +00:00
|
|
|
func NewImageManager(recorder record.EventRecorder, imageService kubecontainer.ImageService, imageBackOff *flowcontrol.Backoff, serialized bool, qps float32, burst int) ImageManager {
|
|
|
|
imageService = throttleImagePulling(imageService, qps, burst)
|
|
|
|
|
2016-07-15 21:54:06 +00:00
|
|
|
var puller imagePuller
|
|
|
|
if serialized {
|
2016-09-21 21:26:17 +00:00
|
|
|
puller = newSerialImagePuller(imageService)
|
2016-07-15 21:54:06 +00:00
|
|
|
} else {
|
2016-09-21 21:26:17 +00:00
|
|
|
puller = newParallelImagePuller(imageService)
|
2016-07-15 21:54:06 +00:00
|
|
|
}
|
|
|
|
return &imageManager{
|
2016-09-21 21:26:17 +00:00
|
|
|
recorder: recorder,
|
|
|
|
imageService: imageService,
|
|
|
|
backOff: imageBackOff,
|
|
|
|
puller: puller,
|
2015-08-10 17:28:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// shouldPullImage returns whether we should pull an image according to
|
|
|
|
// the presence and pull policy of the image.
|
|
|
|
func shouldPullImage(container *api.Container, imagePresent bool) bool {
|
|
|
|
if container.ImagePullPolicy == api.PullNever {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.ImagePullPolicy == api.PullAlways ||
|
|
|
|
(container.ImagePullPolicy == api.PullIfNotPresent && (!imagePresent)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-10-02 13:45:46 +00:00
|
|
|
// records an event using ref, event msg. log to glog using prefix, msg, logFn
|
2016-07-15 21:54:06 +00:00
|
|
|
func (m *imageManager) logIt(ref *api.ObjectReference, eventtype, event, prefix, msg string, logFn func(args ...interface{})) {
|
2015-10-02 13:45:46 +00:00
|
|
|
if ref != nil {
|
2016-07-15 21:54:06 +00:00
|
|
|
m.recorder.Event(ref, eventtype, event, msg)
|
2015-10-02 13:45:46 +00:00
|
|
|
} else {
|
|
|
|
logFn(fmt.Sprint(prefix, " ", msg))
|
2015-08-10 17:28:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 21:54:06 +00:00
|
|
|
// EnsureImageExists pulls the image for the specified pod and container.
|
|
|
|
func (m *imageManager) EnsureImageExists(pod *api.Pod, container *api.Container, pullSecrets []api.Secret) (error, string) {
|
2015-10-02 13:45:46 +00:00
|
|
|
logPrefix := fmt.Sprintf("%s/%s", pod.Name, container.Image)
|
2016-07-14 22:54:02 +00:00
|
|
|
ref, err := kubecontainer.GenerateContainerRef(pod, container)
|
2015-08-10 17:28:39 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Couldn't make a ref to pod %v, container %v: '%v'", pod.Name, container.Name, err)
|
|
|
|
}
|
2015-10-20 00:35:33 +00:00
|
|
|
|
2016-07-14 22:54:02 +00:00
|
|
|
spec := kubecontainer.ImageSpec{Image: container.Image}
|
2016-09-21 21:26:17 +00:00
|
|
|
present, err := m.imageService.IsImagePresent(spec)
|
2015-08-10 17:28:39 +00:00
|
|
|
if err != nil {
|
2015-10-02 13:45:46 +00:00
|
|
|
msg := fmt.Sprintf("Failed to inspect image %q: %v", container.Image, err)
|
2016-07-15 21:54:06 +00:00
|
|
|
m.logIt(ref, api.EventTypeWarning, events.FailedToInspectImage, logPrefix, msg, glog.Warning)
|
2016-07-19 22:42:21 +00:00
|
|
|
return ErrImageInspect, msg
|
2015-08-10 17:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !shouldPullImage(container, present) {
|
2015-10-02 13:45:46 +00:00
|
|
|
if present {
|
|
|
|
msg := fmt.Sprintf("Container image %q already present on machine", container.Image)
|
2016-07-15 21:54:06 +00:00
|
|
|
m.logIt(ref, api.EventTypeNormal, events.PulledImage, logPrefix, msg, glog.Info)
|
2015-10-02 13:45:46 +00:00
|
|
|
return nil, ""
|
|
|
|
} else {
|
|
|
|
msg := fmt.Sprintf("Container image %q is not present with pull policy of Never", container.Image)
|
2016-07-15 21:54:06 +00:00
|
|
|
m.logIt(ref, api.EventTypeWarning, events.ErrImageNeverPullPolicy, logPrefix, msg, glog.Warning)
|
2016-07-19 22:42:21 +00:00
|
|
|
return ErrImageNeverPull, msg
|
2015-08-10 17:28:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 01:01:55 +00:00
|
|
|
backOffKey := fmt.Sprintf("%s_%s", pod.UID, container.Image)
|
2016-07-15 21:54:06 +00:00
|
|
|
if m.backOff.IsInBackOffSinceUpdate(backOffKey, m.backOff.Clock.Now()) {
|
2015-10-02 13:45:46 +00:00
|
|
|
msg := fmt.Sprintf("Back-off pulling image %q", container.Image)
|
2016-07-15 21:54:06 +00:00
|
|
|
m.logIt(ref, api.EventTypeNormal, events.BackOffPullImage, logPrefix, msg, glog.Info)
|
2016-07-19 22:42:21 +00:00
|
|
|
return ErrImagePullBackOff, msg
|
2015-10-02 13:45:46 +00:00
|
|
|
}
|
2016-07-15 21:54:06 +00:00
|
|
|
m.logIt(ref, api.EventTypeNormal, events.PullingImage, logPrefix, fmt.Sprintf("pulling image %q", container.Image), glog.Info)
|
|
|
|
errChan := make(chan error)
|
|
|
|
m.puller.pullImage(spec, pullSecrets, errChan)
|
|
|
|
if err := <-errChan; err != nil {
|
|
|
|
m.logIt(ref, api.EventTypeWarning, events.FailedToPullImage, logPrefix, fmt.Sprintf("Failed to pull image %q: %v", container.Image, err), glog.Warning)
|
|
|
|
m.backOff.Next(backOffKey, m.backOff.Clock.Now())
|
2016-07-19 22:42:21 +00:00
|
|
|
if err == RegistryUnavailable {
|
2016-04-01 17:39:19 +00:00
|
|
|
msg := fmt.Sprintf("image pull failed for %s because the registry is unavailable.", container.Image)
|
2015-09-18 16:09:16 +00:00
|
|
|
return err, msg
|
|
|
|
} else {
|
2016-07-19 22:42:21 +00:00
|
|
|
return ErrImagePull, err.Error()
|
2015-09-18 16:09:16 +00:00
|
|
|
}
|
2015-08-10 17:28:39 +00:00
|
|
|
}
|
2016-07-15 21:54:06 +00:00
|
|
|
m.logIt(ref, api.EventTypeNormal, events.PulledImage, logPrefix, fmt.Sprintf("Successfully pulled image %q", container.Image), glog.Info)
|
|
|
|
m.backOff.GC()
|
2015-10-02 13:45:46 +00:00
|
|
|
return nil, ""
|
2015-08-10 17:28:39 +00:00
|
|
|
}
|