Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: resolve special IP host-gateway #453

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/finch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func virtualMachineCommands(
logger,
optionalDepGroups,
config.NewLimaApplier(fc, ecc, fs, fp.LimaOverrideConfigPath(), system.NewStdLib()),
config.NewNerdctlApplier(fssh.NewDialer(), fs, fp.LimaSSHPrivateKeyPath(), system.NewStdLib().Env("USER")),
config.NewNerdctlApplier(fc, fssh.NewDialer(), fs, fp.LimaSSHPrivateKeyPath(), system.NewStdLib().Env("USER")),
fp,
fs,
disk.NewUserDataDiskManager(lcc, ecc, &afero.OsFs{}, fp, system.NewStdLib().Env("HOME"), fc),
Expand Down
25 changes: 0 additions & 25 deletions cmd/finch/nerdctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
"path/filepath"
"strings"

dockerops "github.com/docker/docker/opts"
"github.com/lima-vm/lima/pkg/networks"

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

Expand Down Expand Up @@ -112,15 +109,6 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error {
}
skip = shouldSkip
fileEnvs = append(fileEnvs, addEnvs...)
case strings.HasPrefix(arg, "--add-host"):
switch arg {
case "--add-host":
args[i+1] = resolveIP(args[i+1], nc.logger)
default:
resolvedIP := resolveIP(arg[11:], nc.logger)
arg = fmt.Sprintf("%s%s", arg[0:11], resolvedIP)
}
nerdctlArgs = append(nerdctlArgs, arg)
default:
nerdctlArgs = append(nerdctlArgs, arg)
}
Expand Down Expand Up @@ -305,19 +293,6 @@ func handleEnvFile(fs afero.Fs, systemDeps NerdctlCommandSystemDeps, arg, arg2 s
return skip, envs, nil
}

func resolveIP(host string, logger flog.Logger) string {
parts := strings.SplitN(host, ":", 2)
// If the IP Address is a string called "host-gateway", replace this value with the IP address that can be used to
// access host from the containers.
// TODO: make the host gateway ip configurable.
if parts[1] == dockerops.HostGatewayName {
resolvedIP := networks.SlirpGateway
logger.Debugf(`Resolving special IP "host-gateway" to %q for host %q`, resolvedIP, parts[0])
return fmt.Sprintf("%s:%s", parts[0], resolvedIP)
}
return host
}

var nerdctlCmds = map[string]string{
"build": "Build an image from Dockerfile",
"builder": "Manage builds",
Expand Down
138 changes: 0 additions & 138 deletions cmd/finch/nerdctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,144 +365,6 @@ func TestNerdctlCommand_run(t *testing.T) {
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
},
},
{
ginglis13 marked this conversation as resolved.
Show resolved Hide resolved
name: "with --add-host flag and special IP by space",
cmdName: "run",
args: []string{"--rm", "--add-host", "name:host-gateway", "alpine:latest"},
wantErr: nil,
mockSvc: func(
t *testing.T,
lcc *mocks.LimaCmdCreator,
ncsd *mocks.NerdctlCommandSystemDeps,
logger *mocks.Logger,
ctrl *gomock.Controller,
fs afero.Fs,
) {
getVMStatusC := mocks.NewCommand(ctrl)
lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC)
getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil)
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
ncsd.EXPECT().LookupEnv("AWS_ACCESS_KEY_ID").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
logger.EXPECT().Debugf(`Resolving special IP "host-gateway" to %q for host %q`, "192.168.5.2", "name")
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
"--rm", "--add-host", "name:192.168.5.2", "alpine:latest").Return(c)
c.EXPECT().Run()
},
},
{
name: "with --add-host flag but without using special IP by space",
cmdName: "run",
args: []string{"--rm", "--add-host", "name:0.0.0.0", "alpine:latest"},
wantErr: nil,
mockSvc: func(
t *testing.T,
lcc *mocks.LimaCmdCreator,
ncsd *mocks.NerdctlCommandSystemDeps,
logger *mocks.Logger,
ctrl *gomock.Controller,
fs afero.Fs,
) {
getVMStatusC := mocks.NewCommand(ctrl)
lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC)
getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil)
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
ncsd.EXPECT().LookupEnv("AWS_ACCESS_KEY_ID").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
"--rm", "--add-host", "name:0.0.0.0", "alpine:latest").Return(c)
c.EXPECT().Run()
},
},
{
name: "with --add-host flag but without subsequent arg",
cmdName: "run",
args: []string{"--rm", "--add-host", "alpine:latest"},
wantErr: errors.New("run cmd error"),
mockSvc: func(
t *testing.T,
lcc *mocks.LimaCmdCreator,
ncsd *mocks.NerdctlCommandSystemDeps,
logger *mocks.Logger,
ctrl *gomock.Controller,
fs afero.Fs,
) {
getVMStatusC := mocks.NewCommand(ctrl)
lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC)
getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil)
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
ncsd.EXPECT().LookupEnv("AWS_ACCESS_KEY_ID").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
"--rm", "--add-host", "alpine:latest").Return(c)
c.EXPECT().Run().Return(errors.New("run cmd error"))
},
},
{
name: "with --add-host flag and special IP by equal",
cmdName: "run",
args: []string{"--rm", "--add-host=name:host-gateway", "alpine:latest"},
wantErr: nil,
mockSvc: func(
t *testing.T,
lcc *mocks.LimaCmdCreator,
ncsd *mocks.NerdctlCommandSystemDeps,
logger *mocks.Logger,
ctrl *gomock.Controller,
fs afero.Fs,
) {
getVMStatusC := mocks.NewCommand(ctrl)
lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC)
getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil)
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
ncsd.EXPECT().LookupEnv("AWS_ACCESS_KEY_ID").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
logger.EXPECT().Debugf(`Resolving special IP "host-gateway" to %q for host %q`, "192.168.5.2", "name")
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
"--rm", "--add-host=name:192.168.5.2", "alpine:latest").Return(c)
c.EXPECT().Run()
},
},
{
name: "with --add-host flag but without using special IP by equal",
cmdName: "run",
args: []string{"--rm", "--add-host=name:0.0.0.0", "alpine:latest"},
wantErr: nil,
mockSvc: func(
t *testing.T,
lcc *mocks.LimaCmdCreator,
ncsd *mocks.NerdctlCommandSystemDeps,
logger *mocks.Logger,
ctrl *gomock.Controller,
fs afero.Fs,
) {
getVMStatusC := mocks.NewCommand(ctrl)
lcc.EXPECT().CreateWithoutStdio("ls", "-f", "{{.Status}}", limaInstanceName).Return(getVMStatusC)
getVMStatusC.EXPECT().Output().Return([]byte("Running"), nil)
logger.EXPECT().Debugf("Status of virtual machine: %s", "Running")
ncsd.EXPECT().LookupEnv("AWS_ACCESS_KEY_ID").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)

c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
"--rm", "--add-host=name:0.0.0.0", "alpine:latest").Return(c)
c.EXPECT().Run()
},
},
{
name: "with multiple nested volumes",
cmdName: "run",
Expand Down
58 changes: 58 additions & 0 deletions e2e/vm/host_gateway_ip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package vm

import (
"os/exec"
"path/filepath"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/option"

"github.com/runfinch/finch/e2e"
)

const defaultImage = "public.ecr.aws/docker/library/alpine:latest"
const nerdctlRootlessCfgPath = "/home/root.linux/.config/nerdctl/nerdctl.toml"
ningziwen marked this conversation as resolved.
Show resolved Hide resolved

var testHostGatewayIp = func(o *option.Option, installed bool) {
ningziwen marked this conversation as resolved.
Show resolved Hide resolved
// it requires disk I/O so this will be serial test
ginkgo.Describe("--add-host tag with host_gateway", ginkgo.Serial, func() {
var limaConfigFilePath string
ginkgo.BeforeEach(func() {
origFinchCfg := readFile(finchConfigFilePath)
limaConfigFilePath = defaultLimaConfigFilePath
if installed {
path, err := exec.LookPath(e2e.InstalledTestSubject)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
realFinchPath, err := filepath.EvalSymlinks(path)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
limaConfigFilePath = filepath.Join(realFinchPath, "../../lima/data/_config/override.yaml")
}
origLimaCfg := readFile(limaConfigFilePath)

ginkgo.DeferCleanup(func() {
writeFile(finchConfigFilePath, origFinchCfg)
writeFile(limaConfigFilePath, origLimaCfg)

command.New(o, virtualMachineRootCmd, "stop").WithoutCheckingExitCode().WithTimeoutInSeconds(90).Run()
command.New(o, virtualMachineRootCmd, "start").WithTimeoutInSeconds(240).Run()
})
})
ginkgo.It("should add a custom host-to-IP mapping with --add-host flag: default value", func() {
startCmdSession := updateAndApplyConfig(o, []byte("memory: 4GiB\ncpus: 6"))
gomega.Expect(startCmdSession).Should(gexec.Exit(0))
mapping := command.StdoutStr(o, "run", "--add-host", "test-host:host-gateway", defaultImage, "cat", "/etc/hosts")
gomega.Expect(mapping).Should(gomega.ContainSubstring("192.168.5.2"))
gomega.Expect(mapping).Should(gomega.ContainSubstring("test-host"))
})
ginkgo.It("should add a custom host-to-IP mapping with --add-host flag: custom value", func() {
startCmdSession := updateAndApplyConfig(o, []byte("memory: 4GiB\ncpus: 6\nhost_gateway_ip: 192.168.31.1"))
gomega.Expect(startCmdSession).Should(gexec.Exit(0))
mapping := command.StdoutStr(o, "run", "--add-host", "test-host:host-gateway", defaultImage, "cat", "/etc/hosts")
gomega.Expect(mapping).Should(gomega.ContainSubstring("192.168.31.1"))
gomega.Expect(mapping).Should(gomega.ContainSubstring("test-host"))
})
})
}
1 change: 1 addition & 0 deletions e2e/vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestVM(t *testing.T) {
testSupportBundle(o)
testCredHelper(o, *e2e.Installed, *e2e.Registry)
testSoci(o, *e2e.Installed)
testHostGatewayIp(o, *e2e.Installed)
})

gomega.RegisterFailHandler(ginkgo.Fail)
Expand Down
4 changes: 3 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ type Finch struct {
// Requires macOS 13.0 or later and an Apple Silicon (ARM64) mac.
// Has no effect on systems where Rosetta 2 is not available (Intel/AMD64 macs, or macOS < 13.0).
// This setting will only be applied on vm init.
Rosetta *bool `yaml:"rosetta,omitempty"`
Rosetta *bool `yaml:"rosetta,omitempty"`
HostGatewayIp *string `yaml:"host_gateway_ip,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the naming consistent with line 76. Probably follow the pattern in nerdctl. https://github.com/containerd/nerdctl/blob/1f8f8af4d19d651a6300d8d48f3f21c91048a716/pkg/config/config.go#L41

HostGatewayIP

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just fix it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add godoc comment i.e.

Suggested change
HostGatewayIp *string `yaml:"host_gateway_ip,omitempty"`
// HostGatewayIp sets the default host gateway ip....
HostGatewayIp *string `yaml:"host_gateway_ip,omitempty"`

}

// Nerdctl is a copy from github.com/containerd/nerdctl/cmd/nerdctl/main.go
Expand All @@ -72,6 +73,7 @@ type Nerdctl struct {
CgroupManager string `toml:"cgroup_manager,omitempty"`
InsecureRegistry bool `toml:"insecure_registry,omitempty"`
HostsDir []string `toml:"hosts_dir,omitempty"`
HostGatewayIP string `toml:"host_gateway_ip,omitempty"`
}

// LimaConfigApplier applies lima configuration changes.
Expand Down
12 changes: 9 additions & 3 deletions pkg/config/nerdctl_config_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
)

type nerdctlConfigApplier struct {
fc *Finch
dialer fssh.Dialer
fs afero.Fs
privateKeyPath string
Expand All @@ -33,8 +34,9 @@ var _ NerdctlConfigApplier = (*nerdctlConfigApplier)(nil)

// NewNerdctlApplier creates a new NerdctlConfigApplier that
// applies nerdctl configuration changes by SSHing to the lima VM to update the nerdctl configuration file in it.
func NewNerdctlApplier(dialer fssh.Dialer, fs afero.Fs, privateKeyPath, hostUser string) NerdctlConfigApplier {
func NewNerdctlApplier(fc *Finch, dialer fssh.Dialer, fs afero.Fs, privateKeyPath, hostUser string) NerdctlConfigApplier {
return &nerdctlConfigApplier{
fc: fc,
dialer: dialer,
fs: fs,
privateKeyPath: privateKeyPath,
Expand Down Expand Up @@ -93,7 +95,7 @@ func updateEnvironment(fs afero.Fs, user string) error {
}

// updateNerdctlConfig reads from the nerdctl config and updates values.
func updateNerdctlConfig(fs afero.Fs, user string, rootless bool) error {
func updateNerdctlConfig(fc *Finch, fs afero.Fs, user string, rootless bool) error {
nerdctlRootlessCfgPath := fmt.Sprintf("/home/%s.linux/.config/nerdctl/nerdctl.toml", user)

var cfgPath string
Expand Down Expand Up @@ -124,6 +126,10 @@ func updateNerdctlConfig(fs afero.Fs, user string, rootless bool) error {
}

cfg.Namespace = nerdctlNamespace
cfg.HostGatewayIP = "192.168.5.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of having this magic string down here, could we define as a constant defaultNerdctlHostGatewayIP here https://github.com/runfinch/finch/pull/453/files#diff-531afb9f97a7384e0de895046f9e14befbebc49c896b080fce4c41c75f14f1b4L20-L21

then you can also reference it in unit tests where you expect the HostGatewayIP to be the default value.

if fc.HostGatewayIp != nil {
cfg.HostGatewayIP = *fc.HostGatewayIp
}

updatedCfg, err := toml.Marshal(cfg)
if err != nil {
Expand Down Expand Up @@ -158,7 +164,7 @@ func (nca *nerdctlConfigApplier) Apply(remoteAddr string) error {
sftpFs := sftpfs.New(sftpClient)

// rootless hardcoded to false for now to match our finch.yaml file
if err := updateNerdctlConfig(sftpFs, user, false); err != nil {
if err := updateNerdctlConfig(nca.fc, sftpFs, user, false); err != nil {
return fmt.Errorf("failed to update the nerdctl config file: %w", err)
}

Expand Down
18 changes: 13 additions & 5 deletions pkg/config/nerdctl_config_applier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xorcare/pointer"

"github.com/runfinch/finch/pkg/mocks"
)
Expand Down Expand Up @@ -128,7 +129,7 @@ func Test_updateNerdctlConfig(t *testing.T) {
postRunCheck: func(t *testing.T, fs afero.Fs) {
fileBytes, err := afero.ReadFile(fs, "/home/mock_user.linux/.config/nerdctl/nerdctl.toml")
require.NoError(t, err)
assert.Equal(t, []byte(`namespace = "finch"`+"\n"), fileBytes)
assert.Equal(t, []byte(`host_gateway_ip = "192.168.31.1"`+"\n"+`namespace = "finch"`+"\n"), fileBytes)
},
want: nil,
},
Expand All @@ -140,7 +141,7 @@ func Test_updateNerdctlConfig(t *testing.T) {
postRunCheck: func(t *testing.T, fs afero.Fs) {
fileBytes, err := afero.ReadFile(fs, "/etc/nerdctl/nerdctl.toml")
require.NoError(t, err)
assert.Equal(t, []byte(`namespace = "finch"`+"\n"), fileBytes)
assert.Equal(t, []byte(`host_gateway_ip = "192.168.31.1"`+"\n"+`namespace = "finch"`+"\n"), fileBytes)
},
want: nil,
},
Expand All @@ -156,7 +157,7 @@ func Test_updateNerdctlConfig(t *testing.T) {
postRunCheck: func(t *testing.T, fs afero.Fs) {
fileBytes, err := afero.ReadFile(fs, "/home/mock_user.linux/.config/nerdctl/nerdctl.toml")
require.NoError(t, err)
assert.Equal(t, []byte(`namespace = "finch"`+"\n"), fileBytes)
assert.Equal(t, []byte(`host_gateway_ip = "192.168.31.1"`+"\n"+`namespace = "finch"`+"\n"), fileBytes)
},
want: nil,
},
Expand All @@ -179,13 +180,20 @@ func Test_updateNerdctlConfig(t *testing.T) {

for _, tc := range testCases {
tc := tc
fc := &Finch{
CPUs: pointer.Int(4),
Memory: pointer.String("4GiB"),
VMType: pointer.String("qemu"),
Rosetta: pointer.Bool(false),
HostGatewayIp: pointer.String("192.168.31.1"),
}
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

fs := afero.NewMemMapFs()

tc.mockSvc(t, fs)
got := updateNerdctlConfig(fs, tc.user, tc.rootless)
got := updateNerdctlConfig(fc, fs, tc.user, tc.rootless)
require.Equal(t, tc.want, got)

tc.postRunCheck(t, fs)
Expand Down Expand Up @@ -243,7 +251,7 @@ func TestNerdctlConfigApplier_Apply(t *testing.T) {
d := mocks.NewDialer(ctrl)

tc.mockSvc(t, fs, d)
got := NewNerdctlApplier(d, fs, tc.path, tc.hostUser).Apply(tc.remoteAddr)
got := NewNerdctlApplier(nil, d, fs, tc.path, tc.hostUser).Apply(tc.remoteAddr)

ningziwen marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, tc.want, got)
})
Expand Down