From 0999b00bbf16c9e14f09a726838fc72423600dc4 Mon Sep 17 00:00:00 2001 From: bom Date: Tue, 7 Jun 2022 18:23:04 +0200 Subject: [PATCH] Rename shynet to gitea, implement generate functions --- src/main/cljc/dda/c4k_gitea/core.cljc | 50 ++++++----------- src/main/cljc/dda/c4k_gitea/gitea.cljc | 49 ++++++++++++++++ src/main/cljc/dda/c4k_gitea/shynet.cljc | 75 ------------------------- 3 files changed, 67 insertions(+), 107 deletions(-) create mode 100644 src/main/cljc/dda/c4k_gitea/gitea.cljc delete mode 100644 src/main/cljc/dda/c4k_gitea/shynet.cljc diff --git a/src/main/cljc/dda/c4k_gitea/core.cljc b/src/main/cljc/dda/c4k_gitea/core.cljc index b841717..c5dbabf 100644 --- a/src/main/cljc/dda/c4k_gitea/core.cljc +++ b/src/main/cljc/dda/c4k_gitea/core.cljc @@ -1,10 +1,8 @@ (ns dda.c4k-gitea.core (:require - [clojure.string :as cs] [clojure.spec.alpha :as s] - #?(:clj [orchestra.core :refer [defn-spec]] - :cljs [orchestra.core :refer-macros [defn-spec]]) [dda.c4k-common.yaml :as yaml] + [dda.c4k-common.common :as cm] [dda.c4k-common.postgres :as postgres] [dda.c4k-gitea.gitea :as gitea])) @@ -13,35 +11,23 @@ (def config? (s/keys :req-un [::gitea/fqdn] :opt-un [::gitea/issuer])) -(def auth? (s/keys :req-un [::gitea/django-secret-key - ::postgres/postgres-db-user ::postgres/postgres-db-password])) +(def auth? (s/keys :req-un [::postgres/postgres-db-user ::postgres/postgres-db-password])) (defn k8s-objects [config] (let [storage-class (if (contains? config :postgres-data-volume-path) :manual :local-path)] - (map yaml/to-string - [(postgres/generate-config {:postgres-size :2gb :db-name "gitea"}) - (postgres/generate-secret config) - (when (contains? config :postgres-data-volume-path) - (postgres/generate-persistent-volume (select-keys config [:postgres-data-volume-path :pv-storage-size-gb]))) - (postgres/generate-pvc {:pv-storage-size-gb 20 - :pvc-storage-class-name storage-class}) - (postgres/generate-deployment {:postgres-image "postgres:14" - :postgres-size :2gb}) - (postgres/generate-service) - (gitea/generate-secret config) - (gitea/generate-webserver-deployment) - (gitea/generate-celeryworker-deployment) - (gitea/generate-ingress config) - (gitea/generate-certificate config) - (gitea/generate-service-redis) - (gitea/generate-service-webserver) - (gitea/generate-statefulset)]))) - -; TODO: Remove once cljs release of common worked -(defn-spec generate any? - [my-config config? - my-auth auth?] - (let [resulting-config (merge config-defaults my-config my-auth)] - (cs/join - "\n---\n" - (k8s-objects resulting-config)))) + (cm/concat-vec + [(yaml/load-resource "gitea/volumes.yaml") + (yaml/load-resource "gitea/services.yaml")] + (map yaml/to-string + [(postgres/generate-config {:postgres-size :2gb :db-name "gitea"}) + (postgres/generate-secret config) + (when (contains? config :postgres-data-volume-path) + (postgres/generate-persistent-volume (select-keys config [:postgres-data-volume-path :pv-storage-size-gb]))) + (postgres/generate-pvc {:pv-storage-size-gb 20 + :pvc-storage-class-name storage-class}) + (postgres/generate-deployment {:postgres-image "postgres:14" + :postgres-size :2gb}) + (postgres/generate-service) + (gitea/generate-appini-configmap) + (gitea/generate-deployment config) + (gitea/generate-ingress config)])))) diff --git a/src/main/cljc/dda/c4k_gitea/gitea.cljc b/src/main/cljc/dda/c4k_gitea/gitea.cljc new file mode 100644 index 0000000..60dc75d --- /dev/null +++ b/src/main/cljc/dda/c4k_gitea/gitea.cljc @@ -0,0 +1,49 @@ +(ns dda.c4k-gitea.gitea + (:require + [clojure.spec.alpha :as s] + #?(:cljs [shadow.resource :as rc]) + [dda.c4k-common.yaml :as yaml] + [dda.c4k-common.common :as cm] + [dda.c4k-common.predicate :as pred])) + +(s/def ::fqdn pred/fqdn-string?) +(s/def ::issuer pred/letsencrypt-issuer?) + +; TODO +#?(:cljs + (defmethod yaml/load-resource :gitea [resource-name] + (case resource-name + "gitea/secret.yaml" (rc/inline "gitea/secret.yaml") + "gitea/certificate.yaml" (rc/inline "gitea/certificate.yaml") + "gitea/deployments.yaml" (rc/inline "gitea/deployments.yaml") + "gitea/ingress.yaml" (rc/inline "gitea/ingress.yaml") + "gitea/service-redis.yaml" (rc/inline "gitea/service-redis.yaml") + "gitea/service-webserver.yaml" (rc/inline "gitea/service-webserver.yaml") + "gitea/statefulset.yaml" (rc/inline "gitea/statefulset.yaml") + (throw (js/Error. "Undefined Resource!"))))) + +#?(:cljs + (defmethod yaml/load-as-edn :gitea [resource-name] + (yaml/from-string (yaml/load-resource resource-name)))) + +(defn generate-appini-configmap [] + (yaml/load-as-edn "gitea/appini-configmap.yaml")) + +(defn generate-deployment [config] + (let [{:keys [postgres-db-user postgres-db-password]} config] + (-> + (yaml/load-as-edn "gitea/deployment.yaml") + (cm/replace-named-value "GITEA__database__USER" postgres-db-user) + (cm/replace-named-value "GITEA__database__PASSWD" postgres-db-password)))) + +(defn generate-ingress [config] + (let [{:keys [fqdn issuer] + :or {issuer :staging}} config + letsencrypt-issuer (name issuer)] + (-> + (yaml/load-as-edn "gitea/ingress.yaml") + (assoc-in [:metadata :annotations :cert-manager.io/cluster-issuer] letsencrypt-issuer) + (cm/replace-all-matching-values-by-new-value "FQDN" fqdn)))) + +(defn generate-volumes [] + (yaml/load-as-edn "gitea/volumes.yaml")) diff --git a/src/main/cljc/dda/c4k_gitea/shynet.cljc b/src/main/cljc/dda/c4k_gitea/shynet.cljc deleted file mode 100644 index 57611ae..0000000 --- a/src/main/cljc/dda/c4k_gitea/shynet.cljc +++ /dev/null @@ -1,75 +0,0 @@ -(ns dda.c4k-gitea.gitea - (:require - [clojure.spec.alpha :as s] - #?(:cljs [shadow.resource :as rc]) - [dda.c4k-common.yaml :as yaml] - [dda.c4k-common.common :as cm] - [dda.c4k-common.predicate :as pred])) - -(s/def ::fqdn pred/fqdn-string?) -(s/def ::issuer pred/letsencrypt-issuer?) -(s/def ::django-secret-key pred/bash-env-string?) - -#?(:cljs - (defmethod yaml/load-resource :gitea [resource-name] - (case resource-name - "gitea/secret.yaml" (rc/inline "gitea/secret.yaml") - "gitea/certificate.yaml" (rc/inline "gitea/certificate.yaml") - "gitea/deployments.yaml" (rc/inline "gitea/deployments.yaml") - "gitea/ingress.yaml" (rc/inline "gitea/ingress.yaml") - "gitea/service-redis.yaml" (rc/inline "gitea/service-redis.yaml") - "gitea/service-webserver.yaml" (rc/inline "gitea/service-webserver.yaml") - "gitea/statefulset.yaml" (rc/inline "gitea/statefulset.yaml") - (throw (js/Error. "Undefined Resource!"))))) - -#?(:cljs - (defmethod yaml/load-as-edn :gitea [resource-name] - (yaml/from-string (yaml/load-resource resource-name)))) - -(defn generate-secret [config] - (let [{:keys [fqdn django-secret-key postgres-db-user postgres-db-password]} config] - (-> - (yaml/load-as-edn "gitea/secret.yaml") - ; TODO: See comment in secret.yaml - ;(assoc-in [:stringData :ALLOWED_HOSTS] fqdn) - (assoc-in [:stringData :DJANGO_SECRET_KEY] django-secret-key) - (assoc-in [:stringData :DB_USER] postgres-db-user) - (assoc-in [:stringData :DB_PASSWORD] postgres-db-password)))) - -(defn generate-certificate [config] - (let [{:keys [fqdn issuer]} config - letsencrypt-issuer (name issuer)] - (-> - (yaml/load-as-edn "gitea/certificate.yaml") - (assoc-in [:spec :commonName] fqdn) - (assoc-in [:spec :dnsNames] [fqdn]) - (assoc-in [:spec :issuerRef :name] letsencrypt-issuer)))) - -(defn generate-webserver-deployment [] - (let [gitea-application "gitea-webserver"] - (-> (yaml/load-as-edn "gitea/deployments.yaml") - (cm/replace-all-matching-values-by-new-value "gitea-application" gitea-application) - (update-in [:spec :template :spec :containers 0] dissoc :command)))) - -(defn generate-celeryworker-deployment [] - (let [gitea-application "gitea-celeryworker"] - (-> (yaml/load-as-edn "gitea/deployments.yaml") - (cm/replace-all-matching-values-by-new-value "gitea-application" gitea-application)))) - -(defn generate-ingress [config] - (let [{:keys [fqdn issuer] - :or {issuer :staging}} config - letsencrypt-issuer (name issuer)] - (-> - (yaml/load-as-edn "gitea/ingress.yaml") - (assoc-in [:metadata :annotations :cert-manager.io/cluster-issuer] letsencrypt-issuer) - (cm/replace-all-matching-values-by-new-value "fqdn" fqdn)))) - -(defn generate-statefulset [] - (yaml/load-as-edn "gitea/statefulset.yaml")) - -(defn generate-service-redis [] - (yaml/load-as-edn "gitea/service-redis.yaml")) - -(defn generate-service-webserver [] - (yaml/load-as-edn "gitea/service-webserver.yaml"))