Skip to content

Commit 4bac64e

Browse files
committed
blindly copy everything from k8s codegen over into this repo, remove old code
On-behalf-of: @SAP christoph.mewes@sap.com
1 parent bdce542 commit 4bac64e

67 files changed

Lines changed: 6623 additions & 4042 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/client-gen/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
See [generating-clientset.md](https://git.k8s.io/community/contributors/devel/sig-api-machinery/generating-clientset.md)
2+

cmd/client-gen/args/args.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
Copyright 2015 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package args
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/pflag"
23+
24+
"github.com/kcp-dev/code-generator/v2/cmd/client-gen/types"
25+
)
26+
27+
type Args struct {
28+
// The directory for the generated results.
29+
OutputDir string
30+
31+
// The Go import-path of the generated results.
32+
OutputPkg string
33+
34+
// The boilerplate header for Go files.
35+
GoHeaderFile string
36+
37+
// A sorted list of group versions to generate. For each of them the package path is found
38+
// in GroupVersionToInputPath.
39+
Groups []types.GroupVersions
40+
41+
// Overrides for which types should be included in the client.
42+
IncludedTypesOverrides map[types.GroupVersion][]string
43+
44+
// ClientsetName is the name of the clientset to be generated. It's
45+
// populated from command-line arguments.
46+
ClientsetName string
47+
// ClientsetAPIPath is the default API HTTP path for generated clients.
48+
ClientsetAPIPath string
49+
// ClientsetOnly determines if we should generate the clients for groups and
50+
// types along with the clientset. It's populated from command-line
51+
// arguments.
52+
ClientsetOnly bool
53+
// FakeClient determines if client-gen generates the fake clients.
54+
FakeClient bool
55+
// PluralExceptions specify list of exceptions used when pluralizing certain types.
56+
// For example 'Endpoints:Endpoints', otherwise the pluralizer will generate 'Endpointes'.
57+
PluralExceptions []string
58+
59+
// ApplyConfigurationPackage is the package of apply builders generated by
60+
// applyconfiguration-gen.
61+
// If non-empty, Apply functions are generated for each type and reference the apply builders.
62+
// If empty (""), Apply functions are not generated.
63+
ApplyConfigurationPackage string
64+
65+
// PrefersProtobuf determines if the generated clientset uses protobuf for API requests.
66+
PrefersProtobuf bool
67+
}
68+
69+
func New() *Args {
70+
return &Args{
71+
ClientsetName: "internalclientset",
72+
ClientsetAPIPath: "/apis",
73+
ClientsetOnly: false,
74+
FakeClient: true,
75+
ApplyConfigurationPackage: "",
76+
}
77+
}
78+
79+
func (args *Args) AddFlags(fs *pflag.FlagSet, inputBase string) {
80+
gvsBuilder := NewGroupVersionsBuilder(&args.Groups)
81+
fs.StringVar(&args.OutputDir, "output-dir", "",
82+
"the base directory under which to generate results")
83+
fs.StringVar(&args.OutputPkg, "output-pkg", args.OutputPkg,
84+
"the Go import-path of the generated results")
85+
fs.StringVar(&args.GoHeaderFile, "go-header-file", "",
86+
"the path to a file containing boilerplate header text; the string \"YEAR\" will be replaced with the current 4-digit year")
87+
fs.Var(NewGVPackagesValue(gvsBuilder, nil), "input",
88+
`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...".`)
89+
fs.Var(NewGVTypesValue(&args.IncludedTypesOverrides, []string{}), "included-types-overrides",
90+
"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.")
91+
fs.Var(NewInputBasePathValue(gvsBuilder, inputBase), "input-base",
92+
"base path to look for the api group.")
93+
fs.StringVarP(&args.ClientsetName, "clientset-name", "n", args.ClientsetName,
94+
"the name of the generated clientset package.")
95+
fs.StringVarP(&args.ClientsetAPIPath, "clientset-api-path", "", args.ClientsetAPIPath,
96+
"the value of default API HTTP path, starting with / and without trailing /.")
97+
fs.BoolVar(&args.ClientsetOnly, "clientset-only", args.ClientsetOnly,
98+
"when set, client-gen only generates the clientset shell, without generating the individual typed clients")
99+
fs.BoolVar(&args.FakeClient, "fake-clientset", args.FakeClient,
100+
"when set, client-gen will generate the fake clientset that can be used in tests")
101+
fs.StringSliceVar(&args.PluralExceptions, "plural-exceptions", args.PluralExceptions,
102+
"list of comma separated plural exception definitions in Type:PluralizedType form")
103+
fs.StringVar(&args.ApplyConfigurationPackage, "apply-configuration-package", args.ApplyConfigurationPackage,
104+
"optional package of apply configurations, generated by applyconfiguration-gen, that are required to generate Apply functions for each type in the clientset. By default Apply functions are not generated.")
105+
fs.BoolVar(&args.PrefersProtobuf, "prefers-protobuf", args.PrefersProtobuf,
106+
"when set, client-gen will generate a clientset that uses protobuf for API requests")
107+
108+
// support old flags
109+
fs.SetNormalizeFunc(mapFlagName("clientset-path", "output-pkg", fs.GetNormalizeFunc()))
110+
}
111+
112+
func (args *Args) Validate() error {
113+
if len(args.OutputDir) == 0 {
114+
return fmt.Errorf("--output-dir must be specified")
115+
}
116+
if len(args.OutputPkg) == 0 {
117+
return fmt.Errorf("--output-pkg must be specified")
118+
}
119+
if len(args.ClientsetName) == 0 {
120+
return fmt.Errorf("--clientset-name must be specified")
121+
}
122+
if len(args.ClientsetAPIPath) == 0 {
123+
return fmt.Errorf("--clientset-api-path cannot be empty")
124+
}
125+
126+
return nil
127+
}
128+
129+
// GroupVersionPackages returns a map from GroupVersion to the package with the types.go.
130+
func (args *Args) GroupVersionPackages() map[types.GroupVersion]string {
131+
res := map[types.GroupVersion]string{}
132+
for _, pkg := range args.Groups {
133+
for _, v := range pkg.Versions {
134+
res[types.GroupVersion{Group: pkg.Group, Version: v.Version}] = v.Package
135+
}
136+
}
137+
return res
138+
}
139+
140+
func mapFlagName(from, to string, old func(fs *pflag.FlagSet, name string) pflag.NormalizedName) func(fs *pflag.FlagSet, name string) pflag.NormalizedName {
141+
return func(fs *pflag.FlagSet, name string) pflag.NormalizedName {
142+
if name == from {
143+
name = to
144+
}
145+
return old(fs, name)
146+
}
147+
}

cmd/client-gen/args/gvpackages.go

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package args
18+
19+
import (
20+
"bytes"
21+
"encoding/csv"
22+
"flag"
23+
"path"
24+
"sort"
25+
"strings"
26+
27+
"github.com/kcp-dev/code-generator/v2/cmd/client-gen/generators/util"
28+
"github.com/kcp-dev/code-generator/v2/cmd/client-gen/types"
29+
)
30+
31+
type inputBasePathValue struct {
32+
builder *groupVersionsBuilder
33+
}
34+
35+
var _ flag.Value = &inputBasePathValue{}
36+
37+
func NewInputBasePathValue(builder *groupVersionsBuilder, def string) *inputBasePathValue {
38+
v := &inputBasePathValue{
39+
builder: builder,
40+
}
41+
v.Set(def)
42+
return v
43+
}
44+
45+
func (s *inputBasePathValue) Set(val string) error {
46+
s.builder.importBasePath = val
47+
return s.builder.update()
48+
}
49+
50+
func (s *inputBasePathValue) Type() string {
51+
return "string"
52+
}
53+
54+
func (s *inputBasePathValue) String() string {
55+
return s.builder.importBasePath
56+
}
57+
58+
type gvPackagesValue struct {
59+
builder *groupVersionsBuilder
60+
groups []string
61+
changed bool
62+
}
63+
64+
func NewGVPackagesValue(builder *groupVersionsBuilder, def []string) *gvPackagesValue {
65+
gvp := new(gvPackagesValue)
66+
gvp.builder = builder
67+
if def != nil {
68+
if err := gvp.set(def); err != nil {
69+
panic(err)
70+
}
71+
}
72+
return gvp
73+
}
74+
75+
var _ flag.Value = &gvPackagesValue{}
76+
77+
func (s *gvPackagesValue) set(vs []string) error {
78+
if s.changed {
79+
s.groups = append(s.groups, vs...)
80+
} else {
81+
s.groups = append([]string(nil), vs...)
82+
}
83+
84+
s.builder.groups = s.groups
85+
return s.builder.update()
86+
}
87+
88+
func (s *gvPackagesValue) Set(val string) error {
89+
vs, err := readAsCSV(val)
90+
if err != nil {
91+
return err
92+
}
93+
if err := s.set(vs); err != nil {
94+
return err
95+
}
96+
s.changed = true
97+
return nil
98+
}
99+
100+
func (s *gvPackagesValue) Type() string {
101+
return "stringSlice"
102+
}
103+
104+
func (s *gvPackagesValue) String() string {
105+
str, _ := writeAsCSV(s.groups)
106+
return "[" + str + "]"
107+
}
108+
109+
type groupVersionsBuilder struct {
110+
value *[]types.GroupVersions
111+
groups []string
112+
importBasePath string
113+
}
114+
115+
func NewGroupVersionsBuilder(groups *[]types.GroupVersions) *groupVersionsBuilder {
116+
return &groupVersionsBuilder{
117+
value: groups,
118+
}
119+
}
120+
121+
func (p *groupVersionsBuilder) update() error {
122+
var seenGroups = make(map[types.Group]*types.GroupVersions)
123+
for _, v := range p.groups {
124+
pth, gvString := util.ParsePathGroupVersion(v)
125+
gv, err := types.ToGroupVersion(gvString)
126+
if err != nil {
127+
return err
128+
}
129+
130+
versionPkg := types.PackageVersion{Package: path.Join(p.importBasePath, pth, gv.Group.NonEmpty(), gv.Version.String()), Version: gv.Version}
131+
if group, ok := seenGroups[gv.Group]; ok {
132+
vers := group.Versions
133+
vers = append(vers, versionPkg)
134+
seenGroups[gv.Group].Versions = vers
135+
} else {
136+
seenGroups[gv.Group] = &types.GroupVersions{
137+
PackageName: gv.Group.NonEmpty(),
138+
Group: gv.Group,
139+
Versions: []types.PackageVersion{versionPkg},
140+
}
141+
}
142+
}
143+
144+
var groupNames []string
145+
for groupName := range seenGroups {
146+
groupNames = append(groupNames, groupName.String())
147+
}
148+
sort.Strings(groupNames)
149+
*p.value = []types.GroupVersions{}
150+
for _, groupName := range groupNames {
151+
*p.value = append(*p.value, *seenGroups[types.Group(groupName)])
152+
}
153+
154+
return nil
155+
}
156+
157+
func readAsCSV(val string) ([]string, error) {
158+
if val == "" {
159+
return []string{}, nil
160+
}
161+
stringReader := strings.NewReader(val)
162+
csvReader := csv.NewReader(stringReader)
163+
return csvReader.Read()
164+
}
165+
166+
func writeAsCSV(vals []string) (string, error) {
167+
b := &bytes.Buffer{}
168+
w := csv.NewWriter(b)
169+
err := w.Write(vals)
170+
if err != nil {
171+
return "", err
172+
}
173+
w.Flush()
174+
return strings.TrimSuffix(b.String(), "\n"), nil
175+
}

0 commit comments

Comments
 (0)