forked from kubernetes/kubernetes
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
218 lines (197 loc) · 7.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
Copyright 2015 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// client-gen makes the individual typed clients using go2idl.
package main
import (
"fmt"
"path/filepath"
"sort"
"strings"
"k8s.io/gengo/args"
clientgenargs "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/args"
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/generators"
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/types"
"github.com/golang/glog"
flag "github.com/spf13/pflag"
)
var (
test = flag.BoolP("test", "t", false, "set this flag to generate the client code for the testdata")
inputVersions = flag.StringSlice("input", []string{
"api/",
"authentication/",
"authorization/",
"autoscaling/",
"batch/",
"certificates/",
"extensions/",
"rbac/",
"storage/",
"apps/",
"policy/",
}, "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...\". Default to \"api/,extensions/,autoscaling/,batch/,rbac/\"")
includedTypesOverrides = flag.StringSlice("included-types-overrides", []string{}, "list of group/version/type for which client should be generated. By default, client is generated for all types which have genclient=true 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=true will be used for other group versions.")
basePath = flag.String("input-base", "k8s.io/kubernetes/pkg/apis", "base path to look for the api group. Default to \"k8s.io/kubernetes/pkg/apis\"")
clientsetName = flag.StringP("clientset-name", "n", "internalclientset", "the name of the generated clientset package.")
clientsetAPIPath = flag.StringP("clientset-api-path", "", "", "the value of default API path.")
clientsetPath = flag.String("clientset-path", "k8s.io/kubernetes/pkg/client/clientset_generated/", "the generated clientset will be output to <clientset-path>/<clientset-name>. Default to \"k8s.io/kubernetes/pkg/client/clientset_generated/\"")
clientsetOnly = flag.Bool("clientset-only", false, "when set, client-gen only generates the clientset shell, without generating the individual typed clients")
fakeClient = flag.Bool("fake-clientset", true, "when set, client-gen will generate the fake clientset that can be used in tests")
)
func versionToPath(gvPath string, group string, version string) (path string) {
// special case for the core group
if group == "api" {
path = filepath.Join(*basePath, "../api", version)
} else {
path = filepath.Join(*basePath, gvPath, group, version)
}
return
}
func parseGroupVersionType(gvtString string) (gvString string, typeStr string, err error) {
invalidFormatErr := fmt.Errorf("invalid value: %s, should be of the form group/version/type", gvtString)
subs := strings.Split(gvtString, "/")
length := len(subs)
switch length {
case 2:
// gvtString of the form group/type, e.g. api/Service,extensions/ReplicaSet
return subs[0] + "/", subs[1], nil
case 3:
return strings.Join(subs[:length-1], "/"), subs[length-1], nil
default:
return "", "", invalidFormatErr
}
}
func parsePathGroupVersion(pgvString string) (gvPath string, gvString string) {
subs := strings.Split(pgvString, "/")
length := len(subs)
switch length {
case 0, 1, 2:
return "", pgvString
default:
return strings.Join(subs[:length-2], "/"), strings.Join(subs[length-2:], "/")
}
}
func parseInputVersions() (paths []string, groups []types.GroupVersions, gvToPath map[types.GroupVersion]string, err error) {
var seenGroups = make(map[types.Group]*types.GroupVersions)
gvToPath = make(map[types.GroupVersion]string)
for _, input := range *inputVersions {
gvPath, gvString := parsePathGroupVersion(input)
gv, err := types.ToGroupVersion(gvString)
if err != nil {
return nil, nil, nil, err
}
if group, ok := seenGroups[gv.Group]; ok {
(*seenGroups[gv.Group]).Versions = append(group.Versions, gv.Version)
} else {
seenGroups[gv.Group] = &types.GroupVersions{
Group: gv.Group,
Versions: []types.Version{gv.Version},
}
}
path := versionToPath(gvPath, gv.Group.String(), gv.Version.String())
paths = append(paths, path)
gvToPath[gv] = path
}
var groupNames []string
for groupName := range seenGroups {
groupNames = append(groupNames, groupName.String())
}
sort.Strings(groupNames)
for _, groupName := range groupNames {
groups = append(groups, *seenGroups[types.Group(groupName)])
}
return paths, groups, gvToPath, nil
}
func parseIncludedTypesOverrides() (map[types.GroupVersion][]string, error) {
overrides := make(map[types.GroupVersion][]string)
for _, input := range *includedTypesOverrides {
gvString, typeStr, err := parseGroupVersionType(input)
if err != nil {
return nil, err
}
gv, err := types.ToGroupVersion(gvString)
if err != nil {
return nil, err
}
types, ok := overrides[gv]
if !ok {
types = []string{}
}
types = append(types, typeStr)
overrides[gv] = types
}
return overrides, nil
}
func main() {
arguments := args.Default()
arguments.GoHeaderFilePath = filepath.Join(args.DefaultSourceTree(), "k8s.io/kubernetes/hack/boilerplate/boilerplate.go.txt")
flag.Parse()
var cmdArgs string
flag.VisitAll(func(f *flag.Flag) {
if !f.Changed || f.Name == "verify-only" {
return
}
cmdArgs = cmdArgs + fmt.Sprintf("--%s=%s ", f.Name, f.Value)
})
dependencies := []string{
"k8s.io/apimachinery/pkg/fields",
"k8s.io/apimachinery/pkg/labels",
"k8s.io/apimachinery/pkg/watch",
"k8s.io/apimachinery/pkg/apimachinery/registered",
}
if *test {
arguments.InputDirs = append(dependencies, []string{
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/test_apis/testgroup",
}...)
arguments.CustomArgs = clientgenargs.Args{
Groups: []types.GroupVersions{{Group: "testgroup", Versions: []types.Version{""}}},
GroupVersionToInputPath: map[types.GroupVersion]string{
types.GroupVersion{Group: "testgroup", Version: ""}: "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/test_apis/testgroup",
},
ClientsetName: "test_internalclientset",
ClientsetOutputPath: "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testoutput/clientset_generated/",
ClientsetOnly: false,
FakeClient: true,
CmdArgs: cmdArgs,
}
} else {
inputPath, groups, gvToPath, err := parseInputVersions()
if err != nil {
glog.Fatalf("Error: %v", err)
}
includedTypesOverrides, err := parseIncludedTypesOverrides()
if err != nil {
glog.Fatalf("Unexpected error: %v", err)
}
glog.V(3).Infof("going to generate clientset from these input paths: %v", inputPath)
arguments.InputDirs = append(inputPath, dependencies...)
arguments.CustomArgs = clientgenargs.Args{
Groups: groups,
GroupVersionToInputPath: gvToPath,
ClientsetName: *clientsetName,
ClientsetAPIPath: *clientsetAPIPath,
ClientsetOutputPath: *clientsetPath,
ClientsetOnly: *clientsetOnly,
FakeClient: *fakeClient,
CmdArgs: cmdArgs,
IncludedTypesOverrides: includedTypesOverrides,
}
glog.V(3).Infof("==arguments: %v\n", arguments)
}
if err := arguments.Execute(
generators.NameSystems(),
generators.DefaultNameSystem(),
generators.Packages,
); err != nil {
glog.Fatalf("Error: %v", err)
}
}