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 ::service-name string?)
(s/def ::service-port pos-int?)
(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]))
; generate a list of host-rules from a list of fqdns
(defn make-host-rules-from-fqdns
[rule fqdns]
;function that creates a rule from host names
(mapv #(assoc-in rule [:host] %) fqdns))
(defn-spec generate-rule pred/map-or-seq?
[service-name ::service-name
service-port ::service-port
fqdn pred/fqdn-string?]
(->
(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
[config]
(let [{:keys [fqdn service-name]} config]
(defn-spec generate-http-ingress pred/map-or-seq?
[config ingress?]
(let [{:keys [service-name service-port fqdns]} config]
(->
(yaml/load-as-edn "ingress/http-ingress.yaml")
(cm/replace-all-matching-values-by-new-value "SERVICENAME" service-name)
(cm/replace-all-matching-values-by-new-value "FQDN" fqdn))))
(assoc-in [:metadata :name] (str service-name "-http-ingress"))
(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]
[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
(is (= {:apiVersion "networking.k8s.io/v1",
:kind "Ingress",
@ -16,9 +31,13 @@
:namespace "default",
:annotations
#:traefik.ingress.kubernetes.io{:router.entrypoints "web",
:router.middlewares "default-redirect-https@kubernetescrd"}},
:spec
{:rules
:router.middlewares "default-redirect-https@kubernetescrd"}}}
(dissoc (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"]}) :spec)))
(is (= {:rules
[{:host "test.de",
:http
{:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "myservice", :port {:number 3000}}}}]}}
@ -30,11 +49,12 @@
{:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "myservice", :port {:number 3000}}}}]}}
{:host "www.test-it.de",
:http
{:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "myservice", :port {:number 3000}}}}]}}]}}
(cut/generate-http-ingress {:issuer "prod"
:service "myservice"
:port 3000
: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
;; (is (= {:apiVersion "networking.k8s.io/v1",