8e5b17cf13
Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
112 lines
3.3 KiB
Go
112 lines
3.3 KiB
Go
/*
|
|
Copyright 2016 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package remote
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/golang/glog"
|
|
"google.golang.org/grpc"
|
|
internalapi "k8s.io/kubernetes/pkg/kubelet/api"
|
|
runtimeapi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
|
)
|
|
|
|
// RemoteImageService is a gRPC implementation of internalapi.ImageManagerService.
|
|
type RemoteImageService struct {
|
|
timeout time.Duration
|
|
imageClient runtimeapi.ImageServiceClient
|
|
}
|
|
|
|
// NewRemoteImageService creates a new internalapi.ImageManagerService.
|
|
func NewRemoteImageService(addr string, connectionTimout time.Duration) (internalapi.ImageManagerService, error) {
|
|
glog.V(3).Infof("Connecting to image service %s", addr)
|
|
conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithTimeout(connectionTimout), grpc.WithDialer(dial))
|
|
if err != nil {
|
|
glog.Errorf("Connect remote image service %s failed: %v", addr, err)
|
|
return nil, err
|
|
}
|
|
|
|
return &RemoteImageService{
|
|
timeout: connectionTimout,
|
|
imageClient: runtimeapi.NewImageServiceClient(conn),
|
|
}, nil
|
|
}
|
|
|
|
// ListImages lists available images.
|
|
func (r *RemoteImageService) ListImages(filter *runtimeapi.ImageFilter) ([]*runtimeapi.Image, error) {
|
|
ctx, cancel := getContextWithTimeout(r.timeout)
|
|
defer cancel()
|
|
|
|
resp, err := r.imageClient.ListImages(ctx, &runtimeapi.ListImagesRequest{
|
|
Filter: filter,
|
|
})
|
|
if err != nil {
|
|
glog.Errorf("ListImages with filter %q from image service failed: %v", filter, err)
|
|
return nil, err
|
|
}
|
|
|
|
return resp.Images, nil
|
|
}
|
|
|
|
// ImageStatus returns the status of the image.
|
|
func (r *RemoteImageService) ImageStatus(image *runtimeapi.ImageSpec) (*runtimeapi.Image, error) {
|
|
ctx, cancel := getContextWithTimeout(r.timeout)
|
|
defer cancel()
|
|
|
|
resp, err := r.imageClient.ImageStatus(ctx, &runtimeapi.ImageStatusRequest{
|
|
Image: image,
|
|
})
|
|
if err != nil {
|
|
glog.Errorf("ImageStatus %q from image service failed: %v", image.GetImage(), err)
|
|
return nil, err
|
|
}
|
|
|
|
return resp.Image, nil
|
|
}
|
|
|
|
// PullImage pulls an image with authentication config.
|
|
func (r *RemoteImageService) PullImage(image *runtimeapi.ImageSpec, auth *runtimeapi.AuthConfig) (string, error) {
|
|
ctx, cancel := getContextWithTimeout(r.timeout)
|
|
defer cancel()
|
|
|
|
resp, err := r.imageClient.PullImage(ctx, &runtimeapi.PullImageRequest{
|
|
Image: image,
|
|
Auth: auth,
|
|
})
|
|
if err != nil {
|
|
glog.Errorf("PullImage %q from image service failed: %v", image.GetImage(), err)
|
|
return "", err
|
|
}
|
|
|
|
return resp.GetImageRef(), nil
|
|
}
|
|
|
|
// RemoveImage removes the image.
|
|
func (r *RemoteImageService) RemoveImage(image *runtimeapi.ImageSpec) error {
|
|
ctx, cancel := getContextWithTimeout(r.timeout)
|
|
defer cancel()
|
|
|
|
_, err := r.imageClient.RemoveImage(ctx, &runtimeapi.RemoveImageRequest{
|
|
Image: image,
|
|
})
|
|
if err != nil {
|
|
glog.Errorf("RemoveImage %q from image service failed: %v", image.GetImage(), err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|