merge happens in uberjar -> website is obsoloete

This commit is contained in:
Michael Jerger 2024-02-29 15:37:30 +01:00
parent 18ec39549e
commit f16ee34782
6 changed files with 143 additions and 194 deletions

View file

@ -2,7 +2,6 @@
(:gen-class)
(:require
[dda.c4k-common.uberjar :as uberjar]
[dda.c4k-website.website :as website]
[dda.c4k-website.core :as core]))
@ -11,6 +10,6 @@
"c4k-website"
core/config?
core/auth?
website/config-defaults
core/config-defaults
core/k8s-objects
cmd-args))

View file

@ -7,19 +7,59 @@
[dda.c4k-common.common :as cm]
[dda.c4k-common.predicate :as cp]
[dda.c4k-common.monitoring :as mon]
[dda.c4k-website.website :as website]))
[dda.c4k-common.namespace :as ns]
[dda.c4k-common.ingress :as ing]
[dda.c4k-website.website.website-internal :as int]))
(s/def ::mon-cfg ::mon/mon-cfg)
(s/def ::mon-auth ::mon/mon-auth)
(s/def ::unique-name ::int/unique-name)
(s/def ::issuer ::int/issuer)
(s/def ::volume-size ::int/volume-size)
(s/def ::authtoken ::int/authtoken)
(s/def ::fqdns ::int/fqdns)
(s/def ::forgejo-host ::int/forgejo-host)
(s/def ::forgejo-repo ::int/forgejo-repo)
(s/def ::branchname ::int/branchname)
(s/def ::username ::int/username)
(s/def ::build-cpu-request ::int/build-cpu-request)
(s/def ::build-memory-request ::int/build-memory-request)
(s/def ::build-cpu-limit ::int/build-cpu-limit)
(s/def ::build-memory-limit ::int/build-memory-limit)
(def config? (s/keys :req-un [::website/websites]
:opt-un [::website/issuer
::website/volume-size
(def websiteconfig? (s/keys :req-un [::unique-name
::fqdns
::forgejo-host
::forgejo-repo
::branchname]
:opt-un [::issuer
::volume-size
::build-cpu-request
::build-cpu-limit
::build-memory-request
::build-memory-limit]))
(def websiteauth? (s/keys :req-un [::unique-name ::username ::authtoken]))
(s/def ::websites (s/coll-of websiteconfig?))
(s/def ::auth (s/coll-of websiteauth?))
(def config? (s/keys :req-un [::websites]
:opt-un [::issuer
::volume-size
::mon-cfg]))
(def auth? (s/keys :req-un [::website/auth]
(def auth? (s/keys :req-un [::auth]
:opt-un [::mon-auth]))
(def config-defaults {:issuer "staging"})
(def website-config-defaults {:build-cpu-request "500m"
:build-cpu-limit "1700m"
:build-memory-request "256Mi"
:build-memory-limit "512Mi"
:volume-size "3"
:redirects []})
(defn-spec sort-config map?
[unsorted-config config?]
(let [sorted-websites (into [] (sort-by :unique-name (unsorted-config :websites)))]
@ -46,34 +86,48 @@
[auth auth?]
(-> auth :auth first))
(defn-spec generate-ingress seq?
[config websiteconfig?]
(let [name (int/replace-dots-by-minus (:unique-name config))
final-config (merge website-config-defaults
{:service-name name
:service-port 80
:namespace name}
config)]
(ing/generate-simple-ingress final-config)))
(defn-spec generate seq?
[config config?
auth auth?]
(loop [config (sort-config config)
auth (sort-auth auth)
sorted-auth (sort-auth auth)
result []]
(if (and (empty? (config :websites)) (empty? (auth :auth)))
(if (and (empty? (config :websites)) (empty? (sorted-auth :auth)))
result
(recur (->
config
(assoc-in [:websites] (rest (config :websites))))
(->
auth
(assoc-in [:auth] (rest (auth :auth))))
(assoc-in [:auth] (rest (sorted-auth :auth))))
(let [final-config
(merge
website-config-defaults
(flatten-and-reduce-config config))
name (int/replace-dots-by-minus (:unique-name final-config))]
(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-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)
(ns/generate (merge {:namespace name} final-config))
[(int/generate-nginx-deployment final-config)
(int/generate-nginx-configmap final-config)
(int/generate-nginx-service final-config)
(int/generate-content-pvc final-config)
(int/generate-hash-state-pvc final-config)
(int/generate-build-cron final-config)
(int/generate-build-secret final-config
(flatten-and-reduce-auth auth))]
(website/generate-ingress (flatten-and-reduce-config config))
)))))
(generate-ingress final-config)))))))
(defn-spec k8s-objects cp/map-or-seq?
[config config?

View file

@ -1,110 +0,0 @@
(ns dda.c4k-website.website
(:require
[clojure.spec.alpha :as s]
#?(:clj [orchestra.core :refer [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)
(s/def ::issuer ::int/issuer)
(s/def ::volume-size ::int/volume-size)
(s/def ::authtoken ::int/authtoken)
(s/def ::fqdns ::int/fqdns)
(s/def ::forgejo-host ::int/forgejo-host)
(s/def ::forgejo-repo ::int/forgejo-repo)
(s/def ::branchname ::int/branchname)
(s/def ::username ::int/username)
(s/def ::build-cpu-request ::int/build-cpu-request)
(s/def ::build-memory-request ::int/build-memory-request)
(s/def ::build-cpu-limit ::int/build-cpu-limit)
(s/def ::build-memory-limit ::int/build-memory-limit)
(def websiteconfig? (s/keys :req-un [::unique-name
::fqdns
::forgejo-host
::forgejo-repo
::branchname]
:opt-un [::issuer
::volume-size
::build-cpu-request
::build-cpu-limit
::build-memory-request
::build-memory-limit]))
(def websiteauth? (s/keys :req-un [::unique-name ::username ::authtoken]))
(def config-defaults {:issuer "staging"
:build-cpu-request "500m"
:build-cpu-limit "1700m"
:build-memory-request "256Mi"
:build-memory-limit "512Mi"
:volume-size "3"
:redirects []})
(defn-spec generate-nginx-deployment map?
[config websiteconfig?]
(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-content-pvc map?
[config websiteconfig?]
(let [final-config (merge config-defaults
config)]
(int/generate-content-pvc final-config)))
(defn-spec generate-hash-state-pvc map?
[config websiteconfig?]
(let [final-config (merge config-defaults
config)]
(int/generate-hash-state-pvc final-config)))
(defn-spec generate-build-cron map?
[config websiteconfig?]
(let [final-config (merge config-defaults
config)]
(int/generate-build-cron final-config)))
(defn-spec generate-build-secret map?
[config websiteconfig?
auth websiteauth?]
(let [final-config (merge config-defaults
config)]
(int/generate-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

@ -105,7 +105,7 @@
(-> (cm/generate-common
(config-from-document)
(br/get-content-from-element "auth" :deserializer edn/read-string)
website/config-defaults
core/config-defaults
core/k8s-objects)
(br/set-output!)))))
(add-validate-listener "websites")

View file

@ -11,6 +11,7 @@
(st/instrument `cut/sort-config)
(st/instrument `cut/flattened-and-reduced-config)
(st/instrument `cut/flatten-and-reduce-auth)
(st/instrument `cut/generate-ingress)
(st/instrument `cut/generate)
#?(:cljs
@ -127,5 +128,63 @@
(deftest test-generate
(is (= 22
(count (cut/generate (yaml/load-as-edn "website-test/valid-config.yaml")
(count (cut/generate
(yaml/load-as-edn "website-test/valid-config.yaml")
(yaml/load-as-edn "website-test/valid-auth.yaml"))))))
(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"
:build-cpu-request "500m"
:build-cpu-limit "1700m"
:build-memory-request "256Mi"
:build-memory-limit "512Mi"
:volume-size "3"
:redirects []})
[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"
:build-cpu-request "500m"
:build-cpu-limit "1700m"
:build-memory-request "256Mi"
:build-memory-limit "512Mi"
:volume-size "3"
:redirects []})
[2 :metadata :namespace]))))

View file

@ -1,53 +0,0 @@
(ns dda.c4k-website.website-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-website.website :as cut]))
(st/instrument `cut/generate-ingress)
(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]))))