istio.io/istio@v0.0.0-20240520182934-d79c90f27776/bin/update_crds.sh (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2019 Istio 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  set -e
    18  
    19  fail() {
    20    echo "$@" 1>&2
    21    exit 1
    22  }
    23  
    24  API_TMP="$(mktemp -d -u)"
    25  
    26  trap 'rm -rf "${API_TMP}"' EXIT
    27  
    28  SCRIPTPATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    29  ROOTDIR=$(dirname "${SCRIPTPATH}")
    30  cd "${ROOTDIR}"
    31  
    32  REPO="github.com/istio/api"
    33  # using the pseudo version we have in go.mod file. e.g. v.0.0.0-<timestamp>-<SHA>
    34  # first check if there's a replace: e.g. replace istio.io/api => github.com/USER/istioapi v0.0.0-<timestamp>-<SHA>
    35  SHA=$(grep "istio.io/api" go.mod | grep "^replace" | awk -F "-" '{print $NF}')
    36  if [ -n "${SHA}" ]; then
    37    REPO=$(grep "istio.io/api" go.mod | grep "^replace" | awk '{print $4}')
    38  else
    39    SHA=$(grep "istio.io/api" go.mod | head -n1 | awk '{ print $2 }')
    40    if [[ ${SHA} == *"-"* && ! ${SHA} =~ -rc.[0-9]$ && ! ${SHA} =~ -beta.[0-9]$ && ! ${SHA} =~ -alpha.[0-9]$ ]]; then
    41      # not an official release or release candidate, so get the commit sha
    42      SHA=$(echo "${SHA}" | awk -F '-' '{ print $NF }')
    43    fi
    44  fi
    45  
    46  if [ -z "${SHA}" ]; then
    47    fail "Unable to retrieve the commit SHA of istio/api from go.mod file. Not updating the CRD file. Please make sure istio/api exists in the Go module.";
    48  fi
    49  
    50  git clone  "https://${REPO}" "${API_TMP}" && cd "${API_TMP}"
    51  git checkout "${SHA}"
    52  if [ ! -f "${API_TMP}/kubernetes/customresourcedefinitions.gen.yaml" ]; then
    53    echo "Generated Custom Resource Definitions file does not exist in the commit SHA ${SHA}. Not updating the CRD file."
    54    exit
    55  fi
    56  rm -f "${ROOTDIR}/manifests/charts/base/crds/crd-all.gen.yaml"
    57  cp "${API_TMP}/kubernetes/customresourcedefinitions.gen.yaml" "${ROOTDIR}/manifests/charts/base/crds/crd-all.gen.yaml"
    58  
    59  cd "${ROOTDIR}"
    60  
    61  GATEWAY_VERSION=$(grep "gateway-api" go.mod | awk '{ print $2 }')
    62  if [[ ${GATEWAY_VERSION} == *"-"* && ! ${GATEWAY_VERSION} =~ -rc[0-9]$ ]]; then
    63    # not an official release or release candidate, so get the commit sha
    64    SHORT_SHA=$(echo "${GATEWAY_VERSION}" | awk -F '-' '{ print $NF }')
    65    GATEWAY_VERSION=$(curl -s -L -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" "https://api.github.com/repos/kubernetes-sigs/gateway-api/commits/${SHORT_SHA}" | jq -r .sha)
    66  fi
    67  if [ -z "${GATEWAY_VERSION}" ]; then
    68    fail "Unable to retrieve the gateway-api version from go.mod file. Not updating the CRD file.";
    69  fi
    70  
    71  echo "# Generated with \`kubectl kustomize \"https://github.com/kubernetes-sigs/gateway-api/config/crd/experimental?ref=${GATEWAY_VERSION}\"\`" > "${API_TMP}/gateway-api-crd.yaml"
    72  if ! kubectl kustomize "github.com/kubernetes-sigs/gateway-api/config/crd/experimental?ref=${GATEWAY_VERSION}" >> "${API_TMP}/gateway-api-crd.yaml"; then
    73    fail "Unable to generate the CRDs for ${GATEWAY_VERSION}. Not updating the CRD file.";
    74  fi
    75  
    76  rm -f "${ROOTDIR}/tests/integration/pilot/testdata/gateway-api-crd.yaml"
    77  cp "${API_TMP}/gateway-api-crd.yaml" "${ROOTDIR}/tests/integration/pilot/testdata/gateway-api-crd.yaml"