Repository: kubernetes/kubernetes (language: go)

Issue:
Restore build-tag flag for code-generator
### What happened?

The `build-tag` flag is removed in 1.30 as code-generator moved to use `gengo/v2` and removed `gengo/args` dependency where `GeneratedBuildTag` arg resides. As a result, it is not possible to inject a custom build tag during `conversion-gen` and `defaulter-gen` process.

This flag is useful for other projects that need a custom tag to differentiate itself from the default build tag of `gengo` which is `ignore_autogenerated`. By default, `gengo` will ignore all files with the build tag of `ignore_autogenerated`. However, in some cases, some projects need to utilize some conversion and defaults funcs in `k8s.io/kubernetes/pkg/apis/core/v1` such as [1] and [2] and both files are ignored by `gengo` due to mentioned build-tag. By using a custom build tag that is different from the default build tag, those two files can be used during `conversion-gen` and `defaulter-gen` processes.

[1] https://github.com/kubernetes/kubernetes/blob/master/pkg/apis/core/v1/zz_generated.conversion.go
[2] https://github.com/kubernetes/kubernetes/blob/master/pkg/apis/core/v1/zz_generated.defaults.go

### What did you expect to happen?

The `build-tag` flag shouldn't be deprecated prematurely as it is still useful to inject a custom build tag.

### How can we reproduce it (as minimally and precisely as possible)?

Run `conversion-gen` or `defaulter-gen` with `--build-tag` flag and the cmd should err out as the flag is deprecated.

### Anything else we need to know?

_No response_

### Kubernetes version

<details>

```console
$ kubectl version
# paste output here
```
1.30
</details>


### Cloud provider

<details>
N/A
</details>


### OS version

<details>

```console
# On Linux:
$ cat /etc/os-release
# paste output here
$ uname -a
# paste output here

# On Windows:
C:\> wmic os get Caption, Version, BuildNumber, OSArchitecture
# paste output here
```

</details>


### Install tools

<details>

</details>


### Container runtime (CRI) and version (if applicable)

<details>

</details>


### Related plugins (CNI, CSI, ...) and versions (if applicable)

<details>

</details>



Discussion / hints:

Fix patch:
diff --git a/staging/src/k8s.io/code-generator/cmd/conversion-gen/args/args.go b/staging/src/k8s.io/code-generator/cmd/conversion-gen/args/args.go
index cc61c48d49f83..02663bfce1a6d 100644
--- a/staging/src/k8s.io/code-generator/cmd/conversion-gen/args/args.go
+++ b/staging/src/k8s.io/code-generator/cmd/conversion-gen/args/args.go
@@ -20,6 +20,8 @@ import (
 	"fmt"
 
 	"github.com/spf13/pflag"
+
+	"k8s.io/gengo/v2"
 )
 
 // DefaultBasePeerDirs are the peer-dirs nearly everybody will use, i.e. those coming from
@@ -57,13 +59,20 @@ type Args struct {
 	// GoHeaderFile is the path to a boilerplate header file for generated
 	// code.
 	GoHeaderFile string
+
+	// GeneratedBuildTag is the tag used to identify code generated by execution
+	// of this type. Each generator should use a different tag, and different
+	// groups of generators (external API that depends on Kube generations) should
+	// keep tags distinct as well.
+	GeneratedBuildTag string
 }
 
 // New returns default arguments for the generator.
 func New() *Args {
 	return &Args{
-		BasePeerDirs: DefaultBasePeerDirs,
-		SkipUnsafe:   false,
+		BasePeerDirs:      DefaultBasePeerDirs,
+		SkipUnsafe:        false,
+		GeneratedBuildTag: gengo.StdBuildTag,
 	}
 }
 
@@ -81,6 +90,7 @@ func (args *Args) AddFlags(fs *pflag.FlagSet) {
 		"If true, will not generate code using unsafe pointer conversions; resulting code may be slower.")
 	fs.StringVar(&args.GoHeaderFile, "go-header-file", "",
 		"the path to a file containing boilerplate header text; the string \"YEAR\" will be replaced with the current 4-digit year")
+	fs.StringVar(&args.GeneratedBuildTag, "build-tag", args.GeneratedBuildTag, "A Go build tag to use to identify files generated by this command. Should be unique.")
 }
 
 // Validate checks the given arguments.
diff --git a/staging/src/k8s.io/code-generator/cmd/conversion-gen/generators/conversion.go b/staging/src/k8s.io/code-generator/cmd/conversion-gen/generators/conversion.go
index b1ac8209a5666..a5f1b0cfb43c1 100644
--- a/staging/src/k8s.io/code-generator/cmd/conversion-gen/generators/conversion.go
+++ b/staging/src/k8s.io/code-generator/cmd/conversion-gen/generators/conversion.go
@@ -196,7 +196,7 @@ func getManualConversionFunctions(context *generator.Context, pkg *types.Package
 }
 
 func GetTargets(context *generator.Context, args *args.Args) []generator.Target {
-	boilerplate, err := gengo.GoBoilerplate(args.GoHeaderFile, gengo.StdBuildTag, gengo.StdGeneratedBy)
+	boilerplate, err := gengo.GoBoilerplate(args.GoHeaderFile, args.GeneratedBuildTag, gengo.StdGeneratedBy)
 	if err != nil {
 		klog.Fatalf("Failed loading boilerplate: %v", err)
 	}
diff --git a/staging/src/k8s.io/code-generator/cmd/conversion-gen/main.go b/staging/src/k8s.io/code-generator/cmd/conversion-gen/main.go
index cd52a9b9643f2..5aec5025d58a5 100644
--- a/staging/src/k8s.io/code-generator/cmd/conversion-gen/main.go
+++ b/staging/src/k8s.io/code-generator/cmd/conversion-gen/main.go
@@ -128,7 +128,7 @@ func main() {
 		generators.NameSystems(),
 		generators.DefaultNameSystem(),
 		myTargets,
-		gengo.StdBuildTag,
+		args.GeneratedBuildTag,
 		pflag.Args(),
 	); err != nil {
 		klog.Fatalf("Error: %v", err)
diff --git a/staging/src/k8s.io/code-generator/cmd/defaulter-gen/args/args.go b/staging/src/k8s.io/code-generator/cmd/defaulter-gen/args/args.go
index 52a9d1c67d05a..8d8dfe97f4226 100644
--- a/staging/src/k8s.io/code-generator/cmd/defaulter-gen/args/args.go
+++ b/staging/src/k8s.io/code-generator/cmd/defaulter-gen/args/args.go
@@ -20,17 +20,27 @@ import (
 	"fmt"
 
 	"github.com/spf13/pflag"
+
+	"k8s.io/gengo/v2"
 )
 
 type Args struct {
 	OutputFile    string
 	ExtraPeerDirs []string // Always consider these as last-ditch possibilities for conversions.
 	GoHeaderFile  string
+
+	// GeneratedBuildTag is the tag used to identify code generated by execution
+	// of this type. Each generator should use a different tag, and different
+	// groups of generators (external API that depends on Kube generations) should
+	// keep tags distinct as well.
+	GeneratedBuildTag string
 }
 
 // New returns default arguments for the generator.
 func New() *Args {
-	return &Args{}
+	return &Args{
+		GeneratedBuildTag: gengo.StdBuildTag,
+	}
 }
 
 // AddFlags add the generator flags to the flag set.
@@ -41,6 +51,7 @@ func (args *Args) AddFlags(fs *pflag.FlagSet) {
 		"Comma-separated list of import paths which are considered, after tag-specified peers, for conversions.")
 	fs.StringVar(&args.GoHeaderFile, "go-header-file", "",
 		"the path to a file containing boilerplate header text; the string \"YEAR\" will be replaced with the current 4-digit year")
+	fs.StringVar(&args.GeneratedBuildTag, "build-tag", args.GeneratedBuildTag, "A Go build tag to use to identify files generated by this command. Should be unique.")
 }
 
 // Validate checks the given arguments.
diff --git a/staging/src/k8s.io/code-generator/cmd/defaulter-gen/generators/defaulter.go b/staging/src/k8s.io/code-generator/cmd/defaulter-gen/generators/defaulter.go
index f7a6864c745e8..82e1cdd207034 100644
--- a/staging/src/k8s.io/code-generator/cmd/defaulter-gen/generators/defaulter.go
+++ b/staging/src/k8s.io/code-generator/cmd/defaulter-gen/generators/defaulter.go
@@ -222,7 +222,7 @@ func getManualDefaultingFunctions(context *generator.Context, pkg *types.Package
 }
 
 func GetTargets(context *generator.Context, args *args.Args) []generator.Target {
-	boilerplate, err := gengo.GoBoilerplate(args.GoHeaderFile, gengo.StdBuildTag, gengo.StdGeneratedBy)
+	boilerplate, err := gengo.GoBoilerplate(args.GoHeaderFile, args.GeneratedBuildTag, gengo.StdGeneratedBy)
 	if err != nil {
 		klog.Fatalf("Failed loading boilerplate: %v", err)
 	}
diff --git a/staging/src/k8s.io/code-generator/cmd/defaulter-gen/main.go b/staging/src/k8s.io/code-generator/cmd/defaulter-gen/main.go
index d57ca0666cd0a..0250c1328fd99 100644
--- a/staging/src/k8s.io/code-generator/cmd/defaulter-gen/main.go
+++ b/staging/src/k8s.io/code-generator/cmd/defaulter-gen/main.go
@@ -74,7 +74,7 @@ func main() {
 		generators.NameSystems(),
 		generators.DefaultNameSystem(),
 		myTargets,
-		gengo.StdBuildTag,
+		args.GeneratedBuildTag,
 		pflag.Args(),
 	); err != nil {
 		klog.Fatalf("Error: %v", err)


Test patch: