2 Copyright 2018 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
27 "github.com/pkg/errors"
28 rbacv1 "k8s.io/api/rbac/v1"
29 "k8s.io/apimachinery/pkg/apis/meta/v1"
30 "k8s.io/apimachinery/pkg/util/sets"
32 "k8s.io/gengo/generator"
34 "sigs.k8s.io/controller-tools/pkg/internal/codegen"
37 // APIs is the information of a collection of API
39 context *generator.Context
40 arguments *args.GeneratorArgs
42 VersionedPkgs sets.String
43 UnversionedPkgs sets.String
45 APIsPkgRaw *types.Package
46 GroupNames sets.String
49 Controllers []codegen.Controller
51 ByGroupKindVersion map[string]map[string]map[string]*codegen.APIResource
52 ByGroupVersionKind map[string]map[string]map[string]*codegen.APIResource
53 SubByGroupVersionKind map[string]map[string]map[string]*types.Type
54 Groups map[string]types.Package
55 Rules []rbacv1.PolicyRule
56 Informers map[v1.GroupVersionKind]bool
59 // NewAPIs returns a new APIs instance with given context.
60 func NewAPIs(context *generator.Context, arguments *args.GeneratorArgs, domain, apisPkg string) *APIs {
72 if len(b.Domain) == 0 {
78 // parseGroupNames initializes b.GroupNames with the set of all groups
79 func (b *APIs) parseGroupNames() {
80 b.GroupNames = sets.String{}
81 for p := range b.UnversionedPkgs {
82 pkg := b.context.Universe[p]
84 // If the input had no Go files, for example.
87 b.GroupNames.Insert(filepath.Base(p))
91 // parsePackages parses out the sets of Versioned, Unversioned packages and identifies the root Apis package.
92 func (b *APIs) parsePackages() {
93 b.VersionedPkgs = sets.NewString()
94 b.UnversionedPkgs = sets.NewString()
95 for _, o := range b.context.Order {
97 versioned := o.Name.Package
98 b.VersionedPkgs.Insert(versioned)
100 unversioned := filepath.Dir(versioned)
101 b.UnversionedPkgs.Insert(unversioned)
106 // parseDomain parses the domain from the apis/doc.go file comment "// +domain=YOUR_DOMAIN".
107 func (b *APIs) parseDomain() {
108 pkg := b.context.Universe[b.APIsPkg]
110 // If the input had no Go files, for example.
111 panic(errors.Errorf("Missing apis package."))
113 comments := Comments(pkg.Comments)
114 b.Domain = comments.getTag("domain", "=")
115 if len(b.Domain) == 0 {
116 b.Domain = parseDomainFromFiles(b.context.Inputs)
117 if len(b.Domain) == 0 {
118 panic("Could not find string matching // +domain=.+ in apis/doc.go")
123 func parseDomainFromFiles(paths []string) string {
125 for _, path := range paths {
126 if strings.HasSuffix(path, "pkg/apis") {
127 filePath := strings.Join([]string{build.Default.GOPATH, "src", path, "doc.go"}, "/")
130 file, err := os.Open(filePath)
135 scanner := bufio.NewScanner(file)
137 if strings.HasPrefix(scanner.Text(), "//") {
138 lines = append(lines, strings.Replace(scanner.Text(), "// ", "", 1))
141 if err := scanner.Err(); err != nil {
145 comments := Comments(lines)
146 domain = comments.getTag("domain", "=")