Skip to content

Commit

Permalink
dns: support for custom hosts (#512)
Browse files Browse the repository at this point in the history
* dns: support passing hostresolver custom hosts information to lima

* dns: move custom hosts functionality to network.dns_hosts, and add new cmd line option --dns-host
  • Loading branch information
FabianPonce authored Dec 14, 2022
1 parent d8adfca commit 20ba5dd
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
28 changes: 26 additions & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Run 'colima template' to set the default configurations or 'colima start --edit'
" colima start --cpu 4 --memory 8 --disk 100\n" +
" colima start --arch aarch64\n" +
" colima start --dns 1.1.1.1 --dns 8.8.8.8\n" +
" colima start --dns-host example.com=1.2.3.4\n" +
" colima start --kubernetes --kubernetes-disable=coredns,servicelb,traefik,local-storage,metrics-server",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -106,6 +107,7 @@ var startCmdArgs struct {
Edit bool
Editor string
ActivateRuntime bool
DNSHosts []string
}
}

Expand Down Expand Up @@ -168,7 +170,25 @@ func init() {
startCmd.Flags().StringToStringVar(&startCmdArgs.Env, "env", nil, "environment variables for the VM")

// dns
startCmd.Flags().IPSliceVarP(&startCmdArgs.Network.DNS, "dns", "n", nil, "DNS servers for the VM")
startCmd.Flags().IPSliceVarP(&startCmdArgs.Network.DNSResolvers, "dns", "n", nil, "DNSResolvers servers for the VM")
startCmd.Flags().StringSliceVarP(&startCmdArgs.Flags.DNSHosts, "dns-host", "", nil, "Custom DNS names to provide to resolver")
}

func dnsHostsFromFlag(hosts []string) map[string]string {
mapping := make(map[string]string)

for _, h := range hosts {
str := strings.SplitN(h, "=", 2)
if len(str) != 2 {
log.Warnf("unable to parse custom dns host: %v, skipping\n", h)
continue
}
src := str[0]
target := str[1]

mapping[src] = target
}
return mapping
}

// mountsFromFlag converts mounts from cli flag format to config file format
Expand Down Expand Up @@ -210,6 +230,7 @@ func prepareConfig(cmd *cobra.Command) {

// convert cli to config file format
startCmdArgs.Mounts = mountsFromFlag(startCmdArgs.Flags.Mounts)
startCmdArgs.Network.DNSHosts = dnsHostsFromFlag(startCmdArgs.Flags.DNSHosts)
startCmdArgs.ActivateRuntime = &startCmdArgs.Flags.ActivateRuntime

// handle macOS virtualization.framework transition
Expand Down Expand Up @@ -280,7 +301,10 @@ func prepareConfig(cmd *cobra.Command) {
startCmdArgs.SSHConfig = current.SSHConfig
}
if !cmd.Flag("dns").Changed {
startCmdArgs.Network.DNS = current.Network.DNS
startCmdArgs.Network.DNSResolvers = current.Network.DNSResolvers
}
if !cmd.Flag("dns-host").Changed {
startCmdArgs.Network.DNSHosts = current.Network.DNSHosts
}
if !cmd.Flag("env").Changed {
startCmdArgs.Env = current.Env
Expand Down
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ type Kubernetes struct {

// Network is VM network configuration
type Network struct {
Address bool `yaml:"address"`
DNS []net.IP `yaml:"dns"`
Address bool `yaml:"address"`
DNSResolvers []net.IP `yaml:"dns"`
DNSHosts map[string]string `yaml:"dns_hosts"`
}

// Mount is volume mount
Expand Down
8 changes: 7 additions & 1 deletion embedded/defaults/colima.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ network:
# Default: []
dns: []

# DNS hostnames to resolve to custom targets using the internal Lima resolver.
# This setting has no effect if a custom DNS resolver list is supplied above.
# It does not configure the /etc/hosts files of any machine or container.
dns_hosts:
host.docker.internal: host.lima.internal

# ===================================================================== #
# ADVANCED CONFIGURATION
# ===================================================================== #
Expand Down Expand Up @@ -168,4 +174,4 @@ mounts: []
# ANOTHER_KEY: another value
#
# Default: {}
env: {}
env: {}
13 changes: 9 additions & 4 deletions environment/vm/lima/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ func newConf(ctx context.Context, conf config.Config) (l Config, err error) {
l.SSH = SSH{LocalPort: 0, LoadDotSSHPubKeys: false, ForwardAgent: conf.ForwardAgent}
l.Containerd = Containerd{System: false, User: false}

l.DNS = conf.Network.DNS
l.HostResolver.Enabled = len(l.DNS) == 0
l.HostResolver.Hosts = map[string]string{
"host.docker.internal": "host.lima.internal",
l.DNS = conf.Network.DNSResolvers
l.HostResolver.Enabled = len(conf.Network.DNSResolvers) == 0
l.HostResolver.Hosts = conf.Network.DNSHosts
if l.HostResolver.Hosts == nil {
l.HostResolver.Hosts = make(map[string]string)
}

if _, ok := l.HostResolver.Hosts["host.docker.internal"]; !ok {
l.HostResolver.Hosts["host.docker.internal"] = "host.lima.internal"
}

l.Env = conf.Env
Expand Down
2 changes: 1 addition & 1 deletion util/yamlutil/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func Test_encode_Docker(t *testing.T) {
conf := config.Config{
Docker: map[string]any{"insecure-registries": []any{"127.0.0.1"}},
Network: config.Network{DNS: []net.IP{net.ParseIP("1.1.1.1")}},
Network: config.Network{DNSResolvers: []net.IP{net.ParseIP("1.1.1.1")}},
Kubernetes: config.Kubernetes{Disable: []string{"treafik"}},
}

Expand Down

0 comments on commit 20ba5dd

Please sign in to comment.