From 999371eaf19422957258bd596803b64453bb3589 Mon Sep 17 00:00:00 2001 From: Sam Chew Date: Thu, 19 Sep 2024 13:53:41 -0700 Subject: [PATCH] Add DevContainers functionality to Finch Signed-off-by: Sam Chew --- cmd/finch/nerdctl_remote.go | 79 +++++++++++++++++++++--------------- pkg/config/config.go | 1 + pkg/config/config_darwin.go | 1 - pkg/config/config_windows.go | 1 - pkg/path/finch_linux.go | 2 +- 5 files changed, 48 insertions(+), 36 deletions(-) diff --git a/cmd/finch/nerdctl_remote.go b/cmd/finch/nerdctl_remote.go index 9441097bd..e3541f6e7 100644 --- a/cmd/finch/nerdctl_remote.go +++ b/cmd/finch/nerdctl_remote.go @@ -72,7 +72,6 @@ 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 { @@ -140,6 +139,13 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { } arg = fmt.Sprintf("%s%s", arg[0:11], resolvedIP) nerdctlArgs = append(nerdctlArgs, arg) + case arg == "--load": + // This is a docker specific command which alias for --output=type=docker. + // This should only applied for build args. + // Long term, this run command potentially needs to be refactored. + // Currently, the way it handles the args is too hacky. + nc.logger.Info("found --load converting to --output flag") + nerdctlArgs = handleLoad(nc.fc, nerdctlArgs) case strings.HasPrefix(arg, "--env-file"): // exact match to --env-file // or arg begins with --env-file @@ -149,11 +155,6 @@ 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 @@ -164,18 +165,27 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { if addEnv != "" { envs = append(envs, addEnv) } - case longFlagBoolSet.Has(strings.Split(arg, "=")[0]) || longFlagBoolSet.Has(arg): - // exact match to a short flag: -? + case shortFlagBoolSet.Has(arg) || longFlagBoolSet.Has(arg): + // exact match to a short no argument flag: -? // or exact match to: -- nerdctlArgs = append(nerdctlArgs, arg) + case longFlagBoolSet.Has(strings.Split(arg, "=")[0]): + // begins with -- + // e.g. --sig-proxy=false + nerdctlArgs = append(nerdctlArgs, arg) case shortFlagBoolSet.Has(arg[:2]): - // begins with a defined short flag, but is adjacent to one or more short flags: -???? + // or begins with a defined short no argument flag, but is adjacent to something + // -???? one or more short bool flags; no following values + // -????="" one or more short bool flags ending with a short arg flag equated to value + // -????"" one or more short bool flags ending with a short arg flag concatenated to value addArg := nc.handleMultipleShortFlags(shortFlagBoolSet, shortFlagArgSet, args, i) nerdctlArgs = append(nerdctlArgs, addArg) case shortFlagArgSet.Has(arg) || shortFlagArgSet.Has(arg[:2]): - // exact match to: -h,-m,-u,-w,-p,-l,-v - // or begins with: -h,-m,-u,-w,-p,-l,-v - // concatenated short flags and values: -p"8080:8080" + // exact match to a short arg flag: -? + // next arg must be the + // or begins with a short arg flag: + // short arg flag concatenated to value: -?"" + // short arg flag equated to value: -?="" or -?= shouldSkip, addKey, addVal := nc.handleFlagArg(arg, args[i+1]) skip = shouldSkip if addKey != "" { @@ -183,7 +193,11 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { nerdctlArgs = append(nerdctlArgs, addVal) } case strings.HasPrefix(arg, "--"): - // --="", -- "" + // exact match to a long arg flag: - + // next arg must be the + // or begins with a long arg flag: + // long arg flag concatenated to value: --"" + // long arg flag equated to value: --="" or --= shouldSkip, addKey, addVal := nc.handleFlagArg(arg, args[i+1]) skip = shouldSkip if addKey != "" { @@ -256,7 +270,6 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { 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) @@ -295,7 +308,7 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { if slices.Contains(args, "run") { ensureRemoteCredentials(nc.fc, nc.ecc, &additionalEnv, nc.logger) } - case "build", "pull", "push", "run": + case "build", "pull", "push", "container run": ensureRemoteCredentials(nc.fc, nc.ecc, &additionalEnv, nc.logger) } @@ -322,8 +335,6 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error { runArgs = handleDockerCompatInspect(nc.fc, runArgs) - nc.logger.Info("Running nerdctl command args ", runArgs, " end") - return nc.ncc.Create(runArgs...).Run() } @@ -525,12 +536,14 @@ func handleEnvFile(fs afero.Fs, systemDeps NerdctlCommandSystemDeps, arg, arg2 s 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" { + logrus.Warn("start of handleCache: ", arg) + if fc == nil || fc.Mode == nil || *fc.Mode != "dockercompat" || runtime.GOOS != "darwin" { + logrus.Warn("handleCache returning unchanged") return arg } if strings.Contains(arg, "consistency") { - arg = strings.Replace(arg, ",consistency=cache", "", 1) + arg = strings.Replace(arg, ",consistency=cached", "", 1) arg = strings.Replace(arg, ",consistency=delegated", "", 1) arg = strings.Replace(arg, ",consistency=consistent", "", 1) } @@ -538,7 +551,7 @@ func handleCache(fc *config.Finch, arg string) string { } func handleLoad(fc *config.Finch, args []string) []string { - if fc.Mode == nil || *fc.Mode != "dockercompat" || args == nil { + if fc == nil || fc.Mode == nil || *fc.Mode != "dockercompat" || args == nil { return args } @@ -553,8 +566,7 @@ func handleLoad(fc *config.Finch, args []string) []string { } func handleDockerCompatInspect(fc *config.Finch, args []string) []string { - - if fc.Mode == nil || *fc.Mode != "dockercompat" { + if fc == nil || fc.Mode == nil || *fc.Mode != "dockercompat" { return args } @@ -575,7 +587,7 @@ func handleDockerCompatInspect(fc *config.Finch, args []string) []string { newArgList = append(newArgList, arg) } - if strings.Contains(arg, "--type") && (idx < len(args)+1) && isInspect { + if strings.Contains(arg, "--type") && (idx < len(args)-1) && isInspect { if strings.Contains(arg, "=") { inspectType = strings.Split(arg, "=")[1] } else { @@ -587,7 +599,6 @@ func handleDockerCompatInspect(fc *config.Finch, args []string) []string { continue } newArgList = append(newArgList, arg) - } if !isInspect { @@ -596,7 +607,9 @@ func handleDockerCompatInspect(fc *config.Finch, args []string) []string { switch inspectType { case "container": - newArgList = append(newArgList[:inspectIndex], append([]string{inspectType}, append([]string{newArgList[inspectIndex+1]}, append([]string{"--mode=dockercompat"}, newArgList[inspectIndex+2:]...)...)...)...) + dcTrailingArgs := append([]string{"--mode=dockercompat"}, newArgList[inspectIndex+2:]...) + dcMidArgs := append([]string{inspectType}, append([]string{newArgList[inspectIndex+1]}, dcTrailingArgs...)...) + newArgList = append(newArgList[:inspectIndex], dcMidArgs...) case "": break default: @@ -607,27 +620,27 @@ func handleDockerCompatInspect(fc *config.Finch, args []string) []string { } 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" { + if fc == nil || 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) + logrus.Warn("handling buildx", limaArgs) + + for _, arg := range limaArgs { if arg == "buildx" { buildx = true newLimaArgs = append(newLimaArgs, "build") + logrus.Warn("newArgs: ", newLimaArgs) logrus.Warn("buildx is not supported. using standard buildkit instead...") + continue } if buildx { - buildxWarnMsg := "buildx %s command is not supported." if arg == "build" { @@ -638,11 +651,11 @@ func handleBuildx(fc *config.Finch, limaArgs []string) (bool, []string) { return skipCmd, nil } - logrus.Warnf("appending build") newLimaArgs = append(newLimaArgs, arg) - + logrus.Warn("newArgs: ", newLimaArgs) } else { newLimaArgs = append(newLimaArgs, arg) + logrus.Warn("newArgs: ", newLimaArgs) } } diff --git a/pkg/config/config.go b/pkg/config/config.go index 0d02ed55c..428319074 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -32,6 +32,7 @@ type AdditionalDirectory struct { // SharedSystemSettings represents all settings shared by virtualized Finch configurations. type SharedSystemSettings struct { VMType *limayaml.VMType `yaml:"vmType,omitempty"` + Mode *string `yaml:"mode,omitempty"` } // SharedSettings represents settings shared by all Finch configurations. diff --git a/pkg/config/config_darwin.go b/pkg/config/config_darwin.go index b19b543d7..1e38aaea3 100644 --- a/pkg/config/config_darwin.go +++ b/pkg/config/config_darwin.go @@ -25,7 +25,6 @@ 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 3fb716382..9a4fff62c 100644 --- a/pkg/config/config_windows.go +++ b/pkg/config/config_windows.go @@ -11,7 +11,6 @@ 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 8b3e84e8a..368452ae8 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(string(fp), "buildkit", "buildkitd.toml") + return filepath.Join(fp.FinchRuntimeDataDir(), "buildkit", "buildkitd.sock") } // FinchDependencyBinDir returns the path to Finch's local helper or dependency binaries.