use ingress & ns from common

This commit is contained in:
Michael Jerger 2024-02-26 16:12:01 +01:00
parent 0cf35755f9
commit 9cb0e50853
5 changed files with 127 additions and 109 deletions

View file

@ -9,9 +9,6 @@
[dda.c4k-common.monitoring :as mon] [dda.c4k-common.monitoring :as mon]
[dda.c4k-website.website :as website])) [dda.c4k-website.website :as website]))
(def config-defaults {:issuer "staging"
:volume-size "3"})
(s/def ::mon-cfg ::mon/mon-cfg) (s/def ::mon-cfg ::mon/mon-cfg)
(s/def ::mon-auth ::mon/mon-auth) (s/def ::mon-auth ::mon/mon-auth)
@ -64,18 +61,19 @@
(-> (->
auth auth
(assoc-in [:auth] (rest (auth :auth)))) (assoc-in [:auth] (rest (auth :auth))))
(conj result (cm/concat-vec
(website/generate-nginx-deployment (flatten-and-reduce-config config)) result
;(website/generate-nginx-configmap (flatten-and-reduce-config config)) (website/generate-namespcae (flatten-and-reduce-config config))
;(website/generate-nginx-service (flatten-and-reduce-config config)) [(website/generate-nginx-deployment (flatten-and-reduce-config config))
;(website/generate-website-content-volume (flatten-and-reduce-config config)) (website/generate-nginx-configmap (flatten-and-reduce-config config))
;(website/generate-hashfile-volume (flatten-and-reduce-config config)) (website/generate-nginx-service (flatten-and-reduce-config config))
;(website/generate-website-ingress (flatten-and-reduce-config config)) (website/generate-website-content-volume (flatten-and-reduce-config config))
;(website/generate-website-certificate (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-cron (flatten-and-reduce-config config))
;(website/generate-website-build-secret (flatten-and-reduce-config config) (website/generate-website-build-secret (flatten-and-reduce-config config)
; (flatten-and-reduce-auth auth)) (flatten-and-reduce-auth auth))]
))))) (website/generate-ingress (flatten-and-reduce-config config))
)))))
(defn-spec k8s-objects cp/map-or-seq? (defn-spec k8s-objects cp/map-or-seq?
[config config? [config config?

View file

@ -2,7 +2,9 @@
(:require (:require
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
#?(:clj [orchestra.core :refer [defn-spec]] #?(:clj [orchestra.core :refer [defn-spec]]
:cljs [orchestra.core :refer-macros [defn-spec]]) :cljs [orchestra.core :refer-macros [defn-spec]])
[dda.c4k-common.ingress :as ing]
[dda.c4k-common.namespace :as ns]
[dda.c4k-website.website.website-internal :as int])) [dda.c4k-website.website.website-internal :as int]))
(s/def ::unique-name ::int/unique-name) (s/def ::unique-name ::int/unique-name)
@ -45,3 +47,63 @@
(let [final-config (merge config-defaults (let [final-config (merge config-defaults
config)] config)]
(int/generate-nginx-deployment final-config))) (int/generate-nginx-deployment final-config)))
(defn-spec generate-nginx-configmap map?
[config websiteconfig?]
(let [final-config (merge config-defaults
config)]
(int/generate-nginx-configmap final-config)))
(defn-spec generate-nginx-service map?
[config websiteconfig?]
(let [final-config (merge config-defaults
config)]
(int/generate-nginx-service final-config)))
(defn-spec generate-website-content-volume map?
[config websiteconfig?]
(let [final-config (merge config-defaults
config)]
(int/generate-website-content-volume final-config)))
(defn-spec generate-hashfile-volume map?
[config websiteconfig?]
(let [final-config (merge config-defaults
config)]
(int/generate-hashfile-volume final-config)))
(defn-spec generate-website-build-cron map?
[config websiteconfig?]
(let [final-config (merge config-defaults
config)]
(int/generate-website-build-cron final-config)))
(defn-spec generate-website-build-secret map?
[config websiteconfig?
auth websiteauth?]
(let [final-config (merge config-defaults
config)]
(int/generate-website-build-secret final-config auth)))
(defn-spec generate-namespcae seq?
[config websiteconfig?]
(let [name (int/replace-dots-by-minus (:unique-name config))
final-config (merge config-defaults
{:namespace name}
config)]
(ns/generate final-config)))
(defn-spec generate-ingress seq?
[config websiteconfig?]
(let [name (int/replace-dots-by-minus (:unique-name config))
final-config (merge config-defaults
{:service-name name
:service-port 80
:namespace name}
config)]
(ing/generate-simple-ingress final-config)))

View file

@ -55,26 +55,6 @@
[fqdn pred/fqdn-string?] [fqdn pred/fqdn-string?]
(str/replace fqdn #"\." "-")) (str/replace fqdn #"\." "-"))
; TODO: remove
(defn-spec generate-app-name string?
[unique-name pred/fqdn-string?]
(str (replace-dots-by-minus unique-name) "-website"))
; TODO: remove
(defn-spec generate-service-name string?
[unique-name pred/fqdn-string?]
(str (replace-dots-by-minus unique-name) "-service"))
; TODO: remove
(defn-spec generate-cert-name string?
[unique-name pred/fqdn-string?]
(str (replace-dots-by-minus unique-name) "-cert"))
; TODO: remove
(defn-spec generate-ingress-name string?
[unique-name pred/fqdn-string?]
(str (replace-dots-by-minus unique-name) "-ingress"))
; https://your.gitea.host/api/v1/repos/<owner>/<repo>/archive/<branch>.zip ; https://your.gitea.host/api/v1/repos/<owner>/<repo>/archive/<branch>.zip
(defn-spec generate-gitrepourl string? (defn-spec generate-gitrepourl string?
[host pred/fqdn-string? [host pred/fqdn-string?
@ -105,7 +85,7 @@
(let [{:keys [unique-name build-cpu-request build-cpu-limit (let [{:keys [unique-name build-cpu-request build-cpu-limit
build-memory-request build-memory-limit]} config build-memory-request build-memory-limit]} config
name (replace-dots-by-minus unique-name)] name (replace-dots-by-minus unique-name)]
(-> (->
(yaml/load-as-edn "website/nginx-deployment.yaml") (yaml/load-as-edn "website/nginx-deployment.yaml")
(assoc-in [:metadata :labels :app.kubernetes.part-of] name) (assoc-in [:metadata :labels :app.kubernetes.part-of] name)
(assoc-in [:metadata :namespace] name) (assoc-in [:metadata :namespace] name)
@ -113,8 +93,7 @@
(cm/replace-all-matching-values-by-new-value "BUILD_CPU_REQUEST" build-cpu-request) (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_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_REQUEST" build-memory-request)
(cm/replace-all-matching-values-by-new-value "BUILD_MEMORY_LIMIT" build-memory-limit) (cm/replace-all-matching-values-by-new-value "BUILD_MEMORY_LIMIT" build-memory-limit))))
)))
(defn-spec generate-nginx-configmap map? (defn-spec generate-nginx-configmap map?
@ -164,27 +143,6 @@
(replace-all-matching-substrings-beginning-with "NAME" name)))) (replace-all-matching-substrings-beginning-with "NAME" name))))
; TODO: remove
(defn-spec generate-website-ingress pred/map-or-seq?
[config websiteconfig?]
(let [{:keys [unique-name fqdns]} config]
(ing/generate-ingress {:fqdns fqdns
:app-name (generate-app-name unique-name)
:ingress-name (generate-ingress-name unique-name)
:service-name (generate-service-name unique-name)
:service-port 80})))
; TODO: remove - using simple ingress instead removes the need of cert handling
(defn-spec generate-website-certificate pred/map-or-seq?
[config websiteconfig?]
(let [{:keys [unique-name issuer fqdns]
:or {issuer "staging"}} config]
(ing/generate-certificate {:fqdns fqdns
:app-name (generate-app-name unique-name)
:cert-name (generate-cert-name unique-name)
:issuer issuer})))
(defn-spec generate-website-build-cron map? (defn-spec generate-website-build-cron map?
[config websiteconfig?] [config websiteconfig?]
(let [{:keys [unique-name build-cpu-request build-cpu-limit build-memory-request (let [{:keys [unique-name build-cpu-request build-cpu-limit build-memory-request

View file

@ -118,3 +118,8 @@
(cut/flatten-and-reduce-auth (cut/sort-auth auth1)))) (cut/flatten-and-reduce-auth (cut/sort-auth auth1))))
(is (= flattened-and-reduced-auth (is (= flattened-and-reduced-auth
(cut/flatten-and-reduce-auth (cut/sort-auth auth2))))) (cut/flatten-and-reduce-auth (cut/sort-auth auth2)))))
(deftest test-generate
(is (= 22
(count (cut/generate (yaml/load-as-edn "website-test/valid-config.yaml")
(yaml/load-as-edn "website-test/valid-auth.yaml"))))))

View file

@ -6,52 +6,47 @@
[dda.c4k-common.test-helper :as th] [dda.c4k-common.test-helper :as th]
[dda.c4k-website.website :as cut])) [dda.c4k-website.website :as cut]))
(deftest should-generate-nginx-deployment (deftest should-generate-ingress
(is (= {:apiVersion "apps/v1", (is (= [{:host "test.de",
:kind "Deployment", :http
:metadata {:name "test-io-deployment", {:paths
:namespace "test-io" [{:pathType "Prefix",
:labels {:app.kubernetes.part-of "test-io"}}, :path "/",
:spec :backend {:service {:name "test-io", :port {:number 80}}}}]}}
{:replicas 1, {:host "test.org",
:selector {:matchLabels {:app "test-io-nginx"}}, :http
:template {:paths
{:metadata {:namespace "webserver" [{:pathType "Prefix",
:labels {:app "test-io-nginx"}}, :path "/",
:spec :backend {:service {:name "test-io", :port {:number 80}}}}]}}
{:containers {:host "www.test.de",
[{:name "test-io-nginx", :http
:image "nginx:latest", {:paths
:imagePullPolicy "IfNotPresent", [{:pathType "Prefix",
:ports [{:containerPort 80}], :path "/",
:volumeMounts :backend {:service {:name "test-io", :port {:number 80}}}}]}}
[{:mountPath "/etc/nginx", :readOnly true, :name "etc-ngingx"} {:host "www.test.org",
{:mountPath "/var/log/nginx", :name "log"} :http
{:mountPath "/var/www/html/website", :name "content-volume", :readOnly true}]}], {:paths
:initContainers [{:pathType "Prefix",
[{:image "domaindrivenarchitecture/c4k-website-build", :path "/",
:name "test-io-init-build-container", :backend {:service {:name "test-io", :port {:number 80}}}}]}}]
:imagePullPolicy "IfNotPresent", (get-in
:resources {:requests {:cpu "500m", :memory "256Mi"}, :limits {:cpu "1700m", :memory "512Mi"}}, (cut/generate-ingress {:forgejo-host "gitlab.de",
:command ["/entrypoint.sh"], :fqdns ["test.de" "test.org" "www.test.de" "www.test.org"],
:envFrom [{:secretRef {:name "test-io-secret"}}], :forgejo-repo "repo",
:volumeMounts [{:name "content-volume", :mountPath "/var/www/html/website"} :sha256sum-output "123456789ab123cd345de script-file-name.sh",
{:name "hashfile-volume", :mountPath "/var/hashfile.d"}]}], :issuer "staging",
:volumes :branchname "main",
[{:name "etc-ngingx", :unique-name "test.io"})
:configMap [2 :spec :rules])))
{:name "etc-ngingx", (is (= "test-io"
:items (get-in
[{:key "nginx.conf", :path "nginx.conf"} (cut/generate-ingress {:forgejo-host "gitlab.de",
{:key "website.conf", :path "conf.d/website.conf"} :fqdns ["test.de" "test.org" "www.test.de" "www.test.org"],
{:key "mime.types", :path "mime.types"}]}} :forgejo-repo "repo",
{:name "log", :emptyDir {}} :sha256sum-output "123456789ab123cd345de script-file-name.sh",
{:name "content-volume", :persistentVolumeClaim {:claimName "test-io-content-volume"}} :issuer "staging",
{:name "hashfile-volume", :persistentVolumeClaim {:claimName "test-io-hashfile-volume"}}]}}}} :branchname "main",
(cut/generate-nginx-deployment {:forgejo-host "gitlab.de", :unique-name "test.io"})
:fqdns ["test.de" "test.org" "www.test.de" "www.test.org"], [2 :metadata :namespace]))))
:forgejo-repo "repo",
:sha256sum-output "123456789ab123cd345de script-file-name.sh",
:issuer "staging",
:branchname "main",
:unique-name "test.io"}))))