Skip to content

Commit 3210f46

Browse files
committed
Add validation-gen to codegen scripts
Add validation-gen to repository wide codegen scripts and introduce `./hack/update-codegen.sh validation` as a quick way to run validation-gen.
1 parent e892dfa commit 3210f46

File tree

2 files changed

+114
-4
lines changed

2 files changed

+114
-4
lines changed

hack/update-codegen.sh

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ fi
5252
# Generate a list of directories we don't want to play in.
5353
DIRS_TO_AVOID=()
5454
kube::util::read-array DIRS_TO_AVOID < <(
55-
git ls-files -cmo --exclude-standard -- ':!:vendor/*' ':(glob)*/**/go.work' \
55+
git ls-files -cmo --exclude-standard \
56+
-- \
57+
':!:vendor/*' \
58+
':(glob)*/**/go.work' \
59+
':(glob)**/_codegenignore/**' \
5660
| while read -r F; do \
5761
echo ':!:'"$(dirname "${F}")"; \
5862
done
@@ -62,15 +66,20 @@ function git_find() {
6266
# Similar to find but faster and easier to understand. We want to include
6367
# modified and untracked files because this might be running against code
6468
# which is not tracked by git yet.
65-
git ls-files -cmo --exclude-standard ':!:vendor/*' "${DIRS_TO_AVOID[@]}" "$@"
69+
git ls-files -cmo --exclude-standard \
70+
':!:vendor/*' \
71+
"${DIRS_TO_AVOID[@]}" \
72+
"$@"
6673
}
6774

6875
function git_grep() {
6976
# We want to include modified and untracked files because this might be
7077
# running against code which is not tracked by git yet.
7178
# We need vendor exclusion added at the end since it has to be part of
7279
# the pathspecs which are specified last.
73-
git grep --untracked "$@" ':!:vendor/*' "${DIRS_TO_AVOID[@]}"
80+
git grep --untracked "$@" \
81+
':!:vendor/*' \
82+
"${DIRS_TO_AVOID[@]}"
7483
}
7584

7685
# Generate a list of all files that have a `+k8s:` comment-tag. This will be
@@ -380,6 +389,74 @@ function codegen::defaults() {
380389
fi
381390
}
382391

392+
# Validation generation
393+
#
394+
# Any package that wants validation functions generated must include a
395+
# comment-tag in column 0 of one file of the form:
396+
# // +k8s:validation-gen=<VALUE>
397+
#
398+
# The <VALUE> depends on context:
399+
# on packages:
400+
# *: all exported types are candidates for having validation generated
401+
# FIELDNAME: any type with a field of this name is a candidate for
402+
# having validation generated
403+
# on types:
404+
# true: always generate validation for this type
405+
# false: never generate validation for this type
406+
function codegen::validation() {
407+
# Build the tool.
408+
GOPROXY=off go install \
409+
k8s.io/code-generator/cmd/validation-gen
410+
411+
# TODO: Where do we want these output? It should be somewhere internal..
412+
# The result file, in each pkg, of validation generation.
413+
local output_file="${GENERATED_FILE_PREFIX}validations.go"
414+
415+
# All directories that request any form of validation generation.
416+
if [[ "${DBG_CODEGEN}" == 1 ]]; then
417+
kube::log::status "DBG: finding all +k8s:validation-gen tags"
418+
fi
419+
local tag_dirs=()
420+
kube::util::read-array tag_dirs < <( \
421+
grep -l --null '+k8s:validation-gen=' "${ALL_K8S_TAG_FILES[@]}" \
422+
| while read -r -d $'\0' F; do dirname "${F}"; done \
423+
| sort -u)
424+
if [[ "${DBG_CODEGEN}" == 1 ]]; then
425+
kube::log::status "DBG: found ${#tag_dirs[@]} +k8s:validation-gen tagged dirs"
426+
fi
427+
428+
local tag_pkgs=()
429+
for dir in "${tag_dirs[@]}"; do
430+
tag_pkgs+=("./$dir")
431+
done
432+
433+
local extra_pkgs=(
434+
k8s.io/apimachinery/pkg/apis/meta/v1
435+
)
436+
437+
kube::log::status "Generating validation code for ${#tag_pkgs[@]} targets"
438+
if [[ "${DBG_CODEGEN}" == 1 ]]; then
439+
kube::log::status "DBG: running validation-gen for:"
440+
for dir in "${tag_dirs[@]}"; do
441+
kube::log::status "DBG: $dir"
442+
done
443+
fi
444+
445+
git_find -z ':(glob)**'/"${output_file}" | xargs -0 rm -f
446+
447+
validation-gen \
448+
-v "${KUBE_VERBOSE}" \
449+
--go-header-file "${BOILERPLATE_FILENAME}" \
450+
--output-file "${output_file}" \
451+
$(printf -- " --extra-pkg %s" "${extra_pkgs[@]}") \
452+
"${tag_pkgs[@]}" \
453+
"$@"
454+
455+
if [[ "${DBG_CODEGEN}" == 1 ]]; then
456+
kube::log::status "Generated validation code"
457+
fi
458+
}
459+
383460
# Conversion generation
384461

385462
# Any package that wants conversion functions generated into it must

staging/src/k8s.io/code-generator/kube_codegen.sh

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function kube::codegen::internal::grep() {
4848
--exclude-dir vendor
4949
}
5050

51-
# Generate tagged helper code: conversions, deepcopy, and defaults
51+
# Generate tagged helper code: conversions, deepcopy, defaults and validations
5252
#
5353
# USAGE: kube::codegen::gen_helpers [FLAGS] <input-dir>
5454
#
@@ -111,6 +111,7 @@ function kube::codegen::gen_helpers() {
111111
conversion-gen"${CODEGEN_VERSION_SPEC}"
112112
deepcopy-gen"${CODEGEN_VERSION_SPEC}"
113113
defaulter-gen"${CODEGEN_VERSION_SPEC}"
114+
validation-gen"${CODEGEN_VERSION_SPEC}"
114115
)
115116
# shellcheck disable=2046 # printf word-splitting is intentional
116117
GO111MODULE=on go install $(printf "k8s.io/code-generator/cmd/%s " "${BINS[@]}")
@@ -150,6 +151,38 @@ function kube::codegen::gen_helpers() {
150151
"${input_pkgs[@]}"
151152
fi
152153

154+
# Validations
155+
#
156+
local input_pkgs=()
157+
while read -r dir; do
158+
pkg="$(cd "${dir}" && GO111MODULE=on go list -find .)"
159+
input_pkgs+=("${pkg}")
160+
done < <(
161+
( kube::codegen::internal::grep -l --null \
162+
-e '^\s*//\s*+k8s:validation-gen=' \
163+
-r "${in_dir}" \
164+
--include '*.go' \
165+
|| true \
166+
) | while read -r -d $'\0' F; do dirname "${F}"; done \
167+
| LC_ALL=C sort -u
168+
)
169+
170+
if [ "${#input_pkgs[@]}" != 0 ]; then
171+
echo "Generating validation code for ${#input_pkgs[@]} targets"
172+
173+
kube::codegen::internal::findz \
174+
"${in_dir}" \
175+
-type f \
176+
-name zz_generated.validations.go \
177+
| xargs -0 rm -f
178+
179+
"${gobin}/validation-gen" \
180+
-v "${v}" \
181+
--output-file zz_generated.validations.go \
182+
--go-header-file "${boilerplate}" \
183+
"${input_pkgs[@]}"
184+
fi
185+
153186
# Defaults
154187
#
155188
local input_pkgs=()

0 commit comments

Comments
 (0)