-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmanager.go
More file actions
396 lines (366 loc) · 13.7 KB
/
manager.go
File metadata and controls
396 lines (366 loc) · 13.7 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package k8s
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
"slices"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kschema "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/rest"
"github.com/grafana/grafana-app-sdk/resource"
)
// ResourceManager is a struct that implements resource.Manager, allowing a user to manage Schemas as
// Custom Resource Definitions in kubernetes.
type ResourceManager struct {
client rest.Interface
}
// NewManager creates a new ResourceManager
func NewManager(cfg rest.Config) (*ResourceManager, error) {
// Create the kubernetes client for CRD's
cfg.GroupVersion = &kschema.GroupVersion{
Group: "apiextensions.k8s.io",
Version: "v1",
}
cfg.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{
CodecFactory: serializer.NewCodecFactory(runtime.NewScheme()),
}
client, err := rest.RESTClientFor(&cfg)
if err != nil {
return nil, err
}
return &ResourceManager{
client: client,
}, nil
}
// WaitForAvailability polls the kubernetes API server every second until it gets a successful response
// for the Schema's CRD name
func (m *ResourceManager) WaitForAvailability(ctx context.Context, schema resource.Schema) error {
name := fmt.Sprintf("%s.%s", schema.Plural(), schema.Group())
sc := 0
t := time.NewTicker(time.Second)
defer t.Stop()
for {
select {
case <-t.C:
err := m.client.Get().Resource("customresourcedefinitions").Name(name).
Do(ctx).StatusCode(&sc).Error()
if err == nil {
return nil
}
if err != nil && sc != http.StatusNotFound {
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
}
// RegisterSchema converts a Schema to a Custom Resource Definition, then attempts to create it in kubernetes.
// If a CRD already exists for the name, it checks to see if this is a new version and attempts to update the CRD
// with the new version.
func (m *ResourceManager) RegisterSchema(ctx context.Context, schema resource.Schema,
options resource.RegisterSchemaOptions) error {
name := fmt.Sprintf("%s.%s", schema.Plural(), schema.Group())
// First, check if the CRD already exists
sc := 0
existing := CustomResourceDefinition{}
err := m.client.Get().Resource("customresourcedefinitions").Name(name).
Do(ctx).StatusCode(&sc).Into(&existing)
if err != nil && sc != http.StatusNotFound {
return ParseKubernetesError(nil, sc, err)
}
if sc == http.StatusNotFound {
// Create new
return m.create(ctx, schema, name)
}
// Check if the provided version already exists
replaced := false
for idx, v := range existing.Spec.Versions {
if v.Name == schema.Version() {
if !options.UpdateOnConflict {
if options.NoErrorOnConflict {
return nil // Quietly exit
}
return errors.New("schema with identical kind, group, and version already registered")
}
// Replace with the new version
existing.Spec.Versions[idx] = toVersion(schema)
replaced = true
break
}
}
if !replaced {
// If we didn't replace a version, append
existing.Spec.Versions = append(existing.Spec.Versions, toVersion(schema))
}
// Make sure the latest is the one with storage = true
slices.SortFunc(existing.Spec.Versions, func(a, b CustomResourceDefinitionSpecVersion) int {
return strings.Compare(b.Name, a.Name)
})
for i := 0; i < len(existing.Spec.Versions); i++ {
existing.Spec.Versions[i].Storage = false
}
existing.Spec.Versions[len(existing.Spec.Versions)-1].Storage = true
bytes, err := json.Marshal(existing)
if err != nil {
return err
}
err = m.client.Put().Resource("customresourcedefinitions").Body(bytes).Do(ctx).StatusCode(&sc).Error()
if err != nil {
return ParseKubernetesError(nil, sc, err)
}
if options.WaitForAvailability {
return m.WaitForAvailability(ctx, schema)
}
return nil
}
func (m *ResourceManager) create(ctx context.Context, schema resource.Schema, name string) error {
crd := CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
APIVersion: "apiextensions.k8s.io/v1",
Kind: "CustomResourceDefinition",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: CustomResourceDefinitionSpec{
Group: schema.Group(),
// Versions defined later
Names: CustomResourceDefinitionSpecNames{
Kind: schema.Kind(),
Plural: schema.Plural(),
},
Scope: "Namespaced",
},
}
version := toVersion(schema)
version.Storage = true
crd.Spec.Versions = []CustomResourceDefinitionSpecVersion{
version,
}
bytes, err := json.Marshal(crd)
if err != nil {
return err
}
sc := 0
err = m.client.Post().Resource("customresourcedefinitions").Body(bytes).Do(ctx).StatusCode(&sc).Error()
return ParseKubernetesError(nil, sc, err)
}
func toVersion(schema resource.Schema) CustomResourceDefinitionSpecVersion {
obj := schema.ZeroValue()
version := CustomResourceDefinitionSpecVersion{
Name: schema.Version(),
Served: true,
Storage: false,
Subresources: make(map[string]any),
}
schemaProperties := map[string]any{
"spec": map[string]any{
"type": openAPITypeObject,
"properties": toOpenAPIV3(reflect.TypeOf(obj.GetSpec())),
},
}
// Check for status, scale subresources
if status, ok := obj.GetSubresources()["status"]; ok {
schemaProperties["status"] = map[string]any{
"type": openAPITypeObject,
"properties": toOpenAPIV3(reflect.TypeOf(status)),
}
// Add the subresource as an empty struct (this signals kubernetes to use the one supplied in the schema)
version.Subresources["status"] = struct{}{}
}
if scale, ok := obj.GetSubresources()["scale"]; ok {
schemaProperties["scale"] = map[string]any{
"type": openAPITypeObject,
"properties": toOpenAPIV3(reflect.TypeOf(scale)),
}
// Add the subresource as an empty struct (this signals kubernetes to use the one supplied in the schema)
version.Subresources["scale"] = struct{}{}
}
version.Schema = map[string]any{
"openAPIV3Schema": map[string]any{
"type": openAPITypeObject,
"properties": schemaProperties,
},
}
return version
}
const (
openAPITypeObject = "object"
)
// toOpenAPIV3 converts a struct into a map[string]any representation of an OpenAPIV3-compliant JSON spec
func toOpenAPIV3(typ reflect.Type) map[string]any { // nolint: funlen
for typ.Kind() == reflect.Pointer {
typ = typ.Elem()
}
m := make(map[string]any)
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
// Process type
fieldType := field.Type
for fieldType.Kind() == reflect.Pointer {
fieldType = fieldType.Elem()
}
v := make(map[string]any)
switch fieldType.Kind() {
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
v["type"] = "integer"
case reflect.Float32, reflect.Float64:
v["type"] = "number"
case reflect.String:
v["type"] = "string"
case reflect.Bool:
v["type"] = "boolean"
case reflect.Map, reflect.Interface:
// A promoted/embedded map or interface should just result in our spec accepting and
// preserving arbitrary keys
if field.Anonymous {
m["x-kubernetes-preserve-unknown-fields"] = true
continue
}
v["type"] = openAPITypeObject
// Use x-kubernetes-preserve-unknown-fields here, because kubernetes acts weird
// when using additionalProperties
v["x-kubernetes-preserve-unknown-fields"] = true
case reflect.Struct:
props := toOpenAPIV3(fieldType)
if field.Anonymous { // Embed anonymous fields
for key, val := range props {
m[key] = val
}
continue
}
v["type"] = openAPITypeObject
v["properties"] = toOpenAPIV3(fieldType)
case reflect.Slice, reflect.Array:
v["type"] = "array"
itemType := fieldType.Elem()
for itemType.Kind() == reflect.Pointer {
itemType = itemType.Elem()
}
// TODO: embedded switch is gross
vv := make(map[string]any)
switch itemType.Kind() {
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
vv["type"] = "integer"
case reflect.Float32, reflect.Float64:
vv["type"] = "number"
case reflect.String:
vv["type"] = "string"
case reflect.Bool:
vv["type"] = "boolean"
case reflect.Struct, reflect.Slice, reflect.Array:
vv["type"] = openAPITypeObject
vv["properties"] = toOpenAPIV3(itemType)
default:
// Any other types (map, interface, anything unknown), treat it like an array of arbitrary objects
vv["type"] = openAPITypeObject
// Use x-kubernetes-preserve-unknown-fields here, because kubernetes acts weird
// when using additionalProperties
vv["x-kubernetes-preserve-unknown-fields"] = true
}
v["items"] = vv
default:
continue // Not a type we can handle
}
m[getFieldKey(&field)] = v
}
return m
}
// getFieldKey will return the field's JSON tag, if present, and if not present, the field name
func getFieldKey(field *reflect.StructField) string {
name := field.Tag.Get("json")
if name == "" {
return field.Name
}
parts := strings.Split(name, ",")
if len(parts) > 1 {
return parts[0]
}
return name
}
// CustomResourceDefinition is the kubernetes-API-compliant representation of a Custom Resource Definition
type CustomResourceDefinition struct {
metav1.TypeMeta `json:",inline" yaml:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
Spec CustomResourceDefinitionSpec `json:"spec"`
}
// DeepCopyObject implements runtime.Object.
func (crd *CustomResourceDefinition) DeepCopyObject() runtime.Object {
return DeepCopyObject(crd)
}
// CustomResourceDefinitionSpec is the body or spec of a kubernetes Custom Resource Definition
type CustomResourceDefinitionSpec struct {
Group string `json:"group" yaml:"group"`
Versions []CustomResourceDefinitionSpecVersion `json:"versions" yaml:"versions"`
Names CustomResourceDefinitionSpecNames `json:"names" yaml:"names"`
Conversion *CustomResourceDefinitionSpecConversion `json:"conversion,omitempty" yaml:"conversion,omitempty"`
Scope string `json:"scope" yaml:"scope"`
}
type CustomResourceDefinitionSpecConversion struct {
Strategy string `json:"strategy" yaml:"strategy"`
Webhook *CustomResourceDefinitionSpecConversionWebhook `json:"webhook,omitempty" yaml:"webhook,omitempty"`
}
type CustomResourceDefinitionSpecConversionWebhook struct {
ConversionReviewVersions []string `json:"conversionReviewVersions" yaml:"conversionReviewVersions"`
ClientConfig CustomResourceDefinitionClientConfig `json:"clientConfig" yaml:"clientConfig"`
}
type CustomResourceDefinitionClientConfig struct {
Service *CustomResourceDefinitionClientConfigService `json:"service,omitempty" yaml:"service,omitempty"`
URL string `json:"url,omitempty" yaml:"url,omitempty"`
}
type CustomResourceDefinitionClientConfigService struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
Path string `json:"path" yaml:"path"`
}
// CustomResourceDefinitionSpecVersion is the representation of a specific version of a CRD, as part of the overall spec
type CustomResourceDefinitionSpecVersion struct {
Name string `json:"name" yaml:"name"`
Served bool `json:"served" yaml:"served"`
Storage bool `json:"storage" yaml:"storage"`
Schema map[string]any `json:"schema" yaml:"schema"`
Subresources map[string]any `json:"subresources,omitempty" yaml:"subresources,omitempty"`
SelectableFields []CustomResourceDefinitionSelectableField `json:"selectableFields,omitempty" yaml:"selectableFields,omitempty"`
AdditionalPrinterColumns []CustomResourceDefinitionAdditionalPrinterColumn `json:"additionalPrinterColumns,omitempty" yaml:"additionalPrinterColumns,omitempty"`
}
// CustomResourceDefinitionSpecNames is the struct representing the names (kind and plural) of a kubernetes CRD
type CustomResourceDefinitionSpecNames struct {
Kind string `json:"kind" yaml:"kind"`
Plural string `json:"plural" yaml:"plural"`
}
// CustomResourceDefinitionSelectableField is the struct representing a selectable field in a kubernetes CRD.
// This is a copy of https://pkg.go.dev/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1#SelectableField
// with YAML tags attached to the field.
type CustomResourceDefinitionSelectableField struct {
JSONPath string `json:"jsonPath" yaml:"jsonPath"`
}
// CustomResourceDefinitionAdditionalPrinterColumn is the struct representing an additional printer column in a kubernetes CRD.
// This is a copy of https://pkg.go.dev/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1#CustomResourceDefinitionAdditionalPrinterColumn
type CustomResourceDefinitionAdditionalPrinterColumn struct {
Name string `json:"name" yaml:"name"`
Type string `json:"type" yaml:"type"`
Format *string `json:"format,omitempty" yaml:"format,omitempty"`
Description *string `json:"description,omitempty" yaml:"description,omitempty"`
Priority *int32 `json:"priority,omitempty" yaml:"priority,omitempty"`
JSONPath string `json:"jsonPath" yaml:"jsonPath"`
}
// DeepCopyObject is an implementation of the receiver method required for implementing runtime.Object.
func DeepCopyObject(in any) runtime.Object {
val := reflect.ValueOf(in).Elem()
cpy := reflect.New(val.Type())
cpy.Elem().Set(val)
// Using the <obj>, <ok> for the type conversion ensures that it doesn't panic if it can't be converted
if obj, ok := cpy.Interface().(runtime.Object); ok {
return obj
}
// TODO: better return than nil?
return nil
}