kuberuntime_image.go 4.4 KB

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