Often times, especially during development of software that talks to the kubernetes API server, I actually find myself looking for a detailed kubernetes API specification. And no, I am not talking about the official kubernetes API reference which mainly reveals core object models. I am also interested in the specific API paths, the possible headers, query parameters and responses.
So the next logical step is to google for something like ‘kubernetes swagger’ or ‘kubernetes openapi spec’ and you’d hope for a Swagger UI to pop up and answer all your questions. But while some of those search results can lead you in the right direction, you won’t be pleased with the Swagger UI you were looking for.
The reason for that is the spec for every kubernetes API server is actually different due to custom resource definitions and therefore exposes different paths and models.
And that requires the kubernetes API server to actually generate it’s very own openapi specification, which we will do in the following:
First make sure that you are connected to your kubernetes API server as your current kube context. You can double check via
kubectl config current-context. You then want to open a reverse proxy to your kubernetes API server:
kubectl proxy --port=8080
That saves us a bunch of authentication configuration that’s now all handled by
kubectl. Run the following commands in a new terminal window to keep the reverse proxy alive (or run the reverse proxy in the background). Save the Swagger file for your kubernetes API server via the following command:
curl localhost:8080/openapi/v2 > k8s-swagger.json
As last step we now just need to start a Swagger ui server with the generated Swagger json as input. We will use a docker container for that:
docker run \ --rm \ -p 80:8080 \ -e SWAGGER_JSON=/k8s-swagger.json \ -v $(pwd)/k8s-swagger.json:/k8s-swagger.json \ swaggerapi/swagger-ui
Now visit http://localhost and you’ll get a Swagger UI including the models and paths of all custom resources installed on your cluster!
Tags: kubernetes, swagger, api, tips & tricks