Skip to content

Instantly share code, notes, and snippets.

@shark8me
Created September 15, 2020 11:49
Show Gist options
  • Save shark8me/b26b3dbff0a6852b7dd7be36c4333af2 to your computer and use it in GitHub Desktop.
Save shark8me/b26b3dbff0a6852b7dd7be36c4333af2 to your computer and use it in GitHub Desktop.
Running the K8S Pi job via the Clojure Repl
(ns clj-k8s-job.core
(:require
[clojure-kubernetes-client.core :as core]
[clojure-kubernetes-client.api.core-v1 :refer [read-namespaced-pod-log
list-pod-for-all-namespaces]]
[clojure-kubernetes-client.api.batch-v1 :refer [create-namespaced-job
read-namespaced-job-status
delete-namespaced-job]]))
(def pi-job-spec
{:apiVersion "batch/v1"
:kind "Job"
:metadata {:name "pi"}
:spec {:ttlSecondsAfterFinished 200
:template
{:spec
{:containers
[{:name "pi"
:imagePullPolicy "Always"
:image "perl"
:command ["perl" "-Mbignum=bpi"
"-wle" "print bpi(50)"]}]
:restartPolicy "Never"}}}})
(defn get-job-status
"read the status for the given job. Will return nil if
job not found (the API will throw a 404 Exception)"
[job-name]
(try
(let [js (read-namespaced-job-status job-name "default")]
(-> js :status))
(catch Exception e
nil)))
(defn get-job-pods
"returns all the pods used by job-name "
[job-name]
(let [resp (list-pod-for-all-namespaces)]
(when resp
(->> resp :items
(filter (fn[{:keys [metadata]}]
(let [{:keys [kind name]}
(-> metadata :ownerReferences first)]
(and (= kind "Job") (= name job-name)))))
(map #(-> % :metadata :name))))))
;;set the REST API URL
(core/set-api-context {:base-url "http://localhost:8080"})
;;create the job in the default (Kubernetes) namespace
(create-namespaced-job "default" pi-job-spec)
;;get the status of the job
(get-job-status "pi")
;;it is currently still running, as indicated by the 'active' field
;;{:startTime "2020-09-15T08:33:44Z", :active 1}
;;wait a few seconds, then re-run
(get-job-status "pi")
;;note that is has succeeded.
;;if mu
;;{:conditions [{:type "Complete", :status "True",
;;:lastProbeTime "2020-09-15T08:33:55Z", :lastTransitionTime "2020-09-15T08:33:55Z"}],
;;:startTime "2020-09-15T08:33:44Z", :completionTime "2020-09-15T08:33:55Z",
;;:succeeded 1}
(->
;;get the pods created by this job,
;;we expect just one
(get-job-pods "pi")
first
;;read the log for the first pod
(read-namespaced-pod-log "default"))
;;which returns the result
;;"3.1415926535897932384626433832795028841971693993751\n"
;;finally, delete the job.
(delete-namespaced-job "pi" "default"
{:kind :DeleteOptions
:propagationPolicy "Foreground"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment