[Skip-ci] WIP Refactor

This commit is contained in:
erik 2022-10-14 14:31:52 +02:00
parent 616d61c2a9
commit 7cecbacf63
5 changed files with 54 additions and 74 deletions

View file

@ -12,3 +12,23 @@
[dda.c4k-common.predicate :as pred] [dda.c4k-common.predicate :as pred]
[clojure.string :as str])) [clojure.string :as str]))
(s/def ::issuer pred/letsencrypt-issuer?)
(s/def ::fqdns (s/coll-of pred/fqdn-string?))
(def ingress? (s/keys :req-un [::fqdns ::service-name ::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 generate-http-ingress
[config]
(let [{:keys [fqdn service-name]} 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))))

View file

@ -76,6 +76,12 @@
(str/replace % value-to-partly-match value-to-inplace) %) (str/replace % value-to-partly-match value-to-inplace) %)
col)) col))
; 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))
#?(:cljs #?(:cljs
(defmethod yaml/load-resource :website [resource-name] (defmethod yaml/load-resource :website [resource-name]
(case resource-name (case resource-name
@ -116,8 +122,6 @@
(let [{:keys [unique-name fqdns]} config (let [{:keys [unique-name fqdns]} config
spec-rules [:spec :rules]] spec-rules [:spec :rules]]
(-> (->
(generate-http-ingress (merge {:service "xy" :port 80 :issuer issuer}
config))
(generate-common-http-ingress (generate-common-http-ingress
{:fqdn (first fqdns) :service-name (generate-service-name unique-name)}) {:fqdn (first fqdns) :service-name (generate-service-name unique-name)})
(cm/replace-all-matching-values-by-new-value "c4k-common-http-ingress" (generate-http-ingress-name unique-name)) (cm/replace-all-matching-values-by-new-value "c4k-common-http-ingress" (generate-http-ingress-name unique-name))

View file

@ -1,20 +0,0 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: c4k-common-http-ingress
namespace: default
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: web
traefik.ingress.kubernetes.io/router.middlewares: default-redirect-https@kubernetescrd
spec:
rules:
- host: FQDN
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: SERVICENAME
port:
number: 80

View file

@ -1,24 +0,0 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: c4k-common-https-ingress
namespace: default
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: websecure
traefik.ingress.kubernetes.io/router.tls: "true"
spec:
tls:
- hosts:
- FQDN
secretName: c4k-common-cert
rules:
- host: FQDN
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: SERVICENAME
port:
number: 80

View file

@ -36,31 +36,31 @@
:port 3000 :port 3000
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})))) :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",
:kind "Ingress", ;; :kind "Ingress",
:metadata ;; :metadata
{:name "test-io-https-ingress", ;; {:name "test-io-https-ingress",
:namespace "default", ;; :namespace "default",
:annotations #:traefik.ingress.kubernetes.io{:router.entrypoints "websecure", :router.tls "true"}}, ;; :annotations #:traefik.ingress.kubernetes.io{:router.entrypoints "websecure", :router.tls "true"}},
:spec ;; :spec
{:tls [{:hosts ["test.de" "www.test.de" "test-it.de" "www.test-it.de"], :secretName "test-io-cert"}], ;; {:tls [{:hosts ["test.de" "www.test.de" "test-it.de" "www.test-it.de"], :secretName "test-io-cert"}],
:rules ;; :rules
[{:host "test.de", ;; [{:host "test.de",
:http ;; :http
{:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}} ;; {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}}
{:host "www.test.de", ;; {:host "www.test.de",
:http ;; :http
{:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}} ;; {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}}
{:host "test-it.de", ;; {:host "test-it.de",
:http ;; :http
{:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}} ;; {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}}
{:host "www.test-it.de", ;; {:host "www.test-it.de",
:http ;; :http
{:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}}]}} ;; {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}}]}}
(cut/generate-website-https-ingress {:unique-name "test.io" ;; (cut/generate-https-ingress {:unique-name "test.io"
:gitea-host "gitea.evilorg" ;; :gitea-host "gitea.evilorg"
:gitea-repo "none" ;; :gitea-repo "none"
:branchname "mablain" ;; :branchname "mablain"
:issuer "prod" ;; :issuer "prod"
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})))) ;; :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}))))