Skip to content

Commit

Permalink
Merge pull request #4981 from werf/refactor-buildah-remove-unused-doc…
Browse files Browse the repository at this point in the history
…ker-with-fuse-backend

refactor(buildah): remove unused docker-with-fuse backend
  • Loading branch information
ilya-lesikov committed Oct 4, 2022
2 parents 8f18fcb + c52f2ed commit d1965ae
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 508 deletions.
13 changes: 2 additions & 11 deletions cmd/buildah-test/main.go
Expand Up @@ -12,13 +12,12 @@ import (
"github.com/opencontainers/runtime-spec/specs-go"

"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/docker"
"github.com/werf/werf/pkg/util"
"github.com/werf/werf/pkg/werf"
)

var errUsage = errors.New(`
./buildah-test {auto|native|docker-with-fuse} { dockerfile DOCKERFILE_PATH [CONTEXT_PATH] |
./buildah-test {auto|native} { dockerfile DOCKERFILE_PATH [CONTEXT_PATH] |
stapel }
`)

Expand Down Expand Up @@ -144,7 +143,7 @@ func do(ctx context.Context) error {
if len(os.Args) < 2 {
return errUsage
}
mode = buildah.ResolveMode(buildah.Mode(os.Args[1]))
mode = buildah.Mode(os.Args[1])

os.Setenv("BUILDAH_TEST_MODE", string(mode))
}
Expand All @@ -161,16 +160,8 @@ func do(ctx context.Context) error {
return fmt.Errorf("unable to init werf subsystem: %w", err)
}

mode = buildah.ResolveMode(mode)

fmt.Printf("Using buildah mode: %s\n", mode)

if mode == buildah.ModeDockerWithFuse {
if err := docker.Init(ctx, "", false, false, ""); err != nil {
return err
}
}

if len(os.Args) < 3 {
return errUsage
}
Expand Down
14 changes: 1 addition & 13 deletions cmd/werf/common/container_backend.go
Expand Up @@ -48,9 +48,6 @@ func GetBuildahMode() (*buildah.Mode, *thirdparty.Isolation, error) {
case "native-chroot":
mode = buildah.ModeNative
isolation = thirdparty.IsolationChroot
case "docker-with-fuse":
mode = buildah.ModeDockerWithFuse
isolation = thirdparty.IsolationChroot
case "default", "auto":
mode = buildah.ModeAuto
var err error
Expand Down Expand Up @@ -95,22 +92,13 @@ func InitProcessContainerBackend(ctx context.Context, cmdData *CmdData) (contain
}

if *buildahMode != buildah.ModeDisabled {
resolvedMode := buildah.ResolveMode(*buildahMode)
if resolvedMode == buildah.ModeDockerWithFuse {
newCtx, err := InitProcessDocker(ctx, cmdData)
if err != nil {
return nil, ctx, fmt.Errorf("unable to init process docker for buildah container backend: %w", err)
}
ctx = newCtx
}

storageDriver, err := GetBuildahStorageDriver()
if err != nil {
return nil, ctx, fmt.Errorf("unable to determine buildah container backend storage driver: %w", err)
}

insecure := *cmdData.InsecureRegistry || *cmdData.SkipTlsVerifyRegistry
b, err := buildah.NewBuildah(resolvedMode, buildah.BuildahOpts{
b, err := buildah.NewBuildah(*buildahMode, buildah.BuildahOpts{
CommonBuildahOpts: buildah.CommonBuildahOpts{
TmpDir: filepath.Join(werf.GetServiceDir(), "tmp", "buildah"),
Insecure: insecure,
Expand Down
2 changes: 1 addition & 1 deletion docs/pages_ru/advanced/buildah_mode.md
Expand Up @@ -24,7 +24,7 @@ permalink: advanced/buildah_mode.html

## Включение Buildah

Buildah включается установкой переменной окружения `WERF_BUILDAH_MODE` в один из вариантов: `auto`, `native-chroot`, `native-rootless` или `docker-with-fuse`.
Buildah включается установкой переменной окружения `WERF_BUILDAH_MODE` в один из вариантов: `auto`, `native-chroot`, `native-rootless`.

* `auto` — автоматический выбор режима в зависимости от платформы и окружения;
* `native-chroot` работает только в Linux и использует `chroot`-изоляцию для сборочных контейнеров;
Expand Down
102 changes: 0 additions & 102 deletions pkg/buildah/base.go

This file was deleted.

82 changes: 49 additions & 33 deletions pkg/buildah/common.go
Expand Up @@ -8,6 +8,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/opencontainers/runtime-spec/specs-go"

Expand Down Expand Up @@ -111,10 +112,9 @@ type Buildah interface {
type Mode string

const (
ModeAuto Mode = "auto"
ModeDisabled Mode = "disabled"
ModeNative Mode = "native"
ModeDockerWithFuse Mode = "docker-with-fuse"
ModeAuto Mode = "auto"
ModeDisabled Mode = "disabled"
ModeNative Mode = "native"
)

type CommonBuildahOpts struct {
Expand All @@ -128,11 +128,8 @@ type NativeModeOpts struct {
Platform string
}

type DockerWithFuseModeOpts struct{}

type BuildahOpts struct {
CommonBuildahOpts
DockerWithFuseModeOpts
NativeModeOpts
}

Expand All @@ -154,21 +151,16 @@ func NewBuildah(mode Mode, opts BuildahOpts) (b Buildah, err error) {
opts.CommonBuildahOpts.TmpDir = filepath.Join(werf.GetHomeDir(), "buildah", "tmp")
}

switch ResolveMode(mode) {
case ModeNative:
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("ModeNative can't be used on this OS")
}
case ModeDockerWithFuse:
b, err = NewDockerWithFuseBuildah(opts.CommonBuildahOpts, opts.DockerWithFuseModeOpts)
if err != nil {
return nil, fmt.Errorf("unable to create new Buildah instance with mode %q: %w", mode, err)
panic(fmt.Sprintf("Mode %q can't be used on this OS", mode))
}
default:
return nil, fmt.Errorf("unsupported mode %q", mode)
Expand All @@ -178,30 +170,14 @@ func NewBuildah(mode Mode, opts BuildahOpts) (b Buildah, err error) {
}

func ProcessStartupHook(mode Mode) (bool, error) {
switch ResolveMode(mode) {
case ModeNative:
switch mode {
case ModeNative, ModeAuto:
return NativeProcessStartupHook(), nil
case ModeDockerWithFuse:
return false, nil
default:
return false, fmt.Errorf("unsupported mode %q", mode)
}
}

func ResolveMode(mode Mode) Mode {
switch mode {
case ModeAuto:
switch runtime.GOOS {
case "linux":
return ModeNative
default:
return ModeDockerWithFuse
}
default:
return mode
}
}

func GetFuseOverlayfsOptions() ([]string, error) {
fuseOverlayBinPath, err := exec.LookPath("fuse-overlayfs")
if err != nil {
Expand All @@ -227,3 +203,43 @@ func GetDefaultIsolation() (thirdparty.Isolation, error) {
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
}

0 comments on commit d1965ae

Please sign in to comment.