2021-07-09 14:21:17 +00:00
|
|
|
(ns dda.c4k-common.postgres
|
|
|
|
(:require
|
|
|
|
[clojure.spec.alpha :as s]
|
2021-07-09 14:53:38 +00:00
|
|
|
#?(:cljs [shadow.resource :as rc])
|
2021-11-05 14:16:04 +00:00
|
|
|
#?(:clj [orchestra.core :refer [defn-spec]]
|
|
|
|
:cljs [orchestra.core :refer-macros [defn-spec]])
|
2021-07-09 14:21:17 +00:00
|
|
|
[dda.c4k-common.yaml :as yaml]
|
|
|
|
[dda.c4k-common.base64 :as b64]
|
2021-11-05 13:11:20 +00:00
|
|
|
[dda.c4k-common.predicate :as cp]
|
2021-09-14 12:45:32 +00:00
|
|
|
[dda.c4k-common.common :as cm]))
|
2021-07-09 14:21:17 +00:00
|
|
|
|
2021-09-13 07:05:08 +00:00
|
|
|
(defn postgres-size?
|
|
|
|
[input]
|
|
|
|
(contains? #{:2gb :4gb :8gb :16gb} input))
|
|
|
|
|
2021-11-05 15:18:37 +00:00
|
|
|
(defn postgres-image?
|
|
|
|
[input]
|
|
|
|
(contains? #{"postgres:13" "postgres:14"} input))
|
|
|
|
|
2021-09-14 12:45:32 +00:00
|
|
|
(s/def ::postgres-db-user cp/bash-env-string?)
|
|
|
|
(s/def ::postgres-db-password cp/bash-env-string?)
|
2021-07-09 14:21:17 +00:00
|
|
|
(s/def ::postgres-data-volume-path string?)
|
2021-09-13 07:05:08 +00:00
|
|
|
(s/def ::postgres-size postgres-size?)
|
2021-10-01 13:46:06 +00:00
|
|
|
(s/def ::db-name cp/bash-env-string?)
|
2021-11-05 15:18:37 +00:00
|
|
|
(defn pg-config? [input]
|
|
|
|
(s/keys :un-opt [::postgres-size ::db-name ::postgres-data-volume-path]))
|
|
|
|
(defn pg-auth? [input]
|
|
|
|
(s/keys :un-opt [::postgres-db-user ::postgres-db-password]))
|
2021-07-09 14:21:17 +00:00
|
|
|
|
2021-11-05 14:16:04 +00:00
|
|
|
(def postgres-function (s/keys :opt-un [::deserializer ::optional]))
|
|
|
|
|
2021-07-09 14:53:38 +00:00
|
|
|
#?(:cljs
|
|
|
|
(defmethod yaml/load-resource :postgres [resource-name]
|
|
|
|
(case resource-name
|
2021-09-13 07:05:08 +00:00
|
|
|
"postgres/config-2gb.yaml" (rc/inline "postgres/config-2gb.yaml")
|
|
|
|
"postgres/config-4gb.yaml" (rc/inline "postgres/config-4gb.yaml")
|
|
|
|
"postgres/config-8gb.yaml" (rc/inline "postgres/config-8gb.yaml")
|
|
|
|
"postgres/config-16gb.yaml" (rc/inline "postgres/config-16gb.yaml")
|
2021-07-09 14:53:38 +00:00
|
|
|
"postgres/deployment.yaml" (rc/inline "postgres/deployment.yaml")
|
|
|
|
"postgres/persistent-volume.yaml" (rc/inline "postgres/persistent-volume.yaml")
|
|
|
|
"postgres/pvc.yaml" (rc/inline "postgres/pvc.yaml")
|
|
|
|
"postgres/secret.yaml" (rc/inline "postgres/secret.yaml")
|
|
|
|
"postgres/service.yaml" (rc/inline "postgres/service.yaml")
|
|
|
|
(throw (js/Error. "Undefined Resource!")))))
|
|
|
|
|
2021-11-05 15:18:37 +00:00
|
|
|
(defn-spec generate-config cp/map-or-seq?
|
|
|
|
[& args pg-config?]
|
2021-09-14 17:38:57 +00:00
|
|
|
(let [{:keys [postgres-size db-name]
|
|
|
|
:or {postgres-size :2gb
|
|
|
|
db-name "postgres"}} args]
|
|
|
|
(->
|
|
|
|
(yaml/from-string (yaml/load-resource (str "postgres/config-" (name postgres-size) ".yaml")))
|
|
|
|
(assoc-in [:data :postgres-db] db-name))))
|
2021-07-09 14:21:17 +00:00
|
|
|
|
2021-11-05 15:18:37 +00:00
|
|
|
(defn-spec generate-deployment cp/map-or-seq?
|
|
|
|
[& args postgres-image?]
|
2021-10-08 09:56:42 +00:00
|
|
|
(let [{:keys [postgres-image]
|
|
|
|
:or {postgres-image "postgres:13"}} args]
|
|
|
|
(->
|
|
|
|
(yaml/from-string (yaml/load-resource "postgres/deployment.yaml"))
|
|
|
|
(assoc-in [:spec :template :spec :containers 0 :image] postgres-image))))
|
2021-07-09 14:21:17 +00:00
|
|
|
|
2021-11-05 14:16:04 +00:00
|
|
|
(defn-spec generate-persistent-volume cp/map-or-seq?
|
2021-11-05 15:18:37 +00:00
|
|
|
[config pg-config?]
|
2021-07-09 14:21:17 +00:00
|
|
|
(let [{:keys [postgres-data-volume-path]} config]
|
|
|
|
(->
|
|
|
|
(yaml/from-string (yaml/load-resource "postgres/persistent-volume.yaml"))
|
|
|
|
(assoc-in [:spec :hostPath :path] postgres-data-volume-path))))
|
|
|
|
|
2021-11-05 15:18:37 +00:00
|
|
|
(defn-spec generate-pvc cp/map-or-seq?
|
|
|
|
[]
|
2021-07-09 14:21:17 +00:00
|
|
|
(yaml/from-string (yaml/load-resource "postgres/pvc.yaml")))
|
|
|
|
|
2021-11-05 14:16:04 +00:00
|
|
|
(defn-spec generate-secret cp/map-or-seq?
|
2021-11-05 15:18:37 +00:00
|
|
|
[my-auth pg-auth?]
|
2021-07-09 14:21:17 +00:00
|
|
|
(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)))))
|
|
|
|
|
2021-11-05 15:18:37 +00:00
|
|
|
(defn-spec generate-service cp/map-or-seq?
|
|
|
|
[]
|
2021-07-09 14:21:17 +00:00
|
|
|
(yaml/from-string (yaml/load-resource "postgres/service.yaml")))
|