diff --git a/src/main/cljc/dda/c4k_common/namespace.cljc b/src/main/cljc/dda/c4k_common/namespace.cljc index 0e8638a..17d5bc6 100644 --- a/src/main/cljc/dda/c4k_common/namespace.cljc +++ b/src/main/cljc/dda/c4k_common/namespace.cljc @@ -3,6 +3,8 @@ #?(:clj [orchestra.core :refer [defn-spec]] :cljs [orchestra.core :refer-macros [defn-spec]]) [clojure.spec.alpha :as s] + [dda.c4k-common.predicate :as cp] + [dda.c4k-common.yaml :as yaml] [dda.c4k-common.namespace.namespace-internal :as int])) (s/def ::namespace ::int/namespace) @@ -17,3 +19,10 @@ (let [final-config (merge default-config config)] [(int/generate-namespace final-config)])) + +(defn-spec load-and-adjust-namespace cp/map-or-seq? + [file string? + namespace ::namespace] + (-> + (yaml/load-as-edn file) + (assoc-in [:metadata :namespace] namespace))) diff --git a/src/test/cljc/dda/c4k_common/namespace_test.cljc b/src/test/cljc/dda/c4k_common/namespace_test.cljc index f400516..5f1f2ca 100644 --- a/src/test/cljc/dda/c4k_common/namespace_test.cljc +++ b/src/test/cljc/dda/c4k_common/namespace_test.cljc @@ -2,10 +2,18 @@ (:require #?(:clj [clojure.test :refer [deftest is are testing run-tests]] :cljs [cljs.test :refer-macros [deftest is are testing run-tests]]) + #?(:cljs [shadow.resource :as rc]) [clojure.spec.test.alpha :as st] [dda.c4k-common.namespace :as cut])) (st/instrument `cut/generate) +(st/instrument `cut/load-and-adjust-namespace) + +#?(:cljs + (defmethod yaml/load-resource :namespace-test [resource-name] + (case resource-name + "namespace-test/dummy.yaml" (rc/inline "namespace-test/dummy.yaml") + (throw (js/Error. (str "Undefined Resource: " resource-name)))))) (deftest should-generate-simple-ingress (is (= [{:apiVersion "v1" @@ -15,4 +23,11 @@ (is (= [{:apiVersion "v1" :kind "Namespace" :metadata {:name "myapp"}}] - (cut/generate {:namespace "myapp"})))) \ No newline at end of file + (cut/generate {:namespace "myapp"})))) + +(deftest should-load-and-replace-ns + (is (= {:apiVersion "v1" + :kind "Dummy" + :metadata {:name "dummy" + :namespace "xy"}} + (cut/load-and-adjust-namespace "namespace-test/dummy.yaml" "xy")))) \ No newline at end of file diff --git a/src/test/resources/namespace-test/dummy.yaml b/src/test/resources/namespace-test/dummy.yaml new file mode 100644 index 0000000..cf7f183 --- /dev/null +++ b/src/test/resources/namespace-test/dummy.yaml @@ -0,0 +1,5 @@ +apiVersion: v1 +kind: Dummy +metadata: + name: dummy + namespace: default \ No newline at end of file