Skip to content

Commit 8105dd7

Browse files
committed
Restore build-tag for conversion and defaulter gen
The build-tag flag is removed but it is still useful to provide a way to inject a custom build tag that is different from the default build tag. This custom build tag will allow gengo to load certain conversion and defaults funcs that reside in files with default build tag which is usually ignored by gengo. Signed-off-by: Vu Dinh <[email protected]>
1 parent 9d967ff commit 8105dd7

File tree

6 files changed

+28
-7
lines changed

6 files changed

+28
-7
lines changed

staging/src/k8s.io/code-generator/cmd/conversion-gen/args/args.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"fmt"
2121

2222
"github.com/spf13/pflag"
23+
24+
"k8s.io/gengo/v2"
2325
)
2426

2527
// DefaultBasePeerDirs are the peer-dirs nearly everybody will use, i.e. those coming from
@@ -57,13 +59,20 @@ type Args struct {
5759
// GoHeaderFile is the path to a boilerplate header file for generated
5860
// code.
5961
GoHeaderFile string
62+
63+
// GeneratedBuildTag is the tag used to identify code generated by execution
64+
// of this type. Each generator should use a different tag, and different
65+
// groups of generators (external API that depends on Kube generations) should
66+
// keep tags distinct as well.
67+
GeneratedBuildTag string
6068
}
6169

6270
// New returns default arguments for the generator.
6371
func New() *Args {
6472
return &Args{
65-
BasePeerDirs: DefaultBasePeerDirs,
66-
SkipUnsafe: false,
73+
BasePeerDirs: DefaultBasePeerDirs,
74+
SkipUnsafe: false,
75+
GeneratedBuildTag: gengo.StdBuildTag,
6776
}
6877
}
6978

@@ -81,6 +90,7 @@ func (args *Args) AddFlags(fs *pflag.FlagSet) {
8190
"If true, will not generate code using unsafe pointer conversions; resulting code may be slower.")
8291
fs.StringVar(&args.GoHeaderFile, "go-header-file", "",
8392
"the path to a file containing boilerplate header text; the string \"YEAR\" will be replaced with the current 4-digit year")
93+
fs.StringVar(&args.GeneratedBuildTag, "build-tag", args.GeneratedBuildTag, "A Go build tag to use to identify files generated by this command. Should be unique.")
8494
}
8595

8696
// Validate checks the given arguments.

staging/src/k8s.io/code-generator/cmd/conversion-gen/generators/conversion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func getManualConversionFunctions(context *generator.Context, pkg *types.Package
196196
}
197197

198198
func GetTargets(context *generator.Context, args *args.Args) []generator.Target {
199-
boilerplate, err := gengo.GoBoilerplate(args.GoHeaderFile, gengo.StdBuildTag, gengo.StdGeneratedBy)
199+
boilerplate, err := gengo.GoBoilerplate(args.GoHeaderFile, args.GeneratedBuildTag, gengo.StdGeneratedBy)
200200
if err != nil {
201201
klog.Fatalf("Failed loading boilerplate: %v", err)
202202
}

staging/src/k8s.io/code-generator/cmd/conversion-gen/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func main() {
128128
generators.NameSystems(),
129129
generators.DefaultNameSystem(),
130130
myTargets,
131-
gengo.StdBuildTag,
131+
args.GeneratedBuildTag,
132132
pflag.Args(),
133133
); err != nil {
134134
klog.Fatalf("Error: %v", err)

staging/src/k8s.io/code-generator/cmd/defaulter-gen/args/args.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,27 @@ import (
2020
"fmt"
2121

2222
"github.com/spf13/pflag"
23+
24+
"k8s.io/gengo/v2"
2325
)
2426

2527
type Args struct {
2628
OutputFile string
2729
ExtraPeerDirs []string // Always consider these as last-ditch possibilities for conversions.
2830
GoHeaderFile string
31+
32+
// GeneratedBuildTag is the tag used to identify code generated by execution
33+
// of this type. Each generator should use a different tag, and different
34+
// groups of generators (external API that depends on Kube generations) should
35+
// keep tags distinct as well.
36+
GeneratedBuildTag string
2937
}
3038

3139
// New returns default arguments for the generator.
3240
func New() *Args {
33-
return &Args{}
41+
return &Args{
42+
GeneratedBuildTag: gengo.StdBuildTag,
43+
}
3444
}
3545

3646
// AddFlags add the generator flags to the flag set.
@@ -41,6 +51,7 @@ func (args *Args) AddFlags(fs *pflag.FlagSet) {
4151
"Comma-separated list of import paths which are considered, after tag-specified peers, for conversions.")
4252
fs.StringVar(&args.GoHeaderFile, "go-header-file", "",
4353
"the path to a file containing boilerplate header text; the string \"YEAR\" will be replaced with the current 4-digit year")
54+
fs.StringVar(&args.GeneratedBuildTag, "build-tag", args.GeneratedBuildTag, "A Go build tag to use to identify files generated by this command. Should be unique.")
4455
}
4556

4657
// Validate checks the given arguments.

staging/src/k8s.io/code-generator/cmd/defaulter-gen/generators/defaulter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func getManualDefaultingFunctions(context *generator.Context, pkg *types.Package
222222
}
223223

224224
func GetTargets(context *generator.Context, args *args.Args) []generator.Target {
225-
boilerplate, err := gengo.GoBoilerplate(args.GoHeaderFile, gengo.StdBuildTag, gengo.StdGeneratedBy)
225+
boilerplate, err := gengo.GoBoilerplate(args.GoHeaderFile, args.GeneratedBuildTag, gengo.StdGeneratedBy)
226226
if err != nil {
227227
klog.Fatalf("Failed loading boilerplate: %v", err)
228228
}

staging/src/k8s.io/code-generator/cmd/defaulter-gen/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func main() {
7474
generators.NameSystems(),
7575
generators.DefaultNameSystem(),
7676
myTargets,
77-
gengo.StdBuildTag,
77+
args.GeneratedBuildTag,
7878
pflag.Args(),
7979
); err != nil {
8080
klog.Fatalf("Error: %v", err)

0 commit comments

Comments
 (0)