From d396e3abae713e2a2c5962e15957c755b1cf5c7d Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Tue, 27 Feb 2024 13:24:01 +0100 Subject: [PATCH 1/4] generate redirects --- src/main/cljc/dda/c4k_website/website.cljc | 3 +- .../c4k_website/website/website_internal.cljc | 99 +++++++++++-------- .../resources/website/nginx-configmap.yaml | 4 +- .../website/website_internal_test.cljc | 88 +++++++++++++---- 4 files changed, 129 insertions(+), 65 deletions(-) diff --git a/src/main/cljc/dda/c4k_website/website.cljc b/src/main/cljc/dda/c4k_website/website.cljc index 8c9b906..99a033c 100644 --- a/src/main/cljc/dda/c4k_website/website.cljc +++ b/src/main/cljc/dda/c4k_website/website.cljc @@ -40,7 +40,8 @@ :build-cpu-limit "1700m" :build-memory-request "256Mi" :build-memory-limit "512Mi" - :volume-size "3"}) + :volume-size "3" + :redirects []}) (defn-spec generate-nginx-deployment map? [config websiteconfig?] diff --git a/src/main/cljc/dda/c4k_website/website/website_internal.cljc b/src/main/cljc/dda/c4k_website/website/website_internal.cljc index 6ee91f9..fc26cf0 100644 --- a/src/main/cljc/dda/c4k_website/website/website_internal.cljc +++ b/src/main/cljc/dda/c4k_website/website/website_internal.cljc @@ -8,8 +8,7 @@ [dda.c4k-common.yaml :as yaml] [dda.c4k-common.common :as cm] [dda.c4k-common.base64 :as b64] - [dda.c4k-common.predicate :as pred] - [dda.c4k-common.ingress :as ing])) + [dda.c4k-common.predicate :as pred])) (defn fqdn-list? [input] @@ -28,6 +27,9 @@ (s/def ::build-memory-request string?) (s/def ::build-cpu-limit string?) (s/def ::build-memory-limit string?) +(s/def ::redirect (s/tuple string? string?)) +(s/def ::redirects (s/coll-of ::redirect)) + (def websiteconfig? (s/keys :req-un [::unique-name ::fqdns @@ -39,7 +41,8 @@ ::build-cpu-request ::build-cpu-limit ::build-memory-request - ::build-memory-limit])) + ::build-memory-limit + ::redirects])) (def websiteauth? (s/keys :req-un [::unique-name ::username ::authtoken])) @@ -70,8 +73,9 @@ user string?] (str "https://" host "/api/v1/repos/" user "/" repo "/git/" "commits/" "HEAD")) -(defn-spec replace-all-matching-substrings-beginning-with pred/map-or-seq? - [col pred/map-or-seq? +; TODO: Rename replace-all-matching-prefixes +(defn-spec replace-all-matching-substrings-beginning-with map? + [col map? value-to-partly-match string? value-to-inplace string?] (clojure.walk/postwalk #(if (and (= (type value-to-partly-match) (type %)) @@ -80,19 +84,15 @@ col)) -(defn-spec generate-nginx-deployment map? - [config websiteconfig?] - (let [{:keys [unique-name build-cpu-request build-cpu-limit - build-memory-request build-memory-limit]} config - name (replace-dots-by-minus unique-name)] - (-> - (yaml/load-as-edn "website/nginx-deployment.yaml") - (assoc-in [:metadata :namespace] name) - (replace-all-matching-substrings-beginning-with "NAME" name) - (cm/replace-all-matching-values-by-new-value "BUILD_CPU_REQUEST" build-cpu-request) - (cm/replace-all-matching-values-by-new-value "BUILD_CPU_LIMIT" build-cpu-limit) - (cm/replace-all-matching-values-by-new-value "BUILD_MEMORY_REQUEST" build-memory-request) - (cm/replace-all-matching-values-by-new-value "BUILD_MEMORY_LIMIT" build-memory-limit)))) +(defn-spec generate-redirects string? + [config websiteconfig? + indent (s/or :pos pos-int? :zero zero?)] + (let [{:keys [redirects]} config] + (str/join + (str "\n" (str/join (take indent (repeat " ")))) + (map + #(str "rewrite ^" (first %1) "$ " (second %1) " permanent;") + redirects)))) (defn-spec generate-nginx-configmap map? @@ -109,30 +109,6 @@ (-> % :data :website.conf) #"FQDN" (str (str/join " " fqdns) ";"))))))) -(defn-spec generate-nginx-service map? - [config websiteconfig?] - (let [{:keys [unique-name]} config - name (replace-dots-by-minus unique-name)] - (-> - (yaml/load-as-edn "website/nginx-service.yaml") - (assoc-in [:metadata :namespace] name) - (replace-all-matching-substrings-beginning-with "NAME" name)))) - - -(defn-spec generate-website-build-cron map? - [config websiteconfig?] - (let [{:keys [unique-name build-cpu-request build-cpu-limit build-memory-request - build-memory-limit]} config - name (replace-dots-by-minus unique-name)] - (-> - (yaml/load-as-edn "website/build-cron.yaml") - (replace-all-matching-substrings-beginning-with "NAME" name) - (cm/replace-all-matching-values-by-new-value "BUILD_CPU_REQUEST" build-cpu-request) - (cm/replace-all-matching-values-by-new-value "BUILD_CPU_LIMIT" build-cpu-limit) - (cm/replace-all-matching-values-by-new-value "BUILD_MEMORY_REQUEST" build-memory-request) - (cm/replace-all-matching-values-by-new-value "BUILD_MEMORY_LIMIT" build-memory-limit)))) - - (defn-spec generate-website-build-secret pred/map-or-seq? [config websiteconfig? auth websiteauth?] @@ -180,6 +156,45 @@ (replace-all-matching-substrings-beginning-with "NAME" name)))) +(defn-spec generate-nginx-deployment map? + [config websiteconfig?] + (let [{:keys [unique-name build-cpu-request build-cpu-limit + build-memory-request build-memory-limit]} config + name (replace-dots-by-minus unique-name)] + (-> + (yaml/load-as-edn "website/nginx-deployment.yaml") + (assoc-in [:metadata :namespace] name) + (replace-all-matching-substrings-beginning-with "NAME" name) + (cm/replace-all-matching-values-by-new-value "BUILD_CPU_REQUEST" build-cpu-request) + (cm/replace-all-matching-values-by-new-value "BUILD_CPU_LIMIT" build-cpu-limit) + (cm/replace-all-matching-values-by-new-value "BUILD_MEMORY_REQUEST" build-memory-request) + (cm/replace-all-matching-values-by-new-value "BUILD_MEMORY_LIMIT" build-memory-limit)))) + + +(defn-spec generate-website-build-cron map? + [config websiteconfig?] + (let [{:keys [unique-name build-cpu-request build-cpu-limit build-memory-request + build-memory-limit]} config + name (replace-dots-by-minus unique-name)] + (-> + (yaml/load-as-edn "website/build-cron.yaml") + (replace-all-matching-substrings-beginning-with "NAME" name) + (cm/replace-all-matching-values-by-new-value "BUILD_CPU_REQUEST" build-cpu-request) + (cm/replace-all-matching-values-by-new-value "BUILD_CPU_LIMIT" build-cpu-limit) + (cm/replace-all-matching-values-by-new-value "BUILD_MEMORY_REQUEST" build-memory-request) + (cm/replace-all-matching-values-by-new-value "BUILD_MEMORY_LIMIT" build-memory-limit)))) + + +(defn-spec generate-nginx-service map? + [config websiteconfig?] + (let [{:keys [unique-name]} config + name (replace-dots-by-minus unique-name)] + (-> + (yaml/load-as-edn "website/nginx-service.yaml") + (assoc-in [:metadata :namespace] name) + (replace-all-matching-substrings-beginning-with "NAME" name)))) + + #?(:cljs (defmethod yaml/load-resource :website [resource-name] (get (inline-resources "website") resource-name))) diff --git a/src/main/resources/website/nginx-configmap.yaml b/src/main/resources/website/nginx-configmap.yaml index 8b1e9bd..3193813 100644 --- a/src/main/resources/website/nginx-configmap.yaml +++ b/src/main/resources/website/nginx-configmap.yaml @@ -2,7 +2,7 @@ apiVersion: v1 kind: ConfigMap metadata: name: etc-nginx - namespace: default + namespace: NAME labels: app.kubernetes.part-of: NAME-website data: @@ -93,5 +93,7 @@ data: location / { try_files $uri $uri/ /index.html =404; } + # redirects + REDIRECTS } \ No newline at end of file diff --git a/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc b/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc index f883a02..e3cff63 100644 --- a/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc +++ b/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc @@ -5,14 +5,49 @@ [clojure.spec.test.alpha :as st] [dda.c4k-website.website.website-internal :as cut])) +(st/instrument `cut/replace-dots-by-minus) +(st/instrument `cut/generate-gitrepourl) +(st/instrument `cut/generate-gitcommiturl) +(st/instrument `cut/replace-all-matching-substrings-beginning-with) +(st/instrument `cut/generate-redirects) (st/instrument `cut/generate-nginx-configmap) -(st/instrument `cut/generate-nginx-service) +(st/instrument `cut/generate-website-build-secret) (st/instrument `cut/generate-website-content-volume) (st/instrument `cut/generate-hashfile-volume) -(st/instrument `cut/generate-website-ingress) -(st/instrument `cut/generate-website-certificate) (st/instrument `cut/generate-website-build-cron) -(st/instrument `cut/generate-website-build-secret) +(st/instrument `cut/generate-nginx-service) + +(deftest should-generate-redirects + (is (= "rewrite ^/products.html$ /offer.html permanent;\n rewrite ^/one-more$ /redirect permanent;" + (cut/generate-redirects {:issuer "staging" + :build-cpu-request "500m" + :build-cpu-limit "1700m" + :build-memory-request "256Mi" + :build-memory-limit "512Mi" + :volume-size "3" + :unique-name "test.io", + :redirects [["/products.html", "/offer.html"] + ["/one-more", "/redirect"]] + :forgejo-host "gitea.evilorg", + :forgejo-repo "none", + :branchname "mablain", + :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]} + 2))) + (is (= "" + (cut/generate-redirects {:issuer "staging" + :build-cpu-request "500m" + :build-cpu-limit "1700m" + :build-memory-request "256Mi" + :build-memory-limit "512Mi" + :volume-size "3" + :unique-name "test.io", + :redirects [] + :forgejo-host "gitea.evilorg", + :forgejo-repo "none", + :branchname "mablain", + :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]} + 0)))) + (deftest should-generate-resource-requests (is (= {:requests {:cpu "1500m", :memory "512Mi"}, :limits {:cpu "3000m", :memory "1024Mi"}} @@ -21,7 +56,8 @@ :forgejo-repo "repo", :issuer "staging", :branchname "main", - :unique-name "test.io" + :unique-name "test.io", + :redirects [], :build-cpu-request "1500m" :build-cpu-limit "3000m" :build-memory-request "512Mi" @@ -34,7 +70,8 @@ :forgejo-repo "repo", :issuer "staging", :branchname "main", - :unique-name "test.io" + :unique-name "test.io", + :redirects [], :build-cpu-request "1500m" :build-cpu-limit "3000m" :build-memory-request "512Mi" @@ -42,9 +79,8 @@ :volume-size 3}) :metadata :namespace)))) - (deftest should-generate-nginx-configmap-website - (is (= "server {\n listen 80 default_server;\n listen [::]:80 default_server;\n server_name test.de www.test.de test-it.de www.test-it.de;\n add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload'; \n add_header X-Frame-Options \"SAMEORIGIN\";\n add_header X-Content-Type-Options nosniff;\n add_header Referrer-Policy \"strict-origin\";\n # add_header Permissions-Policy \"permissions here\";\n root /var/www/html/website/;\n index index.html;\n location / {\n try_files $uri $uri/ /index.html =404;\n }\n}\n" + (is (= "server {\n listen 80 default_server;\n listen [::]:80 default_server;\n server_name test.de www.test.de test-it.de www.test-it.de;\n add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload'; \n add_header X-Frame-Options \"SAMEORIGIN\";\n add_header X-Content-Type-Options nosniff;\n add_header Referrer-Policy \"strict-origin\";\n # add_header Permissions-Policy \"permissions here\";\n root /var/www/html/website/;\n index index.html;\n location / {\n try_files $uri $uri/ /index.html =404;\n }\n # redirects\n REDIRECTS\n}\n" (:website.conf (:data (cut/generate-nginx-configmap {:issuer "staging" :build-cpu-request "500m" :build-cpu-limit "1700m" @@ -52,6 +88,7 @@ :build-memory-limit "512Mi" :volume-size "3" :unique-name "test.io", + :redirects [], :forgejo-host "gitea.evilorg", :forgejo-repo "none", :branchname "mablain", @@ -64,6 +101,7 @@ :build-memory-limit "512Mi" :volume-size "3" :unique-name "test.io", + :redirects [], :forgejo-host "gitea.evilorg", :forgejo-repo "none", :branchname "mablain", @@ -76,6 +114,7 @@ :build-memory-limit "512Mi" :volume-size "3" :unique-name "test.io", + :redirects [], :forgejo-host "gitea.evilorg", :forgejo-repo "none", :branchname "mablain", @@ -92,6 +131,7 @@ :build-memory-limit "512Mi" :volume-size "3" :unique-name "test.io", + :redirects [], :forgejo-host "gitea.evilorg", :forgejo-repo "none", :branchname "mablain", @@ -107,18 +147,20 @@ :spec {:selector {:app "nginx"}, :ports [{:name "nginx-http", :port 80}]}} (cut/generate-nginx-service {:issuer "staging" - :build-cpu-request "500m" - :build-cpu-limit "1700m" - :build-memory-request "256Mi" - :build-memory-limit "512Mi" - :volume-size "3" - :unique-name "test.io", - :forgejo-host "gitea.evilorg", - :forgejo-repo "none", - :branchname "mablain", - :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}) + :build-cpu-request "500m" + :build-cpu-limit "1700m" + :build-memory-request "256Mi" + :build-memory-limit "512Mi" + :volume-size "3" + :unique-name "test.io", + :redirects [], + :forgejo-host "gitea.evilorg", + :forgejo-repo "none", + :branchname "mablain", + :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}) ))) + (deftest should-generate-website-build-cron (is (= {:apiVersion "batch/v1", :kind "CronJob", @@ -159,8 +201,8 @@ :fqdns ["test.de" "test.org" "www.test.de" "www.test.org"], :forgejo-repo "repo", :branchname "main", - :unique-name "test.io"})))) - + :unique-name "test.io", + :redirects [],})))) (deftest should-generate-website-build-secret (is (= {:apiVersion "v1", @@ -177,6 +219,7 @@ :issuer "staging", :branchname "main", :unique-name "test.io", + :redirects [], :forgejo-host "gitlab.de" :build-cpu-request "500m" :build-cpu-limit "1700m" @@ -205,11 +248,13 @@ :build-memory-limit "512Mi" :volume-size "3" :unique-name "test.io", + :redirects [], :forgejo-host "gitea.evilorg", :forgejo-repo "none", :branchname "mablain", :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})))) + (deftest should-generate-hashfile-volume (is (= {:apiVersion "v1", :kind "PersistentVolumeClaim", @@ -227,7 +272,8 @@ :build-memory-limit "512Mi" :volume-size "3" :unique-name "test.io", + :redirects [], :forgejo-host "gitea.evilorg", :forgejo-repo "none", :branchname "mablain", - :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"]})))) \ No newline at end of file From b4ec6d16b3c2e2cd5729af6da6b09e9248b4a038 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Tue, 27 Feb 2024 13:37:46 +0100 Subject: [PATCH 2/4] add redirects to config --- .../dda/c4k_website/website/website_internal.cljc | 15 ++++++++++----- .../website/website_internal_test.cljc | 7 ++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/cljc/dda/c4k_website/website/website_internal.cljc b/src/main/cljc/dda/c4k_website/website/website_internal.cljc index fc26cf0..df6f0a5 100644 --- a/src/main/cljc/dda/c4k_website/website/website_internal.cljc +++ b/src/main/cljc/dda/c4k_website/website/website_internal.cljc @@ -91,7 +91,7 @@ (str/join (str "\n" (str/join (take indent (repeat " ")))) (map - #(str "rewrite ^" (first %1) "$ " (second %1) " permanent;") + #(str "rewrite ^" (first %1) "\\$ " (second %1) " permanent;") redirects)))) @@ -101,12 +101,17 @@ name (replace-dots-by-minus unique-name)] (-> (yaml/load-as-edn "website/nginx-configmap.yaml") - (assoc-in [:metadata :namespace] name) (replace-all-matching-substrings-beginning-with "NAME" name) - (#(assoc-in % - [:data :website.conf] + (#(assoc-in % [:data :website.conf] (str/replace - (-> % :data :website.conf) #"FQDN" (str (str/join " " fqdns) ";"))))))) + (-> % :data :website.conf) + #"FQDN" + (str (str/join " " fqdns) ";")))) + (#(assoc-in % [:data :website.conf] + (str/replace + (-> % :data :website.conf) + #"REDIRECTS" + (generate-redirects config 2))))))) (defn-spec generate-website-build-secret pred/map-or-seq? diff --git a/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc b/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc index e3cff63..ca69ce9 100644 --- a/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc +++ b/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc @@ -18,7 +18,7 @@ (st/instrument `cut/generate-nginx-service) (deftest should-generate-redirects - (is (= "rewrite ^/products.html$ /offer.html permanent;\n rewrite ^/one-more$ /redirect permanent;" + (is (= "rewrite ^/products.html\\$ /offer.html permanent;\n rewrite ^/one-more\\$ /redirect permanent;" (cut/generate-redirects {:issuer "staging" :build-cpu-request "500m" :build-cpu-limit "1700m" @@ -80,7 +80,7 @@ :metadata :namespace)))) (deftest should-generate-nginx-configmap-website - (is (= "server {\n listen 80 default_server;\n listen [::]:80 default_server;\n server_name test.de www.test.de test-it.de www.test-it.de;\n add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload'; \n add_header X-Frame-Options \"SAMEORIGIN\";\n add_header X-Content-Type-Options nosniff;\n add_header Referrer-Policy \"strict-origin\";\n # add_header Permissions-Policy \"permissions here\";\n root /var/www/html/website/;\n index index.html;\n location / {\n try_files $uri $uri/ /index.html =404;\n }\n # redirects\n REDIRECTS\n}\n" + (is (= "server {\n listen 80 default_server;\n listen [::]:80 default_server;\n server_name test.de www.test.de test-it.de www.test-it.de;\n add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload'; \n add_header X-Frame-Options \"SAMEORIGIN\";\n add_header X-Content-Type-Options nosniff;\n add_header Referrer-Policy \"strict-origin\";\n # add_header Permissions-Policy \"permissions here\";\n root /var/www/html/website/;\n index index.html;\n location / {\n try_files $uri $uri/ /index.html =404;\n }\n # redirects\n rewrite ^/products.html$ /offer.html permanent;\n rewrite ^/one-more$ /redirect permanent;\n}\n" (:website.conf (:data (cut/generate-nginx-configmap {:issuer "staging" :build-cpu-request "500m" :build-cpu-limit "1700m" @@ -88,7 +88,8 @@ :build-memory-limit "512Mi" :volume-size "3" :unique-name "test.io", - :redirects [], + :redirects [["/products.html", "/offer.html"] + ["/one-more", "/redirect"]] :forgejo-host "gitea.evilorg", :forgejo-repo "none", :branchname "mablain", From 3a216d52df30e6f11ad073fd41453faaebfc1331 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Tue, 27 Feb 2024 13:42:49 +0100 Subject: [PATCH 3/4] name refactorings --- src/main/cljc/dda/c4k_website/core.cljc | 8 +++--- src/main/cljc/dda/c4k_website/website.cljc | 16 ++++++------ .../c4k_website/website/website_internal.cljc | 26 +++++++++---------- .../website/website_internal_test.cljc | 26 +++++++++---------- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/main/cljc/dda/c4k_website/core.cljc b/src/main/cljc/dda/c4k_website/core.cljc index e1ddffd..9cdb8d0 100644 --- a/src/main/cljc/dda/c4k_website/core.cljc +++ b/src/main/cljc/dda/c4k_website/core.cljc @@ -67,10 +67,10 @@ [(website/generate-nginx-deployment (flatten-and-reduce-config config)) (website/generate-nginx-configmap (flatten-and-reduce-config config)) (website/generate-nginx-service (flatten-and-reduce-config config)) - (website/generate-website-content-volume (flatten-and-reduce-config config)) - (website/generate-hashfile-volume (flatten-and-reduce-config config)) - (website/generate-website-build-cron (flatten-and-reduce-config config)) - (website/generate-website-build-secret (flatten-and-reduce-config config) + (website/generate-content-pvc (flatten-and-reduce-config config)) + (website/generate-hash-state-pvc (flatten-and-reduce-config config)) + (website/generate-build-cron (flatten-and-reduce-config config)) + (website/generate-build-secret (flatten-and-reduce-config config) (flatten-and-reduce-auth auth))] (website/generate-ingress (flatten-and-reduce-config config)) ))))) diff --git a/src/main/cljc/dda/c4k_website/website.cljc b/src/main/cljc/dda/c4k_website/website.cljc index 99a033c..97d63ba 100644 --- a/src/main/cljc/dda/c4k_website/website.cljc +++ b/src/main/cljc/dda/c4k_website/website.cljc @@ -64,32 +64,32 @@ (int/generate-nginx-service final-config))) -(defn-spec generate-website-content-volume map? +(defn-spec generate-content-pvc map? [config websiteconfig?] (let [final-config (merge config-defaults config)] - (int/generate-website-content-volume final-config))) + (int/generate-content-pvc final-config))) -(defn-spec generate-hashfile-volume map? +(defn-spec generate-hash-state-pvc map? [config websiteconfig?] (let [final-config (merge config-defaults config)] - (int/generate-hashfile-volume final-config))) + (int/generate-hash-state-pvc final-config))) -(defn-spec generate-website-build-cron map? +(defn-spec generate-build-cron map? [config websiteconfig?] (let [final-config (merge config-defaults config)] - (int/generate-website-build-cron final-config))) + (int/generate-build-cron final-config))) -(defn-spec generate-website-build-secret map? +(defn-spec generate-build-secret map? [config websiteconfig? auth websiteauth?] (let [final-config (merge config-defaults config)] - (int/generate-website-build-secret final-config auth))) + (int/generate-build-secret final-config auth))) (defn-spec generate-namespcae seq? [config websiteconfig?] diff --git a/src/main/cljc/dda/c4k_website/website/website_internal.cljc b/src/main/cljc/dda/c4k_website/website/website_internal.cljc index df6f0a5..087a705 100644 --- a/src/main/cljc/dda/c4k_website/website/website_internal.cljc +++ b/src/main/cljc/dda/c4k_website/website/website_internal.cljc @@ -73,8 +73,8 @@ user string?] (str "https://" host "/api/v1/repos/" user "/" repo "/git/" "commits/" "HEAD")) -; TODO: Rename replace-all-matching-prefixes -(defn-spec replace-all-matching-substrings-beginning-with map? + +(defn-spec replace-all-matching-prefixes map? [col map? value-to-partly-match string? value-to-inplace string?] @@ -101,7 +101,7 @@ name (replace-dots-by-minus unique-name)] (-> (yaml/load-as-edn "website/nginx-configmap.yaml") - (replace-all-matching-substrings-beginning-with "NAME" name) + (replace-all-matching-prefixes "NAME" name) (#(assoc-in % [:data :website.conf] (str/replace (-> % :data :website.conf) @@ -114,7 +114,7 @@ (generate-redirects config 2))))))) -(defn-spec generate-website-build-secret pred/map-or-seq? +(defn-spec generate-build-secret pred/map-or-seq? [config websiteconfig? auth websiteauth?] (let [{:keys [unique-name @@ -126,7 +126,7 @@ name (replace-dots-by-minus unique-name)] (-> (yaml/load-as-edn "website/build-secret.yaml") - (replace-all-matching-substrings-beginning-with "NAME" name) + (replace-all-matching-prefixes "NAME" name) (cm/replace-all-matching-values-by-new-value "TOKEN" (b64/encode authtoken)) (cm/replace-all-matching-values-by-new-value "REPOURL" (b64/encode (generate-gitrepourl @@ -141,24 +141,24 @@ username)))))) -(defn-spec generate-website-content-volume map? +(defn-spec generate-content-pvc map? [config websiteconfig?] (let [{:keys [unique-name volume-size]} config name (replace-dots-by-minus unique-name)] (-> (yaml/load-as-edn "website/content-pvc.yaml") - (replace-all-matching-substrings-beginning-with "NAME" name) + (replace-all-matching-prefixes "NAME" name) (cm/replace-all-matching-values-by-new-value "WEBSITESTORAGESIZE" (str volume-size "Gi"))))) ; TODO: Non-Secret-Parts should be config map -(defn-spec generate-hashfile-volume map? +(defn-spec generate-hash-state-pvc map? [config websiteconfig?] (let [{:keys [unique-name]} config name (replace-dots-by-minus unique-name)] (-> (yaml/load-as-edn "website/hash-state-pvc.yaml") - (replace-all-matching-substrings-beginning-with "NAME" name)))) + (replace-all-matching-prefixes "NAME" name)))) (defn-spec generate-nginx-deployment map? @@ -169,21 +169,21 @@ (-> (yaml/load-as-edn "website/nginx-deployment.yaml") (assoc-in [:metadata :namespace] name) - (replace-all-matching-substrings-beginning-with "NAME" name) + (replace-all-matching-prefixes "NAME" name) (cm/replace-all-matching-values-by-new-value "BUILD_CPU_REQUEST" build-cpu-request) (cm/replace-all-matching-values-by-new-value "BUILD_CPU_LIMIT" build-cpu-limit) (cm/replace-all-matching-values-by-new-value "BUILD_MEMORY_REQUEST" build-memory-request) (cm/replace-all-matching-values-by-new-value "BUILD_MEMORY_LIMIT" build-memory-limit)))) -(defn-spec generate-website-build-cron map? +(defn-spec generate-build-cron map? [config websiteconfig?] (let [{:keys [unique-name build-cpu-request build-cpu-limit build-memory-request build-memory-limit]} config name (replace-dots-by-minus unique-name)] (-> (yaml/load-as-edn "website/build-cron.yaml") - (replace-all-matching-substrings-beginning-with "NAME" name) + (replace-all-matching-prefixes "NAME" name) (cm/replace-all-matching-values-by-new-value "BUILD_CPU_REQUEST" build-cpu-request) (cm/replace-all-matching-values-by-new-value "BUILD_CPU_LIMIT" build-cpu-limit) (cm/replace-all-matching-values-by-new-value "BUILD_MEMORY_REQUEST" build-memory-request) @@ -197,7 +197,7 @@ (-> (yaml/load-as-edn "website/nginx-service.yaml") (assoc-in [:metadata :namespace] name) - (replace-all-matching-substrings-beginning-with "NAME" name)))) + (replace-all-matching-prefixes "NAME" name)))) #?(:cljs diff --git a/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc b/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc index ca69ce9..377a536 100644 --- a/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc +++ b/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc @@ -8,13 +8,13 @@ (st/instrument `cut/replace-dots-by-minus) (st/instrument `cut/generate-gitrepourl) (st/instrument `cut/generate-gitcommiturl) -(st/instrument `cut/replace-all-matching-substrings-beginning-with) +(st/instrument `cut/replace-all-matching-prefixes) (st/instrument `cut/generate-redirects) (st/instrument `cut/generate-nginx-configmap) -(st/instrument `cut/generate-website-build-secret) -(st/instrument `cut/generate-website-content-volume) -(st/instrument `cut/generate-hashfile-volume) -(st/instrument `cut/generate-website-build-cron) +(st/instrument `cut/generate-build-secret) +(st/instrument `cut/generate-content-pvc) +(st/instrument `cut/generate-hash-state-pvc) +(st/instrument `cut/generate-build-cron) (st/instrument `cut/generate-nginx-service) (deftest should-generate-redirects @@ -162,7 +162,7 @@ ))) -(deftest should-generate-website-build-cron +(deftest should-generate-build-cron (is (= {:apiVersion "batch/v1", :kind "CronJob", :metadata {:name "build-cron", @@ -192,7 +192,7 @@ :volumes [{:name "content-volume", :persistentVolumeClaim {:claimName "content-volume"}} {:name "hash-state-volume", :persistentVolumeClaim {:claimName "hash-state-volume"}}], :restartPolicy "OnFailure"}}}}}} - (cut/generate-website-build-cron {:issuer "staging" + (cut/generate-build-cron {:issuer "staging" :build-cpu-request "500m" :build-cpu-limit "1700m" :build-memory-request "256Mi" @@ -205,7 +205,7 @@ :unique-name "test.io", :redirects [],})))) -(deftest should-generate-website-build-secret +(deftest should-generate-build-secret (is (= {:apiVersion "v1", :kind "Secret", :metadata {:name "build-secret", @@ -215,7 +215,7 @@ {:AUTHTOKEN "YWJlZGpnYmFzZG9kag==", :GITREPOURL "aHR0cHM6Ly9naXRsYWIuZGUvYXBpL3YxL3JlcG9zL3NvbWV1c2VyL3JlcG8vYXJjaGl2ZS9tYWluLnppcA==", :GITCOMMITURL "aHR0cHM6Ly9naXRsYWIuZGUvYXBpL3YxL3JlcG9zL3NvbWV1c2VyL3JlcG8vZ2l0L2NvbW1pdHMvSEVBRA=="}} - (cut/generate-website-build-secret {:fqdns ["test.de" "test.org" "www.test.de" "www.test.org"], + (cut/generate-build-secret {:fqdns ["test.de" "test.org" "www.test.de" "www.test.org"], :forgejo-repo "repo", :issuer "staging", :branchname "main", @@ -231,7 +231,7 @@ :authtoken "abedjgbasdodj", :username "someuser"})))) -(deftest should-generate-website-content-volume +(deftest should-generate-content-pvc (is (= {:apiVersion "v1", :kind "PersistentVolumeClaim", :metadata @@ -242,7 +242,7 @@ {:storageClassName "local-path", :accessModes ["ReadWriteOnce"], :resources {:requests {:storage "3Gi"}}}} - (cut/generate-website-content-volume {:issuer "staging" + (cut/generate-content-pvc {:issuer "staging" :build-cpu-request "500m" :build-cpu-limit "1700m" :build-memory-request "256Mi" @@ -256,7 +256,7 @@ :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})))) -(deftest should-generate-hashfile-volume +(deftest should-generate-hash-state-pvc (is (= {:apiVersion "v1", :kind "PersistentVolumeClaim", :metadata @@ -266,7 +266,7 @@ :spec {:storageClassName "local-path", :accessModes ["ReadWriteOnce"], :resources {:requests {:storage "16Mi"}}}} - (cut/generate-hashfile-volume {:issuer "staging" + (cut/generate-hash-state-pvc {:issuer "staging" :build-cpu-request "500m" :build-cpu-limit "1700m" :build-memory-request "256Mi" From a0bf9064ed95a44d4a7ed705a45fb9f97b4e312d Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Wed, 28 Feb 2024 10:26:59 +0100 Subject: [PATCH 4/4] update deps & fix tests --- package.json | 2 +- project.clj | 2 +- shadow-cljs.edn | 2 +- .../website/website_internal_test.cljc | 116 +++++++++--------- 4 files changed, 61 insertions(+), 61 deletions(-) diff --git a/package.json b/package.json index 85f3fec..b9069e2 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "js-yaml": "^4.0.0" }, "devDependencies": { - "shadow-cljs": "^2.11.18", + "shadow-cljs": "^2.27.4", "source-map-support": "^0.5.21" } } \ No newline at end of file diff --git a/project.clj b/project.clj index 18553f2..b38ee7a 100644 --- a/project.clj +++ b/project.clj @@ -5,7 +5,7 @@ :url "https://www.apache.org/licenses/LICENSE-2.0.html"} :dependencies [[org.clojure/clojure "1.11.1"] [org.clojure/tools.reader "1.4.0"] - [org.domaindrivenarchitecture/c4k-common-clj "6.1.4-SNAPSHOT"] + [org.domaindrivenarchitecture/c4k-common-clj "6.2.1"] [hickory "0.7.1" :exclusions [viebel/codox-klipse-theme]]] :target-path "target/%s/" :source-paths ["src/main/cljc" diff --git a/shadow-cljs.edn b/shadow-cljs.edn index d4415fd..537ae98 100644 --- a/shadow-cljs.edn +++ b/shadow-cljs.edn @@ -4,7 +4,7 @@ "src/test/cljc" "src/test/cljs" "src/test/resources"] - :dependencies [[org.domaindrivenarchitecture/c4k-common-cljs "6.1.3"] + :dependencies [[org.domaindrivenarchitecture/c4k-common-cljs "6.2.1"] [hickory "0.7.1"]] :builds {:frontend {:target :browser :modules {:main {:init-fn dda.c4k-website.browser/init}} diff --git a/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc b/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc index 377a536..c50ce30 100644 --- a/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc +++ b/src/test/cljc/dda/c4k_website/website/website_internal_test.cljc @@ -79,64 +79,64 @@ :volume-size 3}) :metadata :namespace)))) -(deftest should-generate-nginx-configmap-website - (is (= "server {\n listen 80 default_server;\n listen [::]:80 default_server;\n server_name test.de www.test.de test-it.de www.test-it.de;\n add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload'; \n add_header X-Frame-Options \"SAMEORIGIN\";\n add_header X-Content-Type-Options nosniff;\n add_header Referrer-Policy \"strict-origin\";\n # add_header Permissions-Policy \"permissions here\";\n root /var/www/html/website/;\n index index.html;\n location / {\n try_files $uri $uri/ /index.html =404;\n }\n # redirects\n rewrite ^/products.html$ /offer.html permanent;\n rewrite ^/one-more$ /redirect permanent;\n}\n" - (:website.conf (:data (cut/generate-nginx-configmap {:issuer "staging" - :build-cpu-request "500m" - :build-cpu-limit "1700m" - :build-memory-request "256Mi" - :build-memory-limit "512Mi" - :volume-size "3" - :unique-name "test.io", - :redirects [["/products.html", "/offer.html"] - ["/one-more", "/redirect"]] - :forgejo-host "gitea.evilorg", - :forgejo-repo "none", - :branchname "mablain", - :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}))))) - (is (= "types {\n text/html html htm shtml;\n text/css css;\n text/xml xml rss;\n image/gif gif;\n image/jpeg jpeg jpg;\n application/x-javascript js;\n text/plain txt;\n text/x-component htc;\n text/mathml mml;\n image/svg+xml svg svgz;\n image/png png;\n image/x-icon ico;\n image/x-jng jng;\n image/vnd.wap.wbmp wbmp;\n application/java-archive jar war ear;\n application/mac-binhex40 hqx;\n application/pdf pdf;\n application/x-cocoa cco;\n application/x-java-archive-diff jardiff;\n application/x-java-jnlp-file jnlp;\n application/x-makeself run;\n application/x-perl pl pm;\n application/x-pilot prc pdb;\n application/x-rar-compressed rar;\n application/x-redhat-package-manager rpm;\n application/x-sea sea;\n application/x-shockwave-flash swf;\n application/x-stuffit sit;\n application/x-tcl tcl tk;\n application/x-x509-ca-cert der pem crt;\n application/x-xpinstall xpi;\n application/zip zip;\n application/octet-stream deb;\n application/octet-stream bin exe dll;\n application/octet-stream dmg;\n application/octet-stream eot;\n application/octet-stream iso img;\n application/octet-stream msi msp msm;\n audio/mpeg mp3;\n audio/x-realaudio ra;\n video/mpeg mpeg mpg;\n video/quicktime mov;\n video/x-flv flv;\n video/x-msvideo avi;\n video/x-ms-wmv wmv;\n video/x-ms-asf asx asf;\n video/x-mng mng;\n}\n" - (:mime.types (:data (cut/generate-nginx-configmap {:issuer "staging" - :build-cpu-request "500m" - :build-cpu-limit "1700m" - :build-memory-request "256Mi" - :build-memory-limit "512Mi" - :volume-size "3" - :unique-name "test.io", - :redirects [], - :forgejo-host "gitea.evilorg", - :forgejo-repo "none", - :branchname "mablain", - :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}))))) - (is (= "user nginx;\nworker_processes 3;\nerror_log stdout info;\npid /var/log/nginx/nginx.pid;\nworker_rlimit_nofile 8192;\nevents {\n worker_connections 4096;\n}\nhttp {\n include /etc/nginx/mime.types;\n default_type application/octet-stream;\n log_format main '$remote_addr - $remote_user [$time_local] $status'\n '\"$request\" $body_bytes_sent \"$http_referer\"'\n '\"$http_user_agent\" \"$http_x_forwarded_for\"';\n access_log stdout main;\n sendfile on;\n tcp_nopush on;\n keepalive_timeout 65;\n server_names_hash_bucket_size 128;\n include /etc/nginx/conf.d/website.conf;\n}\n" - (:nginx.conf (:data (cut/generate-nginx-configmap {:issuer "staging" - :build-cpu-request "500m" - :build-cpu-limit "1700m" - :build-memory-request "256Mi" - :build-memory-limit "512Mi" - :volume-size "3" - :unique-name "test.io", - :redirects [], - :forgejo-host "gitea.evilorg", - :forgejo-repo "none", - :branchname "mablain", - :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}))))) - (is (= {:apiVersion "v1", - :kind "ConfigMap", - :metadata {:labels {:app.kubernetes.part-of "test-io-website"}, - :namespace "test-io", - :name "etc-nginx"}} - (dissoc (cut/generate-nginx-configmap {:issuer "staging" - :build-cpu-request "500m" - :build-cpu-limit "1700m" - :build-memory-request "256Mi" - :build-memory-limit "512Mi" - :volume-size "3" - :unique-name "test.io", - :redirects [], - :forgejo-host "gitea.evilorg", - :forgejo-repo "none", - :branchname "mablain", - :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}) :data)))) +#?(:clj (deftest should-generate-nginx-configmap-website + (is (= "server {\n listen 80 default_server;\n listen [::]:80 default_server;\n server_name test.de www.test.de test-it.de www.test-it.de;\n add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload'; \n add_header X-Frame-Options \"SAMEORIGIN\";\n add_header X-Content-Type-Options nosniff;\n add_header Referrer-Policy \"strict-origin\";\n # add_header Permissions-Policy \"permissions here\";\n root /var/www/html/website/;\n index index.html;\n location / {\n try_files $uri $uri/ /index.html =404;\n }\n # redirects\n rewrite ^/products.html$ /offer.html permanent;\n rewrite ^/one-more$ /redirect permanent;\n}\n" + (:website.conf (:data (cut/generate-nginx-configmap {:issuer "staging" + :build-cpu-request "500m" + :build-cpu-limit "1700m" + :build-memory-request "256Mi" + :build-memory-limit "512Mi" + :volume-size "3" + :unique-name "test.io", + :redirects [["/products.html", "/offer.html"] + ["/one-more", "/redirect"]] + :forgejo-host "gitea.evilorg", + :forgejo-repo "none", + :branchname "mablain", + :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}))))) + (is (= "types {\n text/html html htm shtml;\n text/css css;\n text/xml xml rss;\n image/gif gif;\n image/jpeg jpeg jpg;\n application/x-javascript js;\n text/plain txt;\n text/x-component htc;\n text/mathml mml;\n image/svg+xml svg svgz;\n image/png png;\n image/x-icon ico;\n image/x-jng jng;\n image/vnd.wap.wbmp wbmp;\n application/java-archive jar war ear;\n application/mac-binhex40 hqx;\n application/pdf pdf;\n application/x-cocoa cco;\n application/x-java-archive-diff jardiff;\n application/x-java-jnlp-file jnlp;\n application/x-makeself run;\n application/x-perl pl pm;\n application/x-pilot prc pdb;\n application/x-rar-compressed rar;\n application/x-redhat-package-manager rpm;\n application/x-sea sea;\n application/x-shockwave-flash swf;\n application/x-stuffit sit;\n application/x-tcl tcl tk;\n application/x-x509-ca-cert der pem crt;\n application/x-xpinstall xpi;\n application/zip zip;\n application/octet-stream deb;\n application/octet-stream bin exe dll;\n application/octet-stream dmg;\n application/octet-stream eot;\n application/octet-stream iso img;\n application/octet-stream msi msp msm;\n audio/mpeg mp3;\n audio/x-realaudio ra;\n video/mpeg mpeg mpg;\n video/quicktime mov;\n video/x-flv flv;\n video/x-msvideo avi;\n video/x-ms-wmv wmv;\n video/x-ms-asf asx asf;\n video/x-mng mng;\n}\n" + (:mime.types (:data (cut/generate-nginx-configmap {:issuer "staging" + :build-cpu-request "500m" + :build-cpu-limit "1700m" + :build-memory-request "256Mi" + :build-memory-limit "512Mi" + :volume-size "3" + :unique-name "test.io", + :redirects [], + :forgejo-host "gitea.evilorg", + :forgejo-repo "none", + :branchname "mablain", + :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}))))) + (is (= "user nginx;\nworker_processes 3;\nerror_log stdout info;\npid /var/log/nginx/nginx.pid;\nworker_rlimit_nofile 8192;\nevents {\n worker_connections 4096;\n}\nhttp {\n include /etc/nginx/mime.types;\n default_type application/octet-stream;\n log_format main '$remote_addr - $remote_user [$time_local] $status'\n '\"$request\" $body_bytes_sent \"$http_referer\"'\n '\"$http_user_agent\" \"$http_x_forwarded_for\"';\n access_log stdout main;\n sendfile on;\n tcp_nopush on;\n keepalive_timeout 65;\n server_names_hash_bucket_size 128;\n include /etc/nginx/conf.d/website.conf;\n}\n" + (:nginx.conf (:data (cut/generate-nginx-configmap {:issuer "staging" + :build-cpu-request "500m" + :build-cpu-limit "1700m" + :build-memory-request "256Mi" + :build-memory-limit "512Mi" + :volume-size "3" + :unique-name "test.io", + :redirects [], + :forgejo-host "gitea.evilorg", + :forgejo-repo "none", + :branchname "mablain", + :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}))))) + (is (= {:apiVersion "v1", + :kind "ConfigMap", + :metadata {:labels {:app.kubernetes.part-of "test-io-website"}, + :namespace "test-io", + :name "etc-nginx"}} + (dissoc (cut/generate-nginx-configmap {:issuer "staging" + :build-cpu-request "500m" + :build-cpu-limit "1700m" + :build-memory-request "256Mi" + :build-memory-limit "512Mi" + :volume-size "3" + :unique-name "test.io", + :redirects [], + :forgejo-host "gitea.evilorg", + :forgejo-repo "none", + :branchname "mablain", + :fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}) :data))))) (deftest should-generate-nginx-service (is (= {:kind "Service",