From bb8ac8a78f6e16b682c042d2a66b3553557a5f48 Mon Sep 17 00:00:00 2001 From: erik Date: Wed, 19 Oct 2022 09:25:37 +0200 Subject: [PATCH 01/11] Add ingress definitions to c4k-common Also update gitignore. --- .gitignore | 3 + src/main/cljc/dda/c4k_common/ingress.cljc | 79 +++++++++++++ src/main/resources/ingress/certificate.yaml | 16 +++ src/main/resources/ingress/host-rule.yaml | 10 ++ src/main/resources/ingress/http-ingress.yaml | 20 ++++ src/main/resources/ingress/https-ingress.yaml | 24 ++++ .../cljc/dda/c4k_common/ingress_test.cljc | 110 ++++++++++++++++++ 7 files changed, 262 insertions(+) create mode 100644 src/main/cljc/dda/c4k_common/ingress.cljc create mode 100644 src/main/resources/ingress/certificate.yaml create mode 100644 src/main/resources/ingress/host-rule.yaml create mode 100644 src/main/resources/ingress/http-ingress.yaml create mode 100644 src/main/resources/ingress/https-ingress.yaml create mode 100644 src/test/cljc/dda/c4k_common/ingress_test.cljc diff --git a/.gitignore b/.gitignore index ca00915..7616f34 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,6 @@ public/js/ valid-auth.edn valid-config.edn my-auth.edn + +.clj-kondo/ +.lsp/ diff --git a/src/main/cljc/dda/c4k_common/ingress.cljc b/src/main/cljc/dda/c4k_common/ingress.cljc new file mode 100644 index 0000000..94824a9 --- /dev/null +++ b/src/main/cljc/dda/c4k_common/ingress.cljc @@ -0,0 +1,79 @@ +(ns dda.c4k-common.ingress + (:require + [clojure.spec.alpha :as s] + #?(:cljs [shadow.resource :as rc]) + #?(:clj [orchestra.core :refer [defn-spec]] + :cljs [orchestra.core :refer-macros [defn-spec]]) + #?(:clj [clojure.edn :as edn] + :cljs [cljs.reader :as edn]) + [dda.c4k-common.yaml :as yaml] + [dda.c4k-common.common :as cm] + [dda.c4k-common.predicate :as pred])) + +(s/def ::issuer pred/letsencrypt-issuer?) +(s/def ::service-name string?) +(s/def ::ingress-name string?) +(s/def ::cert-name string?) +(s/def ::service-port pos-int?) +(s/def ::fqdns (s/coll-of pred/fqdn-string?)) + +(def ingress? (s/keys :req-un [::fqdns ::ingress-name ::service-name ::service-port] + :opt-un [::issuer ::cert-name])) + +(def certificate? (s/keys :req-un [::fqdns ::cert-name] + :opt-un [::issuer])) + +#?(:cljs + (defmethod yaml/load-resource :ingress [resource-name] + (case resource-name + "ingress/host-rule.yaml" (rc/inline "ingress/host-rule.yaml") + "ingress/certificate.yaml" (rc/inline "ingress/certificate.yaml") + "ingress/http-ingress.yaml" (rc/inline "ingress/http-ingress.yaml") + "ingress/https-ingress.yaml" (rc/inline "ingress/https-ingress.yaml") + (throw (js/Error. "Undefined Resource!"))))) + +#?(:cljs + (defmethod yaml/load-as-edn :ingress [resource-name] + (yaml/from-string (yaml/load-resource resource-name)))) + +(defn-spec generate-host-rule pred/map-or-seq? + [service-name ::service-name + service-port ::service-port + fqdn pred/fqdn-string?] + (-> + (yaml/load-as-edn "ingress/host-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-spec generate-http-ingress pred/map-or-seq? + [config ingress?] + (let [{:keys [ingress-name service-name service-port fqdns]} config] + (-> + (yaml/load-as-edn "ingress/http-ingress.yaml") + (assoc-in [:metadata :name] ingress-name) + (assoc-in [:spec :rules] (mapv (partial generate-host-rule service-name service-port) fqdns))))) + +(defn-spec generate-https-ingress pred/map-or-seq? + [config ingress?] + (let [{:keys [ingress-name cert-name service-name service-port fqdns]} config] + (-> + (yaml/load-as-edn "ingress/https-ingress.yaml") + (assoc-in [:metadata :name] ingress-name) + (assoc-in [:spec :tls 0 :secretName] cert-name) + (assoc-in [:spec :tls 0 :hosts] fqdns) + (assoc-in [:spec :rules] (mapv (partial generate-host-rule service-name service-port) fqdns))))) + +(defn-spec generate-certificate pred/map-or-seq? + [config certificate?] + (let [{:keys [cert-name issuer fqdns] + :or {issuer "staging"}} config + letsencrypt-issuer (name issuer)] + (-> + (yaml/load-as-edn "ingress/certificate.yaml") + (assoc-in [:metadata :name] cert-name) + (assoc-in [:spec :secretName] cert-name) + (assoc-in [:spec :commonName] (first fqdns)) + (assoc-in [:spec :dnsNames] fqdns) + (assoc-in [:spec :issuerRef :name] letsencrypt-issuer)))) + \ No newline at end of file diff --git a/src/main/resources/ingress/certificate.yaml b/src/main/resources/ingress/certificate.yaml new file mode 100644 index 0000000..72bf6cd --- /dev/null +++ b/src/main/resources/ingress/certificate.yaml @@ -0,0 +1,16 @@ +apiVersion: cert-manager.io/v1 +kind: Certificate +metadata: + name: c4k-common-cert + namespace: default +spec: + secretName: c4k-common-cert + commonName: FQDN + duration: 2160h # 90d + renewBefore: 360h # 15d + dnsNames: + - FQDN + issuerRef: + name: staging + kind: ClusterIssuer + \ No newline at end of file diff --git a/src/main/resources/ingress/host-rule.yaml b/src/main/resources/ingress/host-rule.yaml new file mode 100644 index 0000000..73d0e9f --- /dev/null +++ b/src/main/resources/ingress/host-rule.yaml @@ -0,0 +1,10 @@ +host: FQDN +http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: SERVICE_NAME + port: + number: SERVICE_PORT diff --git a/src/main/resources/ingress/http-ingress.yaml b/src/main/resources/ingress/http-ingress.yaml new file mode 100644 index 0000000..3ca4151 --- /dev/null +++ b/src/main/resources/ingress/http-ingress.yaml @@ -0,0 +1,20 @@ +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: SERVIC_ENAME + port: + number: 80 diff --git a/src/main/resources/ingress/https-ingress.yaml b/src/main/resources/ingress/https-ingress.yaml new file mode 100644 index 0000000..8798b43 --- /dev/null +++ b/src/main/resources/ingress/https-ingress.yaml @@ -0,0 +1,24 @@ +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: SERVICE_NAME + port: + number: 80 diff --git a/src/test/cljc/dda/c4k_common/ingress_test.cljc b/src/test/cljc/dda/c4k_common/ingress_test.cljc new file mode 100644 index 0000000..56ff0d2 --- /dev/null +++ b/src/test/cljc/dda/c4k_common/ingress_test.cljc @@ -0,0 +1,110 @@ +(ns dda.c4k-common.ingress-test + (:require + #?(:clj [clojure.test :refer [deftest is are testing run-tests]] + :cljs [cljs.test :refer-macros [deftest is are testing run-tests]]) + [clojure.spec.test.alpha :as st] + [dda.c4k-common.ingress :as cut])) + +(st/instrument `cut/generate-host-rule) +(st/instrument `cut/generate-http-ingress) +(st/instrument `cut/generate-https-ingress) +(st/instrument `cut/generate-certificate) + + +(deftest should-generate-rule + (is (= {:host "test.com", + :http + {:paths + [{:pathType "Prefix", + :path "/", + :backend + {:service {:name "myservice", :port {:number 3000}}}}]}} + (cut/generate-host-rule "myservice" 3000 "test.com")))) + + +(deftest should-generate-http-ingress + (is (= {:apiVersion "networking.k8s.io/v1", + :kind "Ingress", + :metadata + {:name "test-io-http-ingress", + :namespace "default", + :annotations + #:traefik.ingress.kubernetes.io{:router.entrypoints "web", + :router.middlewares "default-redirect-https@kubernetescrd"}}} + (dissoc (cut/generate-http-ingress + {:issuer "prod" + :service-name "myservice" + :service-port 3000 + :ingress-name "test-io-http-ingress" + :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}}}}]}} + {:host "www.test.de", + :http + {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "myservice", :port {:number 3000}}}}]}} + {:host "test-it.de", + :http + {: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}}}}]}}]} + (:spec (cut/generate-http-ingress + {:issuer "prod" + :service-name "myservice" + :service-port 3000 + :ingress-name "test-io-http-ingress" + :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"}}} + (dissoc (cut/generate-https-ingress + {:issuer "prod" + :service-name "test-io-service" + :service-port 80 + :ingress-name "test-io-https-ingress" + :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}) :spec))) + (is (= {: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}}}}]}}]} + (:spec (cut/generate-https-ingress {:issuer "prod" + :service-name "test-io-service" + :service-port 80 + :ingress-name "test-io-https-ingress" + :cert-name "test-io-cert" + :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}))))) + +(deftest should-generate-certificate + (is (= {:apiVersion "cert-manager.io/v1", + :kind "Certificate", + :metadata {:name "test-io-cert", :namespace "default"}, + :spec + {:secretName "test-io-cert", + :commonName "test.de", + :duration "2160h", + :renewBefore "360h", + :dnsNames ["test.de" "test.org" "www.test.de" "www.test.org"], + :issuerRef {:name "prod", :kind "ClusterIssuer"}}} + (cut/generate-certificate {:fqdns ["test.de" "test.org" "www.test.de" "www.test.org"] + :cert-name "test-io-cert" + :issuer "prod"})))) \ No newline at end of file From fbc7517840c1d617368c8441cc4aff6c389ad5ec Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 1 Nov 2022 13:56:28 +0100 Subject: [PATCH 02/11] [Skip-Ci] [WIP] Refactor ingress ns * now ingress-cert --- .../cljc/dda/c4k_common/{ingress.cljc => ingress_cert.cljc} | 2 +- src/test/cljc/dda/c4k_common/ingress_test.cljc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/main/cljc/dda/c4k_common/{ingress.cljc => ingress_cert.cljc} (98%) diff --git a/src/main/cljc/dda/c4k_common/ingress.cljc b/src/main/cljc/dda/c4k_common/ingress_cert.cljc similarity index 98% rename from src/main/cljc/dda/c4k_common/ingress.cljc rename to src/main/cljc/dda/c4k_common/ingress_cert.cljc index 94824a9..98c43dc 100644 --- a/src/main/cljc/dda/c4k_common/ingress.cljc +++ b/src/main/cljc/dda/c4k_common/ingress_cert.cljc @@ -1,4 +1,4 @@ -(ns dda.c4k-common.ingress +(ns dda.c4k-common.ingress-cert (:require [clojure.spec.alpha :as s] #?(:cljs [shadow.resource :as rc]) diff --git a/src/test/cljc/dda/c4k_common/ingress_test.cljc b/src/test/cljc/dda/c4k_common/ingress_test.cljc index 56ff0d2..dc4dc2a 100644 --- a/src/test/cljc/dda/c4k_common/ingress_test.cljc +++ b/src/test/cljc/dda/c4k_common/ingress_test.cljc @@ -3,7 +3,7 @@ #?(:clj [clojure.test :refer [deftest is are testing run-tests]] :cljs [cljs.test :refer-macros [deftest is are testing run-tests]]) [clojure.spec.test.alpha :as st] - [dda.c4k-common.ingress :as cut])) + [dda.c4k-common.ingress-cert :as cut])) (st/instrument `cut/generate-host-rule) (st/instrument `cut/generate-http-ingress) From fe2d74a947aa6fe4a375ca7ab75f4ecc4ad7d348 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 1 Nov 2022 13:56:28 +0100 Subject: [PATCH 03/11] [Skip-Ci] [WIP] Refactor ingress ns * now ingress-cert * typo fix --- .../cljc/dda/c4k_common/{ingress.cljc => ingress_cert.cljc} | 2 +- src/main/resources/ingress/http-ingress.yaml | 2 +- src/test/cljc/dda/c4k_common/ingress_test.cljc | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename src/main/cljc/dda/c4k_common/{ingress.cljc => ingress_cert.cljc} (98%) diff --git a/src/main/cljc/dda/c4k_common/ingress.cljc b/src/main/cljc/dda/c4k_common/ingress_cert.cljc similarity index 98% rename from src/main/cljc/dda/c4k_common/ingress.cljc rename to src/main/cljc/dda/c4k_common/ingress_cert.cljc index 94824a9..98c43dc 100644 --- a/src/main/cljc/dda/c4k_common/ingress.cljc +++ b/src/main/cljc/dda/c4k_common/ingress_cert.cljc @@ -1,4 +1,4 @@ -(ns dda.c4k-common.ingress +(ns dda.c4k-common.ingress-cert (:require [clojure.spec.alpha :as s] #?(:cljs [shadow.resource :as rc]) diff --git a/src/main/resources/ingress/http-ingress.yaml b/src/main/resources/ingress/http-ingress.yaml index 3ca4151..35f697f 100644 --- a/src/main/resources/ingress/http-ingress.yaml +++ b/src/main/resources/ingress/http-ingress.yaml @@ -15,6 +15,6 @@ spec: path: "/" backend: service: - name: SERVIC_ENAME + name: SERVICE_NAME port: number: 80 diff --git a/src/test/cljc/dda/c4k_common/ingress_test.cljc b/src/test/cljc/dda/c4k_common/ingress_test.cljc index 56ff0d2..dc4dc2a 100644 --- a/src/test/cljc/dda/c4k_common/ingress_test.cljc +++ b/src/test/cljc/dda/c4k_common/ingress_test.cljc @@ -3,7 +3,7 @@ #?(:clj [clojure.test :refer [deftest is are testing run-tests]] :cljs [cljs.test :refer-macros [deftest is are testing run-tests]]) [clojure.spec.test.alpha :as st] - [dda.c4k-common.ingress :as cut])) + [dda.c4k-common.ingress-cert :as cut])) (st/instrument `cut/generate-host-rule) (st/instrument `cut/generate-http-ingress) From 68d83239216af77e554d90fe09cfe8fdfcf8eedf Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 1 Nov 2022 15:37:29 +0100 Subject: [PATCH 04/11] Refactor labels.app.kubernetes.part-of --- .../cljc/dda/c4k_common/ingress_cert.cljc | 32 +++++++++++-------- src/main/resources/ingress/certificate.yaml | 2 ++ src/main/resources/ingress/http-ingress.yaml | 4 ++- src/main/resources/ingress/https-ingress.yaml | 2 ++ ...gress_test.cljc => ingress_cert_test.cljc} | 19 ++++++++--- 5 files changed, 39 insertions(+), 20 deletions(-) rename src/test/cljc/dda/c4k_common/{ingress_test.cljc => ingress_cert_test.cljc} (85%) diff --git a/src/main/cljc/dda/c4k_common/ingress_cert.cljc b/src/main/cljc/dda/c4k_common/ingress_cert.cljc index 98c43dc..cef31be 100644 --- a/src/main/cljc/dda/c4k_common/ingress_cert.cljc +++ b/src/main/cljc/dda/c4k_common/ingress_cert.cljc @@ -12,15 +12,16 @@ (s/def ::issuer pred/letsencrypt-issuer?) (s/def ::service-name string?) +(s/def ::app-name string?) (s/def ::ingress-name string?) (s/def ::cert-name string?) (s/def ::service-port pos-int?) (s/def ::fqdns (s/coll-of pred/fqdn-string?)) -(def ingress? (s/keys :req-un [::fqdns ::ingress-name ::service-name ::service-port] +(def ingress? (s/keys :req-un [::fqdns ::app-name ::ingress-name ::service-name ::service-port] :opt-un [::issuer ::cert-name])) -(def certificate? (s/keys :req-un [::fqdns ::cert-name] +(def certificate? (s/keys :req-un [::fqdns ::app-name ::cert-name] :opt-un [::issuer])) #?(:cljs @@ -32,48 +33,51 @@ "ingress/https-ingress.yaml" (rc/inline "ingress/https-ingress.yaml") (throw (js/Error. "Undefined Resource!"))))) +; TODO: Review jem 2022/10/26: generalize! #?(:cljs (defmethod yaml/load-as-edn :ingress [resource-name] (yaml/from-string (yaml/load-resource resource-name)))) (defn-spec generate-host-rule pred/map-or-seq? [service-name ::service-name - service-port ::service-port + service-port ::service-port fqdn pred/fqdn-string?] - (-> - (yaml/load-as-edn "ingress/host-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))) + (-> + (yaml/load-as-edn "ingress/host-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-spec generate-http-ingress pred/map-or-seq? [config ingress?] - (let [{:keys [ingress-name service-name service-port fqdns]} config] + (let [{:keys [ingress-name service-name service-port fqdns app-name]} config] (-> (yaml/load-as-edn "ingress/http-ingress.yaml") - (assoc-in [:metadata :name] ingress-name) + (assoc-in [:metadata :name] ingress-name) + (assoc-in [:metadata :labels :app.kubernetes.part-of] app-name) (assoc-in [:spec :rules] (mapv (partial generate-host-rule service-name service-port) fqdns))))) (defn-spec generate-https-ingress pred/map-or-seq? [config ingress?] - (let [{:keys [ingress-name cert-name service-name service-port fqdns]} config] + (let [{:keys [ingress-name cert-name service-name service-port fqdns app-name]} config] (-> (yaml/load-as-edn "ingress/https-ingress.yaml") (assoc-in [:metadata :name] ingress-name) + (assoc-in [:metadata :labels :app.kubernetes.part-of] app-name) (assoc-in [:spec :tls 0 :secretName] cert-name) (assoc-in [:spec :tls 0 :hosts] fqdns) (assoc-in [:spec :rules] (mapv (partial generate-host-rule service-name service-port) fqdns))))) (defn-spec generate-certificate pred/map-or-seq? [config certificate?] - (let [{:keys [cert-name issuer fqdns] + (let [{:keys [cert-name issuer fqdns app-name] :or {issuer "staging"}} config letsencrypt-issuer (name issuer)] (-> - (yaml/load-as-edn "ingress/certificate.yaml") + (yaml/load-as-edn "ingress/certificate.yaml") (assoc-in [:metadata :name] cert-name) + (assoc-in [:metadata :labels :app.kubernetes.part-of] app-name) (assoc-in [:spec :secretName] cert-name) (assoc-in [:spec :commonName] (first fqdns)) (assoc-in [:spec :dnsNames] fqdns) (assoc-in [:spec :issuerRef :name] letsencrypt-issuer)))) - \ No newline at end of file diff --git a/src/main/resources/ingress/certificate.yaml b/src/main/resources/ingress/certificate.yaml index 72bf6cd..5020461 100644 --- a/src/main/resources/ingress/certificate.yaml +++ b/src/main/resources/ingress/certificate.yaml @@ -2,6 +2,8 @@ apiVersion: cert-manager.io/v1 kind: Certificate metadata: name: c4k-common-cert + labels: + app.kubernetes.part-of: c4k-common-app namespace: default spec: secretName: c4k-common-cert diff --git a/src/main/resources/ingress/http-ingress.yaml b/src/main/resources/ingress/http-ingress.yaml index 35f697f..4edc6cf 100644 --- a/src/main/resources/ingress/http-ingress.yaml +++ b/src/main/resources/ingress/http-ingress.yaml @@ -3,6 +3,8 @@ kind: Ingress metadata: name: c4k-common-http-ingress namespace: default + labels: + app.kubernetes.part-of: c4k-common-app annotations: traefik.ingress.kubernetes.io/router.entrypoints: web traefik.ingress.kubernetes.io/router.middlewares: default-redirect-https@kubernetescrd @@ -13,7 +15,7 @@ spec: paths: - pathType: Prefix path: "/" - backend: + backend: # TODO: Review jem 2022/10/26: wo backend as we should only do a redirect here ... ? service: name: SERVICE_NAME port: diff --git a/src/main/resources/ingress/https-ingress.yaml b/src/main/resources/ingress/https-ingress.yaml index 8798b43..e253be0 100644 --- a/src/main/resources/ingress/https-ingress.yaml +++ b/src/main/resources/ingress/https-ingress.yaml @@ -3,6 +3,8 @@ kind: Ingress metadata: name: c4k-common-https-ingress namespace: default + labels: + app.kubernetes.part-of: c4k-common-app annotations: traefik.ingress.kubernetes.io/router.entrypoints: websecure traefik.ingress.kubernetes.io/router.tls: "true" diff --git a/src/test/cljc/dda/c4k_common/ingress_test.cljc b/src/test/cljc/dda/c4k_common/ingress_cert_test.cljc similarity index 85% rename from src/test/cljc/dda/c4k_common/ingress_test.cljc rename to src/test/cljc/dda/c4k_common/ingress_cert_test.cljc index dc4dc2a..5da7964 100644 --- a/src/test/cljc/dda/c4k_common/ingress_test.cljc +++ b/src/test/cljc/dda/c4k_common/ingress_cert_test.cljc @@ -1,4 +1,4 @@ -(ns dda.c4k-common.ingress-test +(ns dda.c4k-common.ingress-cert-test (:require #?(:clj [clojure.test :refer [deftest is are testing run-tests]] :cljs [cljs.test :refer-macros [deftest is are testing run-tests]]) @@ -28,11 +28,13 @@ :metadata {:name "test-io-http-ingress", :namespace "default", + :labels {:app.kubernetes.part-of "c4k-common-app"}, :annotations #:traefik.ingress.kubernetes.io{:router.entrypoints "web", :router.middlewares "default-redirect-https@kubernetescrd"}}} (dissoc (cut/generate-http-ingress {:issuer "prod" + :app-name "c4k-common-app" :service-name "myservice" :service-port 3000 :ingress-name "test-io-http-ingress" @@ -53,6 +55,7 @@ (:spec (cut/generate-http-ingress {:issuer "prod" :service-name "myservice" + :app-name "c4k-common-app" :service-port 3000 :ingress-name "test-io-http-ingress" :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}))))) @@ -63,10 +66,12 @@ :metadata {:name "test-io-https-ingress", :namespace "default", + :labels {:app.kubernetes.part-of "c4k-common-app"}, :annotations #:traefik.ingress.kubernetes.io{:router.entrypoints "websecure", :router.tls "true"}}} (dissoc (cut/generate-https-ingress {:issuer "prod" :service-name "test-io-service" + :app-name "c4k-common-app" :service-port 80 :ingress-name "test-io-https-ingress" :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}) :spec))) @@ -88,6 +93,7 @@ :http {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "test-io-service", :port {:number 80}}}}]}}]} (:spec (cut/generate-https-ingress {:issuer "prod" + :app-name "c4k-common-app" :service-name "test-io-service" :service-port 80 :ingress-name "test-io-https-ingress" @@ -97,7 +103,9 @@ (deftest should-generate-certificate (is (= {:apiVersion "cert-manager.io/v1", :kind "Certificate", - :metadata {:name "test-io-cert", :namespace "default"}, + :metadata {:name "test-io-cert", + :namespace "default", + :labels {:app.kubernetes.part-of "c4k-common-app"}}, :spec {:secretName "test-io-cert", :commonName "test.de", @@ -105,6 +113,7 @@ :renewBefore "360h", :dnsNames ["test.de" "test.org" "www.test.de" "www.test.org"], :issuerRef {:name "prod", :kind "ClusterIssuer"}}} - (cut/generate-certificate {:fqdns ["test.de" "test.org" "www.test.de" "www.test.org"] - :cert-name "test-io-cert" - :issuer "prod"})))) \ No newline at end of file + (cut/generate-certificate {:fqdns ["test.de" "test.org" "www.test.de" "www.test.org"] + :app-name "c4k-common-app" + :cert-name "test-io-cert" + :issuer "prod"})))) \ No newline at end of file From ce73df117c2a6edebd3a37da56cef80910e21ade Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 4 Nov 2022 12:06:44 +0100 Subject: [PATCH 05/11] Remove Todo from http ing[F --- src/main/resources/ingress/http-ingress.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/ingress/http-ingress.yaml b/src/main/resources/ingress/http-ingress.yaml index 4edc6cf..b9d6153 100644 --- a/src/main/resources/ingress/http-ingress.yaml +++ b/src/main/resources/ingress/http-ingress.yaml @@ -15,7 +15,7 @@ spec: paths: - pathType: Prefix path: "/" - backend: # TODO: Review jem 2022/10/26: wo backend as we should only do a redirect here ... ? + backend: service: name: SERVICE_NAME port: From d1f4fb942d2c76ae45c3b3840b0b8c642a66b3c5 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 8 Nov 2022 12:57:54 +0100 Subject: [PATCH 06/11] [WIP] Refactor yaml.cljs Move redundand functionality from modules to common. --- src/main/cljs/dda/c4k_common/yaml.cljs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/cljs/dda/c4k_common/yaml.cljs b/src/main/cljs/dda/c4k_common/yaml.cljs index c8ec13b..11a39e8 100644 --- a/src/main/cljs/dda/c4k_common/yaml.cljs +++ b/src/main/cljs/dda/c4k_common/yaml.cljs @@ -3,6 +3,7 @@ ["js-yaml" :as yaml] [clojure.string :as st] [orchestra.core :refer-macros [defn-spec]] + [shadow.resource :as rc] [dda.c4k-common.predicate :as cp])) (defn-spec from-string cp/map-or-seq? @@ -10,14 +11,24 @@ (js->clj (yaml/load input) :keywordize-keys true)) -(defn-spec to-string string? +(defn-spec to-string string? [edn cp/map-or-seq?] (yaml/dump (clj->js edn))) (defn-spec dispatch-by-resource-name keyword? [resource string?] - (keyword (first (st/split resource #"/")))) + :cljs) (defmulti load-resource dispatch-by-resource-name) -(defmulti load-as-edn dispatch-by-resource-name) \ No newline at end of file +(defmethod load-resource :cljs [allowed-resources resource-name] + (if (some #(= % resource-name) allowed-resources) + (rc/inline resource-name) + (throw (js/Error. "Undefined Resource!")))) + +(defmulti load-as-edn dispatch-by-resource-name) + +(defmethod load-as-edn :cljs [resource-name] + (yaml/from-string (yaml/load-resource resource-name))) + +(defmulti allowed-resources dispatch-by-resource-name) \ No newline at end of file From 65dd1d1ca2b7fe7bfb96b90ca72102efcb3ad9b1 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 8 Nov 2022 13:06:26 +0100 Subject: [PATCH 07/11] [WIP] Check for allowed resources --- src/main/cljs/dda/c4k_common/yaml.cljs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/cljs/dda/c4k_common/yaml.cljs b/src/main/cljs/dda/c4k_common/yaml.cljs index 11a39e8..057ec62 100644 --- a/src/main/cljs/dda/c4k_common/yaml.cljs +++ b/src/main/cljs/dda/c4k_common/yaml.cljs @@ -28,7 +28,10 @@ (defmulti load-as-edn dispatch-by-resource-name) -(defmethod load-as-edn :cljs [resource-name] - (yaml/from-string (yaml/load-resource resource-name))) +(defmethod load-as-edn :cljs [allowed-resource resource-name] + (from-string (load-resource (allowed-resource) resource-name))) -(defmulti allowed-resources dispatch-by-resource-name) \ No newline at end of file +(defmulti allowed-resources dispatch-by-resource-name) + +(defmethod allowed-resources :cljs [] + []) \ No newline at end of file From 1cadd0e993dcc2ac2f992574ca5747d25552d513 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 8 Nov 2022 13:37:08 +0100 Subject: [PATCH 08/11] [WIP] Update postgres and tests --- src/main/cljc/dda/c4k_common/postgres.cljc | 22 ++++++++++----------- src/main/cljs/dda/c4k_common/yaml.cljs | 18 ++++++++++------- src/test/cljs/dda/c4k_common/yaml_test.cljs | 8 +++----- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/main/cljc/dda/c4k_common/postgres.cljc b/src/main/cljc/dda/c4k_common/postgres.cljc index 314e745..97d0956 100644 --- a/src/main/cljc/dda/c4k_common/postgres.cljc +++ b/src/main/cljc/dda/c4k_common/postgres.cljc @@ -33,18 +33,16 @@ (def postgres-function (s/keys :opt-un [::deserializer ::optional])) #?(:cljs - (defmethod yaml/load-resource :postgres [resource-name] - (case resource-name - "postgres/config-2gb.yaml" (rc/inline "postgres/config-2gb.yaml") - "postgres/config-4gb.yaml" (rc/inline "postgres/config-4gb.yaml") - "postgres/config-8gb.yaml" (rc/inline "postgres/config-8gb.yaml") - "postgres/config-16gb.yaml" (rc/inline "postgres/config-16gb.yaml") - "postgres/deployment.yaml" (rc/inline "postgres/deployment.yaml") - "postgres/persistent-volume.yaml" (rc/inline "postgres/persistent-volume.yaml") - "postgres/pvc.yaml" (rc/inline "postgres/pvc.yaml") - "postgres/secret.yaml" (rc/inline "postgres/secret.yaml") - "postgres/service.yaml" (rc/inline "postgres/service.yaml") - (throw (js/Error. "Undefined Resource!"))))) + (defmethod yaml/allowed-resources :postgres [] + ["postgres/config-2gb.yaml" + "postgres/config-4gb.yaml" + "postgres/config-8gb.yaml" + "postgres/config-16gb.yaml" + "postgres/deployment.yaml" + "postgres/persistent-volume.yaml" + "postgres/pvc.yaml" + "postgres/secret.yaml" + "postgres/service.yaml"])) (defn-spec generate-config cp/map-or-seq? [& config (s/? pg-config?)] diff --git a/src/main/cljs/dda/c4k_common/yaml.cljs b/src/main/cljs/dda/c4k_common/yaml.cljs index 057ec62..ef31be5 100644 --- a/src/main/cljs/dda/c4k_common/yaml.cljs +++ b/src/main/cljs/dda/c4k_common/yaml.cljs @@ -6,6 +6,9 @@ [shadow.resource :as rc] [dda.c4k-common.predicate :as cp])) +(defn string-or-keyword? [input] + (or (string? input) (keyword? input))) + (defn-spec from-string cp/map-or-seq? [input string?] (js->clj (yaml/load input) @@ -16,22 +19,23 @@ (yaml/dump (clj->js edn))) (defn-spec dispatch-by-resource-name keyword? - [resource string?] - :cljs) + [resource string-or-keyword?] + (keyword (first (st/split resource #"/")))) (defmulti load-resource dispatch-by-resource-name) -(defmethod load-resource :cljs [allowed-resources resource-name] +(defmethod load-resource :default [allowed-resources resource-name] (if (some #(= % resource-name) allowed-resources) (rc/inline resource-name) (throw (js/Error. "Undefined Resource!")))) (defmulti load-as-edn dispatch-by-resource-name) -(defmethod load-as-edn :cljs [allowed-resource resource-name] - (from-string (load-resource (allowed-resource) resource-name))) +(defmethod load-as-edn :default [resource-name] + (let [allowed-resources (allowed-resources)] + (from-string (load-resource allowed-resources resource-name)))) (defmulti allowed-resources dispatch-by-resource-name) -(defmethod allowed-resources :cljs [] - []) \ No newline at end of file +(defmethod allowed-resources :default [] + []) diff --git a/src/test/cljs/dda/c4k_common/yaml_test.cljs b/src/test/cljs/dda/c4k_common/yaml_test.cljs index cca3846..4f43ce5 100644 --- a/src/test/cljs/dda/c4k_common/yaml_test.cljs +++ b/src/test/cljs/dda/c4k_common/yaml_test.cljs @@ -9,10 +9,8 @@ (st/instrument `cut/to-string) (st/instrument `cut/dispatch-by-resource-name) -(defmethod cut/load-resource :test [resource-name] - (case resource-name - "test/ingress_test.yaml" (rc/inline "test/ingress_test.yaml") - (throw (js/Error. "Undefined Resource!")))) +#?(:cljs (defmethod cut/allowed-resources :cljs [] + ["test/ingress_test.yaml"])) (deftest should-dispatch-by-resource-name (is (= :postgres @@ -56,4 +54,4 @@ [{:backend {:serviceName "another_keycloak" :servicePort 8081}}]}}]}} - (cut/from-string (cut/load-resource "test/ingress_test.yaml"))))) + (cut/load-as-edn "test/ingress_test.yaml")))) From 6358701732ec8a1ac7c61a6ab3264fbf9436fd4c Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 8 Nov 2022 13:43:54 +0100 Subject: [PATCH 09/11] shadow-cljs/resource need literal string --- src/main/cljc/dda/c4k_common/postgres.cljc | 22 +++++++++++---------- src/main/cljs/dda/c4k_common/yaml.cljs | 8 +------- src/test/cljs/dda/c4k_common/yaml_test.cljs | 6 ++++-- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/main/cljc/dda/c4k_common/postgres.cljc b/src/main/cljc/dda/c4k_common/postgres.cljc index 97d0956..314e745 100644 --- a/src/main/cljc/dda/c4k_common/postgres.cljc +++ b/src/main/cljc/dda/c4k_common/postgres.cljc @@ -33,16 +33,18 @@ (def postgres-function (s/keys :opt-un [::deserializer ::optional])) #?(:cljs - (defmethod yaml/allowed-resources :postgres [] - ["postgres/config-2gb.yaml" - "postgres/config-4gb.yaml" - "postgres/config-8gb.yaml" - "postgres/config-16gb.yaml" - "postgres/deployment.yaml" - "postgres/persistent-volume.yaml" - "postgres/pvc.yaml" - "postgres/secret.yaml" - "postgres/service.yaml"])) + (defmethod yaml/load-resource :postgres [resource-name] + (case resource-name + "postgres/config-2gb.yaml" (rc/inline "postgres/config-2gb.yaml") + "postgres/config-4gb.yaml" (rc/inline "postgres/config-4gb.yaml") + "postgres/config-8gb.yaml" (rc/inline "postgres/config-8gb.yaml") + "postgres/config-16gb.yaml" (rc/inline "postgres/config-16gb.yaml") + "postgres/deployment.yaml" (rc/inline "postgres/deployment.yaml") + "postgres/persistent-volume.yaml" (rc/inline "postgres/persistent-volume.yaml") + "postgres/pvc.yaml" (rc/inline "postgres/pvc.yaml") + "postgres/secret.yaml" (rc/inline "postgres/secret.yaml") + "postgres/service.yaml" (rc/inline "postgres/service.yaml") + (throw (js/Error. "Undefined Resource!"))))) (defn-spec generate-config cp/map-or-seq? [& config (s/? pg-config?)] diff --git a/src/main/cljs/dda/c4k_common/yaml.cljs b/src/main/cljs/dda/c4k_common/yaml.cljs index ef31be5..f64612f 100644 --- a/src/main/cljs/dda/c4k_common/yaml.cljs +++ b/src/main/cljs/dda/c4k_common/yaml.cljs @@ -24,16 +24,10 @@ (defmulti load-resource dispatch-by-resource-name) -(defmethod load-resource :default [allowed-resources resource-name] - (if (some #(= % resource-name) allowed-resources) - (rc/inline resource-name) - (throw (js/Error. "Undefined Resource!")))) - (defmulti load-as-edn dispatch-by-resource-name) (defmethod load-as-edn :default [resource-name] - (let [allowed-resources (allowed-resources)] - (from-string (load-resource allowed-resources resource-name)))) + (from-string (load-resource resource-name))) (defmulti allowed-resources dispatch-by-resource-name) diff --git a/src/test/cljs/dda/c4k_common/yaml_test.cljs b/src/test/cljs/dda/c4k_common/yaml_test.cljs index 4f43ce5..9b2775b 100644 --- a/src/test/cljs/dda/c4k_common/yaml_test.cljs +++ b/src/test/cljs/dda/c4k_common/yaml_test.cljs @@ -9,8 +9,10 @@ (st/instrument `cut/to-string) (st/instrument `cut/dispatch-by-resource-name) -#?(:cljs (defmethod cut/allowed-resources :cljs [] - ["test/ingress_test.yaml"])) +(defmethod cut/load-resource :test [resource-name] + (case resource-name + "test/ingress_test.yaml" (rc/inline "test/ingress_test.yaml") + (throw (js/Error. "Undefined Resource!")))) (deftest should-dispatch-by-resource-name (is (= :postgres From 2ee2985139cef4407f1a2f135533022060d1dcee Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 8 Nov 2022 13:52:49 +0100 Subject: [PATCH 10/11] Cleanup --- src/main/cljs/dda/c4k_common/yaml.cljs | 5 ----- src/test/cljc/dda/c4k_common/common_test.cljc | 19 ++++++++++--------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/main/cljs/dda/c4k_common/yaml.cljs b/src/main/cljs/dda/c4k_common/yaml.cljs index f64612f..33fd5c8 100644 --- a/src/main/cljs/dda/c4k_common/yaml.cljs +++ b/src/main/cljs/dda/c4k_common/yaml.cljs @@ -28,8 +28,3 @@ (defmethod load-as-edn :default [resource-name] (from-string (load-resource resource-name))) - -(defmulti allowed-resources dispatch-by-resource-name) - -(defmethod allowed-resources :default [] - []) diff --git a/src/test/cljc/dda/c4k_common/common_test.cljc b/src/test/cljc/dda/c4k_common/common_test.cljc index e0f6d36..f662af5 100644 --- a/src/test/cljc/dda/c4k_common/common_test.cljc +++ b/src/test/cljc/dda/c4k_common/common_test.cljc @@ -15,12 +15,13 @@ (is (= ["a1" "a2" "b1"] (cut/concat-vec '("a1" "a2") '("b1"))))) -(deftest should-refuse-illegal-inputs - (is (thrown? Exception - (cut/concat-vec ["a1" "a2"] "b1"))) - (is (thrown? Exception - (cut/concat-vec ["a1" "a2"] nil))) - (is (thrown? Exception - (cut/concat-vec ["a1" "a2"] 2))) - (is (thrown? Exception - (cut/concat-vec {"a1" "a2"} [])))) \ No newline at end of file +#?(:clj + (deftest should-refuse-illegal-inputs + (is (thrown? Exception + (cut/concat-vec ["a1" "a2"] "b1"))) + (is (thrown? Exception + (cut/concat-vec ["a1" "a2"] nil))) + (is (thrown? Exception + (cut/concat-vec ["a1" "a2"] 2))) + (is (thrown? Exception + (cut/concat-vec {"a1" "a2"} []))))) \ No newline at end of file From efca8c0cd901dfe0b4dd1e8f6a28e25f7816fef2 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 8 Nov 2022 14:00:03 +0100 Subject: [PATCH 11/11] Update ingress --- src/main/cljc/dda/c4k_common/ingress_cert.cljc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/cljc/dda/c4k_common/ingress_cert.cljc b/src/main/cljc/dda/c4k_common/ingress_cert.cljc index cef31be..9461831 100644 --- a/src/main/cljc/dda/c4k_common/ingress_cert.cljc +++ b/src/main/cljc/dda/c4k_common/ingress_cert.cljc @@ -33,11 +33,6 @@ "ingress/https-ingress.yaml" (rc/inline "ingress/https-ingress.yaml") (throw (js/Error. "Undefined Resource!"))))) -; TODO: Review jem 2022/10/26: generalize! -#?(:cljs - (defmethod yaml/load-as-edn :ingress [resource-name] - (yaml/from-string (yaml/load-resource resource-name)))) - (defn-spec generate-host-rule pred/map-or-seq? [service-name ::service-name service-port ::service-port