Skip to content

Commit

Permalink
feat: add Linux support (#1023)
Browse files Browse the repository at this point in the history
* feat(wip): allow finch to build on Linux without Lima

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* fix Makefile

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* refactor lima_cmd_creator => nerdctl_cmd_creator

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* fix some refactoring/renaming misses

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* fix unit tests

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* fix linting

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* remove unnecessary mockgen call

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* remove unused code

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* fix windows unit tests

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* fix linux linting errors

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* refactor to use standard Linux config path

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* update docs

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* fix shared settings not being inlined

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* refactor support bundles to only export files that would be relevant on Linux

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* update build guards

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* fix FinchRootDir

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* refactor cmd/nerdctl for Linux

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* refactor nerdctl to use custom paths for buildkit socket and nerdctl.toml

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* fix linting

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* automatically set the NATIVE_BUILD parameter if using Linux since that's the only supported config

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* allow GITCOMMIT to be overwritten to make RPM builds easier

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* finch-general => finch-all

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* main_local.go => main_native.go

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* remove ensureCredentials check in native mode

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* virtual_machine_local.go => virtual_machine_native.go

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* ensure all remote only tests have _remote suffix

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* prepend path to vended buildkit binaries for Fedora-based systems

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* fix Makefile not handling default case for NATIVE_BUILD not on Linux

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* simplify makefile

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* rename variable

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* add native build tag

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* update NerdctlCmdCreator comment

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* fix Makefile for non-native builds

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* remove native build tag and simplify platform separation

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* remove unnecessary remote in names

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* fix unit tests

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

* fix BuildkitSocketPath

Signed-off-by: Justin Alvarez <alvajus@amazon.com>

---------

Signed-off-by: Justin Alvarez <alvajus@amazon.com>
  • Loading branch information
pendo324 authored Sep 16, 2024
1 parent 35bc4dc commit 82f698b
Show file tree
Hide file tree
Showing 103 changed files with 2,625 additions and 2,328 deletions.
39 changes: 28 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ LIMA_EXTENSION := .tar.gz
LIMA_VDE_SUDOERS_FILE := /etc/sudoers.d/finch-lima
# Final installation prefix for vde created by CLI after installation, only used in uninstall step
VDE_INSTALL ?= /opt/finch
UNAME := $(shell uname -m)
ARCH ?= $(UNAME)
ARCH ?= $(shell uname -m)
SUPPORTED_ARCH = false
LICENSEDIR := $(OUTDIR)/license-files
VERSION ?= $(shell git describe --match 'v[0-9]*' --dirty='.modified' --always --tags)
GITCOMMIT := $(shell git rev-parse HEAD)$(shell test -z "$(git status --porcelain)" || echo .m)
GITCOMMIT ?= $(shell git rev-parse HEAD)$(shell test -z "$(git status --porcelain)" || echo .m)
LDFLAGS = "-w -X $(PACKAGE)/pkg/version.Version=$(VERSION) -X $(PACKAGE)/pkg/version.GitCommit=$(GITCOMMIT)"
MIN_MACOS_VERSION ?= 11.0

Expand All @@ -52,16 +51,29 @@ endif
arch-test:
@if [ $(SUPPORTED_ARCH) != "true" ]; then echo "Unsupported architecture: $(ARCH)"; exit "1"; fi

# OS will be set on Windows (to Windows_NT), and undefined otherwise unless
# it is explicity specified on the commandline.
# On Unix (or, not-Windows), query OS and set it using uname -s
BUILD_OS ?= $(OS)
ifeq ($(BUILD_OS),)
BUILD_OS = $(shell uname -s)
endif

FINCH_CORE_DIR := $(CURDIR)/deps/finch-core

remote-all: arch-test finch install.finch-core-dependencies finch.yaml networks.yaml config.yaml

ifeq ($(BUILD_OS), Windows_NT)
include Makefile.windows
else
all: remote-all
else ifeq ($(BUILD_OS), Darwin)
include Makefile.darwin
all: remote-all
else ifeq ($(BUILD_OS), Linux)
# on Linux, we only need to build "finch"
all: finch
endif

all: arch-test finch install.finch-core-dependencies finch.yaml networks.yaml config.yaml

.PHONY: install.finch-core-dependencies
install.finch-core-dependencies:
OUTDIR=$(OUTDIR) ARCH=$(ARCH) "$(MAKE)" -C $(FINCH_CORE_DIR) install.dependencies
Expand Down Expand Up @@ -106,11 +118,13 @@ uninstall: uninstall.finch

.PHONY: finch
ifeq ($(GOOS),windows)
finch: finch-windows finch-general
finch: finch-windows finch-all
else ifeq ($(GOOS),darwin)
finch: finch-macos
else ifeq ($(NATIVE_BUILD),true)
finch: finch-native
else
finch: finch-unix
finch: finch-all
endif

finch-windows:
Expand All @@ -121,10 +135,13 @@ finch-macos: export CGO_CFLAGS := -mmacosx-version-min=$(MIN_MACOS_VERSION)
finch-macos: export CGO_LDFLAGS := -mmacosx-version-min=$(MIN_MACOS_VERSION)
finch-macos: finch-unix

finch-unix: finch-general
finch-unix: finch-all

finch-native: GO_BUILD_TAGS += native
finch-native: finch-all

finch-general:
$(GO) build -ldflags $(LDFLAGS) -o $(OUTDIR)/bin/$(BINARYNAME) $(PACKAGE)/cmd/finch
finch-all:
$(GO) build -ldflags $(LDFLAGS) -tags "$(GO_BUILD_TAGS)" -o $(OUTDIR)/bin/$(BINARYNAME) $(PACKAGE)/cmd/finch

.PHONY: release
release: check-licenses all download-licenses
Expand Down
20 changes: 0 additions & 20 deletions cmd/finch/lima_args_windows.go

This file was deleted.

70 changes: 2 additions & 68 deletions cmd/finch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ import (
"github.com/runfinch/finch/pkg/config"
"github.com/runfinch/finch/pkg/flog"
"github.com/runfinch/finch/pkg/fmemory"
"github.com/runfinch/finch/pkg/lima/wrapper"
"github.com/runfinch/finch/pkg/path"
"github.com/runfinch/finch/pkg/support"
"github.com/runfinch/finch/pkg/system"
"github.com/runfinch/finch/pkg/version"
)

const finchRootCmd = "finch"
Expand Down Expand Up @@ -83,77 +80,14 @@ func xmain(logger flog.Logger,
).Execute()
}

var newApp = func(
logger flog.Logger,
fp path.Finch,
fs afero.Fs,
fc *config.Finch,
stdOut io.Writer,
home,
finchRootPath string,
ecc command.Creator,
) *cobra.Command {
usage := fmt.Sprintf("%v <command>", finchRootCmd)
rootCmd := &cobra.Command{
Use: usage,
Short: "Finch: open-source container development tool",
SilenceUsage: true,
SilenceErrors: true,
Version: version.Version,
}
// TODO: Decide when to forward --debug to the dependencies
// (e.g. nerdctl for container commands and limactl for VM commands).
rootCmd.PersistentFlags().Bool("debug", false, "running under debug mode")
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
// running commands under debug mode will print out debug logs
debugMode, _ := cmd.Flags().GetBool("debug")
if debugMode {
logger.SetLevel(flog.Debug)
}
return nil
}

lcc := command.NewLimaCmdCreator(ecc,
logger,
fp.LimaHomePath(),
fp.LimactlPath(),
fp.QEMUBinDir(),
system.NewStdLib(),
)
lima := wrapper.NewLimaWrapper()
supportBundleBuilder := support.NewBundleBuilder(
logger,
fs,
support.NewBundleConfig(fp, finchRootPath),
fp,
ecc,
lcc,
lima,
)

// append nerdctl commands
allCommands := initializeNerdctlCommands(lcc, ecc, logger, fs, fc)
// append finch specific commands
allCommands = append(allCommands,
newVersionCommand(lcc, logger, stdOut),
virtualMachineCommands(logger, fp, lcc, ecc, fs, fc, home, finchRootPath),
newSupportBundleCommand(logger, supportBundleBuilder, lcc),
newGenDocsCommand(rootCmd, logger, fs, system.NewStdLib()),
)

rootCmd.AddCommand(allCommands...)

return rootCmd
}

func initializeNerdctlCommands(
lcc command.LimaCmdCreator,
ncc command.NerdctlCmdCreator,
ecc command.Creator,
logger flog.Logger,
fs afero.Fs,
fc *config.Finch,
) []*cobra.Command {
nerdctlCommandCreator := newNerdctlCommandCreator(lcc, ecc, system.NewStdLib(), logger, fs, fc)
nerdctlCommandCreator := newNerdctlCommandCreator(ncc, ecc, system.NewStdLib(), logger, fs, fc)
var allNerdctlCommands []*cobra.Command
for cmdName, cmdDescription := range nerdctlCmds {
allNerdctlCommands = append(allNerdctlCommands, nerdctlCommandCreator.create(cmdName, cmdDescription))
Expand Down
4 changes: 2 additions & 2 deletions cmd/finch/main_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func dependencies(
fc *config.Finch,
fp path.Finch,
fs afero.Fs,
lcc command.LimaCmdCreator,
ncc command.NerdctlCmdCreator,
logger flog.Logger,
finchRootPath string,
) []*dependency.Group {
Expand All @@ -37,6 +37,6 @@ func dependencies(
finchRootPath,
system.NewStdLib().Arch(),
),
vmnet.NewDependencyGroup(ecc, lcc, fs, fp, logger),
vmnet.NewDependencyGroup(ecc, ncc, fs, fp, logger),
}
}
85 changes: 85 additions & 0 deletions cmd/finch/main_native.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//go:build linux

package main

import (
"fmt"
"io"

"github.com/spf13/afero"
"github.com/spf13/cobra"

"github.com/runfinch/finch/pkg/command"
"github.com/runfinch/finch/pkg/config"
"github.com/runfinch/finch/pkg/flog"
"github.com/runfinch/finch/pkg/lima/wrapper"
"github.com/runfinch/finch/pkg/path"
"github.com/runfinch/finch/pkg/support"
"github.com/runfinch/finch/pkg/system"
"github.com/runfinch/finch/pkg/version"
)

var newApp = func(
logger flog.Logger,
fp path.Finch,
fs afero.Fs,
fc *config.Finch,
stdOut io.Writer,
_,
finchRootPath string,
ecc command.Creator,
) *cobra.Command {
usage := fmt.Sprintf("%v <command>", finchRootCmd)
rootCmd := &cobra.Command{
Use: usage,
Short: "Finch: open-source container development tool",
SilenceUsage: true,
SilenceErrors: true,
Version: version.Version,
}
// TODO: Decide when to forward --debug to the dependencies
// (e.g. nerdctl for container commands and limactl for VM commands).
rootCmd.PersistentFlags().Bool("debug", false, "running under debug mode")
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
// running commands under debug mode will print out debug logs
debugMode, _ := cmd.Flags().GetBool("debug")
if debugMode {
logger.SetLevel(flog.Debug)
}
return nil
}

ncc := command.NewNerdctlCmdCreator(ecc,
logger,
fp.NerdctlConfigFilePath(finchRootPath),
fp.BuildkitSocketPath(),
fp.FinchDependencyBinDir(),
system.NewStdLib(),
)
lima := wrapper.NewLimaWrapper()
supportBundleBuilder := support.NewBundleBuilder(
logger,
fs,
support.NewBundleConfig(fp, finchRootPath),
fp,
ecc,
ncc,
lima,
)

// append nerdctl commands
allCommands := initializeNerdctlCommands(ncc, ecc, logger, fs, fc)
// append finch specific commands
allCommands = append(allCommands,
newVersionCommand(ncc, logger, stdOut),
newSupportBundleCommand(logger, supportBundleBuilder, ncc),
newGenDocsCommand(rootCmd, logger, fs, system.NewStdLib()),
)

rootCmd.AddCommand(allCommands...)

return rootCmd
}
86 changes: 86 additions & 0 deletions cmd/finch/main_remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//go:build darwin || windows

package main

import (
"fmt"
"io"

"github.com/spf13/afero"
"github.com/spf13/cobra"

"github.com/runfinch/finch/pkg/command"
"github.com/runfinch/finch/pkg/config"
"github.com/runfinch/finch/pkg/flog"
"github.com/runfinch/finch/pkg/lima/wrapper"
"github.com/runfinch/finch/pkg/path"
"github.com/runfinch/finch/pkg/support"
"github.com/runfinch/finch/pkg/system"
"github.com/runfinch/finch/pkg/version"
)

var newApp = func(
logger flog.Logger,
fp path.Finch,
fs afero.Fs,
fc *config.Finch,
stdOut io.Writer,
home,
finchRootPath string,
ecc command.Creator,
) *cobra.Command {
usage := fmt.Sprintf("%v <command>", finchRootCmd)
rootCmd := &cobra.Command{
Use: usage,
Short: "Finch: open-source container development tool",
SilenceUsage: true,
SilenceErrors: true,
Version: version.Version,
}
// TODO: Decide when to forward --debug to the dependencies
// (e.g. nerdctl for container commands and limactl for VM commands).
rootCmd.PersistentFlags().Bool("debug", false, "running under debug mode")
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
// running commands under debug mode will print out debug logs
debugMode, _ := cmd.Flags().GetBool("debug")
if debugMode {
logger.SetLevel(flog.Debug)
}
return nil
}

ncc := command.NewNerdctlCmdCreator(ecc,
logger,
fp.LimaHomePath(),
fp.LimactlPath(),
fp.QEMUBinDir(),
system.NewStdLib(),
)
lima := wrapper.NewLimaWrapper()
supportBundleBuilder := support.NewBundleBuilder(
logger,
fs,
support.NewBundleConfig(fp, finchRootPath),
fp,
ecc,
ncc,
lima,
)

// append nerdctl commands
allCommands := initializeNerdctlCommands(ncc, ecc, logger, fs, fc)
// append finch specific commands
allCommands = append(allCommands,
newVersionCommand(ncc, logger, stdOut),
virtualMachineCommands(logger, fp, ncc, ecc, fs, fc, home, finchRootPath),
newSupportBundleCommand(logger, supportBundleBuilder, ncc),
newGenDocsCommand(rootCmd, logger, fs, system.NewStdLib()),
)

rootCmd.AddCommand(allCommands...)

return rootCmd
}
2 changes: 1 addition & 1 deletion cmd/finch/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func dependencies(
fc *config.Finch,
fp path.Finch,
fs afero.Fs,
_ command.LimaCmdCreator,
_ command.NerdctlCmdCreator,
logger flog.Logger,
finchDir string,
) []*dependency.Group {
Expand Down
Loading

0 comments on commit 82f698b

Please sign in to comment.