Skip to content

Commit

Permalink
Rework network stack to be exposed as standard module
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Oct 29, 2023
1 parent 2f08942 commit afea066
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 103 deletions.
File renamed without changes.
File renamed without changes.
54 changes: 54 additions & 0 deletions resources/network-module/module.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{:include ["config/dnsmasq-external.conf"
"config/dnsmasq-internal.conf"

"images/Dockerfile.dnsmasq-external"
"images/Dockerfile.dnsmasq-internal"]

:network {:services {:kl-proxy {:endpoints {:container {:url "http://proxy:8080"}}
:default-endpoint :container}}

:routes {:kl-proxy {:host "proxy.test"
:service :kl-proxy}}}

:networks {:kl {:name "kl"
:ipam {:driver "default"
:config [{:subnet "172.5.0.0/24"}]}}}

:containers
{:dnsmasq-external
{:build {:context "{{DIR}}"
:dockerfile "{{DIR}}/images/Dockerfile.dnsmasq-external"}
:platform "linux/amd64"
:restart "unless-stopped"
:command ["--conf-file=/dnsmasq-config/dnsmasq.conf"
"--except-interface=lo"
"--bind-interfaces"
"--log-queries"
"--log-facility=-"]
:ports ["53:53/udp"
"53:53/tcp"]
:cap_add ["NET_ADMIN"]}

:dnsmasq-internal
{:build {:context "."
:dockerfile "{{DIR}}/images/Dockerfile.dnsmasq-internal"}
:platform "linux/amd64"
:restart "unless-stopped"
:command ["--conf-file=/dnsmasq-config/dnsmasq.conf"
"--except-interface=lo"
"--bind-interfaces"
"--log-queries"
"--log-facility=-"]
:networks {:kl {:ipv4_address "172.5.0.100"}}
:cap_add ["NET_ADMIN"]}

:proxy
{:image "traefik:v2.6"
:restart "unless-stopped"
:networks {:kl {:ipv4_address "172.5.0.101"}}
:command ["--api.insecure=true"
"--providers.file.directory=/proxy-config"
"--providers.file.watch=true"]
:ports ["80:80"
"1001:8080"]
:volumes ["~/.config/kl/proxy:/proxy-config"]}}}
62 changes: 0 additions & 62 deletions resources/network.yml

This file was deleted.

21 changes: 12 additions & 9 deletions src/k16/kl/api/executor.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
(:require
[babashka.process :as proc]
[clj-yaml.core :as yaml]
[clojure.java.io :as io]
[k16.kl.api.fs :as api.fs]
[meta-merge.core :as metamerge]))

(set! *warn-on-reflection* true)

(defn- build-docker-compose [module]
(let [base (cond-> {:networks {:kl {:external true}}}
(:volumes module) (assoc :volumes (:volumes module)))
(:volumes module) (assoc :volumes (:volumes module))
(:networks module) (assoc :networks (:networks module)))

containers
(->> (:containers module)
Expand All @@ -26,28 +28,29 @@
(cond-> base
(seq containers) (assoc :services containers))))

(defn- exec-configuration! [{:keys [module-name module direction]}]
(defn run-module-containers! [{:keys [module direction project-name workdir]}]
(let [compose-data (build-docker-compose module)
compose-file (api.fs/from-module-work-dir module-name "docker-compose.yaml")
compose-file (io/file workdir "docker-compose.yaml")

direction (if (:services compose-data) direction :down)

args (case direction
:up ["-f" (.toString compose-file) "up" "-d" "--remove-orphans"]
:down ["down"])]

(io/make-parents compose-file)
(spit compose-file (yaml/generate-string compose-data))

(try
(proc/shell (concat
["docker" "compose"
"--project-name" (str "kl-" module-name)]
"--project-name" project-name]

args))
(catch Exception _))))

(defn start-configuration! [props]
(exec-configuration! (assoc props :direction :up)))

(defn stop-configuration! [props]
(exec-configuration! (assoc props :direction :down)))
(defn run-module! [{:keys [module-name module direction]}]
(run-module-containers! {:module module
:direction direction
:project-name (str "kl-" module-name)
:workdir (api.fs/from-module-work-dir module-name)}))
10 changes: 6 additions & 4 deletions src/k16/kl/commands/containers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@
(let [module (metamerge/meta-merge module updated-state)]
(api.proxy/write-proxy-config! {:module-name module-name
:module module})
(api.executor/start-configuration! {:module-name module-name
:module module}))))})
(api.executor/run-module! {:module-name module-name
:module module
:direction :up}))))})

(def ^:private stop-cmd
{:command "down"
Expand All @@ -90,8 +91,9 @@
(let [module-name (prompt.config/get-module-name props)
{:keys [modules]} (api.resolver/pull! module-name {})
module (api.module/get-resolved-module module-name modules)]
(api.executor/stop-configuration! {:module-name module-name
:module module})))})
(api.executor/run-module! {:module-name module-name
:module module
:direction :down})))})

(def cmd
{:command "containers"
Expand Down
68 changes: 40 additions & 28 deletions src/k16/kl/commands/network.clj
Original file line number Diff line number Diff line change
@@ -1,50 +1,62 @@
(ns k16.kl.commands.network
(:require
[babashka.process :as proc]
[clojure.java.io :as io]))
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str]
[k16.kl.api.executor :as api.executor]
[k16.kl.api.fs :as api.fs]
[k16.kl.api.proxy :as api.proxy]))

(defn- gen-hash [n]
(->> (repeatedly n #(rand-int 256))
(map #(format "%02x" %))
(apply str)))

(defn- get-tmp-dir []
(io/file (System/getProperty "java.io.tmpdir") (str "kl-network-" (gen-hash 5))))

(defn- rm-dir [^java.io.File file]
(when (.isDirectory file)
(run! rm-dir (.listFiles file)))
(io/delete-file file))

(defn- prepare-config []
(let [dir (io/file (System/getProperty "java.io.tmpdir") (str "kl-network" (gen-hash 5)))
files ["config/dnsmasq-internal.conf" "config/dnsmasq-external.conf"
"images/Dockerfile.dnsmasq-external" "images/Dockerfile.dnsmasq-internal"
"network.yml"]]
(defn- write-network-module []
(let [prefix ".kl/network"
module-file (io/resource "network-module/module.edn")
module-raw (-> module-file
slurp
(str/replace "{{DIR}}" (.toString (api.fs/from-config-dir prefix))))
module (edn/read-string module-raw)]

(->> files
(map (fn [file]
(let [source (io/resource file)
sink (io/file dir file)]
(io/make-parents sink)
(spit sink (slurp source)))))
dorun)
(spit (api.fs/from-config-dir prefix "module.edn") module-raw)

dir))
(doseq [file (:include module)]
(let [contents (slurp (io/resource (str "network-module/" file)))]
(spit (api.fs/from-config-dir prefix file) contents)))

(defn- start-network! [_]
(let [dir (prepare-config)]
(proc/shell ["docker" "compose"
"-p" "kl"
"-f" (.toString (io/file dir "network.yml"))
"up" "-d" "--remove-orphans"])
module))

(rm-dir dir)))
(defn- start-network! [_]
(let [workdir (get-tmp-dir)
module (write-network-module)]
(api.proxy/write-proxy-config! {:module-name "kl"
:module module})
(api.executor/run-module-containers! {:module module
:direction :up
:project-name "kl"
:workdir workdir})
(rm-dir workdir)))

(defn- stop-network! [_]
(let [dir (prepare-config)]
(proc/shell ["docker" "compose"
"-p" "kl"
"-f" (.toString (io/file dir "network.yml"))
"stop"])
(rm-dir dir)))
(let [workdir (get-tmp-dir)
module (write-network-module)]
(api.proxy/write-proxy-config! {:module-name "kl"
:module {}})
(api.executor/run-module-containers! {:module module
:direction :down
:project-name "kl"
:workdir workdir})
(rm-dir workdir)))

(def cmd
{:command "network"
Expand Down

0 comments on commit afea066

Please sign in to comment.