1/* 2Copyright 2016 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package images 18 19import ( 20 "time" 21 22 "k8s.io/api/core/v1" 23 "k8s.io/apimachinery/pkg/util/wait" 24 runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" 25 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" 26) 27 28type pullResult struct { 29 imageRef string 30 err error 31} 32 33type imagePuller interface { 34 pullImage(kubecontainer.ImageSpec, []v1.Secret, chan<- pullResult, *runtimeapi.PodSandboxConfig) 35} 36 37var _, _ imagePuller = ¶llelImagePuller{}, &serialImagePuller{} 38 39type parallelImagePuller struct { 40 imageService kubecontainer.ImageService 41} 42 43func newParallelImagePuller(imageService kubecontainer.ImageService) imagePuller { 44 return ¶llelImagePuller{imageService} 45} 46 47func (pip *parallelImagePuller) pullImage(spec kubecontainer.ImageSpec, pullSecrets []v1.Secret, pullChan chan<- pullResult, podSandboxConfig *runtimeapi.PodSandboxConfig) { 48 go func() { 49 imageRef, err := pip.imageService.PullImage(spec, pullSecrets, podSandboxConfig) 50 pullChan <- pullResult{ 51 imageRef: imageRef, 52 err: err, 53 } 54 }() 55} 56 57// Maximum number of image pull requests than can be queued. 58const maxImagePullRequests = 10 59 60type serialImagePuller struct { 61 imageService kubecontainer.ImageService 62 pullRequests chan *imagePullRequest 63} 64 65func newSerialImagePuller(imageService kubecontainer.ImageService) imagePuller { 66 imagePuller := &serialImagePuller{imageService, make(chan *imagePullRequest, maxImagePullRequests)} 67 go wait.Until(imagePuller.processImagePullRequests, time.Second, wait.NeverStop) 68 return imagePuller 69} 70 71type imagePullRequest struct { 72 spec kubecontainer.ImageSpec 73 pullSecrets []v1.Secret 74 pullChan chan<- pullResult 75 podSandboxConfig *runtimeapi.PodSandboxConfig 76} 77 78func (sip *serialImagePuller) pullImage(spec kubecontainer.ImageSpec, pullSecrets []v1.Secret, pullChan chan<- pullResult, podSandboxConfig *runtimeapi.PodSandboxConfig) { 79 sip.pullRequests <- &imagePullRequest{ 80 spec: spec, 81 pullSecrets: pullSecrets, 82 pullChan: pullChan, 83 podSandboxConfig: podSandboxConfig, 84 } 85} 86 87func (sip *serialImagePuller) processImagePullRequests() { 88 for pullRequest := range sip.pullRequests { 89 imageRef, err := sip.imageService.PullImage(pullRequest.spec, pullRequest.pullSecrets, pullRequest.podSandboxConfig) 90 pullRequest.pullChan <- pullResult{ 91 imageRef: imageRef, 92 err: err, 93 } 94 } 95} 96