Use common postgres

This commit is contained in:
bom 2023-01-20 13:51:21 +01:00
parent 2fc3b8f5a7
commit 337fa68592
7 changed files with 26 additions and 173 deletions

View file

@ -4,9 +4,11 @@
[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-keycloak.yaml :as yaml] [dda.c4k-common.yaml :as yaml]
[dda.c4k-keycloak.keycloak :as kc] [dda.c4k-common.postgres :as postgres]
[dda.c4k-keycloak.postgres :as pg])) [dda.c4k-keycloak.keycloak :as kc]))
(def default-storage-class :local-path)
(def config-defaults {:issuer :staging}) (def config-defaults {:issuer :staging})
@ -14,27 +16,28 @@
:opt-un [::kc/issuer])) :opt-un [::kc/issuer]))
(def auth? (s/keys :req-un [::kc/keycloak-admin-user ::kc/keycloak-admin-password (def auth? (s/keys :req-un [::kc/keycloak-admin-user ::kc/keycloak-admin-password
::pg/postgres-db-user ::pg/postgres-db-password])) ::postgres/postgres-db-user ::postgres/postgres-db-password]))
(defn-spec k8s-objects any?
[config (s/merge config? auth?)]
(into
[]
(concat [(yaml/to-string (postgres/generate-config {:postgres-size :2gb :db-name "keycloak"}))
(yaml/to-string (postgres/generate-secret config))
(yaml/to-string (postgres/generate-pvc {:pv-storage-size-gb 30
:pvc-storage-class-name default-storage-class}))
(yaml/to-string (postgres/generate-deployment :postgres-image "postgres:14"))
(yaml/to-string (postgres/generate-service))
(yaml/to-string (kc/generate-secret (:auth config)))
(yaml/to-string (kc/generate-certificate config))
(yaml/to-string (kc/generate-ingress config))
(yaml/to-string (kc/generate-service))
(yaml/to-string (kc/generate-deployment))])))
(defn-spec generate any? (defn-spec generate any?
[my-config config? [my-config config?
my-auth auth?] my-auth auth?]
(let [resulting-config (merge config-defaults my-config)] (let [resulting-config (merge config-defaults my-config my-auth)]
(cs/join "\n" (cs/join
[(yaml/to-string (pg/generate-config)) "\n---\n"
"---" (k8s-objects resulting-config))))
(yaml/to-string (pg/generate-secret my-auth))
"---"
(yaml/to-string (pg/generate-service))
"---"
(yaml/to-string (pg/generate-deployment))
"---"
(yaml/to-string (kc/generate-secret my-auth))
"---"
(yaml/to-string (kc/generate-certificate resulting-config))
"---"
(yaml/to-string (kc/generate-ingress resulting-config))
"---"
(yaml/to-string (kc/generate-service))
"---"
(yaml/to-string (kc/generate-deployment))])))

View file

@ -1,25 +0,0 @@
(ns dda.c4k-keycloak.postgres
(:require
[clojure.spec.alpha :as s]
[dda.c4k-keycloak.yaml :as yaml]
[dda.c4k-common.base64 :as b64]
[dda.c4k-common.common :as cm]))
(s/def ::postgres-db-user cm/bash-env-string?)
(s/def ::postgres-db-password cm/bash-env-string?)
(defn generate-config []
(yaml/from-string (yaml/load-resource "postgres/config.yaml")))
(defn generate-secret [my-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 generate-deployment []
(yaml/from-string (yaml/load-resource "postgres/deployment.yaml")))
(defn generate-service []
(yaml/from-string (yaml/load-resource "postgres/service.yaml")))

View file

@ -1,11 +0,0 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-config
labels:
app: postgres
data:
postgres-db: keycloak
postgresql.conf: |
max_connections = 1000
shared_buffers = 512MB

View file

@ -1,46 +0,0 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgresql
spec:
selector:
matchLabels:
app: postgresql
strategy:
type: Recreate
template:
metadata:
labels:
app: postgresql
spec:
containers:
- image: postgres
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
volumes:
- name: postgres-config-volume
configMap:
name: postgres-config

View file

@ -1,8 +0,0 @@
apiVersion: v1
kind: Secret
metadata:
name: postgres-secret
type: Opaque
data:
postgres-user: "psql-user"
postgres-password: "psql-pw"

View file

@ -1,9 +0,0 @@
apiVersion: v1
kind: Service
metadata:
name: postgresql-service
spec:
selector:
app: postgresql
ports:
- port: 5432

View file

@ -1,51 +0,0 @@
(ns dda.c4k-keycloak.postgres-test
(:require
#?(:clj [clojure.test :refer [deftest is are testing run-tests]]
:cljs [cljs.test :refer-macros [deftest is are testing run-tests]])
[dda.c4k-keycloak.postgres :as cut]))
(deftest should-generate-secret
(is (= {:apiVersion "v1"
:kind "Secret"
:metadata {:name "postgres-secret"}
:type "Opaque"
:data
{:postgres-user "cHNxbHVzZXI="
:postgres-password "dGVzdDEyMzQ="}}
(cut/generate-secret {:postgres-db-user "psqluser" :postgres-db-password "test1234"}))))
(deftest should-generate-postgres-deployment
(is (= {:apiVersion "apps/v1"
:kind "Deployment"
:metadata {:name "postgresql"}
:spec
{:selector {:matchLabels {:app "postgresql"}}
:strategy {:type "Recreate"}
:template
{:metadata {:labels {:app "postgresql"}}
:spec
{:containers
[{:image "postgres"
:name "postgresql"
:env
[{:name "POSTGRES_USER"
:valueFrom
{:secretKeyRef
{:name "postgres-secret", :key "postgres-user"}}}
{:valueFrom
{:secretKeyRef
{:name "postgres-secret"
:key "postgres-password"}}
:name "POSTGRES_PASSWORD"}
{:valueFrom
{:configMapKeyRef
{:name "postgres-config", :key "postgres-db"}}
:name "POSTGRES_DB"}]
:ports [{:containerPort 5432, :name "postgresql"}]
:volumeMounts
[{:name "postgres-config-volume"
:mountPath "/etc/postgresql/postgresql.conf"
:subPath "postgresql.conf"
:readOnly true}]}]
:volumes [{:name "postgres-config-volume", :configMap {:name "postgres-config"}}]}}}}
(cut/generate-deployment))))