-
Notifications
You must be signed in to change notification settings - Fork 7
/
k8s.clj
73 lines (61 loc) · 2.28 KB
/
k8s.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(ns org.purefn.kurosawa.k8s
"Kubernetes configuration and deployment helpers."
(:require [clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as gen]
[clojure.java.io :as io]
[clojure.string :as str]
[org.purefn.kurosawa.result :refer :all])
(:import [java.io File]))
;;------------------------------------------------------------------------------
;; API
;;------------------------------------------------------------------------------
(defn kubernetes?
"Is the program currently running in a Kubernetes container?"
[]
(-> (System/getenv)
(get "KUBERNETES_SERVICE_HOST")
some?))
(defn- read-configs
[dir]
(let [n (-> (str/split dir #"/")
(count))
^File root (io/file dir)
dir? (fn [^File fd] (.isDirectory fd))
files (fn [^File d]
(if (some->> d (.getName) (re-find #"\.\..+"))
[]
(.listFiles d)))
pairs (fn [^File fd]
(let [p (-> (.getPath fd)
(str/split #"/"))
v (-> (slurp fd)
(str/trim))]
[(drop n p)
(-> (attempt (fn [^String s] (Integer. s)) v)
(recover (fn [_] (Long. v)))
(recover (fn [_] (Double. v)))
(recover (constantly v))
(success))]))]
(->> (tree-seq dir? files root)
(filter (comp not dir?))
(map pairs)
(reduce (fn [m [p v]] (assoc-in m p v)) {}))))
(defn config-map
"Read the Kubernetes ConfigMap from container local disk.
- `name` The base name of the configuration (if any).
Returns a nested map of configuration parameters."
([name]
(read-configs (str "/etc/configs/" name)))
([]
(config-map "")))
(defn secrets
"Read the Kubernetes Secrets from container local disk.
- `name` The base name of the secrets (if any).
Returns a nested map of plain text secrets."
([name]
(read-configs (str "/etc/secrets/" name)))
([]
(secrets "")))
;;------------------------------------------------------------------------------
;; Specs.
;;------------------------------------------------------------------------------