-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
109 lines (97 loc) · 2.79 KB
/
resource.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
package santhoshsys
import (
"context"
"fmt"
"io"
"io/fs"
"strings"
"github.com/grafana/kindsys"
jsoniter "github.com/json-iterator/go"
)
var _ kindsys.ResourceKind = &resourceKindFromManifest{}
type resourceKindFromManifest struct {
kindFromManifest // the base properties
names kindsys.MachineNames
migrator kindsys.ResourceMigrator
}
// Load a jsonschema based kind from a file system
// the file system will have a manifest that exists
func NewResourceKind(sfs fs.FS, mig kindsys.ResourceMigrator) (kindsys.ResourceKind, error) {
m := &resourceKindFromManifest{
migrator: mig,
}
info, err := m.init(sfs)
if err != nil {
return m, err
}
if info.ComposableType != "" || len(info.ComposableSlots) > 0 {
return nil, fmt.Errorf("invalid info in the manifest (should not have composable types)")
}
if info.MachineNames != nil {
m.names = *info.MachineNames
}
if m.names.Singular == "" {
m.names.Singular = strings.ToLower(info.Kind)
}
if m.names.Plural == "" {
m.names.Plural = m.names.Singular + "s"
}
return m, nil
}
func (k *resourceKindFromManifest) GetMachineNames() kindsys.MachineNames {
return k.names
}
func (k *resourceKindFromManifest) Read(reader io.Reader, strict bool) (kindsys.Resource, error) {
obj := &kindsys.UnstructuredResource{}
err := kindsys.ReadResourceJSON(reader, kindsys.JSONResourceBuilder{
SetGroupVersionKind: func(group, version, kind string) error {
if group != k.info.Group {
return fmt.Errorf("invalid group")
}
if kind != k.info.Kind {
return fmt.Errorf("invalid kind")
}
return nil
},
SetMetadata: func(s kindsys.StaticMetadata, c kindsys.CommonMetadata) {
obj.StaticMeta = s
obj.CommonMeta = c
},
ReadSpec: func(iter *jsoniter.Iterator) error {
obj.Spec = make(map[string]any)
iter.ReadVal(&obj.Spec)
return iter.Error
},
SetAnnotation: func(key, val string) {
fmt.Printf("??? unknown")
},
ReadStatus: func(iter *jsoniter.Iterator) error {
obj.Status = make(map[string]any)
iter.ReadVal(&obj.Status)
return iter.Error
},
ReadSub: func(name string, iter *jsoniter.Iterator) error {
return fmt.Errorf("unsupported sub resource")
},
})
if err != nil {
return obj, err
}
if strict {
schema, ok := k.parsed[obj.StaticMeta.Version]
if !ok || schema == nil {
return obj, fmt.Errorf("unknown version")
}
// TODO!!! schema is right now just on the spec!!!
doc := obj.SpecObject()
// TODO: need to make sure the doc+resource are ones that we can parse ()
err = schema.ValidateInterface(doc)
}
return obj, err
}
func (k *resourceKindFromManifest) Migrate(ctx context.Context, obj kindsys.Resource, targetVersion string) (kindsys.Resource, error) {
if k.migrator == nil {
return nil, fmt.Errorf("no migrator registered")
}
return k.migrator(ctx, obj, targetVersion)
}