update-openapi-spec.sh 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env bash
  2. # Copyright 2016 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Script to fetch latest openapi spec.
  16. # Puts the updated spec at api/openapi-spec/
  17. set -o errexit
  18. set -o nounset
  19. set -o pipefail
  20. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  21. OPENAPI_ROOT_DIR="${KUBE_ROOT}/api/openapi-spec"
  22. source "${KUBE_ROOT}/hack/lib/init.sh"
  23. kube::util::require-jq
  24. kube::golang::setup_env
  25. make -C "${KUBE_ROOT}" WHAT=cmd/kube-apiserver
  26. function cleanup()
  27. {
  28. if [[ -n ${APISERVER_PID-} ]]; then
  29. kill "${APISERVER_PID}" 1>&2 2>/dev/null
  30. wait "${APISERVER_PID}" || true
  31. fi
  32. unset APISERVER_PID
  33. kube::etcd::cleanup
  34. kube::log::status "Clean up complete"
  35. }
  36. trap cleanup EXIT SIGINT
  37. kube::golang::setup_env
  38. TMP_DIR=$(mktemp -d /tmp/update-openapi-spec.XXXX)
  39. ETCD_HOST=${ETCD_HOST:-127.0.0.1}
  40. ETCD_PORT=${ETCD_PORT:-2379}
  41. API_PORT=${API_PORT:-8050}
  42. API_HOST=${API_HOST:-127.0.0.1}
  43. API_LOGFILE=${API_LOGFILE:-/tmp/openapi-api-server.log}
  44. kube::etcd::start
  45. echo "dummy_token,admin,admin" > "${TMP_DIR}/tokenauth.csv"
  46. # Start kube-apiserver
  47. kube::log::status "Starting kube-apiserver"
  48. "${KUBE_OUTPUT_HOSTBIN}/kube-apiserver" \
  49. --insecure-bind-address="${API_HOST}" \
  50. --bind-address="${API_HOST}" \
  51. --insecure-port="${API_PORT}" \
  52. --etcd-servers="http://${ETCD_HOST}:${ETCD_PORT}" \
  53. --advertise-address="10.10.10.10" \
  54. --cert-dir="${TMP_DIR}/certs" \
  55. --runtime-config="api/all=true,extensions/v1beta1/daemonsets=true,extensions/v1beta1/deployments=true,extensions/v1beta1/replicasets=true,extensions/v1beta1/networkpolicies=true,extensions/v1beta1/podsecuritypolicies=true,extensions/v1beta1/replicationcontrollers=true" \
  56. --token-auth-file="${TMP_DIR}/tokenauth.csv" \
  57. --logtostderr \
  58. --v=2 \
  59. --service-cluster-ip-range="10.0.0.0/24" >"${API_LOGFILE}" 2>&1 &
  60. APISERVER_PID=$!
  61. if ! kube::util::wait_for_url "${API_HOST}:${API_PORT}/healthz" "apiserver: "; then
  62. kube::log::error "Here are the last 10 lines from kube-apiserver (${API_LOGFILE})"
  63. kube::log::error "=== BEGIN OF LOG ==="
  64. tail -10 "${API_LOGFILE}" || :
  65. kube::log::error "=== END OF LOG ==="
  66. exit 1
  67. fi
  68. kube::log::status "Updating " "${OPENAPI_ROOT_DIR}"
  69. curl -w "\n" -fs "${API_HOST}:${API_PORT}/openapi/v2" | jq -S . > "${OPENAPI_ROOT_DIR}/swagger.json"
  70. kube::log::status "SUCCESS"
  71. # ex: ts=2 sw=2 et filetype=sh