puller.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/kubernetes/pkg/api"
  17. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  18. "k8s.io/kubernetes/pkg/util/wait"
  19. )
  20. type imagePuller interface {
  21. pullImage(kubecontainer.ImageSpec, []api.Secret, chan<- error)
  22. }
  23. var _, _ imagePuller = &parallelImagePuller{}, &serialImagePuller{}
  24. type parallelImagePuller struct {
  25. runtime kubecontainer.Runtime
  26. }
  27. func newParallelImagePuller(runtime kubecontainer.Runtime) imagePuller {
  28. return &parallelImagePuller{runtime}
  29. }
  30. func (pip *parallelImagePuller) pullImage(spec kubecontainer.ImageSpec, pullSecrets []api.Secret, errChan chan<- error) {
  31. go func() {
  32. errChan <- pip.runtime.PullImage(spec, pullSecrets)
  33. }()
  34. }
  35. // Maximum number of image pull requests than can be queued.
  36. const maxImagePullRequests = 10
  37. type serialImagePuller struct {
  38. runtime kubecontainer.Runtime
  39. pullRequests chan *imagePullRequest
  40. }
  41. func newSerialImagePuller(runtime kubecontainer.Runtime) imagePuller {
  42. imagePuller := &serialImagePuller{runtime, make(chan *imagePullRequest, maxImagePullRequests)}
  43. go wait.Until(imagePuller.processImagePullRequests, time.Second, wait.NeverStop)
  44. return imagePuller
  45. }
  46. type imagePullRequest struct {
  47. spec kubecontainer.ImageSpec
  48. pullSecrets []api.Secret
  49. errChan chan<- error
  50. }
  51. func (sip *serialImagePuller) pullImage(spec kubecontainer.ImageSpec, pullSecrets []api.Secret, errChan chan<- error) {
  52. sip.pullRequests <- &imagePullRequest{
  53. spec: spec,
  54. pullSecrets: pullSecrets,
  55. errChan: errChan,
  56. }
  57. }
  58. func (sip *serialImagePuller) processImagePullRequests() {
  59. for pullRequest := range sip.pullRequests {
  60. pullRequest.errChan <- sip.runtime.PullImage(pullRequest.spec, pullRequest.pullSecrets)
  61. }
  62. }