-
Notifications
You must be signed in to change notification settings - Fork 420
/
crd.go
318 lines (264 loc) · 9.86 KB
/
crd.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
/*
Copyright 2019 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 markers
import (
"fmt"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"sigs.k8s.io/controller-tools/pkg/markers"
)
// CRDMarkers lists all markers that directly modify the CRD (not validation
// schemas).
var CRDMarkers = []*definitionWithHelp{
// TODO(directxman12): more detailed help
must(markers.MakeDefinition("kubebuilder:subresource:status", markers.DescribesType, SubresourceStatus{})).
WithHelp(SubresourceStatus{}.Help()),
must(markers.MakeDefinition("kubebuilder:subresource:scale", markers.DescribesType, SubresourceScale{})).
WithHelp(SubresourceScale{}.Help()),
must(markers.MakeDefinition("kubebuilder:printcolumn", markers.DescribesType, PrintColumn{})).
WithHelp(PrintColumn{}.Help()),
must(markers.MakeDefinition("kubebuilder:resource", markers.DescribesType, Resource{})).
WithHelp(Resource{}.Help()),
must(markers.MakeDefinition("kubebuilder:storageversion", markers.DescribesType, StorageVersion{})).
WithHelp(StorageVersion{}.Help()),
must(markers.MakeDefinition("kubebuilder:skipversion", markers.DescribesType, SkipVersion{})).
WithHelp(SkipVersion{}.Help()),
must(markers.MakeDefinition("kubebuilder:unservedversion", markers.DescribesType, UnservedVersion{})).
WithHelp(UnservedVersion{}.Help()),
}
// TODO: categories and singular used to be annotations types
// TODO: doc
func init() {
AllDefinitions = append(AllDefinitions, CRDMarkers...)
}
// +controllertools:marker:generateHelp:category=CRD
// SubresourceStatus enables the "/status" subresource on a CRD.
type SubresourceStatus struct{}
func (s SubresourceStatus) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
var subresources *apiext.CustomResourceSubresources
for i := range crd.Versions {
ver := &crd.Versions[i]
if ver.Name != version {
continue
}
if ver.Subresources == nil {
ver.Subresources = &apiext.CustomResourceSubresources{}
}
subresources = ver.Subresources
break
}
if subresources == nil {
return fmt.Errorf("status subresource applied to version %q not in CRD", version)
}
subresources.Status = &apiext.CustomResourceSubresourceStatus{}
return nil
}
// +controllertools:marker:generateHelp:category=CRD
// SubresourceScale enables the "/scale" subresource on a CRD.
type SubresourceScale struct {
// marker names are leftover legacy cruft
// SpecPath specifies the jsonpath to the replicas field for the scale's spec.
SpecPath string `marker:"specpath"`
// StatusPath specifies the jsonpath to the replicas field for the scale's status.
StatusPath string `marker:"statuspath"`
// SelectorPath specifies the jsonpath to the pod label selector field for the scale's status.
//
// The selector field must be the *string* form (serialized form) of a selector.
// Setting a pod label selector is necessary for your type to work with the HorizontalPodAutoscaler.
SelectorPath *string `marker:"selectorpath"`
}
func (s SubresourceScale) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
var subresources *apiext.CustomResourceSubresources
for i := range crd.Versions {
ver := &crd.Versions[i]
if ver.Name != version {
continue
}
if ver.Subresources == nil {
ver.Subresources = &apiext.CustomResourceSubresources{}
}
subresources = ver.Subresources
break
}
if subresources == nil {
return fmt.Errorf("scale subresource applied to version %q not in CRD", version)
}
subresources.Scale = &apiext.CustomResourceSubresourceScale{
SpecReplicasPath: s.SpecPath,
StatusReplicasPath: s.StatusPath,
LabelSelectorPath: s.SelectorPath,
}
return nil
}
// +controllertools:marker:generateHelp:category=CRD
// StorageVersion marks this version as the "storage version" for the CRD for conversion.
//
// When conversion is enabled for a CRD (i.e. it's not a trivial-versions/single-version CRD),
// one version is set as the "storage version" to be stored in etcd. Attempting to store any
// other version will result in conversion to the storage version via a conversion webhook.
type StorageVersion struct{}
func (s StorageVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
if version == "" {
// single-version, do nothing
return nil
}
// multi-version
for i := range crd.Versions {
ver := &crd.Versions[i]
if ver.Name != version {
continue
}
ver.Storage = true
break
}
return nil
}
// +controllertools:marker:generateHelp:category=CRD
// SkipVersion removes the particular version of the CRD from the CRDs spec.
//
// This is useful if you need to skip generating and listing version entries
// for 'internal' resource versions, which typically exist if using the
// Kubernetes upstream conversion-gen tool.
type SkipVersion struct{}
func (s SkipVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
if version == "" {
// single-version, this is an invalid state
return fmt.Errorf("cannot skip a version if there is only a single version")
}
var versions []apiext.CustomResourceDefinitionVersion
// multi-version
for i := range crd.Versions {
ver := crd.Versions[i]
if ver.Name == version {
// skip the skipped version
continue
}
versions = append(versions, ver)
}
crd.Versions = versions
return nil
}
// +controllertools:marker:generateHelp:category=CRD
// PrintColumn adds a column to "kubectl get" output for this CRD.
type PrintColumn struct {
// Name specifies the name of the column.
Name string
// Type indicates the type of the column.
//
// It may be any OpenAPI data type listed at
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types.
Type string
// JSONPath specifies the jsonpath expression used to extract the value of the column.
JSONPath string `marker:"JSONPath"` // legacy cruft
// Description specifies the help/description for this column.
Description string `marker:",optional"`
// Format specifies the format of the column.
//
// It may be any OpenAPI data format corresponding to the type, listed at
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types.
Format string `marker:",optional"`
// Priority indicates how important it is that this column be displayed.
//
// Lower priority (*higher* numbered) columns will be hidden if the terminal
// width is too small.
Priority int32 `marker:",optional"`
}
func (s PrintColumn) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
var columns *[]apiext.CustomResourceColumnDefinition
for i := range crd.Versions {
ver := &crd.Versions[i]
if ver.Name != version {
continue
}
if ver.Subresources == nil {
ver.Subresources = &apiext.CustomResourceSubresources{}
}
columns = &ver.AdditionalPrinterColumns
break
}
if columns == nil {
return fmt.Errorf("printer columns applied to version %q not in CRD", version)
}
*columns = append(*columns, apiext.CustomResourceColumnDefinition{
Name: s.Name,
Type: s.Type,
JSONPath: s.JSONPath,
Description: s.Description,
Format: s.Format,
Priority: s.Priority,
})
return nil
}
// +controllertools:marker:generateHelp:category=CRD
// Resource configures naming and scope for a CRD.
type Resource struct {
// Path specifies the plural "resource" for this CRD.
//
// It generally corresponds to a plural, lower-cased version of the Kind.
// See https://book.kubebuilder.io/cronjob-tutorial/gvks.html.
Path string `marker:",optional"`
// ShortName specifies aliases for this CRD.
//
// Short names are often used when people have work with your resource
// over and over again. For instance, "rs" for "replicaset" or
// "crd" for customresourcedefinition.
ShortName []string `marker:",optional"`
// Categories specifies which group aliases this resource is part of.
//
// Group aliases are used to work with groups of resources at once.
// The most common one is "all" which covers about a third of the base
// resources in Kubernetes, and is generally used for "user-facing" resources.
Categories []string `marker:",optional"`
// Singular overrides the singular form of your resource.
//
// The singular form is otherwise defaulted off the plural (path).
Singular string `marker:",optional"`
// Scope overrides the scope of the CRD (Cluster vs Namespaced).
//
// Scope defaults to "Namespaced". Cluster-scoped ("Cluster") resources
// don't exist in namespaces.
Scope string `marker:",optional"`
}
func (s Resource) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
if s.Path != "" {
crd.Names.Plural = s.Path
}
if s.Singular != "" {
crd.Names.Singular = s.Singular
}
crd.Names.ShortNames = s.ShortName
crd.Names.Categories = s.Categories
switch s.Scope {
case "":
crd.Scope = apiext.NamespaceScoped
default:
crd.Scope = apiext.ResourceScope(s.Scope)
}
return nil
}
// +controllertools:marker:generateHelp:category=CRD
// UnservedVersion does not serve this version.
//
// This is useful if you need to drop support for a version in favor of a newer version.
type UnservedVersion struct{}
func (s UnservedVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
for i := range crd.Versions {
ver := &crd.Versions[i]
if ver.Name != version {
continue
}
ver.Served = false
break
}
return nil
}
// NB(directxman12): singular was historically distinct, so we keep it here for backwards compat