Split for generation of config and auth objects #4

Merged
gec merged 9 commits from split-config-auth into main 2024-07-19 09:06:52 +00:00
15 changed files with 173 additions and 59 deletions

View file

@ -102,9 +102,11 @@
(expound/expound-str auth-spec? auth-edn {:print-specs? false})))))))))))
(defn -main [& cmd-args]
(main-common "c4k-common"
(main-cm
"c4k-common"
core/config?
core/auth?
core/config-defaults
core/k8s-objects
core/config-objects
core/auth-objects
cmd-args))

View file

@ -61,9 +61,9 @@
(let [resulting-config (merge config-defaults my-config)
both (or (and only-config only-auth) (and (not only-config) (not only-auth)))
res-vec (cond
both (concat-vec (config-objects resulting-config) (auth-objects my-auth))
only-config (config-objects my-config)
only-auth (auth-objects my-auth))]
both (concat-vec (config-objects resulting-config) (auth-objects resulting-config my-auth))
only-config (config-objects resulting-config)
only-auth (auth-objects resulting-config my-auth))]
(cs/join
"\n---\n"
res-vec)))

View file

@ -7,15 +7,22 @@
(def config-defaults {})
(def config? (s/keys :req-un []
(def config? (s/keys :req-un [::monitoring/mon-cfg]
:opt-un []))
(def auth? (s/keys :req-un []
(def auth? (s/keys :req-un [::monitoring/mon-auth]
:opt-un []))
(defn k8s-objects [config auth]
(defn config-objects [config]
(let []
(map yaml/to-string
(filter #(not (nil? %))
(cm/concat-vec
(monitoring/generate config auth))))))
(monitoring/generate-config))))))
(defn auth-objects [config auth]
(let []
(map yaml/to-string
(filter #(not (nil? %))
(cm/concat-vec
(monitoring/generate-auth (:mon-cfg config) (:mon-auth auth)))))))

View file

@ -21,7 +21,8 @@
(def filter-regex-string int/filter-regex-string)
(defn-spec generate seq?
(defn-spec ^{:deprecated "6.4.1"} generate seq?
"use generate-config and generate-auth instead"
[config ::mon-cfg
auth ::mon-auth]
[(yaml/load-as-edn "monitoring/namespace.yaml")
@ -29,7 +30,7 @@
(yaml/load-as-edn "monitoring/prometheus-cluster-role-binding.yaml")
(yaml/load-as-edn "monitoring/prometheus-service.yaml")
(yaml/load-as-edn "monitoring/prometheus-service-account.yaml")
(int/generate-config config auth)
(int/generate-config-secret config auth)
(yaml/load-as-edn "monitoring/prometheus-deployment.yaml")
(yaml/load-as-edn "monitoring/node-exporter-service-account.yaml")
(yaml/load-as-edn "monitoring/node-exporter-cluster-role.yaml")
@ -41,3 +42,27 @@
(yaml/load-as-edn "monitoring/kube-state-metrics-deployment.yaml")
(yaml/load-as-edn "monitoring/kube-state-metrics-service-account.yaml")
(yaml/load-as-edn "monitoring/kube-state-metrics-service.yaml")])
(defn-spec generate-config seq?
[]
[(yaml/load-as-edn "monitoring/namespace.yaml")
(yaml/load-as-edn "monitoring/prometheus-cluster-role.yaml")
(yaml/load-as-edn "monitoring/prometheus-cluster-role-binding.yaml")
(yaml/load-as-edn "monitoring/prometheus-service.yaml")
(yaml/load-as-edn "monitoring/prometheus-service-account.yaml")
(yaml/load-as-edn "monitoring/prometheus-deployment.yaml")
(yaml/load-as-edn "monitoring/node-exporter-service-account.yaml")
(yaml/load-as-edn "monitoring/node-exporter-cluster-role.yaml")
(yaml/load-as-edn "monitoring/node-exporter-cluster-role-binding.yaml")
(yaml/load-as-edn "monitoring/node-exporter-daemon-set.yaml")
(yaml/load-as-edn "monitoring/node-exporter-service.yaml")
(yaml/load-as-edn "monitoring/kube-state-metrics-cluster-role-binding.yaml")
(yaml/load-as-edn "monitoring/kube-state-metrics-cluster-role.yaml")
(yaml/load-as-edn "monitoring/kube-state-metrics-deployment.yaml")
(yaml/load-as-edn "monitoring/kube-state-metrics-service-account.yaml")
(yaml/load-as-edn "monitoring/kube-state-metrics-service.yaml")])
(defn-spec generate-auth seq?
[config ::mon-cfg
auth ::mon-auth]
[(int/generate-config-secret config auth)])

View file

@ -26,7 +26,7 @@
"monitoring/node-exporter-service.yaml" (rc/inline "monitoring/node-exporter-service.yaml")
"monitoring/prometheus-cluster-role-binding.yaml" (rc/inline "monitoring/prometheus-cluster-role-binding.yaml")
"monitoring/prometheus-cluster-role.yaml" (rc/inline "monitoring/prometheus-cluster-role.yaml")
"monitoring/prometheus-config.yaml" (rc/inline "monitoring/prometheus-config.yaml")
"monitoring/prometheus-config-secret.yaml" (rc/inline "monitoring/prometheus-config-secret.yaml")
"monitoring/prometheus-deployment.yaml" (rc/inline "monitoring/prometheus-deployment.yaml")
"monitoring/prometheus-prometheus.yaml" (rc/inline "monitoring/prometheus-prometheus.yaml")
"monitoring/prometheus-service-account.yaml" (rc/inline "monitoring/prometheus-service-account.yaml")
@ -82,11 +82,21 @@
grafana-cloud-password)
(cm/replace-all-matching "FILTER_REGEX" filter-regex-string))))
(defn-spec generate-config map?
(defn-spec generate-config-secret map?
[config ::mon-cfg
auth ::mon-auth]
(->
(yaml/load-as-edn "monitoring/prometheus-config.yaml")
(yaml/load-as-edn "monitoring/prometheus-config-secret.yaml")
(assoc-in [:stringData :prometheus.yaml]
(yaml/to-string
(generate-prometheus-config config auth)))))
(defn-spec ^{:deprecated "6.4.1"} generate-config map?
"Use generate-config-secret instead"
[config ::mon-cfg
auth ::mon-auth]
(->
(yaml/load-as-edn "monitoring/prometheus-config-secret.yaml")
(assoc-in [:stringData :prometheus.yaml]
(yaml/to-string
(generate-prometheus-config config auth)))))

View file

@ -34,12 +34,11 @@
:pv-storage-size-gb 10
:pvc-storage-class-name "manual"}))
(defn-spec generate-config map?
(defn-spec generate-configmap map?
[& config (s/? pg-config?)]
(let [final-config (merge default-config
(first config))]
(int/generate-config final-config)))
(int/generate-configmap final-config)))
(defn-spec generate-deployment map?
@ -81,14 +80,32 @@
(int/generate-service final-config)))
(defn-spec generate seq?
(defn-spec ^{:deprecated "6.4.1"} generate seq?
"use generate-config and generate-auth instead"
[config pg-config?
auth pg-auth?]
(let [final-config (merge default-config
config)]
[(int/generate-secret final-config auth)
(int/generate-persistent-volume final-config)
(int/generate-config final-config)
(int/generate-configmap final-config)
(int/generate-pvc final-config)
(int/generate-deployment final-config)
(int/generate-service final-config)]))
(defn-spec generate-config seq?
[config pg-config?]
(let [final-config (merge default-config
config)]
[(int/generate-persistent-volume final-config)
(int/generate-configmap final-config)
(int/generate-pvc final-config)
(int/generate-deployment final-config)
(int/generate-service final-config)]))
(defn-spec generate-auth seq?
[config pg-config?
auth pg-auth?]
(let [final-config (merge default-config
config)]
[(int/generate-secret final-config auth)]))

View file

@ -52,7 +52,17 @@
(def postgres-function (s/keys :opt-un [::deserializer ::optional]))
(defn-spec generate-config map?
(defn-spec generate-configmap map?
[config pg-config?]
(let [{:keys [postgres-size db-name namespace]} config]
(->
(yaml/from-string (yaml/load-resource
(str "postgres/config-" (name postgres-size) ".yaml")))
(assoc-in [:metadata :namespace] namespace)
(assoc-in [:data :postgres-db] db-name))))
(defn-spec ^{:deprecated "6.4.1"} generate-config map?
"use generate-configmap instead"
[config pg-config?]
(let [{:keys [postgres-size db-name namespace]} config]
(->

View file

@ -0,0 +1,22 @@
(ns dda.c4k-common.core-test
(:require
#?(:cljs [shadow.resource :as rc])
#?(:clj [clojure.test :refer [deftest is are testing run-tests]]
:cljs [cljs.test :refer-macros [deftest is are testing run-tests]])
[clojure.spec.alpha :as s]
[dda.c4k-common.yaml :as yaml]
[dda.c4k-common.core :as cut]))
#?(:cljs
(defmethod yaml/load-resource :common-test [resource-name]
(case resource-name
"common-test/valid-auth.yaml" (rc/inline "common-test/valid-auth.yaml")
"common-test/valid-config.yaml" (rc/inline "common-test/valid-config.yaml")
(throw (js/Error. "Undefined Resource!")))))
(def conf (yaml/load-as-edn "common-test/valid-config.yaml"))
(def auth (yaml/load-as-edn "common-test/valid-auth.yaml"))
(deftest validate-valid-resources
(is (s/valid? cut/config? conf))
(is (s/valid? cut/auth? auth)))

View file

@ -8,7 +8,7 @@
(st/instrument `cut/generate-stateful-set)
(st/instrument `cut/generate-agent-config)
(st/instrument `cut/generate-config)
(st/instrument `cut/generate-config-secret)
(def conf {:cluster-name "clustername"
:cluster-stage "test"
@ -29,12 +29,12 @@
(deftest should-not-generate-config
(is (thrown?
#?(:clj Exception :cljs js/Error)
(cut/generate-config invalid-conf auth))))
(cut/generate-config-secret invalid-conf auth))))
(deftest should-not-generate-auth
(is (thrown?
#?(:clj Exception :cljs js/Error)
(cut/generate-config conf invalid-auth))))
(cut/generate-config-secret conf invalid-auth))))
(deftest should-generate-prometheus-remote-write-auth
@ -54,6 +54,6 @@
(deftest should-generate-config
(is (str/starts-with?
(get-in
(cut/generate-config conf auth)
(cut/generate-config-secret conf auth)
[:stringData :prometheus.yaml])
"global:\n scrape_interval:")))

View file

@ -6,6 +6,8 @@
[dda.c4k-common.monitoring :as cut]))
(st/instrument `cut/generate)
(st/instrument `cut/generate-config)
(st/instrument `cut/generate-auth)
(def conf {:cluster-name "clustername"
:cluster-stage "test"
@ -18,4 +20,8 @@
(deftest should-generate
(is (= 17
(count (cut/generate conf auth)))))
(count (cut/generate conf auth))))
(is (= 16
(count (cut/generate-config))))
(is (= 1
(count (cut/generate-auth conf auth)))))

View file

@ -5,18 +5,18 @@
[clojure.spec.test.alpha :as st]
[dda.c4k-common.postgres.postgres-internal :as cut]))
(st/instrument `cut/generate-config)
(st/instrument `cut/generate-configmap)
(st/instrument `cut/generate-deployment)
(st/instrument `cut/generate-persistent-volume)
(st/instrument `cut/generate-pvc)
(st/instrument `cut/generate-secret)
(st/instrument `cut/generate-service)
(deftest should-generate-config
(deftest should-generate-configmap
(is (= {:name "postgres-config",
:namespace "default"
:labels {:app "postgres"}}
(:metadata (cut/generate-config {:postgres-image "postgres:13"
(:metadata (cut/generate-configmap {:postgres-image "postgres:13"
:postgres-size :2gb
:db-name "postgres"
:postgres-data-volume-path "/var/postgres"
@ -26,7 +26,7 @@
(is (= {:name "postgres-config",
:namespace "myapp"
:labels {:app "postgres"}}
(:metadata (cut/generate-config {:postgres-image "postgres:13"
(:metadata (cut/generate-configmap {:postgres-image "postgres:13"
:postgres-size :2gb
:db-name "postgres"
:postgres-data-volume-path "/var/postgres"
@ -36,7 +36,7 @@
(is (= {:postgres-db "postgres"
:postgresql.conf
"max_connections = 100\nwork_mem = 4MB\nshared_buffers = 512MB\n"}
(:data (cut/generate-config {:postgres-image "postgres:13"
(:data (cut/generate-configmap {:postgres-image "postgres:13"
:postgres-size :2gb
:db-name "postgres"
:postgres-data-volume-path "/var/postgres"
@ -46,7 +46,7 @@
(is (= {:postgres-db "postgres"
:postgresql.conf
"max_connections = 700\nwork_mem = 3MB\nshared_buffers = 2048MB\n"}
(:data (cut/generate-config {:postgres-image "postgres:13"
(:data (cut/generate-configmap {:postgres-image "postgres:13"
:postgres-size :8gb
:db-name "postgres"
:postgres-data-volume-path "/var/postgres"
@ -56,7 +56,7 @@
(is (= {:postgres-db "test"
:postgresql.conf
"max_connections = 100\nwork_mem = 4MB\nshared_buffers = 512MB\n"}
(:data (cut/generate-config {:postgres-image "postgres:13"
(:data (cut/generate-configmap {:postgres-image "postgres:13"
:postgres-size :2gb
:db-name "test"
:postgres-data-volume-path "/var/postgres"

View file

@ -5,16 +5,18 @@
[clojure.spec.test.alpha :as st]
[dda.c4k-common.postgres :as cut]))
(st/instrument `cut/generate-config)
(st/instrument `cut/generate-configmap)
(st/instrument `cut/generate-persistent-volume)
(st/instrument `cut/generate-secret)
(st/instrument `cut/generate)
(st/instrument `cut/generate-config)
(st/instrument `cut/generate-auth)
(deftest should-generate-config
(deftest should-generate-configmap
(is (= {:postgres-db "postgres"
:postgresql.conf
"max_connections = 100\nwork_mem = 4MB\nshared_buffers = 512MB\n"}
(:data (cut/generate-config)))))
(:data (cut/generate-configmap)))))
(deftest should-generate-persistent-volume
(is (= {:kind "PersistentVolume"
@ -53,5 +55,11 @@
(deftest should-generate
(is (= 6
(count (cut/generate {}
{:postgres-db-user "user"
:postgres-db-password "password"}))))
(is (= 5
(count (cut/generate-config {}))))
(is (= 1
(count (cut/generate-auth {}
{:postgres-db-user "user"
:postgres-db-password "password"})))))

View file

@ -0,0 +1,3 @@
mon-auth:
grafana-cloud-user: "user"
grafana-cloud-password: "password"

View file

@ -0,0 +1,4 @@
mon-cfg:
grafana-cloud-url: "url-for-your-prom-remote-write-endpoint"
cluster-name: "forgejo"
cluster-stage: "test"