k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/hack/update-openapi-spec.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2016 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # Script to fetch latest openapi spec.
    18  # Puts the updated spec at api/openapi-spec/
    19  
    20  set -o errexit
    21  set -o nounset
    22  set -o pipefail
    23  
    24  KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    25  DISCOVERY_ROOT_DIR="${KUBE_ROOT}/api/discovery"
    26  OPENAPI_ROOT_DIR="${KUBE_ROOT}/api/openapi-spec"
    27  source "${KUBE_ROOT}/hack/lib/init.sh"
    28  
    29  kube::util::require-jq
    30  kube::golang::setup_env
    31  kube::etcd::install
    32  
    33  # We need to call `make` here because that includes all of the compile and link
    34  # flags that we use for a production build, which we need for this script.
    35  make -C "${KUBE_ROOT}" WHAT=cmd/kube-apiserver
    36  
    37  function cleanup()
    38  {
    39      if [[ -n ${APISERVER_PID-} ]]; then
    40        kill "${APISERVER_PID}" 1>&2 2>/dev/null
    41        wait "${APISERVER_PID}" || true
    42      fi
    43      unset APISERVER_PID
    44  
    45      kube::etcd::cleanup
    46  
    47      kube::log::status "Clean up complete"
    48  }
    49  
    50  trap cleanup EXIT SIGINT
    51  
    52  TMP_DIR=${TMP_DIR:-$(kube::realpath "$(mktemp -d -t "$(basename "$0").XXXXXX")")}
    53  ETCD_HOST=${ETCD_HOST:-127.0.0.1}
    54  ETCD_PORT=${ETCD_PORT:-2379}
    55  API_PORT=${API_PORT:-8050}
    56  API_HOST=${API_HOST:-127.0.0.1}
    57  API_LOGFILE=${API_LOGFILE:-${TMP_DIR}/openapi-api-server.log}
    58  
    59  kube::etcd::start
    60  
    61  echo "dummy_token,admin,admin" > "${TMP_DIR}/tokenauth.csv"
    62  
    63  # setup envs for TokenRequest required flags
    64  SERVICE_ACCOUNT_LOOKUP=${SERVICE_ACCOUNT_LOOKUP:-true}
    65  SERVICE_ACCOUNT_KEY=${SERVICE_ACCOUNT_KEY:-${TMP_DIR}/kube-serviceaccount.key}
    66  # Generate ServiceAccount key if needed
    67  if [[ ! -f "${SERVICE_ACCOUNT_KEY}" ]]; then
    68    mkdir -p "$(dirname "${SERVICE_ACCOUNT_KEY}")"
    69    openssl genrsa -out "${SERVICE_ACCOUNT_KEY}" 2048 2>/dev/null
    70  fi
    71  
    72  # Start kube-apiserver
    73  # omit enums from static openapi snapshots used to generate clients until #109177 is resolved
    74  # TODO(aojea) remove ConsistentListFromCache after https://issues.k8s.io/123674
    75  kube::log::status "Starting kube-apiserver"
    76  kube-apiserver \
    77    --bind-address="${API_HOST}" \
    78    --secure-port="${API_PORT}" \
    79    --etcd-servers="http://${ETCD_HOST}:${ETCD_PORT}" \
    80    --advertise-address="10.10.10.10" \
    81    --cert-dir="${TMP_DIR}/certs" \
    82    --feature-gates=AllAlpha=true,OpenAPIEnums=false,ConsistentListFromCache=false \
    83    --runtime-config="api/all=true" \
    84    --token-auth-file="${TMP_DIR}/tokenauth.csv" \
    85    --authorization-mode=RBAC \
    86    --service-account-key-file="${SERVICE_ACCOUNT_KEY}" \
    87    --service-account-lookup="${SERVICE_ACCOUNT_LOOKUP}" \
    88    --service-account-issuer="https://kubernetes.default.svc" \
    89    --service-account-signing-key-file="${SERVICE_ACCOUNT_KEY}" \
    90    --v=2 \
    91    --service-cluster-ip-range="10.0.0.0/24" >"${API_LOGFILE}" 2>&1 &
    92  APISERVER_PID=$!
    93  
    94  if ! kube::util::wait_for_url "https://${API_HOST}:${API_PORT}/healthz" "apiserver: "; then
    95    kube::log::error "Here are the last 10 lines from kube-apiserver (${API_LOGFILE})"
    96    kube::log::error "=== BEGIN OF LOG ==="
    97    tail -10 "${API_LOGFILE}" >&2 || :
    98    kube::log::error "=== END OF LOG ==="
    99    exit 1
   100  fi
   101  
   102  kube::log::status "Updating aggregated discovery"
   103  
   104  rm -fr "${DISCOVERY_ROOT_DIR}"
   105  mkdir -p "${DISCOVERY_ROOT_DIR}"
   106  curl -kfsS -H 'Authorization: Bearer dummy_token' -H 'Accept: application/json;g=apidiscovery.k8s.io;v=v2;as=APIGroupDiscoveryList' "https://${API_HOST}:${API_PORT}/apis" | jq -S . > "${DISCOVERY_ROOT_DIR}/aggregated_v2.json"
   107  
   108  # Deprecated, remove before v1.33
   109  curl -kfsS -H 'Authorization: Bearer dummy_token' -H 'Accept: application/json;g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList' "https://${API_HOST}:${API_PORT}/apis" | jq -S . > "${DISCOVERY_ROOT_DIR}/aggregated_v2beta1.json"
   110  
   111  kube::log::status "Updating " "${OPENAPI_ROOT_DIR} for OpenAPI v2"
   112  
   113  rm -f "${OPENAPI_ROOT_DIR}/swagger.json"
   114  curl -w "\n" -kfsS -H 'Authorization: Bearer dummy_token' \
   115    "https://${API_HOST}:${API_PORT}/openapi/v2" \
   116    | jq -S '.info.version="unversioned"' \
   117    > "${OPENAPI_ROOT_DIR}/swagger.json"
   118  
   119  kube::log::status "Updating " "${OPENAPI_ROOT_DIR}/v3 for OpenAPI v3"
   120  
   121  mkdir -p "${OPENAPI_ROOT_DIR}/v3"
   122  # clean up folder, note that some files start with dot like
   123  # ".well-known__openid-configuration_openapi.json"
   124  rm -r "${OPENAPI_ROOT_DIR}"/v3/{*,.*} || true
   125  
   126  rm -rf "${OPENAPI_ROOT_DIR}/v3/*"
   127  curl -w "\n" -kfsS -H 'Authorization: Bearer dummy_token' \
   128    "https://${API_HOST}:${API_PORT}/openapi/v3" \
   129    | jq -r '.paths | to_entries | .[].key' \
   130    | while read -r group; do
   131      kube::log::status "Updating OpenAPI spec and discovery for group ${group}"
   132      OPENAPI_FILENAME="${group}_openapi.json"
   133      OPENAPI_FILENAME_ESCAPED="${OPENAPI_FILENAME//\//__}"
   134      OPENAPI_PATH="${OPENAPI_ROOT_DIR}/v3/${OPENAPI_FILENAME_ESCAPED}"
   135      curl -w "\n" -kfsS -H 'Authorization: Bearer dummy_token' \
   136        "https://${API_HOST}:${API_PORT}/openapi/v3/{$group}" \
   137        | jq -S '.info.version="unversioned"' \
   138        > "$OPENAPI_PATH"
   139  
   140      if [[ "${group}" == "api"* ]]; then
   141        DISCOVERY_FILENAME="${group}.json"
   142        DISCOVERY_FILENAME_ESCAPED="${DISCOVERY_FILENAME//\//__}"
   143        DISCOVERY_PATH="${DISCOVERY_ROOT_DIR}/${DISCOVERY_FILENAME_ESCAPED}"
   144        curl -kfsS -H 'Authorization: Bearer dummy_token' "https://${API_HOST}:${API_PORT}/{$group}" | jq -S . > "$DISCOVERY_PATH"
   145      fi
   146  done
   147  
   148  kube::log::status "SUCCESS"
   149  
   150  # ex: ts=2 sw=2 et filetype=sh