This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathdocker_util.go
380 lines (349 loc) · 12 KB
/
docker_util.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package docker
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/docker/docker/client"
"github.com/enescakir/emoji"
"github.com/flyteorg/flytectl/clierrors"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/go-connections/nat"
cmdUtil "github.com/flyteorg/flytectl/pkg/commandutils"
f "github.com/flyteorg/flytectl/pkg/filesystemutils"
)
var (
FlyteSandboxConfigDir = f.FilePathJoin(f.UserHomeDir(), ".flyte", "sandbox")
Kubeconfig = f.FilePathJoin(FlyteSandboxConfigDir, "kubeconfig")
SandboxKubeconfig = f.FilePathJoin(f.UserHomeDir(), ".flyte", "k3s", "k3s.yaml")
SuccessMessage = "Deploying Flyte..."
FlyteSandboxClusterName = "flyte-sandbox"
FlyteSandboxVolumeName = "flyte-sandbox"
FlyteSandboxInternalDir = "/var/lib/flyte"
FlyteSandboxInternalConfigDir = f.FilePathJoin(FlyteSandboxInternalDir, "config")
FlyteSandboxInternalStorageDir = f.FilePathJoin(FlyteSandboxInternalDir, "storage")
Environment = []string{"SANDBOX=1", "KUBERNETES_API_PORT=30086", "FLYTE_HOST=localhost:30081", "FLYTE_AWS_ENDPOINT=http://localhost:30084"}
Source = "/root"
K3sDir = "/etc/rancher/"
Client Docker
Volumes = []mount.Mount{
{
Type: mount.TypeBind,
Source: f.FilePathJoin(f.UserHomeDir(), ".flyte"),
Target: K3sDir,
},
}
ExecConfig = types.ExecConfig{
AttachStderr: true,
Tty: true,
WorkingDir: "/",
AttachStdout: true,
Cmd: []string{},
}
StdWriterPrefixLen = 8
StartingBufLen = 32*1024 + StdWriterPrefixLen + 1
ExtraHosts = []string{"host.docker.internal:host-gateway"}
)
// GetDockerClient will returns the docker client
func GetDockerClient() (Docker, error) {
if Client == nil {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
fmt.Printf("%v Please Check your docker client %v \n", emoji.GrimacingFace, emoji.Whale)
return nil, err
}
return cli, nil
}
return Client, nil
}
// GetSandbox will return sandbox container if it exist
func GetSandbox(ctx context.Context, cli Docker) (*types.Container, error) {
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{
All: true,
})
if err != nil {
return nil, err
}
for _, v := range containers {
if strings.TrimLeft(v.Names[0], "/") == FlyteSandboxClusterName {
return &v, nil
}
}
return nil, nil
}
// RemoveSandbox will remove sandbox container if exist
func RemoveSandbox(ctx context.Context, cli Docker, reader io.Reader) error {
c, err := GetSandbox(ctx, cli)
if err != nil {
return err
}
if c != nil {
if cmdUtil.AskForConfirmation("delete existing sandbox cluster", reader) {
err := cli.ContainerRemove(context.Background(), c.ID, types.ContainerRemoveOptions{
Force: true,
})
return err
}
return errors.New(clierrors.ErrSandboxExists)
}
return nil
}
// GetDevPorts will return dev cluster (minio + postgres) ports
func GetDevPorts() (map[nat.Port]struct{}, map[nat.Port][]nat.PortBinding, error) {
return nat.ParsePortSpecs([]string{
"0.0.0.0:30082:30082", // K8s Dashboard Port
"0.0.0.0:30084:30084", // Minio API Port
"0.0.0.0:30086:30086", // K8s cluster
"0.0.0.0:30088:30088", // Minio Console Port
"0.0.0.0:30089:30089", // Postgres Port
})
}
// GetSandboxPorts will return sandbox ports
func GetSandboxPorts() (map[nat.Port]struct{}, map[nat.Port][]nat.PortBinding, error) {
return nat.ParsePortSpecs([]string{
// Notice that two host ports are mapped to the same container port in the case of Flyteconsole, this is done to
// support the generated URLs produced by pyflyte run
"0.0.0.0:30080:30081", // Flyteconsole Port.
"0.0.0.0:30081:30081", // Flyteadmin Port
"0.0.0.0:30082:30082", // K8s Dashboard Port
"0.0.0.0:30084:30084", // Minio API Port
"0.0.0.0:30086:30086", // K8s cluster
"0.0.0.0:30088:30088", // Minio Console Port
"0.0.0.0:30089:30089", // Postgres Port
})
}
// GetDemoPorts will return demo ports
func GetDemoPorts() (map[nat.Port]struct{}, map[nat.Port][]nat.PortBinding, error) {
return nat.ParsePortSpecs([]string{
"0.0.0.0:6443:6443", // K3s API Port
"0.0.0.0:30080:30080", // HTTP Port
"0.0.0.0:30000:30000", // Registry Port
"0.0.0.0:30001:30001", // Postgres Port
"0.0.0.0:30002:30002", // Minio API Port (use HTTP port for minio console)
})
}
// PullDockerImage will Pull docker image
func PullDockerImage(ctx context.Context, cli Docker, image string, pullPolicy ImagePullPolicy,
imagePullOptions ImagePullOptions, dryRun bool) error {
if dryRun {
PrintPullImage(image, imagePullOptions)
return nil
}
fmt.Printf("%v pulling docker image for release %s\n", emoji.Whale, image)
if pullPolicy == ImagePullPolicyAlways || pullPolicy == ImagePullPolicyIfNotPresent {
if pullPolicy == ImagePullPolicyIfNotPresent {
imageSummary, err := cli.ImageList(ctx, types.ImageListOptions{})
if err != nil {
return err
}
for _, img := range imageSummary {
for _, tags := range img.RepoTags {
if image == tags {
return nil
}
}
}
}
r, err := cli.ImagePull(ctx, image, types.ImagePullOptions{
RegistryAuth: imagePullOptions.RegistryAuth,
Platform: imagePullOptions.Platform,
})
if err != nil {
return err
}
_, err = io.Copy(os.Stdout, r)
return err
}
return nil
}
// PrintPullImage helper function to print the sandbox pull image command
func PrintPullImage(image string, pullOptions ImagePullOptions) {
fmt.Printf("%v Run the following command to pull the sandbox image from registry.\n", emoji.Sparkle)
var sb strings.Builder
sb.WriteString("docker pull ")
if len(pullOptions.Platform) > 0 {
sb.WriteString(fmt.Sprintf("--platform %v ", pullOptions.Platform))
}
sb.WriteString(fmt.Sprintf("%v", image))
fmt.Printf(" %v \n", sb.String())
}
// PrintRemoveContainer helper function to remove sandbox container
func PrintRemoveContainer(name string) {
fmt.Printf("%v Run the following command to remove the existing sandbox\n", emoji.Sparkle)
fmt.Printf(" docker container rm %v --force\n", name)
}
// PrintCreateContainer helper function to print the docker command to run
func PrintCreateContainer(volumes []mount.Mount, portBindings map[nat.Port][]nat.PortBinding, name, image string, environment []string) {
var sb strings.Builder
fmt.Printf("%v Run the following command to create new sandbox container\n", emoji.Sparkle)
sb.WriteString(" docker create --privileged ")
for portProto, bindings := range portBindings {
srcPort := portProto.Port()
for _, binding := range bindings {
sb.WriteString(fmt.Sprintf("-p %v:%v:%v ", binding.HostIP, srcPort, binding.HostPort))
}
}
for _, env := range environment {
sb.WriteString(fmt.Sprintf("--env %v ", env))
}
for _, volume := range volumes {
sb.WriteString(fmt.Sprintf("--mount type=%v,source=%v,target=%v ", volume.Type, volume.Source, volume.Target))
}
sb.WriteString(fmt.Sprintf("--name %v ", name))
sb.WriteString(fmt.Sprintf("%v", image))
fmt.Printf("%v\n", sb.String())
fmt.Printf("%v Run the following command to start the sandbox container\n", emoji.Sparkle)
fmt.Printf(" docker start %v\n", name)
fmt.Printf("%v Run the following command to check the logs and monitor the sandbox container and make sure there are no error during startup and then visit flyteconsole\n", emoji.EightSpokedAsterisk)
fmt.Printf(" docker logs -f %v\n", name)
}
// StartContainer will create and start docker container
func StartContainer(ctx context.Context, cli Docker, volumes []mount.Mount, exposedPorts map[nat.Port]struct{},
portBindings map[nat.Port][]nat.PortBinding, name, image string, additionalEnvVars []string, dryRun bool) (string, error) {
// Append the additional env variables to the default list of env
Environment = append(Environment, additionalEnvVars...)
if dryRun {
PrintCreateContainer(volumes, portBindings, name, image, Environment)
return "", nil
}
fmt.Printf("%v booting Flyte-sandbox container\n", emoji.FactoryWorker)
resp, err := cli.ContainerCreate(ctx, &container.Config{
Env: Environment,
Image: image,
Tty: false,
ExposedPorts: exposedPorts,
}, &container.HostConfig{
Mounts: volumes,
PortBindings: portBindings,
Privileged: true,
ExtraHosts: ExtraHosts, // add it because linux machine doesn't have this host name by default
}, nil,
nil, name)
if err != nil {
return "", err
}
if err := cli.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{}); err != nil {
return "", err
}
return resp.ID, nil
}
// CopyContainerFile try to create the container, see if the source file is there, copy it to the destination
func CopyContainerFile(ctx context.Context, cli Docker, source, destination, name, image string) error {
resp, err := cli.ContainerCreate(ctx, &container.Config{Image: image}, &container.HostConfig{}, nil, nil, name)
if err != nil {
return err
}
var removeErr error
defer func() {
removeErr = cli.ContainerRemove(context.Background(), resp.ID, types.ContainerRemoveOptions{
Force: true,
})
}()
_, err = cli.ContainerStatPath(ctx, resp.ID, source)
if err != nil {
return err
}
reader, _, err := cli.CopyFromContainer(ctx, resp.ID, source)
if err != nil {
return err
}
tarFile := destination + ".tar"
outFile, err := os.Create(tarFile)
if err != nil {
return err
}
defer outFile.Close()
defer reader.Close()
_, err = io.Copy(outFile, reader)
if err != nil {
return err
}
r, _ := os.Open(tarFile)
err = f.ExtractTar(r, destination)
if err != nil {
return err
}
return removeErr
}
// ReadLogs will return io scanner for reading the logs of a container
func ReadLogs(ctx context.Context, cli Docker, id string) (*bufio.Scanner, error) {
reader, err := cli.ContainerLogs(ctx, id, types.ContainerLogsOptions{
ShowStderr: true,
ShowStdout: true,
Timestamps: true,
Follow: true,
})
if err != nil {
return nil, err
}
return bufio.NewScanner(reader), nil
}
// WaitForSandbox will wait until it doesn't get success message
func WaitForSandbox(reader *bufio.Scanner, message string) bool {
for reader.Scan() {
if strings.Contains(reader.Text(), message) {
return true
}
fmt.Println(reader.Text())
}
return false
}
// ExecCommend will execute a command in container and returns an execution id
func ExecCommend(ctx context.Context, cli Docker, containerID string, command []string) (types.IDResponse, error) {
ExecConfig.Cmd = command
r, err := cli.ContainerExecCreate(ctx, containerID, ExecConfig)
if err != nil {
return types.IDResponse{}, err
}
return r, err
}
func InspectExecResp(ctx context.Context, cli Docker, containerID string) error {
resp, err := cli.ContainerExecAttach(ctx, containerID, types.ExecStartCheck{})
if err != nil {
return err
}
_, err = stdcopy.StdCopy(os.Stdout, os.Stderr, resp.Reader)
if err != nil {
return err
}
return nil
}
func PrintCreateVolume(name string) {
fmt.Printf("%v Run the following command to create a volume\n", emoji.Sparkle)
fmt.Printf(" docker volume create %v\n", name)
}
func GetOrCreateVolume(
ctx context.Context, cli Docker, volumeName string, dryRun bool,
) (*types.Volume, error) {
if dryRun {
PrintCreateVolume(volumeName)
return nil, nil
}
resp, err := cli.VolumeList(ctx, filters.NewArgs(
filters.KeyValuePair{Key: "name", Value: fmt.Sprintf("^%s$", volumeName)},
))
if err != nil {
return nil, err
}
switch len(resp.Volumes) {
case 0:
v, err := cli.VolumeCreate(ctx, volume.VolumeCreateBody{Name: volumeName})
if err != nil {
return nil, err
}
return &v, nil
case 1:
return resp.Volumes[0], nil
default:
// We don't expect to ever arrive at this point
return nil, fmt.Errorf("unexpected error - found multiple volumes with name: %s", volumeName)
}
}