-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrd-transform.go
More file actions
362 lines (309 loc) · 10.5 KB
/
crd-transform.go
File metadata and controls
362 lines (309 loc) · 10.5 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
Copyright 2020-Present Couchbase, Inc.
Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/
// This takes generated CDR YAML and parses out any broken stuff.
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/couchbase/couchbase-operator/pkg/util/constants"
"github.com/couchbase/couchbase-operator/pkg/version"
"github.com/ghodss/yaml"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// pathMatch matches a JSON path within a versioned kind.
type pathMatch struct {
// group is the group of a CRD resource e.g. "couchbase.com". Leave blank to
// match all groups.
group string
// kind is the CRD kind of resource e.g. "CouchbaseCluster". Leave blank to
// match all kinds.
kind string
// path ts the json path to match e.g. ".spec.foo". This may be prefixed with
// an asterix to perform a suffix match.
path string
// mutate is used to do modify attributes.
mutate func(value interface{}) interface{}
}
// pathMatcher is an abstraction to see if a particular JSON path matches something we
// care about.
type pathMatcher []pathMatch
// get looks up the first path match.
func (p pathMatcher) get(group, kind, path string) *pathMatch {
for i := range p {
pm := &p[i]
if pm.group != "" && pm.group != group {
continue
}
if pm.kind != "" && pm.kind != kind {
continue
}
if pm.path != path {
if pm.path[0] != '*' {
continue
}
if !strings.HasSuffix(path, pm.path[1:]) {
continue
}
}
return pm
}
return nil
}
// contains determines whether there a path match for the resource type.
func (p pathMatcher) contains(group, kind, path string) bool {
glog.V(2).Infof("Examining path %s %s %s", group, kind, path)
return p.get(group, kind, path) != nil
}
// retain is a set of paths we should always keep that would be otherwise pruned.
var retain = pathMatcher{
// This needs to be an empty object in order to work, not be pruned.
{
path: ".spec.versions.subresources.status",
},
}
// discard is a set of paths that we should always remove, typically these are
// due to Kubernetes breaking backwards compatibility.
var discard = pathMatcher{
// Don't emit the status, kubernetes won't accept it.
{
path: ".status",
},
// These native types are invalid, and also unnecessary, so just remove them.
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.networking.properties.adminConsoleServiceTemplate.properties.spec.properties.ports",
},
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.networking.properties.exposedFeatureServiceTemplate.properties.spec.properties.ports",
},
// In operator 2.0 (1.13) this was not marked as omitempty, as a result
// when upgrading to operator 2.1+ (1.17+), the "null" fails validation because
// it's not an object. To support concurrent operation, we just ignore this
// attribute as it's unimportant.
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.volumeClaimTemplates.items.properties.spec.properties.dataSource",
},
// Validation is "broken" for pod templates, in that they require at least
// one container, so remove this restriction, and prevent process injection!
// The removal of required is somewhat imprecise and may need fixing in the
// future, it's a hard problem as we cannot remove array elements by value
// with JSON paths.
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.servers.items.properties.pod.properties.spec.properties.containers",
},
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.servers.items.properties.pod.properties.spec.properties.initContainers",
},
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.servers.items.properties.pod.properties.spec.required",
},
// Stuff we always override.
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.networking.properties.adminConsoleServiceTemplate.properties.spec.properties.publishNotReadyAddresses",
},
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.networking.properties.adminConsoleServiceTemplate.properties.spec.properties.selector",
},
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.networking.properties.exposedFeatureServiceTemplate.properties.spec.properties.publishNotReadyAddresses",
},
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.networking.properties.exposedFeatureServiceTemplate.properties.spec.properties.selector",
},
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.servers.items.properties.pod.properties.spec.properties.restartPolicy",
},
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.servers.items.properties.pod.properties.spec.properties.hostname",
},
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.servers.items.properties.pod.properties.spec.properties.subdomain",
},
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.servers.items.properties.pod.properties.spec.properties.securityContext",
},
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.servers.items.properties.pod.properties.spec.properties.readinessGates",
},
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.servers.items.properties.pod.properties.spec.properties.hostAliases",
},
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.servers.items.properties.pod.properties.spec.properties.volumes",
},
{
group: "couchbase.com",
kind: "CouchbaseCluster",
path: ".spec.versions.schema.openAPIV3Schema.properties.spec.properties.servers.items.properties.pod.properties.spec.properties.ephemeralContainers",
},
}
// mutators allow the modification of attributes. The only reason this is necessary is
// because controller-tools is broken.
var mutators = pathMatcher{
// Kubebuilder has no way of creating an empty object as a default, so we
// need to explicitly mark this and mutate it to a valid CRD default.
{
path: "*.default",
mutate: mutateEmptyObjectDefault,
},
}
// mutateEmptyObjectDefault catches our empty object marker and replaces it with an
// empty object.
func mutateEmptyObjectDefault(v interface{}) interface{} {
if value, ok := v.(string); ok && value == "x-couchbase-object" {
return struct{}{}
}
return v
}
func prune(in interface{}, group, kind, path string) (interface{}, error) {
// Discard anything we are forced to.
if discard.contains(group, kind, path) {
glog.V(1).Infof("Discarding %s %s %s", group, kind, path)
return nil, nil
}
// Mutate any attributes that we've injected markers to do so.
if pm := mutators.get(group, kind, path); pm != nil {
glog.V(1).Infof("Mutating %s %s %s", group, kind, path)
return pm.mutate(in), nil
}
// Keep anything we are forced to.
if retain.contains(group, kind, path) {
glog.V(1).Infof("Retaining %s %s %s", group, kind, path)
return in, nil
}
switch t := in.(type) {
case map[string]interface{}:
out := map[string]interface{}{}
for k, v := range t {
pruned, err := prune(v, group, kind, path+"."+k)
if err != nil {
return nil, err
}
if pruned != nil {
out[k] = pruned
}
}
if len(out) == 0 {
return nil, nil
}
return out, nil
case []interface{}:
out := []interface{}{}
for _, v := range t {
pruned, err := prune(v, group, kind, path)
if err != nil {
return nil, err
}
if pruned != nil {
out = append(out, pruned)
}
}
if len(out) == 0 {
return nil, nil
}
return out, nil
default:
return in, nil
}
}
func main() {
var in string
var out string
flag.StringVar(&in, "in", "example/crd.yaml", "Input file")
flag.StringVar(&out, "out", "example/crd.yaml", "Output file")
flag.Parse()
input, err := os.ReadFile(in)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
manifests := strings.Split(string(input), "\n---\n")
for i, manifest := range manifests {
if strings.TrimSpace(manifest) == "" {
continue
}
raw := unstructured.Unstructured{}
if err := yaml.Unmarshal([]byte(manifest), &raw); err != nil {
glog.Exit(err)
}
group, ok, _ := unstructured.NestedString(raw.Object, "spec", "group")
if !ok {
glog.Exit("CRD doesn't have group attribute")
}
kind, ok, _ := unstructured.NestedString(raw.Object, "spec", "names", "kind")
if !ok {
glog.Exit("CRD doesn't have kind name attribute")
}
// Recusively do a DFS through the tree removing any bad things that
// cause failure (e.g. bugs in Kubernetes types that haven't been fixed).
pruned, err := prune(raw.Object, group, kind, "")
if err != nil {
glog.Exit(err)
}
prunedObject, ok := pruned.(map[string]interface{})
if !ok {
glog.Exit("Pruned CRD in wrong format")
}
output := unstructured.Unstructured{
Object: prunedObject,
}
// Set the version information.
annotations := output.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
annotations[constants.ConfigurationVersionAnnotation] = version.Version
output.SetAnnotations(annotations)
encoded, err := yaml.Marshal(output.Object)
if err != nil {
glog.Exit(err)
}
manifests[i] = string(encoded)
}
output := strings.Join(manifests, "---\n")
if err := os.WriteFile(out, []byte(output), 0o644); err != nil {
glog.Exit(err)
}
}