kubernetes/hack/update-codegen.sh
Kubernetes Submit Queue fb5caac2da
Merge pull request #59797 from ipuustin/shell-bugfix3
Automatic merge from submit-queue (batch tested with PRs 59532, 59685, 59797). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Continue fixing bash scripts

**What this PR does / why we need it**:

This PR continues the work of fixing bugs in shell scripts (see https://github.com/kubernetes/kubernetes/pull/59572 for the previous PR). The fixed scripts are `hack/grab-profiles.sh` and `hack/update-codegen.sh`. Most of the issues are found by using `shellcheck`. The goal is to make the scripts work and behave as they did previously, but fix obvious bugs and make them more robust and resilient for irregular input such as file names with special characters or whitespace in them.

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
2018-02-13 18:48:33 -08:00

140 lines
5.3 KiB
Bash
Executable File

#!/bin/bash
# Copyright 2014 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
BUILD_TARGETS=(
vendor/k8s.io/code-generator/cmd/client-gen
vendor/k8s.io/code-generator/cmd/lister-gen
vendor/k8s.io/code-generator/cmd/informer-gen
)
make -C "${KUBE_ROOT}" WHAT="${BUILD_TARGETS[*]}"
clientgen=$(kube::util::find-binary "client-gen")
listergen=$(kube::util::find-binary "lister-gen")
informergen=$(kube::util::find-binary "informer-gen")
# Please do not add any logic to this shell script. Add logic to the go code
# that generates the set-gen program.
#
IFS=" " read -ra GROUP_VERSIONS <<< "$KUBE_AVAILABLE_GROUP_VERSIONS"
GV_DIRS=()
INTERNAL_DIRS=()
for gv in "${GROUP_VERSIONS[@]}"; do
# add items, but strip off any leading apis/ you find to match command expectations
api_dir=$(kube::util::group-version-to-pkg-path "${gv}")
nopkg_dir=${api_dir#pkg/}
nopkg_dir=${nopkg_dir#vendor/k8s.io/api/}
pkg_dir=${nopkg_dir#apis/}
# skip groups that aren't being served, clients for these don't matter
if [[ " ${KUBE_NONSERVER_GROUP_VERSIONS} " == *" ${gv} "* ]]; then
continue
fi
GV_DIRS+=("${pkg_dir}")
# collect internal groups
int_group="${pkg_dir%/*}/"
if [[ "${pkg_dir}" = core/* ]]; then
int_group="api/"
fi
found=0
for dir in "${INTERNAL_DIRS[@]}"; do
if [[ "$dir" = "$int_group" ]]; then
found=1
break
fi
done
if [[ $found = 0 ]]; then
INTERNAL_DIRS+=("$int_group")
fi
done
# delimit by commas for the command
GV_DIRS_CSV=$(IFS=',';echo "${GV_DIRS[*]// /,}";IFS=$)
INTERNAL_DIRS_CSV=$(IFS=',';echo "${INTERNAL_DIRS[*]// /,}";IFS=$)
# This can be called with one flag, --verify-only, so it works for both the
# update- and verify- scripts.
${clientgen} --input-base="k8s.io/kubernetes/pkg/apis" --input="${INTERNAL_DIRS_CSV}" "$@"
${clientgen} --output-base "${KUBE_ROOT}/vendor" --output-package="k8s.io/client-go" --clientset-name="kubernetes" --input-base="k8s.io/kubernetes/vendor/k8s.io/api" --input="${GV_DIRS_CSV}" --go-header-file ${KUBE_ROOT}/hack/boilerplate/boilerplate.go.txt "$@"
mapfile -t listergen_internal_apis < <(
cd "${KUBE_ROOT}"
sort <(find pkg/apis -maxdepth 2 -name types.go -exec dirname {} \;)
)
listergen_internal_apis=("${listergen_internal_apis[@]/#/k8s.io/kubernetes/}")
listergen_internal_apis_csv=$(IFS=,; echo "${listergen_internal_apis[*]}")
${listergen} --input-dirs "${listergen_internal_apis_csv}" "$@"
mapfile -t listergen_external_apis < <(
cd "${KUBE_ROOT}/staging/src"
sort <(find k8s.io/api -name types.go -exec dirname {} \;)
)
listergen_external_apis_csv=$(IFS=,; echo "${listergen_external_apis[*]}")
${listergen} --output-base "${KUBE_ROOT}/vendor" --output-package "k8s.io/client-go/listers" --input-dirs "${listergen_external_apis_csv}" --go-header-file ${KUBE_ROOT}/hack/boilerplate/boilerplate.go.txt "$@"
mapfile -t informergen_internal_apis < <(
cd "${KUBE_ROOT}"
sort <(find pkg/apis -maxdepth 2 -name types.go -exec dirname {} \;)
)
informergen_internal_apis=("${informergen_internal_apis[@]/#/k8s.io/kubernetes/}")
informergen_internal_apis_csv=$(IFS=,; echo "${informergen_internal_apis[*]}")
${informergen} \
--input-dirs "${informergen_internal_apis_csv}" \
--internal-clientset-package k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset \
--listers-package k8s.io/kubernetes/pkg/client/listers \
--go-header-file ${KUBE_ROOT}/hack/boilerplate/boilerplate.go.txt \
"$@"
mapfile -t informergen_external_apis < <(
cd "${KUBE_ROOT}/staging/src"
# because client-gen doesn't do policy/v1alpha1, we have to skip it too
sort <(find k8s.io/api -name types.go -exec dirname {} \;) | grep -v pkg.apis.policy.v1alpha1
)
informergen_external_apis_csv=$(IFS=,; echo "${informergen_external_apis[*]}")
${informergen} \
--output-base "${KUBE_ROOT}/vendor" \
--output-package "k8s.io/client-go/informers" \
--single-directory \
--input-dirs "${informergen_external_apis_csv}" \
--versioned-clientset-package k8s.io/client-go/kubernetes \
--listers-package k8s.io/client-go/listers \
--go-header-file ${KUBE_ROOT}/hack/boilerplate/boilerplate.go.txt \
"$@"
# You may add additional calls of code generators like set-gen above.
# call generation on sub-project for now
CODEGEN_PKG=./vendor/k8s.io/code-generator vendor/k8s.io/code-generator/hack/update-codegen.sh
CODEGEN_PKG=./vendor/k8s.io/code-generator vendor/k8s.io/kube-aggregator/hack/update-codegen.sh
CODEGEN_PKG=./vendor/k8s.io/code-generator vendor/k8s.io/sample-apiserver/hack/update-codegen.sh
CODEGEN_PKG=./vendor/k8s.io/code-generator vendor/k8s.io/sample-controller/hack/update-codegen.sh
CODEGEN_PKG=./vendor/k8s.io/code-generator vendor/k8s.io/apiextensions-apiserver/hack/update-codegen.sh
CODEGEN_PKG=./vendor/k8s.io/code-generator vendor/k8s.io/metrics/hack/update-codegen.sh