feature/introduce-namespaces #3

Merged
jem merged 11 commits from feature/introduce-namespaces into main 2024-02-27 10:13:49 +00:00
5 changed files with 127 additions and 109 deletions
Showing only changes of commit 9cb0e50853 - Show all commits

View file

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

View file

@ -2,7 +2,9 @@
(:require
[clojure.spec.alpha :as s]
#?(: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]))
(s/def ::unique-name ::int/unique-name)
@ -45,3 +47,63 @@
(let [final-config (merge config-defaults
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?]
(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
(defn-spec generate-gitrepourl string?
[host pred/fqdn-string?
@ -105,7 +85,7 @@
(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 :labels :app.kubernetes.part-of] 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_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)
)))
(cm/replace-all-matching-values-by-new-value "BUILD_MEMORY_LIMIT" build-memory-limit))))
(defn-spec generate-nginx-configmap map?
@ -164,27 +143,6 @@
(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?
[config websiteconfig?]
(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))))
(is (= flattened-and-reduced-auth
(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-website.website :as cut]))
(deftest should-generate-nginx-deployment
(is (= {:apiVersion "apps/v1",
:kind "Deployment",
:metadata {:name "test-io-deployment",
:namespace "test-io"
:labels {:app.kubernetes.part-of "test-io"}},
:spec
{:replicas 1,
:selector {:matchLabels {:app "test-io-nginx"}},
:template
{:metadata {:namespace "webserver"
:labels {:app "test-io-nginx"}},
:spec
{:containers
[{:name "test-io-nginx",
:image "nginx:latest",
:imagePullPolicy "IfNotPresent",
:ports [{:containerPort 80}],
:volumeMounts
[{:mountPath "/etc/nginx", :readOnly true, :name "etc-ngingx"}
{:mountPath "/var/log/nginx", :name "log"}
{:mountPath "/var/www/html/website", :name "content-volume", :readOnly true}]}],
:initContainers
[{:image "domaindrivenarchitecture/c4k-website-build",
:name "test-io-init-build-container",
:imagePullPolicy "IfNotPresent",
:resources {:requests {:cpu "500m", :memory "256Mi"}, :limits {:cpu "1700m", :memory "512Mi"}},
:command ["/entrypoint.sh"],
:envFrom [{:secretRef {:name "test-io-secret"}}],
:volumeMounts [{:name "content-volume", :mountPath "/var/www/html/website"}
{:name "hashfile-volume", :mountPath "/var/hashfile.d"}]}],
:volumes
[{:name "etc-ngingx",
:configMap
{:name "etc-ngingx",
:items
[{:key "nginx.conf", :path "nginx.conf"}
{:key "website.conf", :path "conf.d/website.conf"}
{:key "mime.types", :path "mime.types"}]}}
{:name "log", :emptyDir {}}
{:name "content-volume", :persistentVolumeClaim {:claimName "test-io-content-volume"}}
{:name "hashfile-volume", :persistentVolumeClaim {:claimName "test-io-hashfile-volume"}}]}}}}
(cut/generate-nginx-deployment {:forgejo-host "gitlab.de",
:fqdns ["test.de" "test.org" "www.test.de" "www.test.org"],
:forgejo-repo "repo",
:sha256sum-output "123456789ab123cd345de script-file-name.sh",
:issuer "staging",
:branchname "main",
:unique-name "test.io"}))))
(deftest should-generate-ingress
(is (= [{:host "test.de",
:http
{:paths
[{:pathType "Prefix",
:path "/",
:backend {:service {:name "test-io", :port {:number 80}}}}]}}
{:host "test.org",
:http
{:paths
[{:pathType "Prefix",
:path "/",
:backend {:service {:name "test-io", :port {:number 80}}}}]}}
{:host "www.test.de",
:http
{:paths
[{:pathType "Prefix",
:path "/",
:backend {:service {:name "test-io", :port {:number 80}}}}]}}
{:host "www.test.org",
:http
{:paths
[{:pathType "Prefix",
:path "/",
:backend {:service {:name "test-io", :port {:number 80}}}}]}}]
(get-in
(cut/generate-ingress {:forgejo-host "gitlab.de",
:fqdns ["test.de" "test.org" "www.test.de" "www.test.org"],
:forgejo-repo "repo",
:sha256sum-output "123456789ab123cd345de script-file-name.sh",
:issuer "staging",
:branchname "main",
:unique-name "test.io"})
[2 :spec :rules])))
(is (= "test-io"
(get-in
(cut/generate-ingress {:forgejo-host "gitlab.de",
:fqdns ["test.de" "test.org" "www.test.de" "www.test.org"],
:forgejo-repo "repo",
:sha256sum-output "123456789ab123cd345de script-file-name.sh",
:issuer "staging",
:branchname "main",
:unique-name "test.io"})
[2 :metadata :namespace]))))