| 1 |
/* |
| 2 |
Copyright 2018 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 generators |
| 18 |
|
| 19 |
import ( |
| 20 |
"path" |
| 21 |
|
| 22 |
"k8s.io/gengo/v2/generator" |
| 23 |
"k8s.io/gengo/v2/namer" |
| 24 |
"k8s.io/gengo/v2/types" |
| 25 |
"k8s.io/klog/v2" |
| 26 |
"k8s.io/kube-openapi/cmd/openapi-gen/args" |
| 27 |
) |
| 28 |
|
| 29 |
type identityNamer struct{} |
| 30 |
|
| 31 |
func (_ identityNamer) Name(t *types.Type) string { |
| 32 |
return t.Name.String() |
| 33 |
} |
| 34 |
|
| 35 |
var _ namer.Namer = identityNamer{} |
| 36 |
|
| 37 |
// NameSystems returns the name system used by the generators in this package. |
| 38 |
func NameSystems() namer.NameSystems { |
| 39 |
return namer.NameSystems{ |
| 40 |
"raw": namer.NewRawNamer("", nil), |
| 41 |
"sorting_namer": identityNamer{}, |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// DefaultNameSystem returns the default name system for ordering the types to be |
| 46 |
// processed by the generators in this package. |
| 47 |
func DefaultNameSystem() string { |
| 48 |
return "sorting_namer" |
| 49 |
} |
| 50 |
|
| 51 |
// GetOpenAPITargets returns the targets for OpenAPI definition generation. |
| 52 |
func GetOpenAPITargets(context *generator.Context, args *args.Args, boilerplate []byte) []generator.Target { |
| 53 |
reportPath := "-" |
| 54 |
if args.ReportFilename != "" { |
| 55 |
reportPath = args.ReportFilename |
| 56 |
} |
| 57 |
context.FileTypes[apiViolationFileType] = apiViolationFile{ |
| 58 |
unmangledPath: reportPath, |
| 59 |
} |
| 60 |
|
| 61 |
return []generator.Target{ |
| 62 |
&generator.SimpleTarget{ |
| 63 |
PkgName: path.Base(args.OutputPkg), // `path` vs. `filepath` because packages use '/' |
| 64 |
PkgPath: args.OutputPkg, |
| 65 |
PkgDir: args.OutputDir, |
| 66 |
HeaderComment: boilerplate, |
| 67 |
GeneratorsFunc: func(c *generator.Context) (generators []generator.Generator) { |
| 68 |
return []generator.Generator{ |
| 69 |
newOpenAPIGen( |
| 70 |
args.OutputFile, |
| 71 |
args.OutputPkg, |
| 72 |
), |
| 73 |
newAPIViolationGen(), |
| 74 |
} |
| 75 |
}, |
| 76 |
FilterFunc: apiTypeFilterFunc, |
| 77 |
}, |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// isReadOnlyPkg checks whether pkgPath matches one of the given read-only |
| 82 |
// package paths. |
| 83 |
func isReadOnlyPkg(pkgPath string, readOnlyPkgs []string) bool { |
| 84 |
for _, pkg := range readOnlyPkgs { |
| 85 |
if pkgPath == pkg { |
| 86 |
return true |
| 87 |
} |
| 88 |
} |
| 89 |
return false |
| 90 |
} |
| 91 |
|
| 92 |
// GetModelNameTargets returns the targets for model name generation. |
| 93 |
func GetModelNameTargets(context *generator.Context, args *args.Args, boilerplate []byte) []generator.Target { |
| 94 |
var targets []generator.Target |
| 95 |
for _, i := range context.Inputs { |
| 96 |
klog.V(5).Infof("Considering pkg %q", i) |
| 97 |
|
| 98 |
pkg := context.Universe[i] |
| 99 |
|
| 100 |
// Skip read-only packages — they are input dependencies needed |
| 101 |
// for type resolution but should not have model name files |
| 102 |
// generated (e.g. read-only module cache). |
| 103 |
if isReadOnlyPkg(pkg.Path, args.ReadOnlyPkgs) { |
| 104 |
klog.V(3).Infof("Skipping model name generation for %q: read-only package", pkg.Path) |
| 105 |
continue |
| 106 |
} |
| 107 |
|
| 108 |
openAPISchemaNamePackage, err := extractOpenAPISchemaNamePackage(pkg.Comments) |
| 109 |
if err != nil { |
| 110 |
klog.Fatalf("Package %v: invalid %s:%v", i, tagModelPackage, err) |
| 111 |
} |
| 112 |
hasPackageTag := len(openAPISchemaNamePackage) > 0 |
| 113 |
|
| 114 |
hasCandidates := false |
| 115 |
for _, t := range pkg.Types { |
| 116 |
v, err := singularTag(tagModelPackage, t.CommentLines) |
| 117 |
if err != nil { |
| 118 |
klog.Fatalf("Type %v: invalid %s:%v", t.Name, tagModelPackage, err) |
| 119 |
} |
| 120 |
hasTag := hasPackageTag || v != nil |
| 121 |
hasModel := isSchemaNameType(t) |
| 122 |
if hasModel && hasTag { |
| 123 |
hasCandidates = true |
| 124 |
break |
| 125 |
} |
| 126 |
} |
| 127 |
if !hasCandidates { |
| 128 |
klog.V(5).Infof(" skipping package") |
| 129 |
continue |
| 130 |
} |
| 131 |
|
| 132 |
klog.V(3).Infof("Generating package %q", pkg.Path) |
| 133 |
|
| 134 |
targets = append(targets, |
| 135 |
&generator.SimpleTarget{ |
| 136 |
PkgName: path.Base(pkg.Path), |
| 137 |
PkgPath: pkg.Path, |
| 138 |
PkgDir: pkg.Dir, // output pkg is the same as the input |
| 139 |
HeaderComment: boilerplate, |
| 140 |
FilterFunc: func(c *generator.Context, t *types.Type) bool { |
| 141 |
return t.Name.Package == pkg.Path |
| 142 |
}, |
| 143 |
GeneratorsFunc: func(c *generator.Context) (generators []generator.Generator) { |
| 144 |
return []generator.Generator{ |
| 145 |
NewSchemaNameGen(args.OutputModelNameFile, pkg.Path, openAPISchemaNamePackage), |
| 146 |
} |
| 147 |
}, |
| 148 |
}) |
| 149 |
} |
| 150 |
return targets |
| 151 |
} |