52
52
# Generate a list of directories we don't want to play in.
53
53
DIRS_TO_AVOID=()
54
54
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/**' \
56
60
| while read -r F; do \
57
61
echo ' :!:' " $( dirname " ${F} " ) " ; \
58
62
done
@@ -62,15 +66,20 @@ function git_find() {
62
66
# Similar to find but faster and easier to understand. We want to include
63
67
# modified and untracked files because this might be running against code
64
68
# 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
+ " $@ "
66
73
}
67
74
68
75
function git_grep() {
69
76
# We want to include modified and untracked files because this might be
70
77
# running against code which is not tracked by git yet.
71
78
# We need vendor exclusion added at the end since it has to be part of
72
79
# the pathspecs which are specified last.
73
- git grep --untracked " $@ " ' :!:vendor/*' " ${DIRS_TO_AVOID[@]} "
80
+ git grep --untracked " $@ " \
81
+ ' :!:vendor/*' \
82
+ " ${DIRS_TO_AVOID[@]} "
74
83
}
75
84
76
85
# Generate a list of all files that have a `+k8s:` comment-tag. This will be
@@ -380,6 +389,74 @@ function codegen::defaults() {
380
389
fi
381
390
}
382
391
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
+
383
460
# Conversion generation
384
461
385
462
# Any package that wants conversion functions generated into it must
0 commit comments