Skip to content

Commit 1933bcf

Browse files
jwhitlarkdakrone
authored andcommitted
Add Kubernetes API (from inside pod) Example (#487)
1 parent dba100d commit 1933bcf

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

examples/kubernetes_pod.clj

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(:ns clj-http.examples.kubernetes-pod
2+
"This is an example of calling the Kubernetes API from inside a pod. K8s uses a
3+
custom CA so that you can authenticate the API server, and provides a token per pod
4+
so that each pod can authenticate itself with the APi server.
5+
6+
If you are still having 401/403 errors, look carefully at the message, if it includes
7+
a ServiceAccount name, this part worked, and your problem is likely at the Role/RoleBinding level."
8+
(:require [clj-http.client :as http]
9+
[less.awful.ssl :refer [trust-store]]))
10+
11+
;; Note that this is not a working example, you'll need to figure out your K8s API path.
12+
(let [k8s-trust-store (trust-store (clojure.java.io/file "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"))
13+
bearer-token (format "Bearer %s" (slurp "/var/run/secrets/kubernetes.io/serviceaccount/token"))
14+
kube-api-host (System/getenv "KUBERNETES_SERVICE_HOST")
15+
kube-api-port (System/getenv "KUBERNETES_SERVICE_PORT")]
16+
(http/get
17+
(format "https://%s:%s/apis/<something-protected>" kube-api-host kube-api-port)
18+
{:trust-store k8s-trust-store
19+
:headers {:authorization bearer-token}}))
20+

0 commit comments

Comments
 (0)