Skip to content

Commit

Permalink
Move the OutputBase flag to each tool
Browse files Browse the repository at this point in the history
...and rename to --output-dir

Kubernetes-commit: 6a375b8f4cddbbea041410047aabababa2e12174
  • Loading branch information
thockin authored and k8s-publishing-bot committed Jan 17, 2024
1 parent ed09439 commit 0723f18
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 44 deletions.
13 changes: 8 additions & 5 deletions cmd/applyconfiguration-gen/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

// CustomArgs is a wrapper for arguments to applyconfiguration-gen.
type CustomArgs struct {
OutputDir string // must be a directory path
OutputPackage string // must be a Go import-path

// ExternalApplyConfigurations provides the locations of externally generated
Expand Down Expand Up @@ -65,7 +66,10 @@ func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {
}

func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet, inputBase string) {
fs.StringVar(&ca.OutputPackage, "output-package", ca.OutputPackage, "the Go import-path of the generated results")
fs.StringVar(&ca.OutputDir, "output-dir", "",
"the base directory under which to generate results")
fs.StringVar(&ca.OutputPackage, "output-package", ca.OutputPackage,
"the Go import-path of the generated results")
fs.Var(NewExternalApplyConfigurationValue(&ca.ExternalApplyConfigurations, nil), "external-applyconfigurations",
"list of comma separated external apply configurations locations in <type-package>.<type-name>:<applyconfiguration-package> form."+
"For example: k8s.io/api/apps/v1.Deployment:k8s.io/client-go/applyconfigurations/apps/v1")
Expand All @@ -75,12 +79,11 @@ func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet, inputBase string) {

// Validate checks the given arguments.
func Validate(genericArgs *args.GeneratorArgs) error {
if len(genericArgs.OutputBase) == 0 {
return fmt.Errorf("--output-base must be specified")
}

customArgs := genericArgs.CustomArgs.(*CustomArgs)

if len(customArgs.OutputDir) == 0 {
return fmt.Errorf("--output-dir must be specified")
}
if len(customArgs.OutputPackage) == 0 {
return fmt.Errorf("--output-package must be specified")
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/applyconfiguration-gen/generators/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func GetTargets(context *generator.Context, arguments *args.GeneratorArgs) []gen
// generate the apply configurations
targetList = append(targetList,
targetForApplyConfigurationsPackage(
arguments.OutputBase, customArgs.OutputPackage, pkgSubdir,
customArgs.OutputDir, customArgs.OutputPackage, pkgSubdir,
boilerplate, gv, toGenerate, refs, typeModels))

// group all the generated apply configurations by gv so ForKind() can be generated
Expand All @@ -133,11 +133,11 @@ func GetTargets(context *generator.Context, arguments *args.GeneratorArgs) []gen

// generate ForKind() utility function
targetList = append(targetList,
targetForUtils(arguments.OutputBase, customArgs.OutputPackage,
targetForUtils(customArgs.OutputDir, customArgs.OutputPackage,
boilerplate, groupVersions, applyConfigsForGroupVersion, groupGoNames))
// generate internal embedded schema, required for generated Extract functions
targetList = append(targetList,
targetForInternal(arguments.OutputBase, customArgs.OutputPackage,
targetForInternal(customArgs.OutputDir, customArgs.OutputPackage,
boilerplate, typeModels))

return targetList
Expand Down
15 changes: 10 additions & 5 deletions cmd/client-gen/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var DefaultInputDirs = []string{}

// CustomArgs is a wrapper for arguments to client-gen.
type CustomArgs struct {
// The directory for the generated results.
OutputDir string

// The Go import-path of the generated results.
OutputPackage string

Expand Down Expand Up @@ -78,7 +81,10 @@ func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {

func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet, inputBase string) {
gvsBuilder := NewGroupVersionsBuilder(&ca.Groups)
fs.StringVar(&ca.OutputPackage, "output-package", ca.OutputPackage, "the Go import-path of the generated results")
fs.StringVar(&ca.OutputDir, "output-dir", "",
"the base directory under which to generate results")
fs.StringVar(&ca.OutputPackage, "output-package", ca.OutputPackage,
"the Go import-path of the generated results")
fs.Var(NewGVPackagesValue(gvsBuilder, nil), "input", "group/versions that client-gen will generate clients for. At most one version per group is allowed. Specified in the format \"group1/version1,group2/version2...\".")
fs.Var(NewGVTypesValue(&ca.IncludedTypesOverrides, []string{}), "included-types-overrides", "list of group/version/type for which client should be generated. By default, client is generated for all types which have genclient in types.go. This overrides that. For each groupVersion in this list, only the types mentioned here will be included. The default check of genclient will be used for other group versions.")
fs.Var(NewInputBasePathValue(gvsBuilder, inputBase), "input-base", "base path to look for the api group.")
Expand All @@ -95,12 +101,11 @@ func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet, inputBase string) {
}

func Validate(genericArgs *args.GeneratorArgs) error {
if len(genericArgs.OutputBase) == 0 {
return fmt.Errorf("--output-base must be specified")
}

customArgs := genericArgs.CustomArgs.(*CustomArgs)

if len(customArgs.OutputDir) == 0 {
return fmt.Errorf("--output-dir must be specified")
}
if len(customArgs.OutputPackage) == 0 {
return fmt.Errorf("--output-package must be specified")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/client-gen/generators/client_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func GetTargets(context *generator.Context, arguments *args.GeneratorArgs) []gen
}
}

clientsetDir := filepath.Join(arguments.OutputBase, customArgs.ClientsetName)
clientsetDir := filepath.Join(customArgs.OutputDir, customArgs.ClientsetName)
clientsetPkg := filepath.Join(customArgs.OutputPackage, customArgs.ClientsetName)

var targetList []generator.Target
Expand Down
22 changes: 9 additions & 13 deletions cmd/go-to-protobuf/protobuf/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Generator struct {
Common args.GeneratorArgs
APIMachineryPackages string
Packages string
OutputBase string
OutputDir string
ProtoImport []string
Conditional string
Clean bool
Expand All @@ -52,12 +52,8 @@ type Generator struct {

func New() *Generator {
defaultSourceTree := "."
common := args.GeneratorArgs{
OutputBase: defaultSourceTree,
}
return &Generator{
Common: common,
OutputBase: defaultSourceTree,
OutputDir: defaultSourceTree,
APIMachineryPackages: strings.Join([]string{
`+k8s.io/apimachinery/pkg/util/intstr`,
`+k8s.io/apimachinery/pkg/api/resource`,
Expand All @@ -77,7 +73,7 @@ func (g *Generator) BindFlags(flag *flag.FlagSet) {
flag.BoolVar(&g.Common.VerifyOnly, "verify-only", g.Common.VerifyOnly, "If true, only verify existing output, do not write anything.")
flag.StringVarP(&g.Packages, "packages", "p", g.Packages, "comma-separated list of directories to get input types from. Directories prefixed with '-' are not generated, directories prefixed with '+' only create types with explicit IDL instructions.")
flag.StringVar(&g.APIMachineryPackages, "apimachinery-packages", g.APIMachineryPackages, "comma-separated list of directories to get apimachinery input types from which are needed by any API. Directories prefixed with '-' are not generated, directories prefixed with '+' only create types with explicit IDL instructions.")
flag.StringVarP(&g.OutputBase, "output-base", "o", g.OutputBase, "Output base; defaults to $GOPATH/src/")
flag.StringVar(&g.OutputDir, "output-dir", g.OutputDir, "The base directory under which to generate results.")
flag.StringSliceVar(&g.ProtoImport, "proto-import", g.ProtoImport, "A search path for imported protobufs (may be repeated).")
flag.StringVar(&g.Conditional, "conditional", g.Conditional, "An optional Golang build tag condition to add to the generated Go code")
flag.BoolVar(&g.Clean, "clean", g.Clean, "If true, remove all generated files for the specified Packages.")
Expand Down Expand Up @@ -255,7 +251,7 @@ func Run(g *Generator) {
log.Fatalf("Unable to find 'protoc': %v", err)
}

searchArgs := []string{"-I", ".", "-I", g.OutputBase}
searchArgs := []string{"-I", ".", "-I", g.OutputDir}
if len(g.ProtoImport) != 0 {
for _, s := range g.ProtoImport {
searchArgs = append(searchArgs, "-I", s)
Expand All @@ -266,12 +262,12 @@ func Run(g *Generator) {
// doesn't. Given example.com/foo/bar.proto (found in one of the -I paths
// above), the output becomes
// $output_base/example.com/foo/example.com/foo/bar.pb.go - basically
// useless. Users should set the output-base to a single dir under which
// useless. Users should set the output-dir to a single dir under which
// all the packages in question live (e.g. staging/src in kubernetes).
// Alternately, we could generate into a temp path and then move the
// resulting file back to the input dir, but that seems brittle in other
// ways.
args := append(searchArgs, fmt.Sprintf("--gogo_out=%s", g.OutputBase))
args := append(searchArgs, fmt.Sprintf("--gogo_out=%s", g.OutputDir))

buf := &bytes.Buffer{}
if len(g.Conditional) > 0 {
Expand All @@ -282,8 +278,8 @@ func Run(g *Generator) {
for _, outputPackage := range outputPackages {
p := outputPackage.(*protobufPackage)

path := filepath.Join(g.OutputBase, p.ImportPath())
outputPath := filepath.Join(g.OutputBase, p.OutputPath())
path := filepath.Join(g.OutputDir, p.ImportPath())
outputPath := filepath.Join(g.OutputDir, p.OutputPath())

// generate the gogoprotobuf protoc
cmd := exec.Command("protoc", append(args, path)...)
Expand Down Expand Up @@ -349,7 +345,7 @@ func Run(g *Generator) {
continue
}

pattern := filepath.Join(g.OutputBase, p.Path(), "*.go")
pattern := filepath.Join(g.OutputDir, p.Path(), "*.go")
files, err := filepath.Glob(pattern)
if err != nil {
log.Fatalf("Can't glob pattern %q: %v", pattern, err)
Expand Down
13 changes: 8 additions & 5 deletions cmd/informer-gen/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

// CustomArgs is used by the gengo framework to pass args specific to this generator.
type CustomArgs struct {
OutputDir string // must be a directory path
OutputPackage string // must be a Go import-path
VersionedClientSetPackage string // must be a Go import-path
InternalClientSetPackage string // must be a Go import-path
Expand All @@ -50,7 +51,10 @@ func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {

// AddFlags add the generator flags to the flag set.
func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&ca.OutputPackage, "output-package", ca.OutputPackage, "the Go import-path of the generated results")
fs.StringVar(&ca.OutputDir, "output-dir", "",
"the base directory under which to generate results")
fs.StringVar(&ca.OutputPackage, "output-package", ca.OutputPackage,
"the Go import-path of the generated results")
fs.StringVar(&ca.InternalClientSetPackage, "internal-clientset-package", ca.InternalClientSetPackage, "the Go import-path of the internal clientset to use")
fs.StringVar(&ca.VersionedClientSetPackage, "versioned-clientset-package", ca.VersionedClientSetPackage, "the Go import-path of the versioned clientset to use")
fs.StringVar(&ca.ListersPackage, "listers-package", ca.ListersPackage, "the Go import-path of the listers to use")
Expand All @@ -60,12 +64,11 @@ func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) {

// Validate checks the given arguments.
func Validate(genericArgs *args.GeneratorArgs) error {
if len(genericArgs.OutputBase) == 0 {
return fmt.Errorf("--output-base must be specified")
}

customArgs := genericArgs.CustomArgs.(*CustomArgs)

if len(customArgs.OutputDir) == 0 {
return fmt.Errorf("--output-dir must be specified")
}
if len(customArgs.OutputPackage) == 0 {
return fmt.Errorf("--output-package must be specified")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/informer-gen/generators/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func GetTargets(context *generator.Context, arguments *args.GeneratorArgs) []gen

customArgs := arguments.CustomArgs.(*informergenargs.CustomArgs)

internalVersionOutputDir := arguments.OutputBase
internalVersionOutputDir := customArgs.OutputDir
internalVersionOutputPkg := customArgs.OutputPackage
externalVersionOutputDir := arguments.OutputBase
externalVersionOutputDir := customArgs.OutputDir
externalVersionOutputPkg := customArgs.OutputPackage
if !customArgs.SingleDirectory {
internalVersionOutputDir = filepath.Join(internalVersionOutputDir, "internalversion")
Expand Down
10 changes: 6 additions & 4 deletions cmd/lister-gen/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

// CustomArgs is used by the gengo framework to pass args specific to this generator.
type CustomArgs struct {
OutputDir string // must be a directory path
OutputPackage string // must be a Go import-path

// PluralExceptions specify list of exceptions used when pluralizing certain types.
Expand All @@ -45,6 +46,8 @@ func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {

// AddFlags add the generator flags to the flag set.
func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&ca.OutputDir, "output-dir", "",
"the base directory under which to generate results")
fs.StringVar(&ca.OutputPackage, "output-package", "",
"the base Go import-path under which to generate results")
fs.StringSliceVar(&ca.PluralExceptions, "plural-exceptions", ca.PluralExceptions,
Expand All @@ -53,12 +56,11 @@ func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) {

// Validate checks the given arguments.
func Validate(genericArgs *args.GeneratorArgs) error {
if len(genericArgs.OutputBase) == 0 {
return fmt.Errorf("--output-base must be specified")
}

custom := genericArgs.CustomArgs.(*CustomArgs)

if len(custom.OutputDir) == 0 {
return fmt.Errorf("--output-dir must be specified")
}
if len(custom.OutputPackage) == 0 {
return fmt.Errorf("--output-package must be specified")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/lister-gen/generators/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func GetTargets(context *generator.Context, arguments *args.GeneratorArgs) []gen
typesToGenerate = orderer.OrderTypes(typesToGenerate)

subdir := filepath.Join(groupPackageName, strings.ToLower(gv.Version.NonEmpty()))
outputDir := filepath.Join(arguments.OutputBase, subdir)
outputDir := filepath.Join(customArgs.OutputDir, subdir)
outputPkg := filepath.Join(customArgs.OutputPackage, subdir)
targetList = append(targetList, &generator.SimpleTarget{
PkgName: strings.ToLower(gv.Version.NonEmpty()),
Expand Down
10 changes: 5 additions & 5 deletions kube_codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ function kube::codegen::gen_openapi() {
-v "${v}" \
--output-file-base zz_generated.openapi.go \
--go-header-file "${boilerplate}" \
--output-base "${out_dir}" \
--output-dir "${out_dir}" \
--output-package "${out_pkg}" \
--report-filename "${new_report}" \
--input-dirs "k8s.io/apimachinery/pkg/apis/meta/v1" \
Expand Down Expand Up @@ -611,7 +611,7 @@ function kube::codegen::gen_client() {
"${gobin}/applyconfiguration-gen" \
-v "${v}" \
--go-header-file "${boilerplate}" \
--output-base "${out_dir}/${applyconfig_subdir}" \
--output-dir "${out_dir}/${applyconfig_subdir}" \
--output-package "${applyconfig_pkg}" \
--external-applyconfigurations "${applyconfig_external}" \
"${inputs[@]}"
Expand All @@ -633,7 +633,7 @@ function kube::codegen::gen_client() {
"${gobin}/client-gen" \
-v "${v}" \
--go-header-file "${boilerplate}" \
--output-base "${out_dir}/${clientset_subdir}" \
--output-dir "${out_dir}/${clientset_subdir}" \
--output-package "${out_pkg}/${clientset_subdir}" \
--clientset-name "${clientset_versioned_name}" \
--apply-configuration-package "${applyconfig_pkg}" \
Expand All @@ -658,7 +658,7 @@ function kube::codegen::gen_client() {
"${gobin}/lister-gen" \
-v "${v}" \
--go-header-file "${boilerplate}" \
--output-base "${out_dir}/${listers_subdir}" \
--output-dir "${out_dir}/${listers_subdir}" \
--output-package "${out_pkg}/${listers_subdir}" \
--plural-exceptions "${plural_exceptions}" \
"${inputs[@]}"
Expand All @@ -679,7 +679,7 @@ function kube::codegen::gen_client() {
"${gobin}/informer-gen" \
-v "${v}" \
--go-header-file "${boilerplate}" \
--output-base "${out_dir}/${informers_subdir}" \
--output-dir "${out_dir}/${informers_subdir}" \
--output-package "${out_pkg}/${informers_subdir}" \
--versioned-clientset-package "${out_pkg}/${clientset_subdir}/${clientset_versioned_name}" \
--listers-package "${out_pkg}/${listers_subdir}" \
Expand Down

0 comments on commit 0723f18

Please sign in to comment.