puller.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "time"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/util/wait"
  18. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  19. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  20. )
  21. type pullResult struct {
  22. imageRef string
  23. err error
  24. }
  25. type imagePuller interface {
  26. pullImage(kubecontainer.ImageSpec, []v1.Secret, chan<- pullResult, *runtimeapi.PodSandboxConfig)
  27. }
  28. var _, _ imagePuller = &parallelImagePuller{}, &serialImagePuller{}
  29. type parallelImagePuller struct {
  30. imageService kubecontainer.ImageService
  31. }
  32. func newParallelImagePuller(imageService kubecontainer.ImageService) imagePuller {
  33. return &parallelImagePuller{imageService}
  34. }
  35. func (pip *parallelImagePuller) pullImage(spec kubecontainer.ImageSpec, pullSecrets []v1.Secret, pullChan chan<- pullResult, podSandboxConfig *runtimeapi.PodSandboxConfig) {
  36. go func() {
  37. imageRef, err := pip.imageService.PullImage(spec, pullSecrets, podSandboxConfig)
  38. pullChan <- pullResult{
  39. imageRef: imageRef,
  40. err: err,
  41. }
  42. }()
  43. }
  44. // Maximum number of image pull requests than can be queued.
  45. const maxImagePullRequests = 10
  46. type serialImagePuller struct {
  47. imageService kubecontainer.ImageService
  48. pullRequests chan *imagePullRequest
  49. }
  50. func newSerialImagePuller(imageService kubecontainer.ImageService) imagePuller {
  51. imagePuller := &serialImagePuller{imageService, make(chan *imagePullRequest, maxImagePullRequests)}
  52. go wait.Until(imagePuller.processImagePullRequests, time.Second, wait.NeverStop)
  53. return imagePuller
  54. }
  55. type imagePullRequest struct {
  56. spec kubecontainer.ImageSpec
  57. pullSecrets []v1.Secret
  58. pullChan chan<- pullResult
  59. podSandboxConfig *runtimeapi.PodSandboxConfig
  60. }
  61. func (sip *serialImagePuller) pullImage(spec kubecontainer.ImageSpec, pullSecrets []v1.Secret, pullChan chan<- pullResult, podSandboxConfig *runtimeapi.PodSandboxConfig) {
  62. sip.pullRequests <- &imagePullRequest{
  63. spec: spec,
  64. pullSecrets: pullSecrets,
  65. pullChan: pullChan,
  66. podSandboxConfig: podSandboxConfig,
  67. }
  68. }
  69. func (sip *serialImagePuller) processImagePullRequests() {
  70. for pullRequest := range sip.pullRequests {
  71. imageRef, err := sip.imageService.PullImage(pullRequest.spec, pullRequest.pullSecrets, pullRequest.podSandboxConfig)
  72. pullRequest.pullChan <- pullResult{
  73. imageRef: imageRef,
  74. err: err,
  75. }
  76. }
  77. }