remote_image.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "time"
  16. "github.com/golang/glog"
  17. "google.golang.org/grpc"
  18. internalApi "k8s.io/kubernetes/pkg/kubelet/api"
  19. runtimeApi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
  20. )
  21. // RemoteImageService is a gRPC implementation of internalApi.ImageManagerService.
  22. type RemoteImageService struct {
  23. timeout time.Duration
  24. imageClient runtimeApi.ImageServiceClient
  25. }
  26. // NewRemoteImageService creates a new internalApi.ImageManagerService.
  27. func NewRemoteImageService(addr string, connectionTimout time.Duration) (internalApi.ImageManagerService, error) {
  28. glog.V(3).Infof("Connecting to image service %s", addr)
  29. conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithDialer(dial))
  30. if err != nil {
  31. glog.Errorf("Connect remote image service %s failed: %v", addr, err)
  32. return nil, err
  33. }
  34. return &RemoteImageService{
  35. timeout: connectionTimout,
  36. imageClient: runtimeApi.NewImageServiceClient(conn),
  37. }, nil
  38. }
  39. // ListImages lists available images.
  40. func (r *RemoteImageService) ListImages(filter *runtimeApi.ImageFilter) ([]*runtimeApi.Image, error) {
  41. ctx, cancel := getContextWithTimeout(r.timeout)
  42. defer cancel()
  43. resp, err := r.imageClient.ListImages(ctx, &runtimeApi.ListImagesRequest{
  44. Filter: filter,
  45. })
  46. if err != nil {
  47. glog.Errorf("ListImages with filter %q from image service failed: %v", filter, err)
  48. return nil, err
  49. }
  50. return resp.Images, nil
  51. }
  52. // ImageStatus returns the status of the image.
  53. func (r *RemoteImageService) ImageStatus(image *runtimeApi.ImageSpec) (*runtimeApi.Image, error) {
  54. ctx, cancel := getContextWithTimeout(r.timeout)
  55. defer cancel()
  56. resp, err := r.imageClient.ImageStatus(ctx, &runtimeApi.ImageStatusRequest{
  57. Image: image,
  58. })
  59. if err != nil {
  60. glog.Errorf("ImageStatus %q from image service failed: %v", image.GetImage(), err)
  61. return nil, err
  62. }
  63. return resp.Image, nil
  64. }
  65. // PullImage pulls a image with authentication config.
  66. func (r *RemoteImageService) PullImage(image *runtimeApi.ImageSpec, auth *runtimeApi.AuthConfig) error {
  67. ctx, cancel := getContextWithTimeout(r.timeout)
  68. defer cancel()
  69. _, err := r.imageClient.PullImage(ctx, &runtimeApi.PullImageRequest{
  70. Image: image,
  71. Auth: auth,
  72. })
  73. if err != nil {
  74. glog.Errorf("PullImage %q from image service failed: %v", image.GetImage(), err)
  75. return err
  76. }
  77. return nil
  78. }
  79. // RemoveImage removes the image.
  80. func (r *RemoteImageService) RemoveImage(image *runtimeApi.ImageSpec) error {
  81. ctx, cancel := getContextWithTimeout(r.timeout)
  82. defer cancel()
  83. _, err := r.imageClient.RemoveImage(ctx, &runtimeApi.RemoveImageRequest{
  84. Image: image,
  85. })
  86. if err != nil {
  87. glog.Errorf("RemoveImage %q from image service failed: %v", image.GetImage(), err)
  88. return err
  89. }
  90. return nil
  91. }