Skip to content

Instantly share code, notes, and snippets.

@lalyos
Last active August 6, 2018 22:05
Show Gist options
  • Save lalyos/3b4cce23cdd9db4d561715631355db7e to your computer and use it in GitHub Desktop.
Save lalyos/3b4cce23cdd9db4d561715631355db7e to your computer and use it in GitHub Desktop.
k8s with curl
curl -s -k \
  --cert ~/.minikube/client.p12 \
  --key  ~/.minikube/client.key \
  --pass tcuser \
  https://192.168.64.8:8443/api/v1/pods \
    |jq '.items[].metadata.name'
k8s() { path=${1:? k8s api path required}; [[ "$DEBUG" ]] && set -x; shift ; curl -s -k   --cert ~/.minikube/client.p12   --key  ~/.minikube/client.key   --pass tcuser   https://192.168.64.8:8443/${path#/} "$@"; set +x; }

tl;dr

To be able to use curl agains k8s API you need certs and keys, in appropriate format.

First install a yaml2json converter:

go get -v github.com/bronze1man/yaml2json

Then extract the client cert and key:

yaml2json < ~/.kube/config | jq '.users[]|select(.name=="docker-for-desktop")|.user["client-certificate-data"]' -r | base64 -D > cli.cert
yaml2json < ~/.kube/config | jq '.users[]|select(.name=="docker-for-desktop")|.user["client-key-data"]' -r | base64 -D > cli.key

Convert the cert and key into pkcs12 format:

openssl pkcs12 -export -in cli.cert -inkey cli.key -out cli.p12 -password pass:tcuser

Finally you can use curl against k8s API. List pods from all namespaces:

curl -k --cert cli.p12 --key cli.key --pass tcuser https://127.0.0.1:6443/api/v1/pods

only pod ns and names:

curl -s -k --cert cli.p12 --key cli.key --pass tcuser https://127.0.0.1:6443/api/v1/pods| jq '.items[].metadata|[.namespace,.name]' -c

list all resourcetypes"

curl -s -k --cert cli.p12 --key cli.key --pass tcuser https://127.0.0.1:6443/api/v1|jq '.resources[].name'

delete a pod:

curl -k --cert cli.p12 --key cli.key --pass tcuser -XDELETE https://localhost:6443/api/v1/namespaces/default/pods/firstpod 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment