-
Notifications
You must be signed in to change notification settings - Fork 18
/
containerbackend.go
125 lines (104 loc) · 2.97 KB
/
containerbackend.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
package containerbackend
import (
"context"
"errors"
"fmt"
"io"
"os"
)
var (
ErrNotFound = errors.New("not found")
ErrDone = errors.New("done")
ErrNewFailed = errors.New("failed to create container backend")
ErrBuildFailed = errors.New("failed to build image")
ErrPushFailed = errors.New("failed to push container")
ErrNotImplemented = errors.New("unimplemented container backend method")
)
type ContainerBackend interface {
io.Closer
ImagePull(ctx context.Context, reference string) error
ImagePush(ctx context.Context, reference string) error
ImageBuild(ctx context.Context, tarball io.Reader, input *ImageBuildInput) (<-chan string, <-chan error)
ImageInspect(ctx context.Context, reference string) error
ImageDelete(ctx context.Context, reference string) error
VolumeCreate(ctx context.Context, name string) error
VolumeInspect(ctx context.Context, name string) error
VolumeDelete(ctx context.Context, name string) error
ContainerCreate(ctx context.Context, input *ContainerCreateInput, name string) (*ContainerCreateOutput, error)
ContainerStart(ctx context.Context, id string) error
ContainerWait(ctx context.Context, id string) (<-chan ContainerWaitResult, <-chan error)
ContainerDelete(ctx context.Context, id string) error
SystemInfo(ctx context.Context) (*SystemInfo, error)
}
type ImageBuildInput struct {
Tags []string
Dockerfile string
BuildArgs map[string]string
Pull bool
}
type ContainerCreateInput struct {
Image string
Entrypoint []string
Command []string
Env map[string]string
Mounts []ContainerMount
Network string
Resources ContainerResources
DisableSELinux bool
}
type ContainerMountType int
const (
MountTypeBind ContainerMountType = iota
MountTypeVolume
)
type ContainerMount struct {
Type ContainerMountType
Source string
Target string
ReadOnly bool
}
type ContainerResources struct {
NanoCPUs int64
Memory int64
}
type ContainerCreateOutput struct {
ID string
}
type ContainerWaitResult struct {
StatusCode int64
Error string
}
type SystemInfo struct {
TotalCPUs int64
TotalMemoryBytes int64
}
const (
BackendAuto = "auto"
BackendDocker = "docker"
BackendPodman = "podman"
)
func New(name string) (ContainerBackend, error) {
if name == BackendAuto {
if nameFromEnv, ok := os.LookupEnv("CIRRUS_CONTAINER_BACKEND"); ok {
name = nameFromEnv
}
}
switch name {
case BackendDocker:
return NewDocker()
case BackendPodman:
return NewPodman()
case BackendAuto:
if backend, err := NewDocker(); err == nil {
return backend, nil
}
if backend, err := NewPodman(); err == nil {
return backend, nil
}
return nil, fmt.Errorf("%w: failed to instantiate all supported container backends"+
" (tried %q and %q, are these actually installed on the system?)",
ErrNewFailed, BackendDocker, BackendPodman)
default:
return nil, fmt.Errorf("%w: unknown container backend name %q", ErrNewFailed, name)
}
}