From b4c174de6e9d7c3b1e0595bd3f611e03ef961d75 Mon Sep 17 00:00:00 2001 From: Julien Vincent Date: Sun, 29 Oct 2023 21:52:30 +0000 Subject: [PATCH] Only generate traefik services that are in use --- src/k16/kl/api/proxy.clj | 54 ++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/src/k16/kl/api/proxy.clj b/src/k16/kl/api/proxy.clj index 560cc7c..faad816 100644 --- a/src/k16/kl/api/proxy.clj +++ b/src/k16/kl/api/proxy.clj @@ -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"))) @@ -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)