Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-tools / pkg / internal / codegen / types.go
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 codegen
18
19 import (
20         "sort"
21
22         rbacv1 "k8s.io/api/rbac/v1"
23         "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
24         v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25         "k8s.io/apimachinery/pkg/runtime/schema"
26         "k8s.io/apimachinery/pkg/util/sets"
27         "k8s.io/gengo/types"
28 )
29
30 // APIs is the information of a collection of API
31 type APIs struct {
32         // Domain is the domain portion of the group - e.g. k8s.io
33         Domain string
34
35         // Package is the name of the root API package - e.g. github.com/my-org/my-repo/pkg/apis
36         Package string
37
38         // Pkg the Package for the root API package
39         Pkg *types.Package
40
41         // Groups is the list of API groups found under the apis package
42         Groups map[string]*APIGroup
43
44         Rules []rbacv1.PolicyRule
45
46         Informers map[v1.GroupVersionKind]bool
47 }
48
49 // GetRules get rules of the APIs
50 func (apis *APIs) GetRules() []rbacv1.PolicyRule {
51         rules := []rbacv1.PolicyRule{}
52         rulesIndex := map[v1.GroupResource]sets.String{}
53         for _, rule := range apis.Rules {
54                 for _, g := range rule.APIGroups {
55                         for _, r := range rule.Resources {
56                                 gr := v1.GroupResource{
57                                         Group:    g,
58                                         Resource: r,
59                                 }
60                                 if _, found := rulesIndex[gr]; !found {
61                                         rulesIndex[gr] = sets.NewString()
62                                 }
63                                 rulesIndex[gr].Insert(rule.Verbs...)
64                         }
65                 }
66         }
67         for gr, v := range rulesIndex {
68                 verbs := v.List()
69                 sort.Strings(verbs)
70                 rule := rbacv1.PolicyRule{
71                         Resources: []string{gr.Resource},
72                         APIGroups: []string{gr.Group},
73                         Verbs:     verbs,
74                 }
75                 rules = append(rules, rule)
76         }
77         return rules
78 }
79
80 // APIGroup contains information of an API group.
81 type APIGroup struct {
82         // Package is the name of the go package the api group is under - e.g. github.com/me/apiserver-helloworld/apis
83         Package string
84         // Domain is the domain portion of the group - e.g. k8s.io
85         Domain string
86         // Group is the short name of the group - e.g. mushroomkingdom
87         Group      string
88         GroupTitle string
89         // Versions is the list of all versions for this group keyed by name
90         Versions map[string]*APIVersion
91
92         UnversionedResources map[string]*APIResource
93
94         // Structs is a list of unversioned definitions that must be generated
95         Structs []*Struct
96         Pkg     *types.Package
97         PkgPath string
98 }
99
100 // Struct contains information of a struct.
101 type Struct struct {
102         // Name is the name of the type
103         Name string
104         // genClient
105         GenClient     bool
106         GenDeepCopy   bool
107         NonNamespaced bool
108
109         GenUnversioned bool
110         // Fields is the list of fields appearing in the struct
111         Fields []*Field
112 }
113
114 // Field contains information of a field.
115 type Field struct {
116         // Name is the name of the field
117         Name string
118         // For versioned Kubernetes types, this is the versioned package
119         VersionedPackage string
120         // For versioned Kubernetes types, this is the unversioned package
121         UnversionedImport string
122         UnversionedType   string
123 }
124
125 // APIVersion contains information of an API version.
126 type APIVersion struct {
127         // Domain is the group domain - e.g. k8s.io
128         Domain string
129         // Group is the group name - e.g. mushroomkingdom
130         Group string
131         // Version is the api version - e.g. v1beta1
132         Version string
133         // Resources is a list of resources appearing in the API version keyed by name
134         Resources map[string]*APIResource
135         // Pkg is the Package object from code-gen
136         Pkg *types.Package
137 }
138
139 // APIResource contains information of an API resource.
140 type APIResource struct {
141         // Domain is the group domain - e.g. k8s.io
142         Domain string
143         // Group is the group name - e.g. mushroomkingdom
144         Group string
145         // Version is the api version - e.g. v1beta1
146         Version string
147         // Kind is the resource name - e.g. PeachesCastle
148         Kind string
149         // Resource is the resource name - e.g. peachescastles
150         Resource string
151         // REST is the rest.Storage implementation used to handle requests
152         // This field is optional. The standard REST implementation will be used
153         // by default.
154         REST string
155         // Subresources is a map of subresources keyed by name
156         Subresources map[string]*APISubresource
157         // Type is the Type object from code-gen
158         Type *types.Type
159         // Strategy is name of the struct to use for the strategy
160         Strategy string
161         // Strategy is name of the struct to use for the strategy
162         StatusStrategy string
163         // NonNamespaced indicates that the resource kind is non namespaced
164         NonNamespaced bool
165
166         ShortName string
167
168         JSONSchemaProps    v1beta1.JSONSchemaProps
169         CRD                v1beta1.CustomResourceDefinition
170         Validation         string
171         ValidationComments string
172         // DocAnnotation is a map of annotations by name for doc. e.g. warning, notes message
173         DocAnnotation map[string]string
174         // Categories is a list of categories the resource is part of.
175         Categories []string
176 }
177
178 // APISubresource contains information of an API subresource.
179 type APISubresource struct {
180         // Domain is the group domain - e.g. k8s.io
181         Domain string
182         // Group is the group name - e.g. mushroomkingdom
183         Group string
184         // Version is the api version - e.g. v1beta1
185         Version string
186         // Kind is the resource name - e.g. PeachesCastle
187         Kind string
188         // Resource is the resource name - e.g. peachescastles
189         Resource string
190         // Request is the subresource request type - e.g. ScaleCastle
191         Request string
192         // REST is the rest.Storage implementation used to handle requests
193         REST string
194         // Path is the subresource path - e.g. scale
195         Path string
196
197         // ImportPackage is the import statement that must appear for the Request
198         ImportPackage string
199
200         // RequestType is the type of the request
201         RequestType *types.Type
202
203         // RESTType is the type of the request handler
204         RESTType *types.Type
205 }
206
207 // Controller contains information of a controller.
208 type Controller struct {
209         Target   schema.GroupVersionKind
210         Resource string
211         Pkg      *types.Package
212         Repo     string
213 }