kuberuntime_image.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 kuberuntime
  14. import (
  15. "k8s.io/api/core/v1"
  16. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  17. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  18. "k8s.io/klog"
  19. credentialprovidersecrets "k8s.io/kubernetes/pkg/credentialprovider/secrets"
  20. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  21. "k8s.io/kubernetes/pkg/util/parsers"
  22. )
  23. // PullImage pulls an image from the network to local storage using the supplied
  24. // secrets if necessary.
  25. func (m *kubeGenericRuntimeManager) PullImage(image kubecontainer.ImageSpec, pullSecrets []v1.Secret, podSandboxConfig *runtimeapi.PodSandboxConfig) (string, error) {
  26. img := image.Image
  27. repoToPull, _, _, err := parsers.ParseImageName(img)
  28. if err != nil {
  29. return "", err
  30. }
  31. keyring, err := credentialprovidersecrets.MakeDockerKeyring(pullSecrets, m.keyring)
  32. if err != nil {
  33. return "", err
  34. }
  35. imgSpec := &runtimeapi.ImageSpec{Image: img}
  36. creds, withCredentials := keyring.Lookup(repoToPull)
  37. if !withCredentials {
  38. klog.V(3).Infof("Pulling image %q without credentials", img)
  39. imageRef, err := m.imageService.PullImage(imgSpec, nil, podSandboxConfig)
  40. if err != nil {
  41. klog.Errorf("Pull image %q failed: %v", img, err)
  42. return "", err
  43. }
  44. return imageRef, nil
  45. }
  46. var pullErrs []error
  47. for _, currentCreds := range creds {
  48. auth := &runtimeapi.AuthConfig{
  49. Username: currentCreds.Username,
  50. Password: currentCreds.Password,
  51. Auth: currentCreds.Auth,
  52. ServerAddress: currentCreds.ServerAddress,
  53. IdentityToken: currentCreds.IdentityToken,
  54. RegistryToken: currentCreds.RegistryToken,
  55. }
  56. imageRef, err := m.imageService.PullImage(imgSpec, auth, podSandboxConfig)
  57. // If there was no error, return success
  58. if err == nil {
  59. return imageRef, nil
  60. }
  61. pullErrs = append(pullErrs, err)
  62. }
  63. return "", utilerrors.NewAggregate(pullErrs)
  64. }
  65. // GetImageRef gets the ID of the image which has already been in
  66. // the local storage. It returns ("", nil) if the image isn't in the local storage.
  67. func (m *kubeGenericRuntimeManager) GetImageRef(image kubecontainer.ImageSpec) (string, error) {
  68. status, err := m.imageService.ImageStatus(&runtimeapi.ImageSpec{Image: image.Image})
  69. if err != nil {
  70. klog.Errorf("ImageStatus for image %q failed: %v", image, err)
  71. return "", err
  72. }
  73. if status == nil {
  74. return "", nil
  75. }
  76. return status.Id, nil
  77. }
  78. // ListImages gets all images currently on the machine.
  79. func (m *kubeGenericRuntimeManager) ListImages() ([]kubecontainer.Image, error) {
  80. var images []kubecontainer.Image
  81. allImages, err := m.imageService.ListImages(nil)
  82. if err != nil {
  83. klog.Errorf("ListImages failed: %v", err)
  84. return nil, err
  85. }
  86. for _, img := range allImages {
  87. images = append(images, kubecontainer.Image{
  88. ID: img.Id,
  89. Size: int64(img.Size_),
  90. RepoTags: img.RepoTags,
  91. RepoDigests: img.RepoDigests,
  92. })
  93. }
  94. return images, nil
  95. }
  96. // RemoveImage removes the specified image.
  97. func (m *kubeGenericRuntimeManager) RemoveImage(image kubecontainer.ImageSpec) error {
  98. err := m.imageService.RemoveImage(&runtimeapi.ImageSpec{Image: image.Image})
  99. if err != nil {
  100. klog.Errorf("Remove image %q failed: %v", image.Image, err)
  101. return err
  102. }
  103. return nil
  104. }
  105. // ImageStats returns the statistics of the image.
  106. // Notice that current logic doesn't really work for images which share layers (e.g. docker image),
  107. // this is a known issue, and we'll address this by getting imagefs stats directly from CRI.
  108. // TODO: Get imagefs stats directly from CRI.
  109. func (m *kubeGenericRuntimeManager) ImageStats() (*kubecontainer.ImageStats, error) {
  110. allImages, err := m.imageService.ListImages(nil)
  111. if err != nil {
  112. klog.Errorf("ListImages failed: %v", err)
  113. return nil, err
  114. }
  115. stats := &kubecontainer.ImageStats{}
  116. for _, img := range allImages {
  117. stats.TotalStorageBytes += img.Size_
  118. }
  119. return stats, nil
  120. }