Skip to content

Commit

Permalink
Hostgateway rough solution without testing
Browse files Browse the repository at this point in the history
Signed-off-by: trungbtc <[email protected]>
  • Loading branch information
trungbtc committed Jun 14, 2023
1 parent f69ec5c commit 9e86d79
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ tmp/
.vscode/
tools_bin/
test-coverage.*
restart_test
7 changes: 3 additions & 4 deletions cmd/finch/nerdctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error {
resolvedIP := resolveIP(nc.fc, arg[11:], nc.logger)
arg = fmt.Sprintf("%s%s", arg[0:11], resolvedIP)
}
nerdctlArgs = append(nerdctlArgs, "--host-gateway-ip")
nerdctlArgs = append(nerdctlArgs, *nc.fc.HostGatewayIP)
nerdctlArgs = append(nerdctlArgs, arg)
case strings.HasPrefix(arg, "-v") || strings.HasPrefix(arg, "--volume"):
// TODO: remove the case branch when the Nerdctl fix is released to Finch.
Expand Down Expand Up @@ -353,11 +355,8 @@ func resolveIP(fc *config.Finch, host string, logger flog.Logger) string {
// access host from the containers.
// TODO: make the host gateway ip configurable.
if parts[1] == dockerops.HostGatewayName {
resolvedIP := *fc.HostGatewayIP
logger.Debugf(`Resolving special IP "host-gateway" to %q for host %q`, resolvedIP, parts[0])
return fmt.Sprintf("%s:%s", parts[0], resolvedIP)
logger.Debugf(`Resolving special IP "host-gateway" to %q for host %q`, *fc.HostGatewayIP, parts[0])
}
fmt.Fprint(os.Stderr, host)
return host
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/finch/nerdctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func TestNerdctlCommand_run(t *testing.T) {
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)
"--rm", "--host-gateway-ip", "192.168.5.2", "--add-host", "name:host-gateway", "alpine:latest").Return(c)
c.EXPECT().Run()
},
},
Expand All @@ -401,7 +401,7 @@ func TestNerdctlCommand_run(t *testing.T) {
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)
"--rm", "--host-gateway-ip", "192.168.5.2", "--add-host", "name:0.0.0.0", "alpine:latest").Return(c)
c.EXPECT().Run()
},
},
Expand All @@ -426,7 +426,7 @@ func TestNerdctlCommand_run(t *testing.T) {
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)
"--rm", "--host-gateway-ip", "192.168.5.2", "--add-host", "alpine:latest").Return(c)
c.EXPECT().Run().Return(errors.New("run cmd error"))
},
},
Expand All @@ -452,7 +452,7 @@ func TestNerdctlCommand_run(t *testing.T) {
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)
"--rm", "--host-gateway-ip", "192.168.5.2", "--add-host=name:host-gateway", "alpine:latest").Return(c)
c.EXPECT().Run()
},
},
Expand All @@ -477,7 +477,7 @@ func TestNerdctlCommand_run(t *testing.T) {
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)
"--rm", "--host-gateway-ip", "192.168.5.2", "--add-host=name:0.0.0.0", "alpine:latest").Return(c)
c.EXPECT().Run()
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Finch struct {
// This setting will only be applied on vm init.
Rosetta *bool `yaml:"rosetta,omitempty"`
// Configurable hostgateway IP
HostGatewayIP *string `yaml:"host-gateway-ip"`
HostGatewayIP *string `yaml:"host-gateway-ip,omitempty"`
}

// Nerdctl is a copy from github.com/containerd/nerdctl/cmd/nerdctl/main.go
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ cpus: 8
CPUs: pointer.Int(8),
VMType: pointer.String("qemu"),
Rosetta: pointer.Bool(false),
HostGatewayIP: pointer.String("192.168.5.2"),
},
wantErr: nil,
},
Expand All @@ -62,6 +63,7 @@ cpus: 8
CPUs: pointer.Int(2),
VMType: pointer.String("qemu"),
Rosetta: pointer.Bool(false),
HostGatewayIP: pointer.String("192.168.5.2"),
},
wantErr: nil,
},
Expand All @@ -78,6 +80,7 @@ cpus: 8
CPUs: pointer.Int(2),
VMType: pointer.String("qemu"),
Rosetta: pointer.Bool(false),
HostGatewayIP: pointer.String("192.168.5.2"),
},
wantErr: nil,
},
Expand All @@ -94,6 +97,7 @@ cpus: 8
CPUs: pointer.Int(2),
VMType: pointer.String("qemu"),
Rosetta: pointer.Bool(false),
HostGatewayIP: pointer.String("192.168.5.2"),
},
wantErr: nil,
},
Expand All @@ -110,6 +114,7 @@ cpus: 8
CPUs: pointer.Int(2),
VMType: pointer.String("qemu"),
Rosetta: pointer.Bool(false),
HostGatewayIP: pointer.String("192.168.5.2"),
},
wantErr: nil,
},
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func Test_applyDefaults(t *testing.T) {
Memory: pointer.String("3GiB"),
VMType: pointer.String("qemu"),
Rosetta: pointer.Bool(false),
HostGatewayIP: pointer.String("192.168.5.2"),
},
},
{
Expand All @@ -50,6 +51,7 @@ func Test_applyDefaults(t *testing.T) {
Memory: pointer.String("4GiB"),
VMType: pointer.String("qemu"),
Rosetta: pointer.Bool(false),
HostGatewayIP: pointer.String("192.168.5.2"),
},
},
{
Expand All @@ -66,6 +68,7 @@ func Test_applyDefaults(t *testing.T) {
Memory: pointer.String("3GiB"),
VMType: pointer.String("qemu"),
Rosetta: pointer.Bool(false),
HostGatewayIP: pointer.String("192.168.5.2"),
},
},
{
Expand All @@ -81,6 +84,7 @@ func Test_applyDefaults(t *testing.T) {
Memory: pointer.String("2GiB"),
VMType: pointer.String("qemu"),
Rosetta: pointer.Bool(false),
HostGatewayIP: pointer.String("192.168.5.2"),
},
},
}
Expand Down

0 comments on commit 9e86d79

Please sign in to comment.