/
common.go
288 lines (237 loc) · 7.2 KB
/
common.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
package buildah
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/werf/werf/pkg/buildah/thirdparty"
"github.com/werf/werf/pkg/dockerfile/instruction"
"github.com/werf/werf/pkg/util"
"github.com/werf/werf/pkg/werf"
)
const (
DefaultShmSize = "65536k"
DefaultSignaturePolicy = `{"default": [{"type": "insecureAcceptAnything"}], "transports": {"docker-daemon": {"": [{"type": "insecureAcceptAnything"}]}}}`
DefaultRegistriesConfig = `unqualified-search-registries = ["docker.io"]`
BuildahImage = "registry.werf.io/werf/buildah:v1.22.3-1"
BuildahStorageContainerName = "werf-buildah-storage"
DefaultStorageDriver StorageDriver = StorageDriverOverlay
)
type StorageDriver string
const (
StorageDriverOverlay StorageDriver = "overlay"
StorageDriverVFS StorageDriver = "vfs"
)
type CommonOpts struct {
LogWriter io.Writer
}
type BuildFromCommandsOpts struct {
CommonOpts
}
type BuildFromDockerfileOpts struct {
CommonOpts
ContextDir string
BuildArgs map[string]string
Target string
}
type RunMount struct {
Type string
TmpfsSize string
Source string
Destination string
}
type RunCommandOpts struct {
CommonOpts
ContextDir string
PrependShell bool
Shell []string
AddCapabilities []string
DropCapabilities []string
NetworkType instruction.NetworkType
WorkingDir string
User string
Envs []string
Mounts []instructions.Mount
LegacyMounts []specs.Mount // TODO(ilya-lesikov): migrate to Mounts
}
type RmiOpts struct {
CommonOpts
Force bool
}
type CommitOpts struct {
CommonOpts
Image string
}
type ConfigOpts struct {
CommonOpts
Labels []string
Volumes []string
Expose []string
Envs map[string]string
Cmd []string
CmdPrependShell bool
Entrypoint []string
EntrypointPrependShell bool
User string
Workdir string
Healthcheck *thirdparty.HealthConfig
OnBuild string
StopSignal string
Shell []string
Maintainer string
}
type CopyOpts struct {
CommonOpts
Chown string
Chmod string
Ignores []string
}
type AddOpts struct {
CommonOpts
ContextDir string
Chown string
Chmod string
Ignores []string
}
type (
FromCommandOpts CommonOpts
PushOpts CommonOpts
PullOpts CommonOpts
TagOpts CommonOpts
MountOpts CommonOpts
UmountOpts CommonOpts
RmOpts CommonOpts
)
type Buildah interface {
Tag(ctx context.Context, ref, newRef string, opts TagOpts) error
Push(ctx context.Context, ref string, opts PushOpts) error
BuildFromDockerfile(ctx context.Context, dockerfile string, opts BuildFromDockerfileOpts) (string, error)
RunCommand(ctx context.Context, container string, command []string, opts RunCommandOpts) error
FromCommand(ctx context.Context, container, image string, opts FromCommandOpts) (string, error)
Pull(ctx context.Context, ref string, opts PullOpts) error
Inspect(ctx context.Context, ref string) (*thirdparty.BuilderInfo, error)
Rm(ctx context.Context, ref string, opts RmOpts) error
Rmi(ctx context.Context, ref string, opts RmiOpts) error
Mount(ctx context.Context, container string, opts MountOpts) (string, error)
Umount(ctx context.Context, container string, opts UmountOpts) error
Commit(ctx context.Context, container string, opts CommitOpts) (string, error)
Config(ctx context.Context, container string, opts ConfigOpts) error
Copy(ctx context.Context, container, contextDir string, src []string, dst string, opts CopyOpts) error
Add(ctx context.Context, container string, src []string, dst string, opts AddOpts) error
}
type Mode string
const (
ModeAuto Mode = "auto"
ModeDisabled Mode = "disabled"
ModeNative Mode = "native"
)
type CommonBuildahOpts struct {
Isolation *thirdparty.Isolation
StorageDriver *StorageDriver
TmpDir string
Insecure bool
}
type NativeModeOpts struct {
Platform string
}
type BuildahOpts struct {
CommonBuildahOpts
NativeModeOpts
}
func NewBuildah(mode Mode, opts BuildahOpts) (b Buildah, err error) {
if opts.CommonBuildahOpts.Isolation == nil {
defIsolation, err := GetDefaultIsolation()
if err != nil {
return b, fmt.Errorf("unable to determine default isolation: %w", err)
}
opts.CommonBuildahOpts.Isolation = &defIsolation
}
if opts.CommonBuildahOpts.StorageDriver == nil {
defStorageDriver := DefaultStorageDriver
opts.CommonBuildahOpts.StorageDriver = &defStorageDriver
}
if opts.CommonBuildahOpts.TmpDir == "" {
opts.CommonBuildahOpts.TmpDir = filepath.Join(werf.GetHomeDir(), "buildah", "tmp")
}
switch mode {
case ModeNative, ModeAuto:
switch runtime.GOOS {
case "linux":
b, err = NewNativeBuildah(opts.CommonBuildahOpts, opts.NativeModeOpts)
if err != nil {
return nil, fmt.Errorf("unable to create new Buildah instance with mode %q: %w", mode, err)
}
default:
panic(fmt.Sprintf("Mode %q can't be used on this OS", mode))
}
default:
return nil, fmt.Errorf("unsupported mode %q", mode)
}
return b, nil
}
func ProcessStartupHook(mode Mode) (bool, error) {
switch mode {
case ModeNative, ModeAuto:
return NativeProcessStartupHook(), nil
default:
return false, fmt.Errorf("unsupported mode %q", mode)
}
}
func GetFuseOverlayfsOptions() ([]string, error) {
fuseOverlayBinPath, err := exec.LookPath("fuse-overlayfs")
if err != nil {
return nil, fmt.Errorf("\"fuse-overlayfs\" binary not found in PATH: %w", err)
}
result := []string{fmt.Sprintf("overlay.mount_program=%s", fuseOverlayBinPath)}
if util.IsInContainer() {
result = append(result, fmt.Sprintf("overlay.mountopt=%s", "nodev,fsync=0"))
}
return result, nil
}
func GetDefaultIsolation() (thirdparty.Isolation, error) {
if util.IsInContainer() {
return thirdparty.IsolationChroot, nil
}
return thirdparty.IsolationOCIRootless, nil
}
func debug() bool {
return os.Getenv("WERF_BUILDAH_DEBUG") == "1"
}
type StoreOptions struct {
GraphDriverName string
GraphDriverOptions []string
}
func GetBasicBuildahCliArgs(driver StorageDriver) ([]string, error) {
var result []string
cliStoreOpts, err := newBuildahCliStoreOptions(driver)
if err != nil {
return result, fmt.Errorf("unable to get buildah cli store options: %w", err)
}
if cliStoreOpts.GraphDriverName != "" {
result = append(result, "--storage-driver", cliStoreOpts.GraphDriverName)
}
if len(cliStoreOpts.GraphDriverOptions) > 0 {
result = append(result, "--storage-opt", strings.Join(cliStoreOpts.GraphDriverOptions, ","))
}
return result, nil
}
func newBuildahCliStoreOptions(driver StorageDriver) (*StoreOptions, error) {
var graphDriverOptions []string
if driver == StorageDriverOverlay {
fuseOpts, err := GetFuseOverlayfsOptions()
if err != nil {
return nil, fmt.Errorf("unable to get overlay options: %w", err)
}
graphDriverOptions = append(graphDriverOptions, fuseOpts...)
}
return &StoreOptions{
GraphDriverName: string(driver),
GraphDriverOptions: graphDriverOptions,
}, nil
}