-
Notifications
You must be signed in to change notification settings - Fork 410
/
status.go
491 lines (432 loc) · 16.6 KB
/
status.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
Copyright 2020 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.
*/
package prereleaselifecyclegenerators
import (
"fmt"
"io"
"path/filepath"
"strconv"
"strings"
"k8s.io/gengo/args"
"k8s.io/gengo/examples/set-gen/sets"
"k8s.io/gengo/generator"
"k8s.io/gengo/namer"
"k8s.io/gengo/types"
"k8s.io/klog/v2"
)
// CustomArgs is used tby the go2idl framework to pass args specific to this generator.
type CustomArgs struct {
}
// This is the comment tag that carries parameters for API status generation. Because the cadence is fixed, we can predict
// with near certainty when this lifecycle happens as the API is introduced.
const (
tagEnabledName = "k8s:prerelease-lifecycle-gen"
introducedTagName = tagEnabledName + ":introduced"
deprecatedTagName = tagEnabledName + ":deprecated"
removedTagName = tagEnabledName + ":removed"
replacementTagName = tagEnabledName + ":replacement"
)
// enabledTagValue holds parameters from a tagName tag.
type tagValue struct {
value string
}
func extractEnabledTypeTag(t *types.Type) *tagValue {
comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...)
return extractTag(tagEnabledName, comments)
}
func tagExists(tagName string, t *types.Type) bool {
comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...)
rawTag := extractTag(tagName, comments)
return rawTag != nil
}
func extractKubeVersionTag(tagName string, t *types.Type) (*tagValue, int, int, error) {
comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...)
rawTag := extractTag(tagName, comments)
if rawTag == nil || len(rawTag.value) == 0 {
return nil, -1, -1, fmt.Errorf("%v missing %v=Version tag", t, tagName)
}
splitValue := strings.Split(rawTag.value, ".")
if len(splitValue) != 2 || len(splitValue[0]) == 0 || len(splitValue[1]) == 0 {
return nil, -1, -1, fmt.Errorf("%v format must match %v=xx.yy tag", t, tagName)
}
major, err := strconv.ParseInt(splitValue[0], 10, 32)
if err != nil {
return nil, -1, -1, fmt.Errorf("%v format must match %v=xx.yy : %w", t, tagName, err)
}
minor, err := strconv.ParseInt(splitValue[1], 10, 32)
if err != nil {
return nil, -1, -1, fmt.Errorf("%v format must match %v=xx.yy : %w", t, tagName, err)
}
return rawTag, int(major), int(minor), nil
}
func extractIntroducedTag(t *types.Type) (*tagValue, int, int, error) {
return extractKubeVersionTag(introducedTagName, t)
}
func extractDeprecatedTag(t *types.Type) (*tagValue, int, int, error) {
return extractKubeVersionTag(deprecatedTagName, t)
}
func extractRemovedTag(t *types.Type) (*tagValue, int, int, error) {
return extractKubeVersionTag(removedTagName, t)
}
func extractReplacementTag(t *types.Type) (group, version, kind string, hasReplacement bool, err error) {
comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...)
tagVals := types.ExtractCommentTags("+", comments)[replacementTagName]
if len(tagVals) == 0 {
// No match for the tag.
return "", "", "", false, nil
}
// If there are multiple values, abort.
if len(tagVals) > 1 {
return "", "", "", false, fmt.Errorf("Found %d %s tags: %q", len(tagVals), replacementTagName, tagVals)
}
tagValue := tagVals[0]
parts := strings.Split(tagValue, ",")
if len(parts) != 3 {
return "", "", "", false, fmt.Errorf(`%s value must be "<group>,<version>,<kind>", got %q`, replacementTagName, tagValue)
}
group, version, kind = parts[0], parts[1], parts[2]
if len(version) == 0 || len(kind) == 0 {
return "", "", "", false, fmt.Errorf(`%s value must be "<group>,<version>,<kind>", got %q`, replacementTagName, tagValue)
}
// sanity check the group
if strings.ToLower(group) != group {
return "", "", "", false, fmt.Errorf(`replacement group must be all lower-case, got %q`, group)
}
// sanity check the version
if !strings.HasPrefix(version, "v") || strings.ToLower(version) != version {
return "", "", "", false, fmt.Errorf(`replacement version must start with "v" and be all lower-case, got %q`, version)
}
// sanity check the kind
if strings.ToUpper(kind[:1]) != kind[:1] {
return "", "", "", false, fmt.Errorf(`replacement kind must start with uppercase-letter, got %q`, kind)
}
return group, version, kind, true, nil
}
func extractTag(tagName string, comments []string) *tagValue {
tagVals := types.ExtractCommentTags("+", comments)[tagName]
if tagVals == nil {
// No match for the tag.
return nil
}
// If there are multiple values, abort.
if len(tagVals) > 1 {
klog.Fatalf("Found %d %s tags: %q", len(tagVals), tagName, tagVals)
}
// If we got here we are returning something.
tag := &tagValue{}
// Get the primary value.
parts := strings.Split(tagVals[0], ",")
if len(parts) >= 1 {
tag.value = parts[0]
}
// Parse extra arguments.
parts = parts[1:]
for i := range parts {
kv := strings.SplitN(parts[i], "=", 2)
k := kv[0]
if k != "" {
klog.Fatalf("Unsupported %s param: %q", tagName, parts[i])
}
}
return tag
}
// NameSystems returns the name system used by the generators in this package.
func NameSystems() namer.NameSystems {
return namer.NameSystems{
"public": namer.NewPublicNamer(1),
"raw": namer.NewRawNamer("", nil),
}
}
// DefaultNameSystem returns the default name system for ordering the types to be
// processed by the generators in this package.
func DefaultNameSystem() string {
return "public"
}
// Packages makes the package definition.
func Packages(context *generator.Context, arguments *args.GeneratorArgs) generator.Packages {
boilerplate, err := arguments.LoadGoBoilerplate()
if err != nil {
klog.Fatalf("Failed loading boilerplate: %v", err)
}
inputs := sets.NewString(context.Inputs...)
packages := generator.Packages{}
header := append([]byte(fmt.Sprintf("// +build !%s\n\n", arguments.GeneratedBuildTag)), boilerplate...)
for i := range inputs {
klog.V(5).Infof("Considering pkg %q", i)
pkg := context.Universe[i]
if pkg == nil {
// If the input had no Go files, for example.
continue
}
ptag := extractTag(tagEnabledName, pkg.Comments)
pkgNeedsGeneration := false
if ptag != nil {
pkgNeedsGeneration, err = strconv.ParseBool(ptag.value)
if err != nil {
klog.Fatalf("Package %v: unsupported %s value: %q :%w", i, tagEnabledName, ptag.value, err)
}
}
if !pkgNeedsGeneration {
klog.V(5).Infof(" skipping package")
continue
}
klog.V(3).Infof("Generating package %q", pkg.Path)
// If the pkg-scoped tag says to generate, we can skip scanning types.
if !pkgNeedsGeneration {
// If the pkg-scoped tag did not exist, scan all types for one that
// explicitly wants generation.
for _, t := range pkg.Types {
klog.V(5).Infof(" considering type %q", t.Name.String())
ttag := extractEnabledTypeTag(t)
if ttag != nil && ttag.value == "true" {
klog.V(5).Infof(" tag=true")
if !isAPIType(t) {
klog.Fatalf("Type %v requests deepcopy generation but is not copyable", t)
}
pkgNeedsGeneration = true
break
}
}
}
if pkgNeedsGeneration {
path := pkg.Path
// if the source path is within a /vendor/ directory (for example,
// k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/apis/meta/v1), allow
// generation to output to the proper relative path (under vendor).
// Otherwise, the generator will create the file in the wrong location
// in the output directory.
// TODO: build a more fundamental concept in gengo for dealing with modifications
// to vendored packages.
if strings.HasPrefix(pkg.SourcePath, arguments.OutputBase) {
expandedPath := strings.TrimPrefix(pkg.SourcePath, arguments.OutputBase)
if strings.Contains(expandedPath, "/vendor/") {
path = expandedPath
}
}
packages = append(packages,
&generator.DefaultPackage{
PackageName: strings.Split(filepath.Base(pkg.Path), ".")[0],
PackagePath: path,
HeaderText: header,
GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
return []generator.Generator{
NewPrereleaseLifecycleGen(arguments.OutputFileBaseName, pkg.Path),
}
},
FilterFunc: func(c *generator.Context, t *types.Type) bool {
return t.Name.Package == pkg.Path
},
})
}
}
return packages
}
// genDeepCopy produces a file with autogenerated deep-copy functions.
type genPreleaseLifecycle struct {
generator.DefaultGen
targetPackage string
imports namer.ImportTracker
typesForInit []*types.Type
}
// NewPrereleaseLifecycleGen creates a generator for the prerelease-lifecycle-generator
func NewPrereleaseLifecycleGen(sanitizedName, targetPackage string) generator.Generator {
return &genPreleaseLifecycle{
DefaultGen: generator.DefaultGen{
OptionalName: sanitizedName,
},
targetPackage: targetPackage,
imports: generator.NewImportTracker(),
typesForInit: make([]*types.Type, 0),
}
}
func (g *genPreleaseLifecycle) Namers(c *generator.Context) namer.NameSystems {
return namer.NameSystems{
"public": namer.NewPublicNamer(1),
"intrapackage": namer.NewPublicNamer(0),
"raw": namer.NewRawNamer("", nil),
}
}
func (g *genPreleaseLifecycle) Filter(c *generator.Context, t *types.Type) bool {
// Filter out types not being processed or not copyable within the package.
if !isAPIType(t) {
klog.V(2).Infof("Type %v is not a valid target for status", t)
return false
}
g.typesForInit = append(g.typesForInit, t)
return true
}
// versionMethod returns the signature of an <methodName>() method, nil or an error
// if the type is wrong. Introduced() allows more efficient deep copy
// implementations to be defined by the type's author. The correct signature
//
// func (t *T) <methodName>() string
func versionMethod(methodName string, t *types.Type) (*types.Signature, error) {
f, found := t.Methods[methodName]
if !found {
return nil, nil
}
if len(f.Signature.Parameters) != 0 {
return nil, fmt.Errorf("type %v: invalid %v signature, expected no parameters", t, methodName)
}
if len(f.Signature.Results) != 2 {
return nil, fmt.Errorf("type %v: invalid %v signature, expected exactly two result types", t, methodName)
}
ptrRcvr := f.Signature.Receiver != nil && f.Signature.Receiver.Kind == types.Pointer && f.Signature.Receiver.Elem.Name == t.Name
nonPtrRcvr := f.Signature.Receiver != nil && f.Signature.Receiver.Name == t.Name
if !ptrRcvr && !nonPtrRcvr {
// this should never happen
return nil, fmt.Errorf("type %v: invalid %v signature, expected a receiver of type %s or *%s", t, methodName, t.Name.Name, t.Name.Name)
}
return f.Signature, nil
}
// versionedMethodOrDie returns the signature of a <methodName>() method, nil or calls klog.Fatalf
// if the type is wrong.
func versionedMethodOrDie(methodName string, t *types.Type) *types.Signature {
ret, err := versionMethod(methodName, t)
if err != nil {
klog.Fatal(err)
}
return ret
}
// isAPIType indicates whether or not a type could be used to serve an API. That means, "does it have TypeMeta".
// This doesn't mean the type is served, but we will handle all TypeMeta types.
func isAPIType(t *types.Type) bool {
// Filter out private types.
if namer.IsPrivateGoName(t.Name.Name) {
return false
}
if t.Kind != types.Struct {
return false
}
for _, currMember := range t.Members {
if currMember.Embedded && currMember.Name == "TypeMeta" {
return true
}
}
if t.Kind == types.Alias {
return isAPIType(t.Underlying)
}
return false
}
func (g *genPreleaseLifecycle) isOtherPackage(pkg string) bool {
if pkg == g.targetPackage {
return false
}
if strings.HasSuffix(pkg, "\""+g.targetPackage+"\"") {
return false
}
return true
}
func (g *genPreleaseLifecycle) Imports(c *generator.Context) (imports []string) {
importLines := []string{}
for _, singleImport := range g.imports.ImportLines() {
if g.isOtherPackage(singleImport) {
importLines = append(importLines, singleImport)
}
}
return importLines
}
func (g *genPreleaseLifecycle) argsFromType(c *generator.Context, t *types.Type) (generator.Args, error) {
a := generator.Args{
"type": t,
}
_, introducedMajor, introducedMinor, err := extractIntroducedTag(t)
if err != nil {
return nil, err
}
a = a.
With("introducedMajor", introducedMajor).
With("introducedMinor", introducedMinor)
// compute based on our policy
deprecatedMajor := introducedMajor
deprecatedMinor := introducedMinor + 3
// if someone intentionally override the deprecation release
if tagExists(deprecatedTagName, t) {
_, deprecatedMajor, deprecatedMinor, err = extractDeprecatedTag(t)
if err != nil {
return nil, err
}
}
a = a.
With("deprecatedMajor", deprecatedMajor).
With("deprecatedMinor", deprecatedMinor)
// compute based on our policy
removedMajor := deprecatedMajor
removedMinor := deprecatedMinor + 3
// if someone intentionally override the removed release
if tagExists(removedTagName, t) {
_, removedMajor, removedMinor, err = extractRemovedTag(t)
if err != nil {
return nil, err
}
}
a = a.
With("removedMajor", removedMajor).
With("removedMinor", removedMinor)
replacementGroup, replacementVersion, replacementKind, hasReplacement, err := extractReplacementTag(t)
if err != nil {
return nil, err
}
if hasReplacement {
gvkType := c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/runtime/schema", Name: "GroupVersionKind"})
g.imports.AddType(gvkType)
a = a.
With("replacementGroup", replacementGroup).
With("replacementVersion", replacementVersion).
With("replacementKind", replacementKind).
With("GroupVersionKind", gvkType)
}
return a, nil
}
func (g *genPreleaseLifecycle) Init(c *generator.Context, w io.Writer) error {
return nil
}
func (g *genPreleaseLifecycle) GenerateType(c *generator.Context, t *types.Type, w io.Writer) error {
klog.V(3).Infof("Generating prerelease-lifecycle for type %v", t)
sw := generator.NewSnippetWriter(w, c, "$", "$")
args, err := g.argsFromType(c, t)
if err != nil {
return err
}
if versionedMethodOrDie("APILifecycleIntroduced", t) == nil {
sw.Do("// APILifecycleIntroduced is an autogenerated function, returning the release in which the API struct was introduced as int versions of major and minor for comparison.\n", args)
sw.Do("// It is controlled by \""+introducedTagName+"\" tags in types.go.\n", args)
sw.Do("func (in *$.type|intrapackage$) APILifecycleIntroduced() (major, minor int) {\n", args)
sw.Do(" return $.introducedMajor$, $.introducedMinor$\n", args)
sw.Do("}\n\n", nil)
}
if versionedMethodOrDie("APILifecycleDeprecated", t) == nil {
sw.Do("// APILifecycleDeprecated is an autogenerated function, returning the release in which the API struct was or will be deprecated as int versions of major and minor for comparison.\n", args)
sw.Do("// It is controlled by \""+deprecatedTagName+"\" tags in types.go or \""+introducedTagName+"\" plus three minor.\n", args)
sw.Do("func (in *$.type|intrapackage$) APILifecycleDeprecated() (major, minor int) {\n", args)
sw.Do(" return $.deprecatedMajor$, $.deprecatedMinor$\n", args)
sw.Do("}\n\n", nil)
}
if _, hasReplacement := args["replacementKind"]; hasReplacement {
if versionedMethodOrDie("APILifecycleReplacement", t) == nil {
sw.Do("// APILifecycleReplacement is an autogenerated function, returning the group, version, and kind that should be used instead of this deprecated type.\n", args)
sw.Do("// It is controlled by \""+replacementTagName+"=<group>,<version>,<kind>\" tags in types.go.\n", args)
sw.Do("func (in *$.type|intrapackage$) APILifecycleReplacement() ($.GroupVersionKind|raw$) {\n", args)
sw.Do(" return $.GroupVersionKind|raw${Group:\"$.replacementGroup$\", Version:\"$.replacementVersion$\", Kind:\"$.replacementKind$\"}\n", args)
sw.Do("}\n\n", nil)
}
}
if versionedMethodOrDie("APILifecycleRemoved", t) == nil {
sw.Do("// APILifecycleRemoved is an autogenerated function, returning the release in which the API is no longer served as int versions of major and minor for comparison.\n", args)
sw.Do("// It is controlled by \""+removedTagName+"\" tags in types.go or \""+deprecatedTagName+"\" plus three minor.\n", args)
sw.Do("func (in *$.type|intrapackage$) APILifecycleRemoved() (major, minor int) {\n", args)
sw.Do(" return $.removedMajor$, $.removedMinor$\n", args)
sw.Do("}\n\n", nil)
}
return sw.Error()
}