Skip to content

Commit

Permalink
Only generate traefik services that are in use
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Oct 29, 2023
1 parent f44b7f1 commit b4c174d
Showing 1 changed file with 22 additions and 32 deletions.
54 changes: 22 additions & 32 deletions src/k16/kl/api/proxy.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(ns k16.kl.api.proxy
(:require
[clj-yaml.core :as yaml]
[k16.kl.api.fs :as api.fs]
[meta-merge.core :as metamerge]))
[k16.kl.api.fs :as api.fs]))

(defn- get-proxies-projection-file [module-name]
(api.fs/from-config-dir "proxy/" (str module-name ".yaml")))
Expand All @@ -12,36 +11,27 @@
path-prefix (str " && PathPrefix(`" path-prefix "`)")))

(defn- build-routes [module]
(let [services
(->> (get-in module [:network :services])
(reduce (fn [acc [service-name service-def]]
(->> (:endpoints service-def)
(reduce (fn [acc [endpoint-name endpoint]]
(let [full-name (str (name service-name) "-" (name endpoint-name))]
(assoc-in acc [:http :services full-name :loadbalancer :servers]
[{:url (:url endpoint)}])))
acc)))

{}))

routes
(->> (get-in module [:network :routes])
(filter (fn [[_ route]] (get route :enabled true)))
(reduce (fn [acc [route-name route]]
(let [service-name (keyword (:service route))
service (get-in module [:network :services service-name])

endpoint-name (or (:endpoint route)
(:default-endpoint service))]

(if (and service-name endpoint-name)
(assoc-in acc [:http :routers (name route-name)]
{:rule (route->traefik-rule route)
:service (str (name service-name) "-" (name endpoint-name))})
acc)))
{}))]

(metamerge/meta-merge services routes)))
(->> (get-in module [:network :routes])
(filter (fn [[_ route]] (get route :enabled true)))
(reduce (fn [acc [route-name route]]
(let [service-name (keyword (:service route))
service (get-in module [:network :services service-name])

endpoint-name (or (:endpoint route)
(:default-endpoint service))
endpoint (get-in service [:endpoints endpoint-name])

traefik-service-name (str (name service-name) "-" (name endpoint-name))]

(if (and service endpoint)
(-> acc
(assoc-in [:http :routers (name route-name)]
{:rule (route->traefik-rule route)
:service traefik-service-name})
(assoc-in [:http :services traefik-service-name :loadbalancer :servers]
[{:url (:url endpoint)}]))
acc)))
{})))

(defn write-proxy-config! [{:keys [module-name module]}]
(let [routes (build-routes module)
Expand Down

0 comments on commit b4c174d

Please sign in to comment.