remote_image.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 remote
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "time"
  19. "google.golang.org/grpc"
  20. "k8s.io/klog"
  21. internalapi "k8s.io/cri-api/pkg/apis"
  22. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  23. "k8s.io/kubernetes/pkg/kubelet/util"
  24. )
  25. // RemoteImageService is a gRPC implementation of internalapi.ImageManagerService.
  26. type RemoteImageService struct {
  27. timeout time.Duration
  28. imageClient runtimeapi.ImageServiceClient
  29. }
  30. // NewRemoteImageService creates a new internalapi.ImageManagerService.
  31. func NewRemoteImageService(endpoint string, connectionTimeout time.Duration) (internalapi.ImageManagerService, error) {
  32. klog.V(3).Infof("Connecting to image service %s", endpoint)
  33. addr, dialer, err := util.GetAddressAndDialer(endpoint)
  34. if err != nil {
  35. return nil, err
  36. }
  37. ctx, cancel := context.WithTimeout(context.Background(), connectionTimeout)
  38. defer cancel()
  39. conn, err := grpc.DialContext(ctx, addr, grpc.WithInsecure(), grpc.WithContextDialer(dialer), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)))
  40. if err != nil {
  41. klog.Errorf("Connect remote image service %s failed: %v", addr, err)
  42. return nil, err
  43. }
  44. return &RemoteImageService{
  45. timeout: connectionTimeout,
  46. imageClient: runtimeapi.NewImageServiceClient(conn),
  47. }, nil
  48. }
  49. // ListImages lists available images.
  50. func (r *RemoteImageService) ListImages(filter *runtimeapi.ImageFilter) ([]*runtimeapi.Image, error) {
  51. ctx, cancel := getContextWithTimeout(r.timeout)
  52. defer cancel()
  53. resp, err := r.imageClient.ListImages(ctx, &runtimeapi.ListImagesRequest{
  54. Filter: filter,
  55. })
  56. if err != nil {
  57. klog.Errorf("ListImages with filter %+v from image service failed: %v", filter, err)
  58. return nil, err
  59. }
  60. return resp.Images, nil
  61. }
  62. // ImageStatus returns the status of the image.
  63. func (r *RemoteImageService) ImageStatus(image *runtimeapi.ImageSpec) (*runtimeapi.Image, error) {
  64. ctx, cancel := getContextWithTimeout(r.timeout)
  65. defer cancel()
  66. resp, err := r.imageClient.ImageStatus(ctx, &runtimeapi.ImageStatusRequest{
  67. Image: image,
  68. })
  69. if err != nil {
  70. klog.Errorf("ImageStatus %q from image service failed: %v", image.Image, err)
  71. return nil, err
  72. }
  73. if resp.Image != nil {
  74. if resp.Image.Id == "" || resp.Image.Size_ == 0 {
  75. errorMessage := fmt.Sprintf("Id or size of image %q is not set", image.Image)
  76. klog.Errorf("ImageStatus failed: %s", errorMessage)
  77. return nil, errors.New(errorMessage)
  78. }
  79. }
  80. return resp.Image, nil
  81. }
  82. // PullImage pulls an image with authentication config.
  83. func (r *RemoteImageService) PullImage(image *runtimeapi.ImageSpec, auth *runtimeapi.AuthConfig, podSandboxConfig *runtimeapi.PodSandboxConfig) (string, error) {
  84. ctx, cancel := getContextWithCancel()
  85. defer cancel()
  86. resp, err := r.imageClient.PullImage(ctx, &runtimeapi.PullImageRequest{
  87. Image: image,
  88. Auth: auth,
  89. SandboxConfig: podSandboxConfig,
  90. })
  91. if err != nil {
  92. klog.Errorf("PullImage %q from image service failed: %v", image.Image, err)
  93. return "", err
  94. }
  95. if resp.ImageRef == "" {
  96. errorMessage := fmt.Sprintf("imageRef of image %q is not set", image.Image)
  97. klog.Errorf("PullImage failed: %s", errorMessage)
  98. return "", errors.New(errorMessage)
  99. }
  100. return resp.ImageRef, nil
  101. }
  102. // RemoveImage removes the image.
  103. func (r *RemoteImageService) RemoveImage(image *runtimeapi.ImageSpec) error {
  104. ctx, cancel := getContextWithTimeout(r.timeout)
  105. defer cancel()
  106. _, err := r.imageClient.RemoveImage(ctx, &runtimeapi.RemoveImageRequest{
  107. Image: image,
  108. })
  109. if err != nil {
  110. klog.Errorf("RemoveImage %q from image service failed: %v", image.Image, err)
  111. return err
  112. }
  113. return nil
  114. }
  115. // ImageFsInfo returns information of the filesystem that is used to store images.
  116. func (r *RemoteImageService) ImageFsInfo() ([]*runtimeapi.FilesystemUsage, error) {
  117. // Do not set timeout, because `ImageFsInfo` takes time.
  118. // TODO(random-liu): Should we assume runtime should cache the result, and set timeout here?
  119. ctx, cancel := getContextWithCancel()
  120. defer cancel()
  121. resp, err := r.imageClient.ImageFsInfo(ctx, &runtimeapi.ImageFsInfoRequest{})
  122. if err != nil {
  123. klog.Errorf("ImageFsInfo from image service failed: %v", err)
  124. return nil, err
  125. }
  126. return resp.GetImageFilesystems(), nil
  127. }