Skip to content

Commit

Permalink
Support --add-host with defaults for linux
Browse files Browse the repository at this point in the history
Add a new `--add-host` multi flag to the `kl network start` command
which allows configuring additional hosts on the proxy container.

In addition to this, on linux, the `host.docker.internal:172.17.0.1`
host is added to be consistent with macos.
  • Loading branch information
julienvincent committed Nov 22, 2023
1 parent 0acf481 commit 4719d87
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Now you can start the docker network and proxy containers:
kl network start
```

> [!NOTE]
> [!NOTE]
> On linux the host DNS network container's port defaults to binding to `5343`. If you would like to change this you can start the networking components with a different port by running `kl network start --host-dns-port=<custom-port>`. You will then also need to update your `/etc/systemd/resolved.conf` file to match.
---
Expand Down Expand Up @@ -208,18 +208,16 @@ Below are some common ways of addressing services running in different contexts:
+ `http://example:8080`
+ `http://<container-ip>:8080`
+ A process running on the host and bound to port `8080` could be addressed as
+ On macos - `http://host.docker.internal:8080`
+ On macos and linux* - `http://host.docker.internal:8080`
+ On linux - `http://172.17.0.1:8080`

Because of this host address discrepancy between linux and macos it is highly recommended for **linux based users** to add the following to their `/etc/hosts` file:

```bash
172.17.0.1 host.docker.internal
```

**DO NOT** do this if you are on macos.

This is an acceptable compromise to allow for a stable way of addressing the host in module configs that are intended to be consumed by developers on different operating systems.
> [!NOTE]
>
> On **linux\*** docker does not configure the `host.docker.internal` domain which is typically only available on macos when using something like Docker Desktop.
>
> To allow for a consistent way of addressing the host that works across all operating systems kl manually adds the `host.docker.internal:172.17.0.1` host to to the proxy container.
>
> If you don't want this behaviour you can disable it by running `kl network start --add-host "host.docker.internal:"` (note the empty ip after the ':').
## Routes

Expand Down
38 changes: 31 additions & 7 deletions src/k16/kl/commands/network.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,38 @@

module))

(defn- start-network! [{:keys [host-dns host-dns-port]}]
(def ^:private os
(-> (System/getProperty "os.name") .toLowerCase))

(def ^:private default-hosts
(cond
(str/includes? os "linux") ["host.docker.internal:172.17.0.1"]
:else []))

(defn- start-network! [{:keys [host-dns host-dns-port add-host]}]
(let [workdir (api.fs/from-config-dir ".kl/network")

extra-hosts (->> (concat default-hosts add-host)
(reduce (fn [acc host]
(let [[host ip] (str/split host #":")]
(assoc acc host ip)))
{})
(filter (fn [[_ ip]]
(and ip (not= "" ip))))
(map (fn [[host ip]] (str host ":" ip))))

module (cond-> (write-network-module)
(not host-dns)
(assoc-in [:containers :dnsmasq-external :enabled]
false)

host-dns-port
(assoc-in [:containers :dnsmasq-external :ports]
[(str host-dns-port ":53/udp") (str host-dns-port ":53/tcp")]))]
[(str host-dns-port ":53/udp") (str host-dns-port ":53/tcp")])

(seq extra-hosts)
(assoc-in [:containers :proxy :extra_hosts]
extra-hosts))]

(api.proxy/write-proxy-config! {:module-name "kl"
:module module})
Expand All @@ -61,11 +83,10 @@
(api.fs/rm-dir! workdir)))

(def ^:private default-port
(let [os (-> (System/getProperty "os.name") .toLowerCase)]
(cond
(str/includes? os "mac") "53"
(str/includes? os "linux") "5343"
:else "5343")))
(cond
(str/includes? os "mac") "53"
(str/includes? os "linux") "5343"
:else "5343"))

(def cmd
{:command "network"
Expand All @@ -79,6 +100,9 @@
:type :with-flag}
{:option "host-dns-port"
:default default-port
:type :string}
{:option "add-host"
:multiple true
:type :string}]

:runs start-network!}
Expand Down

0 comments on commit 4719d87

Please sign in to comment.