From 337fa68592ba45881d40f4f31fbe92d29ea13cce Mon Sep 17 00:00:00 2001 From: bom Date: Fri, 20 Jan 2023 13:51:21 +0100 Subject: [PATCH] Use common postgres --- src/main/cljc/dda/c4k_keycloak/core.cljc | 49 +++++++++--------- src/main/cljc/dda/c4k_keycloak/postgres.cljc | 25 --------- src/main/resources/postgres/config.yaml | 11 ---- src/main/resources/postgres/deployment.yaml | 46 ----------------- src/main/resources/postgres/secret.yaml | 8 --- src/main/resources/postgres/service.yaml | 9 ---- .../cljc/dda/c4k_keycloak/postgres_test.cljc | 51 ------------------- 7 files changed, 26 insertions(+), 173 deletions(-) delete mode 100644 src/main/cljc/dda/c4k_keycloak/postgres.cljc delete mode 100644 src/main/resources/postgres/config.yaml delete mode 100644 src/main/resources/postgres/deployment.yaml delete mode 100644 src/main/resources/postgres/secret.yaml delete mode 100644 src/main/resources/postgres/service.yaml delete mode 100644 src/test/cljc/dda/c4k_keycloak/postgres_test.cljc diff --git a/src/main/cljc/dda/c4k_keycloak/core.cljc b/src/main/cljc/dda/c4k_keycloak/core.cljc index bf25e01..1390fb8 100644 --- a/src/main/cljc/dda/c4k_keycloak/core.cljc +++ b/src/main/cljc/dda/c4k_keycloak/core.cljc @@ -4,9 +4,11 @@ [clojure.spec.alpha :as s] #?(:clj [orchestra.core :refer [defn-spec]] :cljs [orchestra.core :refer-macros [defn-spec]]) - [dda.c4k-keycloak.yaml :as yaml] - [dda.c4k-keycloak.keycloak :as kc] - [dda.c4k-keycloak.postgres :as pg])) + [dda.c4k-common.yaml :as yaml] + [dda.c4k-common.postgres :as postgres] + [dda.c4k-keycloak.keycloak :as kc])) + +(def default-storage-class :local-path) (def config-defaults {:issuer :staging}) @@ -14,27 +16,28 @@ :opt-un [::kc/issuer])) (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? [my-config config? my-auth auth?] - (let [resulting-config (merge config-defaults my-config)] - (cs/join "\n" - [(yaml/to-string (pg/generate-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))]))) + (let [resulting-config (merge config-defaults my-config my-auth)] + (cs/join + "\n---\n" + (k8s-objects resulting-config)))) \ No newline at end of file diff --git a/src/main/cljc/dda/c4k_keycloak/postgres.cljc b/src/main/cljc/dda/c4k_keycloak/postgres.cljc deleted file mode 100644 index 01681bf..0000000 --- a/src/main/cljc/dda/c4k_keycloak/postgres.cljc +++ /dev/null @@ -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"))) diff --git a/src/main/resources/postgres/config.yaml b/src/main/resources/postgres/config.yaml deleted file mode 100644 index 4006743..0000000 --- a/src/main/resources/postgres/config.yaml +++ /dev/null @@ -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 diff --git a/src/main/resources/postgres/deployment.yaml b/src/main/resources/postgres/deployment.yaml deleted file mode 100644 index 5448e3d..0000000 --- a/src/main/resources/postgres/deployment.yaml +++ /dev/null @@ -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 diff --git a/src/main/resources/postgres/secret.yaml b/src/main/resources/postgres/secret.yaml deleted file mode 100644 index ebf2b69..0000000 --- a/src/main/resources/postgres/secret.yaml +++ /dev/null @@ -1,8 +0,0 @@ -apiVersion: v1 -kind: Secret -metadata: - name: postgres-secret -type: Opaque -data: - postgres-user: "psql-user" - postgres-password: "psql-pw" diff --git a/src/main/resources/postgres/service.yaml b/src/main/resources/postgres/service.yaml deleted file mode 100644 index d67fea1..0000000 --- a/src/main/resources/postgres/service.yaml +++ /dev/null @@ -1,9 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: postgresql-service -spec: - selector: - app: postgresql - ports: - - port: 5432 diff --git a/src/test/cljc/dda/c4k_keycloak/postgres_test.cljc b/src/test/cljc/dda/c4k_keycloak/postgres_test.cljc deleted file mode 100644 index 8344771..0000000 --- a/src/test/cljc/dda/c4k_keycloak/postgres_test.cljc +++ /dev/null @@ -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))))