[WIP] Add and review specs
* add spec for flatten-and-reduce-config, * checks if config and auth are present * combine websitedata? and websiteauth? * due to input from flatten-and-reduce-config
This commit is contained in:
parent
caa8b5c886
commit
03b6e2c234
2 changed files with 24 additions and 22 deletions
|
@ -1,15 +1,20 @@
|
||||||
(ns dda.c4k-website.core
|
(ns dda.c4k-website.core
|
||||||
(:require
|
(:require
|
||||||
[clojure.spec.alpha :as s]
|
[clojure.spec.alpha :as s]
|
||||||
|
#?(:clj [orchestra.core :refer [defn-spec]]
|
||||||
|
:cljs [orchestra.core :refer-macros [defn-spec]])
|
||||||
[dda.c4k-common.yaml :as yaml]
|
[dda.c4k-common.yaml :as yaml]
|
||||||
[dda.c4k-common.common :as cm]
|
[dda.c4k-common.common :as cm]
|
||||||
|
[dda.c4k-common.predicate :as pred]
|
||||||
[dda.c4k-website.website :as website]))
|
[dda.c4k-website.website :as website]))
|
||||||
|
|
||||||
(def config-defaults {:issuer "staging"
|
(def config-defaults {:issuer "staging"
|
||||||
:volume-size "3"})
|
:volume-size "3"})
|
||||||
|
|
||||||
(defn flatten-and-reduce-config
|
(def merged-config-and-auth? (s/and website/config? website/auth?))
|
||||||
[unsorted-config]
|
|
||||||
|
(defn-spec flatten-and-reduce-config pred/map-or-seq?
|
||||||
|
[unsorted-config merged-config-and-auth?]
|
||||||
(let [sorted-websites (into [] (sort-by :unique-name (unsorted-config :websites)))
|
(let [sorted-websites (into [] (sort-by :unique-name (unsorted-config :websites)))
|
||||||
sorted-auth (into [] (sort-by :unique-name (unsorted-config :auth)))
|
sorted-auth (into [] (sort-by :unique-name (unsorted-config :auth)))
|
||||||
config (-> unsorted-config
|
config (-> unsorted-config
|
||||||
|
|
|
@ -28,10 +28,12 @@
|
||||||
(s/def ::username string?)
|
(s/def ::username string?)
|
||||||
|
|
||||||
(def websitedata? (s/keys :req-un [::unique-name ::fqdns ::gitea-host ::gitea-repo ::branchname]
|
(def websitedata? (s/keys :req-un [::unique-name ::fqdns ::gitea-host ::gitea-repo ::branchname]
|
||||||
:opt-un [::issuer]))
|
:opt-un [::issuer ::volume-size]))
|
||||||
|
|
||||||
(def websiteauth? (s/keys :req-un [::unique-name ::username ::authtoken]))
|
(def websiteauth? (s/keys :req-un [::unique-name ::username ::authtoken]))
|
||||||
|
|
||||||
|
(def flattened-and-reduced-config? (s/and websitedata? websiteauth?))
|
||||||
|
|
||||||
(s/def ::auth (s/coll-of websiteauth?))
|
(s/def ::auth (s/coll-of websiteauth?))
|
||||||
|
|
||||||
(s/def ::websites (s/coll-of websitedata?))
|
(s/def ::websites (s/coll-of websitedata?))
|
||||||
|
@ -70,11 +72,10 @@
|
||||||
(str "https://" host "/api/v1/repos/" user "/" repo "/archive/" branch ".zip"))
|
(str "https://" host "/api/v1/repos/" user "/" repo "/archive/" branch ".zip"))
|
||||||
|
|
||||||
; ToDo: Move to common?
|
; ToDo: Move to common?
|
||||||
; ToDo richtig spec-en
|
(defn-spec replace-all-matching-subvalues-in-string-start pred/map-or-seq?
|
||||||
(defn replace-all-matching-subvalues-in-string-start
|
[col pred/map-or-seq?
|
||||||
[col
|
value-to-partly-match string?
|
||||||
value-to-partly-match
|
value-to-inplace string?]
|
||||||
value-to-inplace]
|
|
||||||
(clojure.walk/postwalk #(if (and (= (type value-to-partly-match) (type %))
|
(clojure.walk/postwalk #(if (and (= (type value-to-partly-match) (type %))
|
||||||
(re-matches (re-pattern (str value-to-partly-match ".*")) %))
|
(re-matches (re-pattern (str value-to-partly-match ".*")) %))
|
||||||
(str/replace % value-to-partly-match value-to-inplace) %)
|
(str/replace % value-to-partly-match value-to-inplace) %)
|
||||||
|
@ -97,11 +98,8 @@
|
||||||
(defmethod yaml/load-as-edn :website [resource-name]
|
(defmethod yaml/load-as-edn :website [resource-name]
|
||||||
(yaml/from-string (yaml/load-resource resource-name))))
|
(yaml/from-string (yaml/load-resource resource-name))))
|
||||||
|
|
||||||
; TODO: gec 2022/10/28: The specs for config in the following functions are not correct,
|
|
||||||
; since config is the result of "flatten-and-reduce-config".
|
|
||||||
; Use correct specs!
|
|
||||||
(defn-spec generate-website-http-ingress pred/map-or-seq?
|
(defn-spec generate-website-http-ingress pred/map-or-seq?
|
||||||
[config websitedata?]
|
[config flattened-and-reduced-config?]
|
||||||
(let [{:keys [unique-name fqdns]} config]
|
(let [{:keys [unique-name fqdns]} config]
|
||||||
(ing/generate-http-ingress {:fqdns fqdns
|
(ing/generate-http-ingress {:fqdns fqdns
|
||||||
:ingress-name (generate-http-ingress-name unique-name)
|
:ingress-name (generate-http-ingress-name unique-name)
|
||||||
|
@ -109,7 +107,7 @@
|
||||||
:service-port 80})))
|
:service-port 80})))
|
||||||
|
|
||||||
(defn-spec generate-website-https-ingress pred/map-or-seq?
|
(defn-spec generate-website-https-ingress pred/map-or-seq?
|
||||||
[config websitedata?]
|
[config flattened-and-reduced-config?]
|
||||||
(let [{:keys [unique-name fqdns]} config]
|
(let [{:keys [unique-name fqdns]} config]
|
||||||
(ing/generate-https-ingress {:fqdns fqdns
|
(ing/generate-https-ingress {:fqdns fqdns
|
||||||
:cert-name (generate-cert-name unique-name)
|
:cert-name (generate-cert-name unique-name)
|
||||||
|
@ -118,7 +116,7 @@
|
||||||
:service-port 80})))
|
:service-port 80})))
|
||||||
|
|
||||||
(defn-spec generate-website-certificate pred/map-or-seq?
|
(defn-spec generate-website-certificate pred/map-or-seq?
|
||||||
[config websitedata?]
|
[config flattened-and-reduced-config?]
|
||||||
(let [{:keys [unique-name issuer fqdns]
|
(let [{:keys [unique-name issuer fqdns]
|
||||||
:or {issuer "staging"}} config]
|
:or {issuer "staging"}} config]
|
||||||
(ing/generate-certificate {:fqdns fqdns
|
(ing/generate-certificate {:fqdns fqdns
|
||||||
|
@ -126,7 +124,7 @@
|
||||||
:issuer issuer})))
|
:issuer issuer})))
|
||||||
|
|
||||||
(defn-spec generate-nginx-configmap pred/map-or-seq?
|
(defn-spec generate-nginx-configmap pred/map-or-seq?
|
||||||
[config websitedata?]
|
[config flattened-and-reduced-config?]
|
||||||
(let [{:keys [unique-name fqdns]} config]
|
(let [{:keys [unique-name fqdns]} config]
|
||||||
(->
|
(->
|
||||||
(yaml/load-as-edn "website/nginx-configmap.yaml")
|
(yaml/load-as-edn "website/nginx-configmap.yaml")
|
||||||
|
@ -137,21 +135,21 @@
|
||||||
(-> % :data :website.conf) #"FQDN" (str (str/join " " fqdns) ";")))))))
|
(-> % :data :website.conf) #"FQDN" (str (str/join " " fqdns) ";")))))))
|
||||||
|
|
||||||
(defn-spec generate-nginx-deployment pred/map-or-seq?
|
(defn-spec generate-nginx-deployment pred/map-or-seq?
|
||||||
[config websitedata?]
|
[config flattened-and-reduced-config?]
|
||||||
(let [{:keys [unique-name]} config]
|
(let [{:keys [unique-name]} config]
|
||||||
(->
|
(->
|
||||||
(yaml/load-as-edn "website/nginx-deployment.yaml")
|
(yaml/load-as-edn "website/nginx-deployment.yaml")
|
||||||
(replace-all-matching-subvalues-in-string-start "NAME" (replace-dots-by-minus unique-name)))))
|
(replace-all-matching-subvalues-in-string-start "NAME" (replace-dots-by-minus unique-name)))))
|
||||||
|
|
||||||
(defn-spec generate-nginx-service pred/map-or-seq?
|
(defn-spec generate-nginx-service pred/map-or-seq?
|
||||||
[config websitedata?]
|
[config flattened-and-reduced-config?]
|
||||||
(let [{:keys [unique-name]} config]
|
(let [{:keys [unique-name]} config]
|
||||||
(->
|
(->
|
||||||
(yaml/load-as-edn "website/nginx-service.yaml")
|
(yaml/load-as-edn "website/nginx-service.yaml")
|
||||||
(replace-all-matching-subvalues-in-string-start "NAME" (replace-dots-by-minus unique-name)))))
|
(replace-all-matching-subvalues-in-string-start "NAME" (replace-dots-by-minus unique-name)))))
|
||||||
|
|
||||||
(defn-spec generate-website-content-volume pred/map-or-seq?
|
(defn-spec generate-website-content-volume pred/map-or-seq?
|
||||||
[config websitedata?]
|
[config flattened-and-reduced-config?]
|
||||||
(let [{:keys [unique-name volume-size]
|
(let [{:keys [unique-name volume-size]
|
||||||
:or {volume-size "3"}} config]
|
:or {volume-size "3"}} config]
|
||||||
(->
|
(->
|
||||||
|
@ -160,22 +158,21 @@
|
||||||
(cm/replace-all-matching-values-by-new-value "WEBSITESTORAGESIZE" (str volume-size "Gi")))))
|
(cm/replace-all-matching-values-by-new-value "WEBSITESTORAGESIZE" (str volume-size "Gi")))))
|
||||||
|
|
||||||
(defn-spec generate-website-build-cron pred/map-or-seq?
|
(defn-spec generate-website-build-cron pred/map-or-seq?
|
||||||
[config websitedata?]
|
[config flattened-and-reduced-config?]
|
||||||
(let [{:keys [unique-name]} config]
|
(let [{:keys [unique-name]} config]
|
||||||
(->
|
(->
|
||||||
(yaml/load-as-edn "website/website-build-cron.yaml")
|
(yaml/load-as-edn "website/website-build-cron.yaml")
|
||||||
(replace-all-matching-subvalues-in-string-start "NAME" (replace-dots-by-minus unique-name)))))
|
(replace-all-matching-subvalues-in-string-start "NAME" (replace-dots-by-minus unique-name)))))
|
||||||
|
|
||||||
(defn-spec generate-website-build-deployment pred/map-or-seq?
|
(defn-spec generate-website-build-deployment pred/map-or-seq?
|
||||||
[config websitedata?]
|
[config flattened-and-reduced-config?]
|
||||||
(let [{:keys [unique-name]} config]
|
(let [{:keys [unique-name]} config]
|
||||||
(->
|
(->
|
||||||
(yaml/load-as-edn "website/website-build-deployment.yaml")
|
(yaml/load-as-edn "website/website-build-deployment.yaml")
|
||||||
(replace-all-matching-subvalues-in-string-start "NAME" (replace-dots-by-minus unique-name)))))
|
(replace-all-matching-subvalues-in-string-start "NAME" (replace-dots-by-minus unique-name)))))
|
||||||
|
|
||||||
; TODO: gec 2022/10/28: gitea-host, gitea-repo, branchname are not in "websiteauth?"
|
|
||||||
(defn-spec generate-website-build-secret pred/map-or-seq?
|
(defn-spec generate-website-build-secret pred/map-or-seq?
|
||||||
[auth websiteauth?]
|
[auth flattened-and-reduced-config?]
|
||||||
(let [{:keys [unique-name
|
(let [{:keys [unique-name
|
||||||
authtoken
|
authtoken
|
||||||
gitea-host
|
gitea-host
|
||||||
|
|
Loading…
Reference in a new issue