Skip to content

Commit

Permalink
feat(network): make port-forwarding host interface configurable (#45)
Browse files Browse the repository at this point in the history
Signed-off-by: Abiola Ibrahim <abiola89@gmail.com>
  • Loading branch information
abiosoft authored Oct 26, 2021
1 parent 21f39f2 commit d263df1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ The --runtime, --disk and --arch flags are only used on initial start and ignore
if !cmd.Flag("mount").Changed {
startCmdArgs.VM.Mounts = current.VM.Mounts
}
if !cmd.Flag("port-interface").Changed {
startCmdArgs.PortInterface = current.PortInterface
}

log.Println("using", current.Runtime, "runtime")

Expand Down Expand Up @@ -106,6 +109,7 @@ func randomAvailablePort() int {
func init() {
runtimes := strings.Join(environment.ContainerRuntimes(), ", ")
defaultArch := string(environment.Arch(runtime.GOARCH).Value())
defaultPortInterface := net.ParseIP("127.0.0.1")

root.Cmd().AddCommand(startCmd)
startCmd.Flags().StringVarP(&startCmdArgs.Runtime, "runtime", "r", docker.Name, "container runtime ("+runtimes+")")
Expand All @@ -118,6 +122,9 @@ func init() {
// mounts
startCmd.Flags().StringSliceVarP(&startCmdArgs.VM.Mounts, "mount", "v", nil, "directories to mount, suffix ':w' for writable")

// port forwarding
startCmd.Flags().IPVarP(&startCmdArgs.PortInterface, "port-interface", "i", defaultPortInterface, "interface to use for forwarded ports")

// k8s
startCmd.Flags().BoolVarP(&startCmdArgs.Kubernetes.Enabled, "with-kubernetes", "k", false, "start VM with Kubernetes")
startCmd.Flags().StringVar(&startCmdArgs.Kubernetes.Version, "kubernetes-version", defaultKubernetesVersion, "the Kubernetes version")
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ type Config struct {

// Kubernetes sets if kubernetes should be enabled.
Kubernetes Kubernetes `yaml:"kubernetes"`

// Network address to forward VM ports to.
PortInterface net.IP
}

// Kubernetes is kubernetes configuration
Expand Down
29 changes: 29 additions & 0 deletions environment/vm/lima/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ func newConf(conf config.Config) (l Config, err error) {
l.Env[k] = v
}

// handle port forwarding to allow listening on 0.0.0.0
l.PortForwards = append(l.PortForwards,
PortForward{
GuestIP: net.ParseIP("127.0.0.1"),
GuestPortRange: [2]int{1, 65535},
HostIP: conf.PortInterface,
HostPortRange: [2]int{1, 65535},
Proto: TCP,
},
)

if len(conf.VM.Mounts) == 0 {
l.Mounts = append(l.Mounts,
Mount{Location: "~", Writable: false},
Expand Down Expand Up @@ -84,6 +95,7 @@ type Config struct {
DNS []net.IP `yaml:"-"` // will be handled manually by colima
Firmware Firmware `yaml:"firmware"`
UseHostResolver bool `yaml:"useHostResolver"`
PortForwards []PortForward `yaml:"portForwards,omitempty"`
}

type File struct {
Expand Down Expand Up @@ -114,6 +126,23 @@ type Firmware struct {
LegacyBIOS bool `yaml:"legacyBIOS"`
}

type Proto = string

const (
TCP Proto = "tcp"
)

type PortForward struct {
GuestIP net.IP `yaml:"guestIP,omitempty" json:"guestIP,omitempty"`
GuestPort int `yaml:"guestPort,omitempty" json:"guestPort,omitempty"`
GuestPortRange [2]int `yaml:"guestPortRange,omitempty" json:"guestPortRange,omitempty"`
HostIP net.IP `yaml:"hostIP,omitempty" json:"hostIP,omitempty"`
HostPort int `yaml:"hostPort,omitempty" json:"hostPort,omitempty"`
HostPortRange [2]int `yaml:"hostPortRange,omitempty" json:"hostPortRange,omitempty"`
Proto Proto `yaml:"proto,omitempty" json:"proto,omitempty"`
Ignore bool `yaml:"ignore,omitempty" json:"ignore,omitempty"`
}

type volumeMount string

func (v volumeMount) Writable() bool {
Expand Down

0 comments on commit d263df1

Please sign in to comment.