k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/kubelet/images/puller.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package images 18 19 import ( 20 "context" 21 "time" 22 23 v1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/util/wait" 25 runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" 26 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" 27 ) 28 29 type pullResult struct { 30 imageRef string 31 imageSize uint64 32 err error 33 pullDuration time.Duration 34 } 35 36 type imagePuller interface { 37 pullImage(context.Context, kubecontainer.ImageSpec, []v1.Secret, chan<- pullResult, *runtimeapi.PodSandboxConfig) 38 } 39 40 var _, _ imagePuller = ¶llelImagePuller{}, &serialImagePuller{} 41 42 type parallelImagePuller struct { 43 imageService kubecontainer.ImageService 44 tokens chan struct{} 45 } 46 47 func newParallelImagePuller(imageService kubecontainer.ImageService, maxParallelImagePulls *int32) imagePuller { 48 if maxParallelImagePulls == nil || *maxParallelImagePulls < 1 { 49 return ¶llelImagePuller{imageService, nil} 50 } 51 return ¶llelImagePuller{imageService, make(chan struct{}, *maxParallelImagePulls)} 52 } 53 54 func (pip *parallelImagePuller) pullImage(ctx context.Context, spec kubecontainer.ImageSpec, pullSecrets []v1.Secret, pullChan chan<- pullResult, podSandboxConfig *runtimeapi.PodSandboxConfig) { 55 go func() { 56 if pip.tokens != nil { 57 pip.tokens <- struct{}{} 58 defer func() { <-pip.tokens }() 59 } 60 startTime := time.Now() 61 imageRef, err := pip.imageService.PullImage(ctx, spec, pullSecrets, podSandboxConfig) 62 var size uint64 63 if err == nil && imageRef != "" { 64 // Getting the image size with best effort, ignoring the error. 65 size, _ = pip.imageService.GetImageSize(ctx, spec) 66 } 67 pullChan <- pullResult{ 68 imageRef: imageRef, 69 imageSize: size, 70 err: err, 71 pullDuration: time.Since(startTime), 72 } 73 }() 74 } 75 76 // Maximum number of image pull requests than can be queued. 77 const maxImagePullRequests = 10 78 79 type serialImagePuller struct { 80 imageService kubecontainer.ImageService 81 pullRequests chan *imagePullRequest 82 } 83 84 func newSerialImagePuller(imageService kubecontainer.ImageService) imagePuller { 85 imagePuller := &serialImagePuller{imageService, make(chan *imagePullRequest, maxImagePullRequests)} 86 go wait.Until(imagePuller.processImagePullRequests, time.Second, wait.NeverStop) 87 return imagePuller 88 } 89 90 type imagePullRequest struct { 91 ctx context.Context 92 spec kubecontainer.ImageSpec 93 pullSecrets []v1.Secret 94 pullChan chan<- pullResult 95 podSandboxConfig *runtimeapi.PodSandboxConfig 96 } 97 98 func (sip *serialImagePuller) pullImage(ctx context.Context, spec kubecontainer.ImageSpec, pullSecrets []v1.Secret, pullChan chan<- pullResult, podSandboxConfig *runtimeapi.PodSandboxConfig) { 99 sip.pullRequests <- &imagePullRequest{ 100 ctx: ctx, 101 spec: spec, 102 pullSecrets: pullSecrets, 103 pullChan: pullChan, 104 podSandboxConfig: podSandboxConfig, 105 } 106 } 107 108 func (sip *serialImagePuller) processImagePullRequests() { 109 for pullRequest := range sip.pullRequests { 110 startTime := time.Now() 111 imageRef, err := sip.imageService.PullImage(pullRequest.ctx, pullRequest.spec, pullRequest.pullSecrets, pullRequest.podSandboxConfig) 112 var size uint64 113 if err == nil && imageRef != "" { 114 // Getting the image size with best effort, ignoring the error. 115 size, _ = sip.imageService.GetImageSize(pullRequest.ctx, pullRequest.spec) 116 } 117 pullRequest.pullChan <- pullResult{ 118 imageRef: imageRef, 119 imageSize: size, 120 err: err, 121 // Note: pullDuration includes credential resolution and getting the image size. 122 pullDuration: time.Since(startTime), 123 } 124 } 125 }