Created
January 29, 2023 23:00
-
-
Save learnitall/4c1802f1230a81439a78179a54def568 to your computer and use it in GitHub Desktop.
Generate K8s OpenAPI v3 Spec Using from a K8s API Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# --- | |
# k8s-openapi.sh | |
# | |
# Generate openapi v3 spec from a k8s apiserver and launch a swagger UI | |
# instance to check it out. | |
# | |
# Assumes that k8s api server has been proxied on localhost:8001 | |
# (ie be sure to run `kubectl proxy` prior to execution). | |
# | |
# The OpenAPI v3 spec is split out by api group on the k8s api server. | |
# All the different api groups and their OpenAPI v3 spec urls are | |
# located at /openapi/v3. This script takes all the spec urls listed | |
# from /openapi/v3, downloads them, and merges them together into | |
# a singular spec document that can be read by the swagger ui. | |
# | |
# More information on k8s OpenAPI v3 support: | |
# https://kubernetes.io/docs/concepts/overview/kubernetes-api/#openapi-v3 | |
# | |
# Process for OpenAPI v2 can be found here: | |
# https://jonnylangefeld.com/blog/kubernetes-how-to-view-swagger-ui | |
# | |
# jq cheatsheet: https://lzone.de/cheat-sheet/jq | |
# | |
# jq map merge magic: https://gist.github.com/crazed/b5448baeb204eb816eb9 | |
# | |
# Bash multi-line variable magic: | |
# https://unix.stackexchange.com/questions/275794/iterating-over-multiple-line-string-stored-in-variable | |
# --- | |
# | |
# --- BEGIN LICENSE --- | |
# Copyright 2023 Ryan Drew | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining | |
# a copy of this software and associated documentation files (the "Software"), | |
# to deal in the Software without restriction, including without limitation the | |
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
# sell copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
# --- END LICENSE --- | |
set -e | |
mkdir -p /tmp/k8s-openapi | |
pushd /tmp/k8s-openapi | |
curl localhost:8001/openapi/v3 > ./paths.json | |
paths="$(cat ./paths.json | \ | |
jq -r '.paths | to_entries[] | [.key, .value.serverRelativeURL] | join(" ")')" | |
while IFS= read -r line | |
do | |
name=$(echo $line | cut -d ' ' -f 1) | |
url=$(echo $line | cut -d ' ' -f 2) | |
echo $name | |
curl localhost:8001$url > $(echo $name | tr / _).openapi.json | |
done <<< $paths | |
jq -r -s 'reduce .[] as $x ({}; . * $x)' *.openapi.json > ./k8s-openapi-spec.json | |
docker run --rm -it \ | |
-v /tmp/k8s-openapi/k8s-openapi-spec.json:/k8s-openapi-spec.json:Z \ | |
-e SWAGGER_JSON=/k8s-openapi-spec.json \ | |
-p 8080:8080 \ | |
swaggerapi/swagger-ui |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment