/
client.go
187 lines (157 loc) · 4.82 KB
/
client.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package client
//go:generate mockery -name Client
import (
"context"
"io"
"os"
"time"
"github.com/golang/protobuf/ptypes"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding/gzip"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/kelda-inc/kelda/pkg/errors"
"github.com/kelda-inc/kelda/pkg/proto/dev"
"github.com/kelda-inc/kelda/pkg/sync"
)
// Client is the interface for syncing code from the local machine into a
// development pod in Kubernetes.
type Client interface {
SetTargetVersion(dev.SyncConfig, string) error
GetMirrorSnapshot() (sync.MirrorSnapshot, error)
Mirror(sync.SourceFile) error
Remove(string) error
SyncComplete() error
Close() error
}
type client struct {
namespace string
pod string
kubeClient kubernetes.Interface
restConfig *rest.Config
pbClient dev.DevClient
grpcConn *grpc.ClientConn
}
// New returns a new sync Client.
func New(kubeClient kubernetes.Interface, restConfig *rest.Config, namespace,
pod string) (Client, error) {
addr, err := getSyncTunnelAddress(kubeClient, restConfig, namespace, pod)
if err != nil {
return nil, err
}
conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name)))
if err != nil {
return nil, errors.WithContext(err, "dial")
}
return &client{
namespace: namespace,
pod: pod,
kubeClient: kubeClient,
restConfig: restConfig,
pbClient: dev.NewDevClient(conn),
grpcConn: conn,
}, nil
}
func (c *client) SetTargetVersion(syncConfig dev.SyncConfig, version string) error {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
req := &dev.SetTargetVersionRequest{
Version: &dev.Version{
SyncConfig: &syncConfig,
Version: version,
},
}
resp, err := c.pbClient.SetTargetVersion(ctx, req)
return errors.Unmarshal(err, resp.GetError())
}
func (c *client) GetMirrorSnapshot() (sync.MirrorSnapshot, error) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
resp, err := c.pbClient.GetMirrorSnapshot(ctx, &dev.GetMirrorSnapshotRequest{})
err = errors.Unmarshal(err, resp.GetError())
if err != nil {
return nil, err
}
return sync.UnmarshalMirrorSnapshot(*resp.GetSnapshot())
}
func (c *client) SyncComplete() error {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
resp, err := c.pbClient.SyncComplete(ctx, &dev.SyncCompleteRequest{})
return errors.Unmarshal(err, resp.GetError())
}
func (c *client) Close() error {
return c.grpcConn.Close()
}
const chunkSize = 1024
func (c *client) Mirror(toMirror sync.SourceFile) error {
f, err := os.Open(toMirror.ContentsPath)
if err != nil {
if os.IsNotExist(err) {
// Ignore the error that the file doesn't exist. These kinds of
// files are typically temporary files like vim's .swp files.
return nil
}
return errors.WithContext(err, "open")
}
defer f.Close()
modTime, err := ptypes.TimestampProto(toMirror.ModTime)
if err != nil {
return errors.WithContext(err, "marshal modtime timestamp")
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
stream, err := c.pbClient.Mirror(ctx)
if err != nil {
return errors.WithContext(err, "start stream")
}
// Inform the server what file we're staging in the first request.
header := &dev.MirrorFile{
SyncSourcePath: toMirror.SyncSourcePath,
FileAttributes: &dev.FileAttributes{
ContentsHash: toMirror.ContentsHash,
Mode: uint32(toMirror.Mode),
ModTime: modTime,
},
}
err = stream.Send(&dev.MirrorFileRequest{Header: header})
if err != nil {
return errors.WithContext(err, "send header")
}
// Send the contents of the file to the server in `chunkSize` increments.
buf := make([]byte, chunkSize)
for {
n, err := f.Read(buf)
if err != nil {
if err == io.EOF {
break
}
return errors.WithContext(err, "read file")
}
err = stream.Send(&dev.MirrorFileRequest{
Chunk: buf[:n],
})
if err != nil {
// The server has closed the stream, likely because of an error
// while processing the stream. Read the last message from the
// server to discover the error.
if err == io.EOF {
resp := &dev.MirrorFileResponse{}
recvErr := stream.RecvMsg(resp)
err = errors.Unmarshal(recvErr, resp.GetError())
}
return errors.WithContext(err, "send file chunk")
}
}
resp, err := stream.CloseAndRecv()
return errors.Unmarshal(err, resp.GetError())
}
// Remove informs the server that `path` is no longer tracked locally.
// This can happen if the file is deleted, or if the sync config is changed so
// that the file isn't covered.
func (c *client) Remove(path string) error {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
_, err := c.pbClient.Remove(ctx, &dev.RemoveFileRequest{Path: path})
return err
}