introduce namespace for postgres

This commit is contained in:
Michael Jerger 2024-02-20 18:05:33 +01:00
parent 674045eba3
commit 7e9c813e33
4 changed files with 111 additions and 38 deletions

View file

@ -3,6 +3,7 @@
[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.namespace :as ns]
[dda.c4k-common.postgres.postgres-internal :as int])) [dda.c4k-common.postgres.postgres-internal :as int]))
(def postgres-size? int/postgres-size?) (def postgres-size? int/postgres-size?)
@ -19,18 +20,19 @@
(def pg-config? (def pg-config?
(s/keys :opt-un [::postgres-size ::db-name ::postgres-data-volume-path (s/keys :opt-un [::postgres-size ::db-name ::postgres-data-volume-path
::pvc-storage-class-name ::pv-storage-size-gb])) ::pvc-storage-class-name ::pv-storage-size-gb ::ns/namespace]))
(def pg-auth? (def pg-auth?
(s/keys :opt-un [::postgres-db-user ::postgres-db-password])) (s/keys :opt-un [::postgres-db-user ::postgres-db-password]))
(def postgres-function (s/keys :opt-un [::deserializer ::optional])) (def postgres-function (s/keys :opt-un [::deserializer ::optional]))
(def default-config {:postgres-image "postgres:13" (def default-config (merge ns/default-config
{:postgres-image "postgres:13"
:postgres-size :2gb :postgres-size :2gb
:db-name "postgres" :db-name "postgres"
:postgres-data-volume-path "/var/postgres" :postgres-data-volume-path "/var/postgres"
:pv-storage-size-gb 10 :pv-storage-size-gb 10
:pvc-storage-class-name "manual"}) :pvc-storage-class-name "manual"}))
(defn-spec generate-config map? (defn-spec generate-config map?
@ -62,13 +64,16 @@
(defn-spec generate-secret map? (defn-spec generate-secret map?
[my-auth pg-auth?] [config pg-config?
(int/generate-secret my-auth)) auth pg-auth?]
(int/generate-secret config auth))
(defn-spec generate-service map? (defn-spec generate-service map?
[] [config pg-config?]
(int/generate-service)) (let [final-config (merge default-config
config)]
(int/generate-service final-config)))
(defn-spec generate seq? (defn-spec generate seq?
@ -76,9 +81,9 @@
auth pg-auth?] auth pg-auth?]
(let [final-config (merge default-config (let [final-config (merge default-config
config)] config)]
[(int/generate-secret auth) [(int/generate-secret final-config auth)
(int/generate-persistent-volume final-config) (int/generate-persistent-volume final-config)
(int/generate-config final-config) (int/generate-config final-config)
(int/generate-pvc final-config) (int/generate-pvc final-config)
(int/generate-deployment final-config) (int/generate-deployment final-config)
(int/generate-service)])) (int/generate-service final-config)]))

View file

@ -8,6 +8,7 @@
[dda.c4k-common.base64 :as b64] [dda.c4k-common.base64 :as b64]
[dda.c4k-common.predicate :as cp] [dda.c4k-common.predicate :as cp]
[dda.c4k-common.common :as cm] [dda.c4k-common.common :as cm]
[dda.c4k-common.namespace :as ns]
[dda.c4k-common.postgres.postgres-internal :as int])) [dda.c4k-common.postgres.postgres-internal :as int]))
(defn postgres-size? (defn postgres-size?
@ -28,7 +29,7 @@
(def pg-config? (def pg-config?
(s/keys :req-un [::postgres-size ::db-name ::postgres-data-volume-path (s/keys :req-un [::postgres-size ::db-name ::postgres-data-volume-path
::pvc-storage-class-name ::pv-storage-size-gb])) ::pvc-storage-class-name ::pv-storage-size-gb ::ns/namespace]))
(def pg-auth? (def pg-auth?
(s/keys :req-un [::postgres-db-user ::postgres-db-password])) (s/keys :req-un [::postgres-db-user ::postgres-db-password]))
@ -37,51 +38,61 @@
(defn-spec generate-config map? (defn-spec generate-config map?
[config pg-config?] [config pg-config?]
(let [{:keys [postgres-size db-name]} config] (let [{:keys [postgres-size db-name namespace]} config]
(-> (->
(yaml/from-string (yaml/load-resource (yaml/from-string (yaml/load-resource
(str "postgres/config-" (name postgres-size) ".yaml"))) (str "postgres/config-" (name postgres-size) ".yaml")))
(assoc-in [:metadata :namespace] namespace)
(assoc-in [:data :postgres-db] db-name)))) (assoc-in [:data :postgres-db] db-name))))
(defn-spec generate-deployment map? (defn-spec generate-deployment map?
[config pg-config?] [config pg-config?]
(let [{:keys [postgres-image]} config] (let [{:keys [postgres-image namespace]} config]
(-> (->
(yaml/from-string (yaml/load-resource "postgres/deployment.yaml")) (yaml/from-string (yaml/load-resource "postgres/deployment.yaml"))
(assoc-in [:metadata :namespace] namespace)
(assoc-in [:spec :template :spec :containers 0 :image] postgres-image)))) (assoc-in [:spec :template :spec :containers 0 :image] postgres-image))))
(defn-spec generate-persistent-volume map? (defn-spec generate-persistent-volume map?
[config pg-config?] [config pg-config?]
(let [{:keys [postgres-data-volume-path pv-storage-size-gb]} config] (let [{:keys [postgres-data-volume-path pv-storage-size-gb namespace]} config]
(-> (->
(yaml/from-string (yaml/load-resource "postgres/persistent-volume.yaml")) (yaml/from-string (yaml/load-resource "postgres/persistent-volume.yaml"))
(assoc-in [:metadata :namespace] namespace)
(assoc-in [:spec :hostPath :path] postgres-data-volume-path) (assoc-in [:spec :hostPath :path] postgres-data-volume-path)
(assoc-in [:spec :capacity :storage] (str pv-storage-size-gb "Gi"))))) (assoc-in [:spec :capacity :storage] (str pv-storage-size-gb "Gi")))))
(defn-spec generate-pvc map? (defn-spec generate-pvc map?
[config pg-config?] [config pg-config?]
(let [{:keys [pv-storage-size-gb pvc-storage-class-name]} config] (let [{:keys [pv-storage-size-gb pvc-storage-class-name namespace]} config]
(-> (->
(yaml/from-string (yaml/load-resource "postgres/pvc.yaml")) (yaml/from-string (yaml/load-resource "postgres/pvc.yaml"))
(assoc-in [:metadata :namespace] namespace)
(assoc-in [:spec :resources :requests :storage] (str pv-storage-size-gb "Gi")) (assoc-in [:spec :resources :requests :storage] (str pv-storage-size-gb "Gi"))
(assoc-in [:spec :storageClassName] (name pvc-storage-class-name))))) (assoc-in [:spec :storageClassName] (name pvc-storage-class-name)))))
(defn-spec generate-secret map? (defn-spec generate-secret map?
[my-auth pg-auth?] [config pg-config?
(let [{:keys [postgres-db-user postgres-db-password]} my-auth] auth pg-auth?]
(let [{:keys [namespace]} config
{:keys [postgres-db-user postgres-db-password]} auth]
(-> (->
(yaml/from-string (yaml/load-resource "postgres/secret.yaml")) (yaml/from-string (yaml/load-resource "postgres/secret.yaml"))
(assoc-in [:metadata :namespace] namespace)
(cm/replace-key-value :postgres-user (b64/encode postgres-db-user)) (cm/replace-key-value :postgres-user (b64/encode postgres-db-user))
(cm/replace-key-value :postgres-password (b64/encode postgres-db-password))))) (cm/replace-key-value :postgres-password (b64/encode postgres-db-password)))))
(defn-spec generate-service map? (defn-spec generate-service map?
[] [config pg-config?]
(yaml/from-string (yaml/load-resource "postgres/service.yaml"))) (let [{:keys [namespace]} config]
(->
(yaml/from-string (yaml/load-resource "postgres/service.yaml"))
(assoc-in [:metadata :namespace] namespace))))
#?(:cljs #?(:cljs

View file

@ -3,7 +3,6 @@
#?(:clj [clojure.test :refer [deftest is are testing run-tests]] #?(:clj [clojure.test :refer [deftest is are testing run-tests]]
:cljs [cljs.test :refer-macros [deftest is are testing run-tests]]) :cljs [cljs.test :refer-macros [deftest is are testing run-tests]])
[clojure.spec.test.alpha :as st] [clojure.spec.test.alpha :as st]
[dda.c4k-common.test-helper :as ct]
[dda.c4k-common.postgres.postgres-internal :as cut])) [dda.c4k-common.postgres.postgres-internal :as cut]))
(st/instrument `cut/generate-config) (st/instrument `cut/generate-config)
@ -14,6 +13,26 @@
(st/instrument `cut/generate-service) (st/instrument `cut/generate-service)
(deftest should-generate-config (deftest should-generate-config
(is (= {:name "postgres-config",
:namespace "default"
:labels {:app "postgres"}}
(:metadata (cut/generate-config {:postgres-image "postgres:13"
:postgres-size :2gb
:db-name "postgres"
:postgres-data-volume-path "/var/postgres"
:pv-storage-size-gb 10
:pvc-storage-class-name "manual"
:namespace "default"}))))
(is (= {:name "postgres-config",
:namespace "myapp"
:labels {:app "postgres"}}
(:metadata (cut/generate-config {:postgres-image "postgres:13"
:postgres-size :2gb
:db-name "postgres"
:postgres-data-volume-path "/var/postgres"
:pv-storage-size-gb 10
:pvc-storage-class-name "manual"
:namespace "myapp"}))))
(is (= {:postgres-db "postgres" (is (= {:postgres-db "postgres"
:postgresql.conf :postgresql.conf
"max_connections = 100\nwork_mem = 4MB\nshared_buffers = 512MB\n"} "max_connections = 100\nwork_mem = 4MB\nshared_buffers = 512MB\n"}
@ -22,7 +41,8 @@
:db-name "postgres" :db-name "postgres"
:postgres-data-volume-path "/var/postgres" :postgres-data-volume-path "/var/postgres"
:pv-storage-size-gb 10 :pv-storage-size-gb 10
:pvc-storage-class-name "manual"})))) :pvc-storage-class-name "manual"
:namespace "default"}))))
(is (= {:postgres-db "postgres" (is (= {:postgres-db "postgres"
:postgresql.conf :postgresql.conf
"max_connections = 700\nwork_mem = 3MB\nshared_buffers = 2048MB\n"} "max_connections = 700\nwork_mem = 3MB\nshared_buffers = 2048MB\n"}
@ -31,7 +51,8 @@
:db-name "postgres" :db-name "postgres"
:postgres-data-volume-path "/var/postgres" :postgres-data-volume-path "/var/postgres"
:pv-storage-size-gb 10 :pv-storage-size-gb 10
:pvc-storage-class-name "manual"})))) :pvc-storage-class-name "manual"
:namespace "default"}))))
(is (= {:postgres-db "test" (is (= {:postgres-db "test"
:postgresql.conf :postgresql.conf
"max_connections = 100\nwork_mem = 4MB\nshared_buffers = 512MB\n"} "max_connections = 100\nwork_mem = 4MB\nshared_buffers = 512MB\n"}
@ -40,7 +61,8 @@
:db-name "test" :db-name "test"
:postgres-data-volume-path "/var/postgres" :postgres-data-volume-path "/var/postgres"
:pv-storage-size-gb 10 :pv-storage-size-gb 10
:pvc-storage-class-name "manual"})))) :pvc-storage-class-name "manual"
:namespace "default"}))))
) )
(deftest should-generate-deployment (deftest should-generate-deployment
@ -72,8 +94,18 @@
:db-name "test" :db-name "test"
:postgres-data-volume-path "/var/postgres" :postgres-data-volume-path "/var/postgres"
:pv-storage-size-gb 10 :pv-storage-size-gb 10
:pvc-storage-class-name "manual"}) :pvc-storage-class-name "manual"
[:spec :template :spec :containers])))) :namespace "default"})
[:spec :template :spec :containers])))
(is (= {:name "postgresql",
:namespace "myapp"}
(:metadata (cut/generate-deployment {:postgres-image "postgres:14"
:postgres-size :2gb
:db-name "test"
:postgres-data-volume-path "/var/postgres"
:pv-storage-size-gb 10
:pvc-storage-class-name "manual"
:namespace "myapp"})))))
@ -81,7 +113,9 @@
(is (= {:kind "PersistentVolume" (is (= {:kind "PersistentVolume"
:apiVersion "v1" :apiVersion "v1"
:metadata :metadata
{:name "postgres-pv-volume", :labels {:type "local"}} {:name "postgres-pv-volume",
:namespace "default"
:labels {:type "local"}}
:spec :spec
{:storageClassName "manual" {:storageClassName "manual"
:accessModes ["ReadWriteOnce"] :accessModes ["ReadWriteOnce"]
@ -92,14 +126,17 @@
:db-name "test" :db-name "test"
:pvc-storage-class-name "manual" :pvc-storage-class-name "manual"
:postgres-data-volume-path "xx" :postgres-data-volume-path "xx"
:pv-storage-size-gb 20})))) :pv-storage-size-gb 20
:namespace "default"}))))
(deftest should-generate-persistent-volume-claim (deftest should-generate-persistent-volume-claim
(is (= {:apiVersion "v1" (is (= {:apiVersion "v1"
:kind "PersistentVolumeClaim" :kind "PersistentVolumeClaim"
:metadata :metadata
{:name "postgres-claim", :labels {:app "postgres"}} {:name "postgres-claim",
:namespace "default"
:labels {:app "postgres"}}
:spec :spec
{:storageClassName "local-path" {:storageClassName "local-path"
:accessModes ["ReadWriteOnce"] :accessModes ["ReadWriteOnce"]
@ -109,14 +146,35 @@
:db-name "postgres" :db-name "postgres"
:postgres-data-volume-path "/var/postgres" :postgres-data-volume-path "/var/postgres"
:pv-storage-size-gb 20 :pv-storage-size-gb 20
:pvc-storage-class-name "local-path"})))) :pvc-storage-class-name "local-path"
:namespace "default"}))))
(deftest should-generate-secret (deftest should-generate-secret
(is (= {:apiVersion "v1" (is (= {:apiVersion "v1"
:kind "Secret" :kind "Secret"
:metadata {:name "postgres-secret"} :metadata {:name "postgres-secret" :namespace "default"}
:type "Opaque" :type "Opaque"
:data :data
{:postgres-user "eHgtdXM=", :postgres-password "eHgtcHc="}} {:postgres-user "eHgtdXM=", :postgres-password "eHgtcHc="}}
(cut/generate-secret {:postgres-db-user "xx-us" :postgres-db-password "xx-pw"})))) (cut/generate-secret {:postgres-image "postgres:13"
:postgres-size :2gb
:db-name "postgres"
:postgres-data-volume-path "/var/postgres"
:pv-storage-size-gb 20
:pvc-storage-class-name "local-path"
:namespace "default"}
{:postgres-db-user "xx-us" :postgres-db-password "xx-pw"}))))
(deftest should-generate-service
(is (= {:name "postgresql-service" :namespace "default"}
(:metadata (cut/generate-service
{:postgres-image "postgres:13"
:postgres-size :2gb
:db-name "postgres"
:postgres-data-volume-path "/var/postgres"
:pv-storage-size-gb 20
:pvc-storage-class-name "local-path"
:namespace "default"})))))

View file

@ -3,12 +3,11 @@
#?(:clj [clojure.test :refer [deftest is are testing run-tests]] #?(:clj [clojure.test :refer [deftest is are testing run-tests]]
:cljs [cljs.test :refer-macros [deftest is are testing run-tests]]) :cljs [cljs.test :refer-macros [deftest is are testing run-tests]])
[clojure.spec.test.alpha :as st] [clojure.spec.test.alpha :as st]
[dda.c4k-common.test-helper :as ct]
[dda.c4k-common.postgres :as cut])) [dda.c4k-common.postgres :as cut]))
(st/instrument `cut/generate-config) (st/instrument `cut/generate-config)
(st/instrument `cut/generate-persistent-volume) (st/instrument `cut/generate-persistent-volume)
;;(st/instrument `cut/generate) (st/instrument `cut/generate)
(deftest should-generate-config (deftest should-generate-config
(is (= {:postgres-db "postgres" (is (= {:postgres-db "postgres"
@ -20,7 +19,7 @@
(is (= {:kind "PersistentVolume" (is (= {:kind "PersistentVolume"
:apiVersion "v1" :apiVersion "v1"
:metadata :metadata
{:name "postgres-pv-volume", :labels {:type "local"}} {:name "postgres-pv-volume", :namespace "default" :labels {:type "local"}}
:spec :spec
{:storageClassName "manual" {:storageClassName "manual"
:accessModes ["ReadWriteOnce"] :accessModes ["ReadWriteOnce"]
@ -33,7 +32,7 @@
(is (= {:apiVersion "v1" (is (= {:apiVersion "v1"
:kind "PersistentVolumeClaim" :kind "PersistentVolumeClaim"
:metadata :metadata
{:name "postgres-claim", :labels {:app "postgres"}} {:name "postgres-claim", :namespace "default" :labels {:app "postgres"}}
:spec :spec
{:storageClassName "manual" {:storageClassName "manual"
:accessModes ["ReadWriteOnce"] :accessModes ["ReadWriteOnce"]