seperate postgres & postgres-internal
parent
8626b91b16
commit
cca93311e4
@ -0,0 +1,89 @@
|
|||||||
|
(ns dda.c4k-common.postgres.postgres-internal
|
||||||
|
(:require
|
||||||
|
[clojure.spec.alpha :as s]
|
||||||
|
#?(:clj [orchestra.core :refer [defn-spec]]
|
||||||
|
:cljs [orchestra.core :refer-macros [defn-spec]])
|
||||||
|
#?(:cljs [dda.c4k-common.macros :refer-macros [inline-resources]])
|
||||||
|
[dda.c4k-common.yaml :as yaml]
|
||||||
|
[dda.c4k-common.base64 :as b64]
|
||||||
|
[dda.c4k-common.predicate :as cp]
|
||||||
|
[dda.c4k-common.common :as cm]
|
||||||
|
[dda.c4k-common.postgres.postgres-internal :as int]))
|
||||||
|
|
||||||
|
(defn postgres-size?
|
||||||
|
[input]
|
||||||
|
(contains? #{:2gb :4gb :8gb :16gb} input))
|
||||||
|
|
||||||
|
(defn postgres-image?
|
||||||
|
[input]
|
||||||
|
(contains? #{"postgres:13" "postgres:14" "postgres:15" "postgres:16"} input))
|
||||||
|
|
||||||
|
(s/def ::postgres-db-user cp/bash-env-string?)
|
||||||
|
(s/def ::postgres-db-password cp/bash-env-string?)
|
||||||
|
(s/def ::postgres-data-volume-path string?)
|
||||||
|
(s/def ::postgres-size postgres-size?)
|
||||||
|
(s/def ::db-name cp/bash-env-string?)
|
||||||
|
(s/def ::pvc-storage-class-name cp/pvc-storage-class-name?)
|
||||||
|
(s/def ::pv-storage-size-gb pos?)
|
||||||
|
|
||||||
|
(def pg-config?
|
||||||
|
(s/keys :req-un [::postgres-size ::db-name ::postgres-data-volume-path
|
||||||
|
::pvc-storage-class-name ::pv-storage-size-gb]))
|
||||||
|
(def pg-auth?
|
||||||
|
(s/keys :opt-un [::postgres-db-user ::postgres-db-password]))
|
||||||
|
|
||||||
|
(def postgres-function (s/keys :opt-un [::deserializer ::optional]))
|
||||||
|
|
||||||
|
|
||||||
|
(defn-spec generate-config map?
|
||||||
|
[config pg-config?]
|
||||||
|
(let [{:keys [postgres-size db-name]} config]
|
||||||
|
(->
|
||||||
|
(yaml/from-string (yaml/load-resource
|
||||||
|
(str "postgres/config-" (name postgres-size) ".yaml")))
|
||||||
|
(assoc-in [:data :postgres-db] db-name))))
|
||||||
|
|
||||||
|
|
||||||
|
(defn-spec generate-deployment map?
|
||||||
|
[config pg-config?]
|
||||||
|
(let [{:keys [postgres-image]} config]
|
||||||
|
(->
|
||||||
|
(yaml/from-string (yaml/load-resource "postgres/deployment.yaml"))
|
||||||
|
(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]
|
||||||
|
(->
|
||||||
|
(yaml/from-string (yaml/load-resource "postgres/persistent-volume.yaml"))
|
||||||
|
(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]
|
||||||
|
(->
|
||||||
|
(yaml/from-string (yaml/load-resource "postgres/pvc.yaml"))
|
||||||
|
(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]
|
||||||
|
(->
|
||||||
|
(yaml/from-string (yaml/load-resource "postgres/secret.yaml"))
|
||||||
|
(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")))
|
||||||
|
|
||||||
|
|
||||||
|
#?(:cljs
|
||||||
|
(defmethod yaml/load-resource :postgres [resource-name]
|
||||||
|
(get (inline-resources "postgres") resource-name)))
|
@ -0,0 +1,122 @@
|
|||||||
|
(ns dda.c4k-common.postgres.postgres-internal-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-common.test-helper :as ct]
|
||||||
|
[dda.c4k-common.postgres.postgres-internal :as cut]))
|
||||||
|
|
||||||
|
(st/instrument `cut/generate-config)
|
||||||
|
(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
|
||||||
|
(is (= {:postgres-db "postgres"
|
||||||
|
:postgresql.conf
|
||||||
|
"max_connections = 100\nwork_mem = 4MB\nshared_buffers = 512MB\n"}
|
||||||
|
(:data (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"}))))
|
||||||
|
(is (= {:postgres-db "postgres"
|
||||||
|
:postgresql.conf
|
||||||
|
"max_connections = 700\nwork_mem = 3MB\nshared_buffers = 2048MB\n"}
|
||||||
|
(:data (cut/generate-config {:postgres-image "postgres:13"
|
||||||
|
:postgres-size :8gb
|
||||||
|
:db-name "postgres"
|
||||||
|
:postgres-data-volume-path "/var/postgres"
|
||||||
|
:pv-storage-size-gb 10
|
||||||
|
:pvc-storage-class-name "manual"}))))
|
||||||
|
(is (= {:postgres-db "test"
|
||||||
|
:postgresql.conf
|
||||||
|
"max_connections = 100\nwork_mem = 4MB\nshared_buffers = 512MB\n"}
|
||||||
|
(:data (cut/generate-config {:postgres-image "postgres:13"
|
||||||
|
:postgres-size :2gb
|
||||||
|
:db-name "test"
|
||||||
|
:postgres-data-volume-path "/var/postgres"
|
||||||
|
:pv-storage-size-gb 10
|
||||||
|
:pvc-storage-class-name "manual"}))))
|
||||||
|
)
|
||||||
|
|
||||||
|
(deftest should-generate-deployment
|
||||||
|
(is (= [{:image "postgres:14"
|
||||||
|
:name "postgresql"
|
||||||
|
:env
|
||||||
|
[{:name "POSTGRES_USER"
|
||||||
|
:valueFrom
|
||||||
|
{:secretKeyRef
|
||||||
|
{:name "postgres-secret", :key "postgres-user"}}}
|
||||||
|
{:name "POSTGRES_PASSWORD"
|
||||||
|
:valueFrom
|
||||||
|
{:secretKeyRef
|
||||||
|
{:name "postgres-secret", :key "postgres-password"}}}
|
||||||
|
{:name "POSTGRES_DB"
|
||||||
|
:valueFrom
|
||||||
|
{:configMapKeyRef
|
||||||
|
{:name "postgres-config", :key "postgres-db"}}}]
|
||||||
|
:ports [{:containerPort 5432, :name "postgresql"}]
|
||||||
|
:volumeMounts
|
||||||
|
[{:name "postgres-config-volume"
|
||||||
|
:mountPath "/etc/postgresql/postgresql.conf"
|
||||||
|
:subPath "postgresql.conf"
|
||||||
|
:readOnly true}
|
||||||
|
{:name "postgre-data-volume"
|
||||||
|
:mountPath "/var/lib/postgresql/data"}]}]
|
||||||
|
(get-in (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"})
|
||||||
|
[:spec :template :spec :containers]))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(deftest should-generate-persistent-volume
|
||||||
|
(is (= {:kind "PersistentVolume"
|
||||||
|
:apiVersion "v1"
|
||||||
|
:metadata
|
||||||
|
{:name "postgres-pv-volume", :labels {:type "local"}}
|
||||||
|
:spec
|
||||||
|
{:storageClassName "manual"
|
||||||
|
:accessModes ["ReadWriteOnce"]
|
||||||
|
:capacity {:storage "20Gi"}
|
||||||
|
:hostPath {:path "xx"}}}
|
||||||
|
(cut/generate-persistent-volume {:postgres-image "postgres:14"
|
||||||
|
:postgres-size :2gb
|
||||||
|
:db-name "test"
|
||||||
|
:pvc-storage-class-name "manual"
|
||||||
|
:postgres-data-volume-path "xx"
|
||||||
|
:pv-storage-size-gb 20}))))
|
||||||
|
|
||||||
|
|
||||||
|
(deftest should-generate-persistent-volume-claim
|
||||||
|
(is (= {:apiVersion "v1"
|
||||||
|
:kind "PersistentVolumeClaim"
|
||||||
|
:metadata
|
||||||
|
{:name "postgres-claim", :labels {:app "postgres"}}
|
||||||
|
:spec
|
||||||
|
{:storageClassName "local-path"
|
||||||
|
:accessModes ["ReadWriteOnce"]
|
||||||
|
:resources {:requests {:storage "20Gi"}}}}
|
||||||
|
(cut/generate-pvc {: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"}))))
|
||||||
|
|
||||||
|
|
||||||
|
(deftest should-generate-secret
|
||||||
|
(is (= {:apiVersion "v1"
|
||||||
|
:kind "Secret"
|
||||||
|
:metadata {:name "postgres-secret"}
|
||||||
|
:type "Opaque"
|
||||||
|
:data
|
||||||
|
{:postgres-user "eHgtdXM=", :postgres-password "eHgtcHc="}}
|
||||||
|
(cut/generate-secret {:postgres-db-user "xx-us" :postgres-db-password "xx-pw"}))))
|
Loading…
Reference in New Issue