introduce namespace for postgres
This commit is contained in:
parent
674045eba3
commit
7e9c813e33
4 changed files with 111 additions and 38 deletions
|
@ -3,6 +3,7 @@
|
|||
[clojure.spec.alpha :as s]
|
||||
#?(:clj [orchestra.core :refer [defn-spec]]
|
||||
:cljs [orchestra.core :refer-macros [defn-spec]])
|
||||
[dda.c4k-common.namespace :as ns]
|
||||
[dda.c4k-common.postgres.postgres-internal :as int]))
|
||||
|
||||
(def postgres-size? int/postgres-size?)
|
||||
|
@ -19,18 +20,19 @@
|
|||
|
||||
(def pg-config?
|
||||
(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?
|
||||
(s/keys :opt-un [::postgres-db-user ::postgres-db-password]))
|
||||
|
||||
(def postgres-function (s/keys :opt-un [::deserializer ::optional]))
|
||||
|
||||
(def default-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"})
|
||||
(def default-config (merge ns/default-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"}))
|
||||
|
||||
|
||||
(defn-spec generate-config map?
|
||||
|
@ -62,13 +64,16 @@
|
|||
|
||||
|
||||
(defn-spec generate-secret map?
|
||||
[my-auth pg-auth?]
|
||||
(int/generate-secret my-auth))
|
||||
[config pg-config?
|
||||
auth pg-auth?]
|
||||
(int/generate-secret config auth))
|
||||
|
||||
|
||||
(defn-spec generate-service map?
|
||||
[]
|
||||
(int/generate-service))
|
||||
[config pg-config?]
|
||||
(let [final-config (merge default-config
|
||||
config)]
|
||||
(int/generate-service final-config)))
|
||||
|
||||
|
||||
(defn-spec generate seq?
|
||||
|
@ -76,9 +81,9 @@
|
|||
auth pg-auth?]
|
||||
(let [final-config (merge default-config
|
||||
config)]
|
||||
[(int/generate-secret auth)
|
||||
[(int/generate-secret final-config auth)
|
||||
(int/generate-persistent-volume final-config)
|
||||
(int/generate-config final-config)
|
||||
(int/generate-pvc final-config)
|
||||
(int/generate-deployment final-config)
|
||||
(int/generate-service)]))
|
||||
(int/generate-service final-config)]))
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
[dda.c4k-common.base64 :as b64]
|
||||
[dda.c4k-common.predicate :as cp]
|
||||
[dda.c4k-common.common :as cm]
|
||||
[dda.c4k-common.namespace :as ns]
|
||||
[dda.c4k-common.postgres.postgres-internal :as int]))
|
||||
|
||||
(defn postgres-size?
|
||||
|
@ -28,7 +29,7 @@
|
|||
|
||||
(def pg-config?
|
||||
(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?
|
||||
(s/keys :req-un [::postgres-db-user ::postgres-db-password]))
|
||||
|
||||
|
@ -37,51 +38,61 @@
|
|||
|
||||
(defn-spec generate-config map?
|
||||
[config pg-config?]
|
||||
(let [{:keys [postgres-size db-name]} 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 generate-deployment map?
|
||||
[config pg-config?]
|
||||
(let [{:keys [postgres-image]} config]
|
||||
(let [{:keys [postgres-image namespace]} config]
|
||||
(->
|
||||
(yaml/from-string (yaml/load-resource "postgres/deployment.yaml"))
|
||||
(assoc-in [:metadata :namespace] namespace)
|
||||
(assoc-in [:spec :template :spec :containers 0 :image] postgres-image))))
|
||||
|
||||
|
||||
(defn-spec generate-persistent-volume map?
|
||||
[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"))
|
||||
(assoc-in [:metadata :namespace] namespace)
|
||||
(assoc-in [:spec :hostPath :path] postgres-data-volume-path)
|
||||
(assoc-in [:spec :capacity :storage] (str pv-storage-size-gb "Gi")))))
|
||||
|
||||
|
||||
(defn-spec generate-pvc map?
|
||||
[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"))
|
||||
(assoc-in [:metadata :namespace] namespace)
|
||||
(assoc-in [:spec :resources :requests :storage] (str pv-storage-size-gb "Gi"))
|
||||
(assoc-in [:spec :storageClassName] (name pvc-storage-class-name)))))
|
||||
|
||||
|
||||
(defn-spec generate-secret map?
|
||||
[my-auth pg-auth?]
|
||||
(let [{:keys [postgres-db-user postgres-db-password]} my-auth]
|
||||
[config pg-config?
|
||||
auth pg-auth?]
|
||||
(let [{:keys [namespace]} config
|
||||
{:keys [postgres-db-user postgres-db-password]} auth]
|
||||
(->
|
||||
(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-password (b64/encode postgres-db-password)))))
|
||||
|
||||
|
||||
(defn-spec generate-service map?
|
||||
[]
|
||||
(yaml/from-string (yaml/load-resource "postgres/service.yaml")))
|
||||
[config pg-config?]
|
||||
(let [{:keys [namespace]} config]
|
||||
(->
|
||||
(yaml/from-string (yaml/load-resource "postgres/service.yaml"))
|
||||
(assoc-in [:metadata :namespace] namespace))))
|
||||
|
||||
|
||||
#?(:cljs
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
#?(: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-common.test-helper :as ct]
|
||||
[dda.c4k-common.postgres.postgres-internal :as cut]))
|
||||
|
||||
(st/instrument `cut/generate-config)
|
||||
|
@ -14,6 +13,26 @@
|
|||
(st/instrument `cut/generate-service)
|
||||
|
||||
(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"
|
||||
:postgresql.conf
|
||||
"max_connections = 100\nwork_mem = 4MB\nshared_buffers = 512MB\n"}
|
||||
|
@ -22,7 +41,8 @@
|
|||
:db-name "postgres"
|
||||
:postgres-data-volume-path "/var/postgres"
|
||||
:pv-storage-size-gb 10
|
||||
:pvc-storage-class-name "manual"}))))
|
||||
:pvc-storage-class-name "manual"
|
||||
:namespace "default"}))))
|
||||
(is (= {:postgres-db "postgres"
|
||||
:postgresql.conf
|
||||
"max_connections = 700\nwork_mem = 3MB\nshared_buffers = 2048MB\n"}
|
||||
|
@ -31,7 +51,8 @@
|
|||
:db-name "postgres"
|
||||
:postgres-data-volume-path "/var/postgres"
|
||||
:pv-storage-size-gb 10
|
||||
:pvc-storage-class-name "manual"}))))
|
||||
:pvc-storage-class-name "manual"
|
||||
:namespace "default"}))))
|
||||
(is (= {:postgres-db "test"
|
||||
:postgresql.conf
|
||||
"max_connections = 100\nwork_mem = 4MB\nshared_buffers = 512MB\n"}
|
||||
|
@ -40,7 +61,8 @@
|
|||
:db-name "test"
|
||||
:postgres-data-volume-path "/var/postgres"
|
||||
:pv-storage-size-gb 10
|
||||
:pvc-storage-class-name "manual"}))))
|
||||
:pvc-storage-class-name "manual"
|
||||
:namespace "default"}))))
|
||||
)
|
||||
|
||||
(deftest should-generate-deployment
|
||||
|
@ -72,8 +94,18 @@
|
|||
:db-name "test"
|
||||
:postgres-data-volume-path "/var/postgres"
|
||||
:pv-storage-size-gb 10
|
||||
:pvc-storage-class-name "manual"})
|
||||
[:spec :template :spec :containers]))))
|
||||
:pvc-storage-class-name "manual"
|
||||
: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"
|
||||
:apiVersion "v1"
|
||||
:metadata
|
||||
{:name "postgres-pv-volume", :labels {:type "local"}}
|
||||
{:name "postgres-pv-volume",
|
||||
:namespace "default"
|
||||
:labels {:type "local"}}
|
||||
:spec
|
||||
{:storageClassName "manual"
|
||||
:accessModes ["ReadWriteOnce"]
|
||||
|
@ -92,14 +126,17 @@
|
|||
:db-name "test"
|
||||
:pvc-storage-class-name "manual"
|
||||
:postgres-data-volume-path "xx"
|
||||
:pv-storage-size-gb 20}))))
|
||||
:pv-storage-size-gb 20
|
||||
:namespace "default"}))))
|
||||
|
||||
|
||||
(deftest should-generate-persistent-volume-claim
|
||||
(is (= {:apiVersion "v1"
|
||||
:kind "PersistentVolumeClaim"
|
||||
:metadata
|
||||
{:name "postgres-claim", :labels {:app "postgres"}}
|
||||
{:name "postgres-claim",
|
||||
:namespace "default"
|
||||
:labels {:app "postgres"}}
|
||||
:spec
|
||||
{:storageClassName "local-path"
|
||||
:accessModes ["ReadWriteOnce"]
|
||||
|
@ -109,14 +146,35 @@
|
|||
:db-name "postgres"
|
||||
:postgres-data-volume-path "/var/postgres"
|
||||
:pv-storage-size-gb 20
|
||||
:pvc-storage-class-name "local-path"}))))
|
||||
:pvc-storage-class-name "local-path"
|
||||
:namespace "default"}))))
|
||||
|
||||
|
||||
(deftest should-generate-secret
|
||||
(is (= {:apiVersion "v1"
|
||||
:kind "Secret"
|
||||
:metadata {:name "postgres-secret"}
|
||||
:metadata {:name "postgres-secret" :namespace "default"}
|
||||
:type "Opaque"
|
||||
:data
|
||||
{: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"})))))
|
||||
|
||||
|
|
|
@ -3,12 +3,11 @@
|
|||
#?(: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-common.test-helper :as ct]
|
||||
[dda.c4k-common.postgres :as cut]))
|
||||
|
||||
(st/instrument `cut/generate-config)
|
||||
(st/instrument `cut/generate-persistent-volume)
|
||||
;;(st/instrument `cut/generate)
|
||||
(st/instrument `cut/generate)
|
||||
|
||||
(deftest should-generate-config
|
||||
(is (= {:postgres-db "postgres"
|
||||
|
@ -20,7 +19,7 @@
|
|||
(is (= {:kind "PersistentVolume"
|
||||
:apiVersion "v1"
|
||||
:metadata
|
||||
{:name "postgres-pv-volume", :labels {:type "local"}}
|
||||
{:name "postgres-pv-volume", :namespace "default" :labels {:type "local"}}
|
||||
:spec
|
||||
{:storageClassName "manual"
|
||||
:accessModes ["ReadWriteOnce"]
|
||||
|
@ -33,7 +32,7 @@
|
|||
(is (= {:apiVersion "v1"
|
||||
:kind "PersistentVolumeClaim"
|
||||
:metadata
|
||||
{:name "postgres-claim", :labels {:app "postgres"}}
|
||||
{:name "postgres-claim", :namespace "default" :labels {:app "postgres"}}
|
||||
:spec
|
||||
{:storageClassName "manual"
|
||||
:accessModes ["ReadWriteOnce"]
|
||||
|
|
Loading…
Reference in a new issue