From 2e10744ca07f69d6b49b4bfe19e40bf883e62c43 Mon Sep 17 00:00:00 2001 From: Sam Chew Date: Thu, 19 Sep 2024 12:33:26 -0700 Subject: [PATCH] Port changes from Shubham fork Signed-off-by: Sam Chew --- cmd/finch/main.go | 7 ++ cmd/finch/nerdctl.go | 6 +- cmd/finch/nerdctl_remote.go | 151 ++++++++++++++++++++++++++++++++++- pkg/config/config_darwin.go | 1 + pkg/config/config_windows.go | 1 + pkg/path/finch_linux.go | 2 +- 6 files changed, 164 insertions(+), 4 deletions(-) diff --git a/cmd/finch/main.go b/cmd/finch/main.go index 9a43fcd60..92575b49a 100644 --- a/cmd/finch/main.go +++ b/cmd/finch/main.go @@ -44,5 +44,12 @@ func initializeNerdctlCommands( for cmdName, cmdDescription := range nerdctlCmds { allNerdctlCommands = append(allNerdctlCommands, nerdctlCommandCreator.create(cmdName, cmdDescription)) } + + if fc.Mode != nil && *fc.Mode == "dockercompat" { + for cmdName, cmdDescription := range dockerCompatCmds { + allNerdctlCommands = append(allNerdctlCommands, nerdctlCommandCreator.create(cmdName, cmdDescription)) + } + } + return allNerdctlCommands } diff --git a/cmd/finch/nerdctl.go b/cmd/finch/nerdctl.go index c372c9e27..f10a12341 100644 --- a/cmd/finch/nerdctl.go +++ b/cmd/finch/nerdctl.go @@ -192,12 +192,16 @@ var nerdctlCmds = map[string]string{ "wait": "Block until one or more containers stop, then print their exit codes", } +var dockerCompatCmds = map[string]string{ + "buildx": "build version", +} + var cmdFlagSetMap = map[string]map[string]sets.Set[string]{ "container run": { "shortBoolFlags": sets.New[string]("-d", "-i", "-t"), "longBoolFlags": sets.New[string]( "--detach", "--init", "--interactive", "--oom-kill-disable", - "--privileged", "--read-only", "--rm", "--rootfs", "--tty"), + "--privileged", "--read-only", "--rm", "--rootfs", "--tty", "--sig-proxy"), "shortArgFlags": sets.New[string]("-e", "-h", "-m", "-u", "-w", "-p", "-l", "-v"), }, "exec": { diff --git a/cmd/finch/nerdctl_remote.go b/cmd/finch/nerdctl_remote.go index f1659df50..9441097bd 100644 --- a/cmd/finch/nerdctl_remote.go +++ b/cmd/finch/nerdctl_remote.go @@ -12,6 +12,7 @@ import ( "runtime" "strings" + "github.com/sirupsen/logrus" orderedmap "github.com/wk8/go-ordered-map" "golang.org/x/exp/slices" "k8s.io/apimachinery/pkg/util/sets" @@ -19,6 +20,7 @@ import ( "github.com/spf13/afero" "github.com/runfinch/finch/pkg/command" + "github.com/runfinch/finch/pkg/config" "github.com/runfinch/finch/pkg/flog" "github.com/runfinch/finch/pkg/lima" ) @@ -70,6 +72,7 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { } switch cmdName { + case "container run", "exec", "compose": // check if an option flag is present; immediately following the command switch { @@ -103,6 +106,7 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { arg = args[i] } } + arg = handleCache(nc.fc, arg) // parsing arguments from the command line // may pre-fetch and consume the next argument; // the loop variable will skip any pre-consumed args @@ -116,7 +120,6 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { cmdArgs = append(cmdArgs, arg) continue } - switch { case arg == "--debug": nc.logger.SetLevel(flog.Debug) @@ -146,6 +149,11 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { } skip = shouldSkip fileEnvs = append(fileEnvs, addEnvs...) + // This is a docker specific command which alias for --output=type=docker. This should only applied for build args. + // On a long term this run command potentially needs to be refactored, currently it is too hacky the way it handles the args. + case arg == "--load": + nc.logger.Info("found --load converting to --output flag") + nerdctlArgs = handleLoad(nc.fc, nerdctlArgs) case argIsEnv(arg): // exact match to either -e or --env // or arg begins with -e or --env @@ -156,7 +164,7 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { if addEnv != "" { envs = append(envs, addEnv) } - case shortFlagBoolSet.Has(arg) || longFlagBoolSet.Has(arg): + case longFlagBoolSet.Has(strings.Split(arg, "=")[0]) || longFlagBoolSet.Has(arg): // exact match to a short flag: -? // or exact match to: -- nerdctlArgs = append(nerdctlArgs, arg) @@ -247,6 +255,9 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { nc.logger.SetLevel(flog.Debug) case arg == "--help": nerdctlArgs = append(nerdctlArgs, arg) + case arg == "--load": + nc.logger.Info("found --load converting to --output flag") + nerdctlArgs = handleLoad(nc.fc, nerdctlArgs) default: nerdctlArgs = append(nerdctlArgs, arg) } @@ -303,6 +314,16 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { return nc.ncc.RunWithReplacingStdout([]command.Replacement{{Source: "nerdctl", Target: "finch"}}, runArgs...) } + // Handle buildx version and build commands. + skipCmd, runArgs := handleBuildx(nc.fc, runArgs) + if skipCmd { + return nil + } + + runArgs = handleDockerCompatInspect(nc.fc, runArgs) + + nc.logger.Info("Running nerdctl command args ", runArgs, " end") + return nc.ncc.Create(runArgs...).Run() } @@ -501,3 +522,129 @@ func handleEnvFile(fs afero.Fs, systemDeps NerdctlCommandSystemDeps, arg, arg2 s } return skip, envs, nil } + +func handleCache(fc *config.Finch, arg string) string { + // Hack to handle consistency params during mounts. This is assuming no other commands or env variable will have the word consistency. + if fc.Mode == nil || *fc.Mode != "dockercompat" || runtime.GOOS != "darwin" { + return arg + } + + if strings.Contains(arg, "consistency") { + arg = strings.Replace(arg, ",consistency=cache", "", 1) + arg = strings.Replace(arg, ",consistency=delegated", "", 1) + arg = strings.Replace(arg, ",consistency=consistent", "", 1) + } + return arg +} + +func handleLoad(fc *config.Finch, args []string) []string { + if fc.Mode == nil || *fc.Mode != "dockercompat" || args == nil { + return args + } + + if *fc.Mode == "dockercompat" { + logrus.Warn("appending --output-type!!") + logrus.Warn("args before appending", args) + args = append(args, "--output=type=docker") + logrus.Warn("args after appending", args) + } + + return args +} + +func handleDockerCompatInspect(fc *config.Finch, args []string) []string { + + if fc.Mode == nil || *fc.Mode != "dockercompat" { + return args + } + + newArgList := []string{} + isInspect := false + inspectIndex := -1 + skipNext := false + inspectType := "" + + for idx, arg := range args { + if skipNext { + skipNext = false + continue + } + if arg == "inspect" { + isInspect = true + inspectIndex = idx + newArgList = append(newArgList, arg) + } + + if strings.Contains(arg, "--type") && (idx < len(args)+1) && isInspect { + if strings.Contains(arg, "=") { + inspectType = strings.Split(arg, "=")[1] + } else { + inspectType = args[idx+1] + } + if arg == "--type" { + skipNext = true + } + continue + } + newArgList = append(newArgList, arg) + + } + + if !isInspect { + return args + } + + switch inspectType { + case "container": + newArgList = append(newArgList[:inspectIndex], append([]string{inspectType}, append([]string{newArgList[inspectIndex+1]}, append([]string{"--mode=dockercompat"}, newArgList[inspectIndex+2:]...)...)...)...) + case "": + break + default: + newArgList = append(newArgList[:inspectIndex], append([]string{inspectType}, newArgList[inspectIndex+1:]...)...) + } + + return newArgList +} + +func handleBuildx(fc *config.Finch, limaArgs []string) (bool, []string) { + logrus.Warn("handling buildx") + + buildx := false + skipCmd := true + var newLimaArgs []string + buildxSubcommands := []string{"bake", "create", "debug", "du", "imagetools", "inspect", "ls", "prune", "rm", "stop", "use", "version"} + + if fc.Mode == nil || *fc.Mode != "dockercompat" { + return !skipCmd, limaArgs + } + + for idx, arg := range limaArgs { + logrus.Warnf("looking at arg %s at index %d", arg, idx) + if arg == "buildx" { + buildx = true + newLimaArgs = append(newLimaArgs, "build") + logrus.Warn("buildx is not supported. using standard buildkit instead...") + } + + if buildx { + + buildxWarnMsg := "buildx %s command is not supported." + + if arg == "build" { + logrus.Warnf("found build") + continue + } else if slices.Contains(buildxSubcommands, arg) { + logrus.Warnf(buildxWarnMsg, arg) + return skipCmd, nil + } + + logrus.Warnf("appending build") + newLimaArgs = append(newLimaArgs, arg) + + } else { + newLimaArgs = append(newLimaArgs, arg) + } + } + + return !skipCmd, newLimaArgs +} diff --git a/pkg/config/config_darwin.go b/pkg/config/config_darwin.go index 1e38aaea3..b19b543d7 100644 --- a/pkg/config/config_darwin.go +++ b/pkg/config/config_darwin.go @@ -25,6 +25,7 @@ type SystemSettings struct { Memory *string `yaml:"memory,omitempty"` AdditionalDirectories []AdditionalDirectory `yaml:"additional_directories,omitempty"` Rosetta *bool `yaml:"rosetta,omitempty"` + Mode *string `yaml:"mode,omitempty"` SharedSystemSettings `yaml:",inline"` } diff --git a/pkg/config/config_windows.go b/pkg/config/config_windows.go index 9a4fff62c..3fb716382 100644 --- a/pkg/config/config_windows.go +++ b/pkg/config/config_windows.go @@ -11,6 +11,7 @@ import ( // SystemSettings represents the system configuration specifc to Windows. type SystemSettings struct { + Mode *string `yaml:"mode,omitempty"` SharedSystemSettings `yaml:",inline"` } diff --git a/pkg/path/finch_linux.go b/pkg/path/finch_linux.go index 368452ae8..8b3e84e8a 100644 --- a/pkg/path/finch_linux.go +++ b/pkg/path/finch_linux.go @@ -31,7 +31,7 @@ func (fp Finch) NerdctlConfigFilePath() string { // BuildkitSocketPath returns the path to the Buildkit socket file. func (fp Finch) BuildkitSocketPath() string { - return filepath.Join(fp.FinchRuntimeDataDir(), "buildkit", "buildkitd.sock") + return filepath.Join(string(fp), "buildkit", "buildkitd.toml") } // FinchDependencyBinDir returns the path to Finch's local helper or dependency binaries.