Rename shynet to gitea, implement generate functions

This commit is contained in:
bom 2022-06-07 18:23:04 +02:00
parent 71fc3ba6ef
commit 0999b00bbf
3 changed files with 67 additions and 107 deletions

View file

@ -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,11 +11,13 @@
(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)]
(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)
@ -28,20 +28,6 @@
(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))))
(gitea/generate-appini-configmap)
(gitea/generate-deployment config)
(gitea/generate-ingress config)]))))

View file

@ -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"))

View file

@ -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"))