github.com/kubeflow/training-operator@v1.7.0/hack/update-codegen.sh (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2017 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  # This shell is used to auto generate some useful tools for k8s, such as lister,
    18  # informer, deepcopy, defaulter and so on.
    19  
    20  # Ignore shellcheck for IDEs
    21  # shellcheck disable=SC2116
    22  # shellcheck disable=SC2046
    23  # shellcheck disable=SC2006
    24  
    25  set -o errexit
    26  set -o nounset
    27  set -o pipefail
    28  
    29  SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    30  ROOT_PKG=github.com/kubeflow/training-operator
    31  
    32  GET_PKG_LOCATION() {
    33    pkg_name="${1:-}"
    34  
    35    pkg_location="$(go list -m -f '{{.Dir}}' "${pkg_name}" 2>/dev/null)"
    36    if [ "${pkg_location}" = "" ]; then
    37      echo "${pkg_name} is missing. Running 'go mod download'."
    38  
    39      go mod download
    40      pkg_location=$(go list -m -f '{{.Dir}}' "${pkg_name}")
    41    fi
    42    echo "${pkg_location}"
    43  }
    44  
    45  # Grab code-generator version from go.sum
    46  CODEGEN_PKG="$(GET_PKG_LOCATION "k8s.io/code-generator")"
    47  echo ">> Using ${CODEGEN_PKG}"
    48  
    49  # Grab openapi-gen version from go.mod
    50  OPENAPI_PKG="$(GET_PKG_LOCATION 'k8s.io/kube-openapi')"
    51  echo ">> Using ${OPENAPI_PKG}"
    52  
    53  # code-generator does work with go.mod but makes assumptions about
    54  # the project living in `$GOPATH/src`. To work around this and support
    55  # any location; create a temporary directory, use this as an output
    56  # base, and copy everything back once generated.
    57  TEMP_DIR=$(mktemp -d)
    58  cleanup() {
    59      echo ">> Removing ${TEMP_DIR}"
    60      rm -rf ${TEMP_DIR}
    61  }
    62  trap "cleanup" EXIT SIGINT
    63  
    64  echo ">> Temporary output directory ${TEMP_DIR}"
    65  
    66  # Ensure we can execute.
    67  chmod +x ${CODEGEN_PKG}/generate-groups.sh
    68  # generate the code with:
    69  # --output-base    because this script should also be able to run inside the vendor dir of
    70  #                  k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir
    71  #                  instead of the $GOPATH directly. For normal projects this can be dropped.
    72  cd ${SCRIPT_ROOT}
    73  ${CODEGEN_PKG}/generate-groups.sh "client,lister,informer" \
    74      github.com/kubeflow/training-operator/pkg/client github.com/kubeflow/training-operator/pkg/apis \
    75      kubeflow.org:v1 \
    76      --output-base "${TEMP_DIR}" \
    77      --go-header-file hack/boilerplate/boilerplate.go.txt
    78  
    79  # Notice: The code in code-generator does not generate defaulter by default.
    80  # We need to build binary from vendor cmd folder.
    81  #echo "Building defaulter-gen"
    82  #go build -o defaulter-gen ${CODEGEN_PKG}/cmd/defaulter-gen
    83  
    84  # $(go env GOPATH)/bin/defaulter-gen is automatically built from ${CODEGEN_PKG}/generate-groups.sh
    85  echo "Generating defaulters for kubeflow.org/v1"
    86  $(go env GOPATH)/bin/defaulter-gen --input-dirs github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1 \
    87      -O zz_generated.defaults \
    88      --output-package github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1 \
    89      --go-header-file hack/boilerplate/boilerplate.go.txt "$@" \
    90      --output-base "${TEMP_DIR}"
    91  
    92  cd - >/dev/null
    93  
    94  # Notice: The code in kube-openapi does not generate defaulter by default.
    95  # We need to build binary from pkg cmd folder.
    96  echo "Building openapi-gen"
    97  go build -o openapi-gen ${OPENAPI_PKG}/cmd/openapi-gen
    98  
    99  echo "Generating OpenAPI specification for kubeflow.org/v1"
   100  ./openapi-gen --input-dirs github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1 \
   101      --report-filename=hack/violation_exception.list \
   102      --output-package github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1 \
   103      --go-header-file hack/boilerplate/boilerplate.go.txt "$@" \
   104      --output-base "${TEMP_DIR}"
   105  
   106  cd - >/dev/null
   107  
   108  # Copy everything back.
   109  cp -a "${TEMP_DIR}/${ROOT_PKG}/." "${SCRIPT_ROOT}/"
   110  # Clean up binaries we build for update codegen
   111  rm ./openapi-gen