From 7cecbacf6399b29e3d549a44c426ffb0c24e875b Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 14 Oct 2022 14:31:52 +0200 Subject: [PATCH] [Skip-ci] WIP Refactor --- src/main/cljc/dda/c4k_website/ingress.cljc | 20 +++++++ src/main/cljc/dda/c4k_website/website.cljc | 8 ++- src/main/resources/website/http-ingress.yaml | 20 ------- src/main/resources/website/https-ingress.yaml | 24 -------- .../cljc/dda/c4k_website/ingress_test.cljc | 56 +++++++++---------- 5 files changed, 54 insertions(+), 74 deletions(-) delete mode 100644 src/main/resources/website/http-ingress.yaml delete mode 100644 src/main/resources/website/https-ingress.yaml diff --git a/src/main/cljc/dda/c4k_website/ingress.cljc b/src/main/cljc/dda/c4k_website/ingress.cljc index 475502a..ba2ac33 100644 --- a/src/main/cljc/dda/c4k_website/ingress.cljc +++ b/src/main/cljc/dda/c4k_website/ingress.cljc @@ -12,3 +12,23 @@ [dda.c4k-common.predicate :as pred] [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)))) \ No newline at end of file diff --git a/src/main/cljc/dda/c4k_website/website.cljc b/src/main/cljc/dda/c4k_website/website.cljc index 22370d9..84687be 100644 --- a/src/main/cljc/dda/c4k_website/website.cljc +++ b/src/main/cljc/dda/c4k_website/website.cljc @@ -76,6 +76,12 @@ (str/replace % value-to-partly-match value-to-inplace) %) 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 (defmethod yaml/load-resource :website [resource-name] (case resource-name @@ -116,8 +122,6 @@ (let [{:keys [unique-name fqdns]} config spec-rules [:spec :rules]] (-> - (generate-http-ingress (merge {:service "xy" :port 80 :issuer issuer} - config)) (generate-common-http-ingress {: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)) diff --git a/src/main/resources/website/http-ingress.yaml b/src/main/resources/website/http-ingress.yaml deleted file mode 100644 index 3b274a4..0000000 --- a/src/main/resources/website/http-ingress.yaml +++ /dev/null @@ -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 diff --git a/src/main/resources/website/https-ingress.yaml b/src/main/resources/website/https-ingress.yaml deleted file mode 100644 index 4d7fbe2..0000000 --- a/src/main/resources/website/https-ingress.yaml +++ /dev/null @@ -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 diff --git a/src/test/cljc/dda/c4k_website/ingress_test.cljc b/src/test/cljc/dda/c4k_website/ingress_test.cljc index 4c80caf..6d10cba 100644 --- a/src/test/cljc/dda/c4k_website/ingress_test.cljc +++ b/src/test/cljc/dda/c4k_website/ingress_test.cljc @@ -36,31 +36,31 @@ :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", - :kind "Ingress", - :metadata - {:name "test-io-https-ingress", - :namespace "default", - :annotations #:traefik.ingress.kubernetes.io{:router.entrypoints "websecure", :router.tls "true"}}, - :spec - {:tls [{:hosts ["test.de" "www.test.de" "test-it.de" "www.test-it.de"], :secretName "test-io-cert"}], - :rules - [{:host "test.de", - :http - {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}} - {:host "www.test.de", - :http - {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}} - {:host "test-it.de", - :http - {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}} - {:host "www.test-it.de", - :http - {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}}]}} - (cut/generate-website-https-ingress {:unique-name "test.io" - :gitea-host "gitea.evilorg" - :gitea-repo "none" - :branchname "mablain" - :issuer "prod" - :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})))) \ No newline at end of file +;; (deftest should-generate-https-ingress +;; (is (= {:apiVersion "networking.k8s.io/v1", +;; :kind "Ingress", +;; :metadata +;; {:name "test-io-https-ingress", +;; :namespace "default", +;; :annotations #:traefik.ingress.kubernetes.io{:router.entrypoints "websecure", :router.tls "true"}}, +;; :spec +;; {:tls [{:hosts ["test.de" "www.test.de" "test-it.de" "www.test-it.de"], :secretName "test-io-cert"}], +;; :rules +;; [{:host "test.de", +;; :http +;; {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}} +;; {:host "www.test.de", +;; :http +;; {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}} +;; {:host "test-it.de", +;; :http +;; {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}} +;; {:host "www.test-it.de", +;; :http +;; {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}}]}} +;; (cut/generate-https-ingress {:unique-name "test.io" +;; :gitea-host "gitea.evilorg" +;; :gitea-repo "none" +;; :branchname "mablain" +;; :issuer "prod" +;; :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})))) \ No newline at end of file