forked from kata-containers/runtime
/
grpccache.go
68 lines (56 loc) · 1.84 KB
/
grpccache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2019 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// grpccache implements base vm factory that get base vm from grpc
package grpccache
import (
"context"
"fmt"
types "github.com/gogo/protobuf/types"
pb "github.com/kata-containers/runtime/protocols/cache"
vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/kata-containers/runtime/virtcontainers/factory/base"
"github.com/pkg/errors"
"google.golang.org/grpc"
)
type grpccache struct {
conn *grpc.ClientConn
config *vc.VMConfig
}
// New returns a new direct vm factory.
func New(ctx context.Context, endpoint string) (base.FactoryBase, error) {
conn, err := grpc.Dial(fmt.Sprintf("unix://%s", endpoint), grpc.WithInsecure())
if err != nil {
return nil, errors.Wrapf(err, "failed to connect %q", endpoint)
}
jConfig, err := pb.NewCacheServiceClient(conn).Config(ctx, &types.Empty{})
if err != nil {
return nil, errors.Wrapf(err, "failed to Config")
}
config, err := vc.GrpcToVMConfig(jConfig)
if err != nil {
return nil, errors.Wrapf(err, "failed to convert JSON to VMConfig")
}
return &grpccache{conn: conn, config: config}, nil
}
// Config returns the direct factory's configuration.
func (g *grpccache) Config() vc.VMConfig {
return *g.config
}
// GetBaseVM create a new VM directly.
func (g *grpccache) GetBaseVM(ctx context.Context, config vc.VMConfig) (*vc.VM, error) {
defer g.conn.Close()
gVM, err := pb.NewCacheServiceClient(g.conn).GetBaseVM(ctx, &types.Empty{})
if err != nil {
return nil, errors.Wrapf(err, "failed to GetBaseVM")
}
return vc.NewVMFromGrpc(ctx, gVM, *g.config)
}
// CloseFactory closes the direct vm factory.
func (g *grpccache) CloseFactory(ctx context.Context) {
}
// GetVMStatus is not supported
func (g *grpccache) GetVMStatus() []*pb.GrpcVMStatus {
panic("ERROR: package grpccache does not support GetVMStatus")
}