github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/hack/make-rules/update/codegen.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2021 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  
    16  set -o errexit
    17  set -o nounset
    18  set -o pipefail
    19  
    20  # darwin is great
    21  SED=sed
    22  if which gsed &>/dev/null; then
    23    SED=gsed
    24  fi
    25  if ! (${SED} --version 2>&1 | grep -q GNU); then
    26    echo "!!! GNU sed is required.  If on OS X, use 'brew install gnu-sed'." >&2
    27    exit 1
    28  fi
    29  
    30  REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd -P)"
    31  cd $REPO_ROOT
    32  
    33  echo "Ensuring go version."
    34  source ./hack/build/setup-go.sh
    35  
    36  # build codegen tools
    37  echo "Install codegen tools."
    38  cd "hack/tools"
    39  clientgen=${REPO_ROOT}/_bin/client-gen
    40  go build -o "${clientgen}" k8s.io/code-generator/cmd/client-gen
    41  deepcopygen=${REPO_ROOT}/_bin/deepcopy-gen
    42  go build -o "${deepcopygen}" k8s.io/code-generator/cmd/deepcopy-gen
    43  informergen=${REPO_ROOT}/_bin/informer-gen
    44  go build -o "${informergen}" k8s.io/code-generator/cmd/informer-gen
    45  listergen=${REPO_ROOT}/_bin/lister-gen
    46  go build -o "${listergen}" k8s.io/code-generator/cmd/lister-gen
    47  go_bindata=${REPO_ROOT}/_bin/go-bindata
    48  go build -o "${go_bindata}" github.com/go-bindata/go-bindata/v3/go-bindata
    49  controller_gen=${REPO_ROOT}/_bin/controller-gen
    50  go build -o "${controller_gen}" sigs.k8s.io/controller-tools/cmd/controller-gen
    51  protoc_gen_go="${REPO_ROOT}/_bin/protoc-gen-go" # golang protobuf plugin
    52  GOBIN="${REPO_ROOT}/_bin" go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.32.0
    53  GOBIN="${REPO_ROOT}/_bin" go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.3.0
    54  
    55  cd "${REPO_ROOT}"
    56  ensure-protoc-deps(){
    57    # Install protoc
    58    if [[ ! -f "_bin/protoc/bin/protoc" ]]; then
    59      mkdir -p _bin/protoc
    60      OS="linux"
    61      if [[ $(uname -s) == "Darwin" ]]; then
    62            OS="osx"
    63      fi
    64      ARCH="x86_64"
    65      if [[ $(uname -m) == "arm64" ]]; then
    66        ARCH="aarch_64"
    67      fi
    68      # See https://developers.google.com/protocol-buffers/docs/news/2022-05-06 for
    69      # a note on the versioning scheme change.
    70      PROTOC_VERSION=25.2
    71      PROTOC_ZIP="protoc-${PROTOC_VERSION}-${OS}-${ARCH}.zip"
    72      curl -OL "https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/${PROTOC_ZIP}"
    73      unzip -o $PROTOC_ZIP -d _bin/protoc bin/protoc
    74      unzip -o $PROTOC_ZIP -d _bin/protoc 'include/*'
    75      rm -f $PROTOC_ZIP
    76    fi
    77  
    78    # Clone proto dependencies.
    79    if ! [[ -f "${REPO_ROOT}"/_bin/protoc/include/googleapis/google/api/annotations.proto ]]; then
    80      # This SHA was retrieved on 2024-02-09.
    81      GOOGLEAPIS_VERSION="e183baf16610b925fc99e628fc539e118bc3348a"
    82      >/dev/null pushd "${REPO_ROOT}"/_bin/protoc/include
    83      curl -OL "https://github.com/googleapis/googleapis/archive/${GOOGLEAPIS_VERSION}.zip"
    84      >/dev/null unzip -o ${GOOGLEAPIS_VERSION}.zip
    85      mv googleapis-${GOOGLEAPIS_VERSION} googleapis
    86      rm -f ${GOOGLEAPIS_VERSION}.zip
    87      >/dev/null popd
    88    fi
    89  }
    90  ensure-protoc-deps
    91  
    92  echo "Finished installations."
    93  do_clean=${1:-}
    94  
    95  # FAKE_GOPATH is for mimicking GOPATH layout.
    96  # K8s code-generator tools all assume the structure of ${GOPATH}/src/k8s.io/...,
    97  # faking GOPATH so that the output are dropped correctly.
    98  # All the clean/copy functions below are for transferring output to this repo.
    99  FAKE_GOPATH=""
   100  
   101  cleanup() {
   102    if [[ -n ${FAKE_GOPATH:-} ]]; then chmod -R u+rwx $FAKE_GOPATH && rm -rf $FAKE_GOPATH; fi
   103    if [[ -n ${TEMP_GOCACHE:-} ]]; then rm -rf $TEMP_GOCACHE; fi
   104  }
   105  trap cleanup EXIT
   106  
   107  ensure-in-gopath() {
   108    FAKE_GOPATH=$(mktemp -d -t codegen.gopath.XXXX)
   109  
   110    fake_repopath=$FAKE_GOPATH/src/sigs.k8s.io/prow
   111    mkdir -p "$(dirname "$fake_repopath")"
   112    if [[ -n "$do_clean" ]]; then
   113      cp -LR "${REPO_ROOT}/" "$fake_repopath"
   114    else
   115      cp -R "${REPO_ROOT}/" "$fake_repopath"
   116    fi
   117  
   118    export GOPATH=$FAKE_GOPATH
   119    cd "$fake_repopath"
   120  }
   121  
   122  gen-prow-config-documented() {
   123    go run ./hack/gen-prow-documented
   124  }
   125  
   126  # copyfiles will copy all files in 'path' in the fake gopath over to the
   127  # workspace directory as the code generators output directly into GOPATH,
   128  # meaning without this function the generated files are left in /tmp
   129  copyfiles() {
   130    path=$1
   131    name=$2
   132    if [[ ! -d "$path" ]]; then
   133      return 0
   134    fi
   135    (
   136      cd "$GOPATH/src/sigs.k8s.io/prow/$path"
   137      find "." -name "$name" -exec cp {} "$REPO_ROOT/$path/{}" \;
   138    )
   139  }
   140  
   141  # clean will delete files matching name in path.
   142  #
   143  # When inside bazel test the files are read-only.
   144  # Any attempts to write a file that already exists will fail.
   145  # So resolve by deleting the files before generating them.
   146  clean() {
   147    path=$1
   148    name=$2
   149    if [[ ! -d "$path" || -z "$do_clean" ]]; then
   150      return 0
   151    fi
   152    find "$path" -name "$name" -delete
   153    find "${REPO_ROOT}"/"$path" -name "$name" -delete
   154  }
   155  
   156  gen-deepcopy() {
   157    clean pkg/apis 'zz_generated.deepcopy.go'
   158    clean pkg/config 'zz_generated.deepcopy.non_k8s.go'
   159    echo "Generating DeepCopy() methods..." >&2
   160    "$deepcopygen" \
   161      --go-header-file hack/boilerplate/boilerplate.generated.go.txt \
   162      --input-dirs sigs.k8s.io/prow/pkg/apis/prowjobs/v1 \
   163      --output-file-base zz_generated.deepcopy \
   164      --bounding-dirs sigs.k8s.io/prow/pkg/apis
   165    copyfiles "pkg/apis" "zz_generated.deepcopy.go"
   166  
   167    "$deepcopygen" \
   168      --go-header-file hack/boilerplate/boilerplate.generated.go.txt \
   169      --input-dirs sigs.k8s.io/prow/pkg/config \
   170      --output-file-base zz_generated.deepcopy
   171    copyfiles "pkg/config" "zz_generated.deepcopy.go"
   172  
   173  }
   174  
   175  gen-client() {
   176    clean pkg/client/clientset '*.go'
   177    echo "Generating client..." >&2
   178    "$clientgen" \
   179      --go-header-file hack/boilerplate/boilerplate.generated.go.txt \
   180      --clientset-name versioned \
   181      --input-base "" \
   182      --input sigs.k8s.io/prow/pkg/apis/prowjobs/v1 \
   183      --output-package sigs.k8s.io/prow/pkg/client/clientset
   184    copyfiles "./pkg/client/clientset" "*.go"
   185  
   186    clean pkg/pipeline/clientset '*.go'
   187    echo "Generating client for pipeline..." >&2
   188    "$clientgen" \
   189      --go-header-file hack/boilerplate/boilerplate.generated.go.txt \
   190      --clientset-name versioned \
   191      --input-base "" \
   192      --input github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1 \
   193      --output-package sigs.k8s.io/prow/pkg/pipeline/clientset
   194    copyfiles "./pkg/pipeline/clientset" "*.go"
   195  }
   196  
   197  gen-lister() {
   198    clean pkg/client/listers '*.go'
   199    echo "Generating lister..." >&2
   200    "$listergen" \
   201      --go-header-file hack/boilerplate/boilerplate.generated.go.txt \
   202      --input-dirs sigs.k8s.io/prow/pkg/apis/prowjobs/v1 \
   203      --output-package sigs.k8s.io/prow/pkg/client/listers
   204    copyfiles "./pkg/client/listers" "*.go"
   205  
   206    clean pkg/pipeline/listers '*.go'
   207    echo "Generating lister for pipeline..." >&2
   208    "$listergen" \
   209      --go-header-file hack/boilerplate/boilerplate.generated.go.txt \
   210      --input-dirs github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1 \
   211      --output-package sigs.k8s.io/prow/pkg/pipeline/listers
   212    copyfiles "./pkg/pipeline/listers" "*.go"
   213  }
   214  
   215  gen-informer() {
   216    clean pkg/client/informers '*.go'
   217    echo "Generating informer..." >&2
   218    "$informergen" \
   219      --go-header-file hack/boilerplate/boilerplate.generated.go.txt \
   220      --input-dirs sigs.k8s.io/prow/pkg/apis/prowjobs/v1 \
   221      --versioned-clientset-package sigs.k8s.io/prow/pkg/client/clientset/versioned \
   222      --listers-package sigs.k8s.io/prow/pkg/client/listers \
   223      --output-package sigs.k8s.io/prow/pkg/client/informers
   224    copyfiles "./pkg/client/informers" "*.go"
   225  
   226    clean pkg/pipeline/informers '*.go'
   227    echo "Generating informer for pipeline..." >&2
   228    "$informergen" \
   229      --go-header-file hack/boilerplate/boilerplate.generated.go.txt \
   230      --input-dirs github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1 \
   231      --versioned-clientset-package sigs.k8s.io/prow/pkg/pipeline/clientset/versioned \
   232      --listers-package sigs.k8s.io/prow/pkg/pipeline/listers \
   233      --output-package sigs.k8s.io/prow/pkg/pipeline/informers
   234    copyfiles "./pkg/pipeline/informers" "*.go"
   235  }
   236  
   237  gen-spyglass-bindata(){
   238    cd pkg/spyglass/lenses/common/
   239    echo "Generating spyglass bindata..." >&2
   240    $go_bindata -pkg=common static/
   241    gofmt -s -w ./
   242    cd - >/dev/null
   243  }
   244  
   245  gen-prowjob-crd(){
   246    clean "./config/prow/cluster" "prowjob_customresourcedefinition.yaml"
   247    echo "Generating prowjob crd..." >&2
   248    if [[ -z ${HOME:-} ]]; then export HOME=$PWD; fi
   249    $controller_gen crd:preserveUnknownFields=false,crdVersions=v1 paths=./pkg/apis/prowjobs/v1 output:stdout \
   250      | $SED '/^$/d' \
   251      | $SED '/^spec:.*/a  \  preserveUnknownFields: false' \
   252      | $SED '/^  annotations.*/a  \    api-approved.kubernetes.io: https://github.com/kubernetes/test-infra/pull/8669' \
   253      | $SED '/^          status:/r'<(cat<<EOF
   254              anyOf:
   255              - not:
   256                  properties:
   257                    state:
   258                      enum:
   259                      - "success"
   260                      - "failure"
   261                      - "error"
   262              - required:
   263                - completionTime
   264  EOF
   265      ) > ./config/prow/cluster/prowjob-crd/prowjob_customresourcedefinition.yaml
   266    copyfiles "./config/prow/cluster/prowjob-crd" "prowjob_customresourcedefinition.yaml"
   267    unset HOME
   268  }
   269  
   270  # Generate gRPC stubs for a given protobuf file.
   271  gen-proto-stubs(){
   272    local dir
   273    dir="$(dirname "$1")"
   274  
   275    # We need the "paths=source_relative" bits to prevent a nested directory
   276    # structure (so that the generated files can sit next to the .proto files,
   277    # instead of under a "k8.io/test-infra/prow/..." subfolder).
   278    "${REPO_ROOT}/_bin/protoc/bin/protoc" \
   279      "--plugin=${protoc_gen_go}" \
   280      "--proto_path=${REPO_ROOT}/_bin/protoc/include/google/protobuf" \
   281      "--proto_path=${REPO_ROOT}/_bin/protoc/include/googleapis" \
   282      "--proto_path=${dir}" \
   283      --go_out="${dir}" --go_opt=paths=source_relative \
   284      --go-grpc_out="${dir}" --go-grpc_opt=paths=source_relative \
   285      "$1"
   286  }
   287  
   288  gen-all-proto-stubs(){
   289    echo >&2 "Generating proto stubs"
   290  
   291    # Expose the golang protobuf plugin binaries (protoc-gen-go,
   292    # protoc-gen-go-grpc) to the PATH so that protoc can find it.
   293    export PATH="${REPO_ROOT}/_bin:$PATH"
   294  
   295    while IFS= read -r -d '' proto; do
   296      echo >&2 "  $proto"
   297      gen-proto-stubs "$proto"
   298    done < <(find "${REPO_ROOT}" \
   299      -not '(' -path "${REPO_ROOT}/vendor" -prune ')' \
   300      -not '(' -path "${REPO_ROOT}/node_modules" -prune ')' \
   301      -not '(' -path "${REPO_ROOT}/_bin" -prune ')' \
   302      -name '*.proto' \
   303      -print0 | sort -z)
   304  }
   305  
   306  gen-gangway-apidescriptorpb-for-cloud-endpoints(){
   307    echo >&2 "Generating self-describing proto stub (gangway_api_descriptor.pb) for gangway.proto"
   308  
   309    "${REPO_ROOT}/_bin/protoc/bin/protoc" \
   310      "--proto_path=${REPO_ROOT}/_bin/protoc/include/google/protobuf" \
   311      "--proto_path=${REPO_ROOT}/_bin/protoc/include/googleapis" \
   312      "--proto_path=${REPO_ROOT}/pkg/gangway" \
   313      --include_imports \
   314      --include_source_info \
   315      --descriptor_set_out "${REPO_ROOT}/pkg/gangway/gangway_api_descriptor.pb" \
   316      gangway.proto
   317  }
   318  
   319  gen-prow-config-documented
   320  
   321  export GO111MODULE=off
   322  ensure-in-gopath
   323  old=${GOCACHE:-}
   324  export TEMP_GOCACHE=$(mktemp -d -t codegen.gocache.XXXX)
   325  export GOCACHE=$TEMP_GOCACHE
   326  export GO111MODULE=on
   327  export GOPROXY=https://proxy.golang.org
   328  export GOSUMDB=sum.golang.org
   329  go mod vendor
   330  export GO111MODULE=off
   331  export GOCACHE=$old
   332  gen-deepcopy
   333  gen-client
   334  gen-lister
   335  gen-informer
   336  gen-spyglass-bindata
   337  gen-prowjob-crd
   338  export GO111MODULE=on
   339  
   340  gen-all-proto-stubs
   341  gen-gangway-apidescriptorpb-for-cloud-endpoints