This commit is contained in:
jem 2021-07-09 16:39:36 +02:00
parent 8e3b007e06
commit 67631747ae
7 changed files with 78 additions and 15 deletions

View file

@ -19,6 +19,9 @@ Our convention 4 kubernetes c4k-* tools combine the advantages of both approache
* yaml / edn as input and output, no more magic * yaml / edn as input and output, no more magic
* good validation, integration as api, cli or in the browser * good validation, integration as api, cli or in the browser
## Usage
## License ## License
Copyright © 2021 meissa GmbH Copyright © 2021 meissa GmbH

View file

@ -2,6 +2,7 @@
"src/main/cljs" "src/main/cljs"
"src/main/resources" "src/main/resources"
"src/test/cljc" "src/test/cljc"
"src/test/cljs"
"src/test/resources"] "src/test/resources"]
:dependencies [[aero "1.1.6"] :dependencies [[aero "1.1.6"]
[orchestra "2021.01.01-1"] [orchestra "2021.01.01-1"]

View file

@ -10,9 +10,6 @@
(into [] %) (into [] %)
%) lazy-seq)) %) lazy-seq))
(defmethod load-resource :clj [resource-name]
(slurp (io/resource resource-name)))
(defn from-string [input] (defn from-string [input]
(cast-lazy-seq-to-vec (yaml/parse-string input))) (cast-lazy-seq-to-vec (yaml/parse-string input)))
@ -20,6 +17,10 @@
(yaml/generate-string edn :dumper-options {:flow-style :block})) (yaml/generate-string edn :dumper-options {:flow-style :block}))
(defn dispatch-by-resource-name (defn dispatch-by-resource-name
[resource]) [resource]
:clj)
(defmulti load-resource dispatch-by-resource-name) (defmulti load-resource dispatch-by-resource-name)
(defmethod load-resource :clj [resource-name]
(slurp (io/resource resource-name)))

View file

@ -1,12 +1,8 @@
(ns dda.c4k-common.yaml (ns dda.c4k-common.yaml
(:require (:require
["js-yaml" :as yaml] ["js-yaml" :as yaml]
[shadow.resource :as rc])) [shadow.resource :as rc]
[clojure.string :as st]))
(defn load-resource [resource-name]
(case resource-name
"ingress_test.yaml" (rc/inline "ingress_test.yaml")
(throw (js/Error. "Undefined Resource!"))))
(defn from-string [input] (defn from-string [input]
(js->clj (yaml/load input) (js->clj (yaml/load input)
@ -14,3 +10,14 @@
(defn to-string [edn] (defn to-string [edn]
(yaml/dump (clj->js edn))) (yaml/dump (clj->js edn)))
(defn dispatch-by-resource-name
[resource]
(keyword (first (st/split resource #"/"))))
(defmulti load-resource dispatch-by-resource-name)
(defmethod load-resource :test [resource-name]
(case resource-name
"test/ingress_test.yaml" (rc/inline "test/ingress_test.yaml")
(throw (js/Error. "Undefined Resource!"))))

View file

@ -1,9 +1,12 @@
(ns dda.c4k-common.yaml-test (ns dda.c4k-common.yaml-test
(:require (:require
#?(:clj [clojure.test :refer [deftest is are testing run-tests]] [clojure.test :refer [deftest is are testing run-tests]]
:cljs [cljs.test :refer-macros [deftest is are testing run-tests]])
[dda.c4k-common.yaml :as cut])) [dda.c4k-common.yaml :as cut]))
(deftest should-dispatch-by-resource-name
(is (= :clj
(cut/dispatch-by-resource-name "postgres/etc"))))
(deftest should-parse-yaml-string (deftest should-parse-yaml-string
(is (= {:hallo "welt"} (is (= {:hallo "welt"}
(cut/from-string "hallo: welt")))) (cut/from-string "hallo: welt"))))
@ -42,4 +45,4 @@
[{:backend [{:backend
{:serviceName "another_keycloak" {:serviceName "another_keycloak"
:servicePort 8081}}]}}]}} :servicePort 8081}}]}}]}}
(cut/from-string (cut/load-resource "ingress_test.yaml"))))) (cut/from-string (cut/load-resource "test/ingress_test.yaml")))))

View file

@ -0,0 +1,48 @@
(ns dda.c4k-common.yaml-test
(:require
[cljs.test :refer-macros [deftest is are testing run-tests]]
[dda.c4k-common.yaml :as cut]))
(deftest should-dispatch-by-resource-name
(is (= :postgres
(cut/dispatch-by-resource-name "postgres/etc"))))
(deftest should-parse-yaml-string
(is (= {:hallo "welt"}
(cut/from-string "hallo: welt"))))
(deftest should-generate-yaml-string
(is (= "hallo: welt
"
(cut/to-string {:hallo "welt"}))))
(deftest should-convert-config-yml-to-map
(is (= {:apiVersion "networking.k8s.io/v1beta1"
:kind "Ingress"
:metadata
{:name "ingress-cloud"
:annotations
{:cert-manager.io/cluster-issuer
"letsencrypt-staging-issuer"
:nginx.ingress.kubernetes.io/proxy-body-size "256m"
:nginx.ingress.kubernetes.io/ssl-redirect "true"
:nginx.ingress.kubernetes.io/rewrite-target "/"
:nginx.ingress.kubernetes.io/proxy-connect-timeout "300"
:nginx.ingress.kubernetes.io/proxy-send-timeout "300"
:nginx.ingress.kubernetes.io/proxy-read-timeout "300"}
:namespace "default"}
:spec
{:tls [{:hosts ["fqdn"], :secretName "keycloak-secret"}]
:rules
[{:host "fqdn"
:http
{:paths
[{:backend
{:serviceName "keycloak", :servicePort 8080}}]}}
{:host "fqdn"
:http
{:paths
[{:backend
{:serviceName "another_keycloak"
:servicePort 8081}}]}}]}}
(cut/from-string (cut/load-resource "test/ingress_test.yaml")))))