Skip to content

Commit

Permalink
Add DevContainers functionality to Finch
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Chew <[email protected]>
  • Loading branch information
chews93319 committed Sep 23, 2024
1 parent 4420d2b commit de74f5b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 43 deletions.
101 changes: 61 additions & 40 deletions cmd/finch/nerdctl_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type (
)

func (nc *nerdctlCommand) run(cmdName string, args []string) error {

Check failure on line 35 in cmd/finch/nerdctl_remote.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

logrus.Warn("nc.run invoked: ", append([]string{cmdName}, args...))

err := nc.assertVMIsRunning(nc.ncc, nc.logger)
if err != nil {
return err
Expand Down Expand Up @@ -72,7 +75,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 {
Expand Down Expand Up @@ -140,6 +142,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
Expand All @@ -149,11 +158,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
Expand All @@ -164,26 +168,39 @@ 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: --<long_flag>
nerdctlArgs = append(nerdctlArgs, arg)
case longFlagBoolSet.Has(strings.Split(arg, "=")[0]):
// begins with --<long_flag>
// 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
// -????="<value>" one or more short bool flags ending with a short arg flag equated to value
// -????"<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 <value>
// or begins with a short arg flag:
// short arg flag concatenated to value: -?"<value>"
// short arg flag equated to value: -?="<value>" or -?=<value>
shouldSkip, addKey, addVal := nc.handleFlagArg(arg, args[i+1])
skip = shouldSkip
if addKey != "" {
nerdctlArgs = append(nerdctlArgs, addKey)
nerdctlArgs = append(nerdctlArgs, addVal)
}
case strings.HasPrefix(arg, "--"):
// --<long_flag>="<value>", --<long_flag> "<value>"
// exact match to a long arg flag: -<long_flag>
// next arg must be the <value>
// or begins with a long arg flag:
// long arg flag concatenated to value: --<long_flag>"<value>"
// long arg flag equated to value: --<long_flag>="<value>" or --<long_flag>=<value>
shouldSkip, addKey, addVal := nc.handleFlagArg(arg, args[i+1])
skip = shouldSkip
if addKey != "" {
Expand Down Expand Up @@ -256,7 +273,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)
Expand Down Expand Up @@ -295,7 +311,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)
}

Expand All @@ -322,7 +338,7 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error {

runArgs = handleDockerCompatInspect(nc.fc, runArgs)

nc.logger.Info("Running nerdctl command args ", runArgs, " end")
logrus.Warn("Attempting to run: ", runArgs)

return nc.ncc.Create(runArgs...).Run()
}
Expand Down Expand Up @@ -525,20 +541,24 @@ 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" {
if fc == nil || fc.Mode == nil || *fc.Mode != "dockercompat" || runtime.GOOS != "darwin" {
logrus.Warn("handleCache returning unchanged: ", arg)
//logrus.Warn("handleCache info: ", fc.Mode, *fc.Mode, runtime.GOOS)

Check failure on line 546 in cmd/finch/nerdctl_remote.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
return arg
}

if strings.Contains(arg, "consistency") {
arg = strings.Replace(arg, ",consistency=cache", "", 1)
logrus.Warn("handleCache manipulating: ", arg)
arg = strings.Replace(arg, ",consistency=cached", "", 1)
arg = strings.Replace(arg, ",consistency=delegated", "", 1)
arg = strings.Replace(arg, ",consistency=consistent", "", 1)
logrus.Warn("handleCache returning: ", arg)
}
return arg
}

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
}

Expand All @@ -553,8 +573,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
}

Expand All @@ -575,7 +594,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 {
Expand All @@ -587,7 +606,6 @@ func handleDockerCompatInspect(fc *config.Finch, args []string) []string {
continue
}
newArgList = append(newArgList, arg)

}

if !isInspect {
Expand All @@ -596,7 +614,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:
Expand All @@ -607,43 +627,44 @@ 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)
for _, arg := range limaArgs {
if arg == "buildx" {
buildx = true
newLimaArgs = append(newLimaArgs, "build")
logrus.Warn("buildx is not supported. using standard buildkit instead...")
continue
}

if buildx {

buildxWarnMsg := "buildx %s command is not supported."
//// Find standard vsc container identifier to truncate to less than 76chars
//re := regexp.MustCompile(`(vsc)\-(\S+?)\-(\S+?)\-(\S+)`)

Check failure on line 648 in cmd/finch/nerdctl_remote.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
//if re.MatchString(arg) {
// matchSlice := re.FindStringSubmatch(arg)
// matchSlice[3] = matchSlice[3][:6]
// newArg := strings.Join(matchSlice[1:], "-")
// arg = newArg
//}

if buildx {
if arg == "build" {
logrus.Warnf("found build")
logrus.Warnf("skipping the redundant 'build' arg")
// Do not add "build" again to the newLimaArgs
continue
} else if slices.Contains(buildxSubcommands, arg) {
logrus.Warnf(buildxWarnMsg, arg)
logrus.Warn("Unsupported buildx command: ", arg)
return skipCmd, nil
}

logrus.Warnf("appending build")
newLimaArgs = append(newLimaArgs, arg)

} else {
newLimaArgs = append(newLimaArgs, arg)
}

newLimaArgs = append(newLimaArgs, arg)
}

return !skipCmd, newLimaArgs
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion pkg/config/config_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
1 change: 0 additions & 1 deletion pkg/config/config_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

// SystemSettings represents the system configuration specifc to Windows.
type SystemSettings struct {
Mode *string `yaml:"mode,omitempty"`
SharedSystemSettings `yaml:",inline"`
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/path/finch_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit de74f5b

Please sign in to comment.