forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator-for-group.go
184 lines (163 loc) · 5.35 KB
/
generator-for-group.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
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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.
*/
package generators
import (
"io"
"k8s.io/kubernetes/cmd/libs/go2idl/generator"
"k8s.io/kubernetes/cmd/libs/go2idl/namer"
"k8s.io/kubernetes/cmd/libs/go2idl/types"
)
// genGroup produces a file for a group client, e.g. ExtensionsClient for the extension group.
type genGroup struct {
generator.DefaultGen
outputPackage string
group string
// types in this group
types []*types.Type
imports *generator.ImportTracker
}
// We only want to call GenerateType() once per group.
func (g *genGroup) Filter(c *generator.Context, t *types.Type) bool {
return t == g.types[0]
}
func (g *genGroup) Namers(c *generator.Context) namer.NameSystems {
return namer.NameSystems{
"raw": namer.NewRawNamer(g.outputPackage, g.imports),
}
}
func (g *genGroup) Imports(c *generator.Context) (imports []string) {
return append(g.imports.ImportLines(), "fmt")
}
func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer) error {
sw := generator.NewSnippetWriter(w, c, "$", "$")
const pkgUnversioned = "k8s.io/kubernetes/pkg/client/unversioned"
const pkgLatest = "k8s.io/kubernetes/pkg/api/latest"
prefix := func(group string) string {
if group == "legacy" {
return `"/api"`
}
return `"/apis"`
}
canonize := func(group string) string {
if group == "legacy" {
return ""
}
return group
}
m := map[string]interface{}{
"group": g.group,
"Group": namer.IC(g.group),
"canonicalGroup": canonize(g.group),
"types": g.types,
"Config": c.Universe.Type(types.Name{Package: pkgUnversioned, Name: "Config"}),
"DefaultKubernetesUserAgent": c.Universe.Function(types.Name{Package: pkgUnversioned, Name: "DefaultKubernetesUserAgent"}),
"RESTClient": c.Universe.Type(types.Name{Package: pkgUnversioned, Name: "RESTClient"}),
"RESTClientFor": c.Universe.Function(types.Name{Package: pkgUnversioned, Name: "RESTClientFor"}),
"latestGroup": c.Universe.Variable(types.Name{Package: pkgLatest, Name: "Group"}),
"GroupOrDie": c.Universe.Variable(types.Name{Package: pkgLatest, Name: "GroupOrDie"}),
"prefix": prefix(g.group),
}
sw.Do(groupInterfaceTemplate, m)
sw.Do(groupClientTemplate, m)
for _, t := range g.types {
wrapper := map[string]interface{}{
"type": t,
"Group": namer.IC(g.group),
}
sw.Do(namespacerImplTemplate, wrapper)
}
sw.Do(newClientForConfigTemplate, m)
sw.Do(newClientForConfigOrDieTemplate, m)
sw.Do(newClientForRESTClientTemplate, m)
sw.Do(setClientDefaultsTemplate, m)
return sw.Error()
}
var groupInterfaceTemplate = `
type $.Group$Interface interface {
$range .types$ $.Name.Name$Namespacer
$end$
}
`
var groupClientTemplate = `
// $.Group$Client is used to interact with features provided by the $.Group$ group.
type $.Group$Client struct {
*$.RESTClient|raw$
}
`
var namespacerImplTemplate = `
func (c *$.Group$Client) $.type|publicPlural$(namespace string) $.type.Name.Name$Interface {
return new$.type|publicPlural$(c, namespace)
}
`
var newClientForConfigTemplate = `
// NewForConfig creates a new $.Group$Client for the given config.
func NewForConfig(c *$.Config|raw$) (*$.Group$Client, error) {
config := *c
if err := setConfigDefaults(&config); err != nil {
return nil, err
}
client, err := $.RESTClientFor|raw$(&config)
if err != nil {
return nil, err
}
return &$.Group$Client{client}, nil
}
`
var newClientForConfigOrDieTemplate = `
// NewForConfigOrDie creates a new $.Group$Client for the given config and
// panics if there is an error in the config.
func NewForConfigOrDie(c *$.Config|raw$) *$.Group$Client {
client, err := NewForConfig(c)
if err != nil {
panic(err)
}
return client
}
`
var newClientForRESTClientTemplate = `
// New creates a new $.Group$Client for the given RESTClient.
func New(c *$.RESTClient|raw$) *$.Group$Client {
return &$.Group$Client{c}
}
`
var setClientDefaultsTemplate = `
func setConfigDefaults(config *$.Config|raw$) error {
// if $.group$ group is not registered, return an error
g, err := $.latestGroup|raw$("$.canonicalGroup$")
if err != nil {
return err
}
config.Prefix = $.prefix$
if config.UserAgent == "" {
config.UserAgent = $.DefaultKubernetesUserAgent|raw$()
}
// TODO: Unconditionally set the config.Version, until we fix the config.
//if config.Version == "" {
copyGroupVersion := g.GroupVersion
config.GroupVersion = ©GroupVersion
//}
versionInterfaces, err := g.InterfacesFor(*config.GroupVersion)
if err != nil {
return fmt.Errorf("$.Group$ API version '%s' is not recognized (valid values: %s)",
config.GroupVersion, g.GroupVersions)
}
config.Codec = versionInterfaces.Codec
if config.QPS == 0 {
config.QPS = 5
}
if config.Burst == 0 {
config.Burst = 10
}
return nil
}
`