From 00ab3c3e2fb39a2ec3ef7676fc6153fe77afe4ae Mon Sep 17 00:00:00 2001 From: Clemens Geibel Date: Thu, 27 May 2021 15:53:17 +0200 Subject: [PATCH] Added postgres deployment --- src/main/cljc/dda/k8s_keycloak/core.cljc | 22 ++++++++++- .../resources/postgres/postgres-config.yaml | 10 +++++ .../postgres/postgres-deployment.yaml | 38 +++++++++++++++++++ .../resources/postgres/postgres-service.yaml | 9 +++++ src/test/cljc/dda/k8s_keycloak/core_test.cljc | 27 +++++++++++++ 5 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/postgres/postgres-config.yaml create mode 100644 src/main/resources/postgres/postgres-deployment.yaml create mode 100644 src/main/resources/postgres/postgres-service.yaml diff --git a/src/main/cljc/dda/k8s_keycloak/core.cljc b/src/main/cljc/dda/k8s_keycloak/core.cljc index 9824c70..f2be1d1 100644 --- a/src/main/cljc/dda/k8s_keycloak/core.cljc +++ b/src/main/cljc/dda/k8s_keycloak/core.cljc @@ -40,6 +40,9 @@ (assoc-in [:data :config.edn] (str my-config)) (assoc-in [:data :credentials.edn] (str my-auth)))) +(defn generate-postgres-config [] + (yaml/from-string (yaml/load-resource "postgres/postgres-config.yaml"))) + (defn generate-deployment [my-auth] (let [{:keys [user-name user-password]} my-auth] (-> @@ -47,6 +50,14 @@ (assoc-in [:spec :template :spec :containers 0 :env 0 :value] user-name) (assoc-in [:spec :template :spec :containers 0 :env 1 :value] user-password)))) +(defn generate-postgres-deployment [my-auth] + (let [{:keys [postgres-user postgres-password postgres-db]} my-auth] + (-> + (yaml/from-string (yaml/load-resource "postgres/postgres-deployment.yaml")) + (assoc-in [:spec :template :spec :containers 0 :env 0 :value] postgres-user) + (assoc-in [:spec :template :spec :containers 0 :env 1 :value] postgres-db) + (assoc-in [:spec :template :spec :containers 0 :env 2 :value] postgres-password)))) + (defn generate-certificate [config] (let [{:keys [fqdn issuer] :or {issuer :staging}} config @@ -69,11 +80,20 @@ (defn generate-service [] (yaml/from-string (yaml/load-resource "service.yaml"))) +(defn generate-postgres-service [] + (yaml/from-string (yaml/load-resource "postgres/postgres-service.yaml"))) + (defn-spec generate any? [my-config config? my-auth auth?] (cs/join "\n" - [(yaml/to-string (generate-config my-config my-auth)) + [(yaml/to-string (generate-postgres-config)) + "---" + (yaml/to-string (generate-postgres-service)) + "---" + (yaml/to-string (generate-postgres-deployment my-auth)) + "---" + (yaml/to-string (generate-config my-config my-auth)) "---" (yaml/to-string (generate-certificate my-config)) "---" diff --git a/src/main/resources/postgres/postgres-config.yaml b/src/main/resources/postgres/postgres-config.yaml new file mode 100644 index 0000000..799cc45 --- /dev/null +++ b/src/main/resources/postgres/postgres-config.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: postgres-config + labels: + app: postgres +data: + postgresql.conf: | + max_connections = 1000 + shared_buffers = 512MB diff --git a/src/main/resources/postgres/postgres-deployment.yaml b/src/main/resources/postgres/postgres-deployment.yaml new file mode 100644 index 0000000..49da204 --- /dev/null +++ b/src/main/resources/postgres/postgres-deployment.yaml @@ -0,0 +1,38 @@ +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 + value: "psql-user" + - name: POSTGRES_DB + value: "psql-db" + - name: POSTGRES_PASSWORD + value: "psql-pw" + ports: + - containerPort: 5432 + name: postgresql + cmd: + volumeMounts: + - name: postgres-config-volume + mountPath: /etc/postgresql/postgresql.conf + subPath: postgresql.conf + readOnly: true + volumes: + - name: postgres-config-volume + configMap: + name: postgres-config diff --git a/src/main/resources/postgres/postgres-service.yaml b/src/main/resources/postgres/postgres-service.yaml new file mode 100644 index 0000000..d67fea1 --- /dev/null +++ b/src/main/resources/postgres/postgres-service.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Service +metadata: + name: postgresql-service +spec: + selector: + app: postgresql + ports: + - port: 5432 diff --git a/src/test/cljc/dda/k8s_keycloak/core_test.cljc b/src/test/cljc/dda/k8s_keycloak/core_test.cljc index caf629b..3297db7 100644 --- a/src/test/cljc/dda/k8s_keycloak/core_test.cljc +++ b/src/test/cljc/dda/k8s_keycloak/core_test.cljc @@ -83,3 +83,30 @@ :ports [{:name "http", :containerPort 8080}] :readinessProbe {:httpGet {:path "/auth/realms/master", :port 8080}}}]}}}} (cut/generate-deployment {:user-name "testuser" :user-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", :value "psqluser"} + {:name "POSTGRES_DB", :value "keycloak"} + {:name "POSTGRES_PASSWORD", :value "test1234"}] + :ports [{:containerPort 5432, :name "postgresql"}] + :cmd nil + :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-postgres-deployment {:postgres-user "psqluser" :postgres-db "keycloak" :postgres-password "test1234"}))))