kubernetes-api is a Clojure library that acts as a kubernetes client
We had a good experience with cognitect-labs/aws-api, and missed something like that for Kubernetes API. We had some client libraries that generated a lot of code, but it lacked discoverability and documentation.
{:deps {nubank/k8s-api {:mvn/version "0.1.3"}}}
[nubank/k8s-api "0.1.3"]
;; In your ns statement
(ns my.ns
(:require [kubernetes-api.core :as k8s]))
There're multiple options for authentication while instantiating a client. You can explicit set a token:
(def k8s (k8s/client "http://some.host" {:token "..."}))
Or a function that returns the token
(def k8s (k8s/client "http://some.host" {:token-fn (constantly "...")}))
You can also define client certificates
(def k8s (k8s/client "http://some.host" {:ca-cert "/some/path/ca-docker.crt"
:client-cert "/some/path/client-cert.pem"
:client-key "/some/path/client-java.key"}))
You can list all operations with
(k8s/explore k8s)
or specify a specific entity
(k8s/explore k8s :Deployment)
;=>
[:Deployment
[:get "read the specified Deployment"]
[:update "replace the specified Deployment"]
[:delete "delete a Deployment"]
[:patch "partially update the specified Deployment"]
[:list "list or watch objects of kind Deployment"]
[:create "create a Deployment"]
[:deletecollection "delete collection of Deployment"]
[:list-all "list or watch objects of kind Deployment"]]
get info on an operation
(k8s/info k8s {:kind :Deployment
:action :create})
;=>
{:summary "create a Deployment",
:parameters {:namespace java.lang.String,
#schema.core.OptionalKey{:k :pretty} (maybe Str),
#schema.core.OptionalKey{:k :dry-run} (maybe Str),
#schema.core.OptionalKey{:k :field-manager} (maybe Str),
:body ...},
:returns {200 {#schema.core.OptionalKey{:k :apiVersion} (maybe Str),
#schema.core.OptionalKey{:k :kind} (maybe Str),
#schema.core.OptionalKey{:k :metadata} ...,
#schema.core.OptionalKey{:k :spec} ...,
#schema.core.OptionalKey{:k :status} ...},
201 {#schema.core.OptionalKey{:k :apiVersion} (maybe Str),
#schema.core.OptionalKey{:k :kind} (maybe Str),
#schema.core.OptionalKey{:k :metadata} ...,
#schema.core.OptionalKey{:k :spec} ...,
#schema.core.OptionalKey{:k :status} ...},
202 {#schema.core.OptionalKey{:k :apiVersion} (maybe Str),
#schema.core.OptionalKey{:k :kind} (maybe Str),
#schema.core.OptionalKey{:k :metadata} ...,
#schema.core.OptionalKey{:k :spec} ...,
#schema.core.OptionalKey{:k :status} ...},
401 Any}}
You can call an operation with:
(k8s/invoke k8s {:kind :ConfigMap
:action :create
:request {:namespace "default"
:body {:apiVersion "v1"
:metadata {:name "my-awesome-cm"}
:data {"foo" "bar"}}}})
invoke it will return the body, with :request
and :response
in metadata
(meta (k8s/invoke ...))
;=>
{:request ...
:response {:status ...
:body ...}}
You can debug the request map with:
(k8s/request k8s {:kind :ConfigMap
:action :create
:request {:namespace "default"
:body {:apiVersion "v1"
:metadata {:name "my-awesome-cm"}
:data {"foo" "bar"}}}})
The :body
parameter in PATCH is a bit different from the other operations, it must be a String
.
It's due to the need for the request body to comply to the chosen patch strategy (that can be a
Strategic Merge Patch, a JSON Patch or a JSON Merge Patch. More details
here).
You can call a PATCH operation with:
(k8s/invoke k8s {:kind :ConfigMap
:action :patch
:request {:namespace "default"
:name "my-awesome-cm"
:body "{\"data\": {\"foo\": \"another-value\"}}"}})
Can you improve this documentation? These fine people already did:
Rafael Leal & Diogo NicoletiEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close