This repository was archived by the owner on Dec 11, 2021. It is now read-only.
forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrd.go
333 lines (307 loc) · 9.06 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2018 The Operator-SDK 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 scaffold
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/input"
"github.com/operator-framework/operator-sdk/internal/util/k8sutil"
"github.com/ghodss/yaml"
"github.com/spf13/afero"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
crdgenerator "sigs.k8s.io/controller-tools/pkg/crd/generator"
)
// CRD is the input needed to generate a deploy/crds/<group>_<version>_<kind>_crd.yaml file
type CRD struct {
input.Input
// Resource defines the inputs for the new custom resource definition
Resource *Resource
// IsOperatorGo is true when the operator is written in Go.
IsOperatorGo bool
}
func (s *CRD) GetInput() (input.Input, error) {
if s.Path == "" {
fileName := fmt.Sprintf("%s_%s_%s_crd.yaml",
strings.ToLower(s.Resource.Group),
strings.ToLower(s.Resource.Version),
s.Resource.LowerKind)
s.Path = filepath.Join(CRDsDir, fileName)
}
initCache()
return s.Input, nil
}
type fsCache struct {
afero.Fs
}
func (c *fsCache) fileExists(path string) bool {
_, err := c.Stat(path)
return err == nil
}
var (
// Global cache so users can use new CRD structs.
cache *fsCache
once sync.Once
)
func initCache() {
once.Do(func() {
cache = &fsCache{Fs: afero.NewMemMapFs()}
})
}
func (s *CRD) SetFS(_ afero.Fs) {}
func (s *CRD) CustomRender() ([]byte, error) {
i, err := s.GetInput()
if err != nil {
return nil, err
}
// controller-tools generates crd file names with no _crd.yaml suffix:
// <group>_<version>_<kind>.yaml.
path := strings.Replace(filepath.Base(i.Path), "_crd.yaml", ".yaml", 1)
// controller-tools' generators read and make crds for all apis in pkg/apis,
// so generate crds in a cached, in-memory fs to extract the data we need.
if s.IsOperatorGo && !cache.fileExists(path) {
g := &crdgenerator.Generator{
RootPath: s.AbsProjectPath,
Domain: strings.SplitN(s.Resource.FullGroup, ".", 2)[1],
OutputDir: ".",
SkipMapValidation: false,
OutFs: cache,
}
if err := g.ValidateAndInitFields(); err != nil {
return nil, err
}
if err := g.Do(); err != nil {
return nil, err
}
}
dstCRD := newCRDForResource(s.Resource)
// Get our generated crd's from the in-memory fs. If it doesn't exist in the
// fs, the corresponding API does not exist yet, so scaffold a fresh crd
// without a validation spec.
// If the crd exists in the fs, and a local crd exists, append the validation
// spec. If a local crd does not exist, use the generated crd.
if _, err := cache.Stat(path); err != nil && !os.IsNotExist(err) {
return nil, err
} else if err == nil {
b, err := afero.ReadFile(cache, path)
if err != nil {
return nil, err
}
dstCRD = &apiextv1beta1.CustomResourceDefinition{}
if err = yaml.Unmarshal(b, dstCRD); err != nil {
return nil, err
}
val := dstCRD.Spec.Validation.DeepCopy()
// If the crd exists at i.Path, append the validation spec to its crd spec.
if _, err := os.Stat(i.Path); err == nil {
cb, err := ioutil.ReadFile(i.Path)
if err != nil {
return nil, err
}
if len(cb) > 0 {
dstCRD = &apiextv1beta1.CustomResourceDefinition{}
if err = yaml.Unmarshal(cb, dstCRD); err != nil {
return nil, err
}
// val contains all validation fields from source code tagged with
// '+kubebuilder' directives. These fields should overwrite dstCRD
// validation fields except for those added manually, hence the merge.
mergeValidations(dstCRD.Spec.Validation, val)
}
}
// controller-tools does not set ListKind or Singular names.
dstCRD.Spec.Names = getCRDNamesForResource(s.Resource)
// Remove controller-tools default label.
delete(dstCRD.Labels, "controller-tools.k8s.io")
}
addCRDSubresource(dstCRD)
addCRDVersions(dstCRD)
return k8sutil.GetObjectBytes(dstCRD)
}
func newCRDForResource(r *Resource) *apiextv1beta1.CustomResourceDefinition {
return &apiextv1beta1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
APIVersion: "apiextensions.k8s.io/v1beta1",
Kind: "CustomResourceDefinition",
},
ObjectMeta: metav1.ObjectMeta{
Name: r.Resource + "." + r.FullGroup,
},
Spec: apiextv1beta1.CustomResourceDefinitionSpec{
Group: r.FullGroup,
Names: getCRDNamesForResource(r),
Scope: apiextv1beta1.NamespaceScoped,
Version: r.Version,
Subresources: &apiextv1beta1.CustomResourceSubresources{
Status: &apiextv1beta1.CustomResourceSubresourceStatus{},
},
},
}
}
func getCRDNamesForResource(r *Resource) apiextv1beta1.CustomResourceDefinitionNames {
return apiextv1beta1.CustomResourceDefinitionNames{
Kind: r.Kind,
ListKind: r.Kind + "List",
Plural: r.Resource,
Singular: r.LowerKind,
}
}
func addCRDSubresource(crd *apiextv1beta1.CustomResourceDefinition) {
if crd.Spec.Subresources == nil {
crd.Spec.Subresources = &apiextv1beta1.CustomResourceSubresources{}
}
if crd.Spec.Subresources.Status == nil {
crd.Spec.Subresources.Status = &apiextv1beta1.CustomResourceSubresourceStatus{}
}
}
func addCRDVersions(crd *apiextv1beta1.CustomResourceDefinition) {
// crd.Version is deprecated, use crd.Versions instead.
var crdVersions []apiextv1beta1.CustomResourceDefinitionVersion
if crd.Spec.Version != "" {
var verExists, hasStorageVer bool
for _, ver := range crd.Spec.Versions {
if crd.Spec.Version == ver.Name {
verExists = true
}
// There must be exactly one version flagged as a storage version.
if ver.Storage {
hasStorageVer = true
}
}
if !verExists {
crdVersions = []apiextv1beta1.CustomResourceDefinitionVersion{
{Name: crd.Spec.Version, Served: true, Storage: !hasStorageVer},
}
}
} else {
crdVersions = []apiextv1beta1.CustomResourceDefinitionVersion{
{Name: "v1alpha1", Served: true, Storage: true},
}
}
if len(crd.Spec.Versions) > 0 {
// crd.Version should always be the first element in crd.Versions.
crd.Spec.Versions = append(crdVersions, crd.Spec.Versions...)
} else {
crd.Spec.Versions = crdVersions
}
}
func mergeValidations(dstv, srcv *apiextv1beta1.CustomResourceValidation) {
mergeJSONSchemaProps(dstv.OpenAPIV3Schema, srcv.OpenAPIV3Schema)
return
}
func mergeJSONSchemaProps(dstp, srcp *apiextv1beta1.JSONSchemaProps) {
if dstp == nil || srcp == nil {
if srcp != nil {
dstp = srcp
}
return
}
if reflect.DeepEqual(dstp, srcp) {
return
}
prop := srcp.DeepCopy()
// Overwrite fields not writeable by kubebuilder directives with old values.
if prop.ID == "" {
prop.ID = dstp.ID
}
if prop.Schema == "" {
prop.Schema = dstp.Schema
}
if prop.Ref == nil {
prop.Ref = dstp.Ref
}
if prop.Description == "" {
prop.Description = dstp.Description
}
if prop.Title == "" {
prop.Title = dstp.Title
}
if prop.Default == nil {
prop.Default = dstp.Default
}
if prop.MaxProperties == nil {
prop.MaxProperties = dstp.MaxProperties
}
if prop.MinProperties == nil {
prop.MinProperties = dstp.MinProperties
}
if prop.ExternalDocs == nil {
prop.ExternalDocs = dstp.ExternalDocs
}
if prop.Example == nil {
prop.Example = dstp.Example
}
if prop.Items == nil {
prop.Items = dstp.Items
}
if prop.Not == nil {
prop.Not = dstp.Not
}
if prop.AdditionalProperties == nil {
prop.AdditionalProperties = dstp.AdditionalProperties
}
if prop.AdditionalItems == nil {
prop.AdditionalItems = dstp.AdditionalItems
}
if len(prop.Required) == 0 {
prop.Required = dstp.Required
}
if len(prop.AllOf) == 0 {
prop.AllOf = dstp.AllOf
}
if len(prop.OneOf) == 0 {
prop.OneOf = dstp.OneOf
}
if len(prop.AnyOf) == 0 {
prop.AnyOf = dstp.AnyOf
}
// Recursively merge the following fields until no sub schema props exist.
for dk, dv := range dstp.Properties {
if pv, ok := prop.Properties[dk]; ok {
mergeJSONSchemaProps(&dv, &pv)
prop.Properties[dk] = dv
}
}
for dk, dv := range dstp.PatternProperties {
if pv, ok := prop.PatternProperties[dk]; ok {
mergeJSONSchemaProps(&dv, &pv)
prop.PatternProperties[dk] = dv
}
}
for dk, dv := range dstp.Definitions {
if pv, ok := prop.Definitions[dk]; ok {
mergeJSONSchemaProps(&dv, &pv)
prop.Definitions[dk] = dv
}
}
for dk, dv := range dstp.Dependencies {
if pv, ok := prop.Dependencies[dk]; ok {
if dv.Schema != nil {
if pv.Schema != nil {
mergeJSONSchemaProps(dv.Schema, pv.Schema)
}
prop.Dependencies[dk] = dv
} else if len(dv.Property) != 0 && len(pv.Property) == 0 && pv.Schema == nil {
prop.Dependencies[dk] = dv
}
}
}
*dstp = *prop
return
}