This commit is contained in:
jerger 2022-10-14 15:13:15 +02:00
parent 60156805c3
commit 8989679d57
3 changed files with 66 additions and 30 deletions

View file

@ -14,21 +14,27 @@
(s/def ::issuer pred/letsencrypt-issuer?) (s/def ::issuer pred/letsencrypt-issuer?)
(s/def ::service-name string?)
(s/def ::service-port pos-int?)
(s/def ::fqdns (s/coll-of pred/fqdn-string?)) (s/def ::fqdns (s/coll-of pred/fqdn-string?))
(def ingress? (s/keys :req-un [::fqdns ::service-name ::port] (def ingress? (s/keys :req-un [::fqdns ::service-name ::service-port]
:opt-un [::issuer])) :opt-un [::issuer]))
; generate a list of host-rules from a list of fqdns (defn-spec generate-rule pred/map-or-seq?
(defn make-host-rules-from-fqdns [service-name ::service-name
[rule fqdns] service-port ::service-port
;function that creates a rule from host names fqdn pred/fqdn-string?]
(mapv #(assoc-in rule [:host] %) fqdns)) (->
(yaml/load-as-edn "ingress/rule.yaml")
(cm/replace-all-matching-values-by-new-value "FQDN" fqdn)
(cm/replace-all-matching-values-by-new-value "SERVICE_PORT" service-port)
(cm/replace-all-matching-values-by-new-value "SERVICE_NAME" service-name)))
(defn generate-http-ingress (defn-spec generate-http-ingress pred/map-or-seq?
[config] [config ingress?]
(let [{:keys [fqdn service-name]} config] (let [{:keys [service-name service-port fqdns]} config]
(-> (->
(yaml/load-as-edn "ingress/http-ingress.yaml") (yaml/load-as-edn "ingress/http-ingress.yaml")
(cm/replace-all-matching-values-by-new-value "SERVICENAME" service-name) (assoc-in [:metadata :name] (str service-name "-http-ingress"))
(cm/replace-all-matching-values-by-new-value "FQDN" fqdn)))) (assoc-in [:spec :rules] (mapv (partial generate-rule service-name service-port) fqdns)))))

View file

@ -0,0 +1,10 @@
host: FQDN
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: SERVICE_NAME
port:
number: SERVICE_PORT

View file

@ -8,6 +8,21 @@
[dda.c4k-website.ingress :as cut] [dda.c4k-website.ingress :as cut]
[clojure.spec.alpha :as s])) [clojure.spec.alpha :as s]))
(st/instrument `cut/generate-rule)
(st/instrument `cut/generate-http-ingress)
(deftest should-genereate-rule
(is (= {:host "test.com",
:http
{:paths
[{:pathType "Prefix",
:path "/",
:backend
{:service {:name "myservice", :port {:number 3000}}}}]}}
(cut/generate-rule "myservice" 3000 "test.com"))))
(deftest should-generate-http-ingress (deftest should-generate-http-ingress
(is (= {:apiVersion "networking.k8s.io/v1", (is (= {:apiVersion "networking.k8s.io/v1",
:kind "Ingress", :kind "Ingress",
@ -16,25 +31,30 @@
:namespace "default", :namespace "default",
:annotations :annotations
#:traefik.ingress.kubernetes.io{:router.entrypoints "web", #:traefik.ingress.kubernetes.io{:router.entrypoints "web",
:router.middlewares "default-redirect-https@kubernetescrd"}}, :router.middlewares "default-redirect-https@kubernetescrd"}}}
:spec (dissoc (cut/generate-http-ingress
{:rules {:issuer "prod"
[{:host "test.de", :service-name "myservice"
:http :service-port 3000
{:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "myservice", :port {:number 3000}}}}]}} :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}) :spec)))
{:host "www.test.de", (is (= {:rules
:http [{:host "test.de",
{:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "myservice", :port {:number 3000}}}}]}} :http
{:host "test-it.de", {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "myservice", :port {:number 3000}}}}]}}
:http {:host "www.test.de",
{:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "myservice", :port {:number 3000}}}}]}} :http
{:host "www.test-it.de", {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "myservice", :port {:number 3000}}}}]}}
:http {:host "test-it.de",
{:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "myservice", :port {:number 3000}}}}]}}]}} :http
(cut/generate-http-ingress {:issuer "prod" {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "myservice", :port {:number 3000}}}}]}}
:service "myservice" {:host "www.test-it.de",
:port 3000 :http
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})))) {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "myservice", :port {:number 3000}}}}]}}]}
(:spec (cut/generate-http-ingress
{:issuer "prod"
:service-name "myservice"
:service-port 3000
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})))))
;; (deftest should-generate-https-ingress ;; (deftest should-generate-https-ingress
;; (is (= {:apiVersion "networking.k8s.io/v1", ;; (is (= {:apiVersion "networking.k8s.io/v1",