diff --git a/project-cljs.clj b/project-cljs.clj index fdd1a58..d03781b 100644 --- a/project-cljs.clj +++ b/project-cljs.clj @@ -1,4 +1,4 @@ -(defproject org.domaindrivenarchitecture/c4k-common-cljs "1.0.1-SNAPSHOT" +(defproject org.domaindrivenarchitecture/c4k-common-cljs "1.1.0-SNAPSHOT" :description "Contains predicates and tools for c4k" :url "https://domaindrivenarchitecture.org" :license {:name "Apache License, Version 2.0" diff --git a/project.clj b/project.clj index 2ba1700..2599c50 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject org.domaindrivenarchitecture/c4k-common-clj "1.0.1-SNAPSHOT" +(defproject org.domaindrivenarchitecture/c4k-common-clj "1.1.0-SNAPSHOT" :description "Contains predicates and tools for c4k" :url "https://domaindrivenarchitecture.org" :license {:name "Apache License, Version 2.0" diff --git a/src/main/cljc/dda/c4k_common/common.cljc b/src/main/cljc/dda/c4k_common/common.cljc index 9e10360..bbf1ffd 100644 --- a/src/main/cljc/dda/c4k_common/common.cljc +++ b/src/main/cljc/dda/c4k_common/common.cljc @@ -1,6 +1,7 @@ (ns dda.c4k-common.common (:require [clojure.walk] + [clojure.spec.alpha :as s] #?(:clj [orchestra.core :refer [defn-spec]] :cljs [orchestra.core :refer-macros [defn-spec]]) [dda.c4k-common.predicate :as cp])) @@ -50,3 +51,8 @@ value-to-replace %) coll)) + +(defn-spec concat-vec vector? + [& vs (s/* seq?)] + (into [] + (apply concat vs))) diff --git a/src/main/cljc/dda/c4k_common/postgres.cljc b/src/main/cljc/dda/c4k_common/postgres.cljc index 2615b15..314e745 100644 --- a/src/main/cljc/dda/c4k_common/postgres.cljc +++ b/src/main/cljc/dda/c4k_common/postgres.cljc @@ -22,8 +22,11 @@ (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?) (defn pg-config? [input] - (s/keys :opt-un [::postgres-size ::db-name ::postgres-data-volume-path])) + (s/keys :opt-un [::postgres-size ::db-name ::postgres-data-volume-path + ::pvc-storage-class-name ::pv-storage-size-gb])) (defn pg-auth? [input] (s/keys :opt-un [::postgres-db-user ::postgres-db-password])) @@ -53,7 +56,7 @@ (str "postgres/config-" (name postgres-size) ".yaml"))) (assoc-in [:data :postgres-db] db-name)))) - +; TODO: why do we need a sequence of configs? (defn-spec generate-deployment cp/map-or-seq? [& config (s/? pg-config?)] (let [{:keys [postgres-image] @@ -64,14 +67,23 @@ (defn-spec generate-persistent-volume cp/map-or-seq? [config pg-config?] - (let [{:keys [postgres-data-volume-path]} config] + (let [{:keys [postgres-data-volume-path pv-storage-size-gb] + :or {postgres-data-volume-path "/var/postgres" + pv-storage-size-gb 10}} config] (-> (yaml/from-string (yaml/load-resource "postgres/persistent-volume.yaml")) - (assoc-in [:spec :hostPath :path] postgres-data-volume-path)))) + (assoc-in [:spec :hostPath :path] postgres-data-volume-path) + (assoc-in [:spec :capacity :storage] (str pv-storage-size-gb "Gi"))))) (defn-spec generate-pvc cp/map-or-seq? - [] - (yaml/from-string (yaml/load-resource "postgres/pvc.yaml"))) + [config pg-config?] + (let [{:keys [pv-storage-size-gb pvc-storage-class-name] + :or {pv-storage-size-gb 10 + pvc-storage-class-name :manual}} 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 cp/map-or-seq? [my-auth any?] diff --git a/src/main/cljc/dda/c4k_common/predicate.cljc b/src/main/cljc/dda/c4k_common/predicate.cljc index a5ffa2c..9c7c992 100644 --- a/src/main/cljc/dda/c4k_common/predicate.cljc +++ b/src/main/cljc/dda/c4k_common/predicate.cljc @@ -16,4 +16,8 @@ (defn map-or-seq? [input] - (or (map? input) (seq? input))) \ No newline at end of file + (or (map? input) (seq? input))) + +(defn pvc-storage-class-name? + [input] + (contains? #{:manual :local-path} input)) \ No newline at end of file diff --git a/src/main/resources/postgres/pvc.yaml b/src/main/resources/postgres/pvc.yaml index 3a127c7..7d94bd1 100644 --- a/src/main/resources/postgres/pvc.yaml +++ b/src/main/resources/postgres/pvc.yaml @@ -5,9 +5,9 @@ metadata: labels: app: postgres spec: - storageClassName: manual + storageClassName: REPLACEME accessModes: - ReadWriteOnce resources: requests: - storage: 10Gi \ No newline at end of file + storage: REPLACEME \ No newline at end of file diff --git a/src/test/cljc/dda/c4k_common/postgres_test.cljc b/src/test/cljc/dda/c4k_common/postgres_test.cljc index 6d007d1..4b698c0 100644 --- a/src/test/cljc/dda/c4k_common/postgres_test.cljc +++ b/src/test/cljc/dda/c4k_common/postgres_test.cljc @@ -37,7 +37,39 @@ :accessModes ["ReadWriteOnce"] :capacity {:storage "10Gi"} :hostPath {:path "xx"}}} - (cut/generate-persistent-volume {:postgres-data-volume-path "xx"})))) + (cut/generate-persistent-volume {:postgres-data-volume-path "xx"}))) + (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-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 "manual" + :accessModes ["ReadWriteOnce"] + :resources {:requests {:storage "10Gi"}}}} + (cut/generate-pvc {}))) + (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 {:pv-storage-size-gb 20 + :pvc-storage-class-name :local-path})))) (deftest should-generate-secret (is (= {:apiVersion "v1"