-
Notifications
You must be signed in to change notification settings - Fork 209
/
readresource.go
166 lines (149 loc) · 5.02 KB
/
readresource.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
package helpers
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
kyaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/klog/v2"
"sigs.k8s.io/yaml"
)
// InstallFunc is the "normal" function for installing scheme
type InstallFunc func(scheme *runtime.Scheme) error
// ReadYAMLToInternal reads content of a reader and returns the runtime.Object that matches it. It chooses the match from
// the scheme installation that you provide. It converts to internal for you.
func ReadYAMLToInternal(reader io.Reader, schemeFns ...InstallFunc) (runtime.Object, error) {
if reader == nil || reflect.ValueOf(reader).IsNil() {
return nil, nil
}
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
jsonData, err := kyaml.ToJSON(data)
if err != nil {
// maybe we were already json
jsonData = data
}
scheme := runtime.NewScheme()
for _, schemeFn := range schemeFns {
err := schemeFn(scheme)
if err != nil {
return nil, err
}
}
codec := serializer.NewCodecFactory(scheme).LegacyCodec(scheme.PrioritizedVersionsAllGroups()...)
obj, err := runtime.Decode(codec, jsonData)
if err != nil {
return nil, captureSurroundingJSONForError("error reading config: ", jsonData, err)
}
// make sure there are no extra fields in jsonData
if err := strictDecodeCheck(jsonData, obj, scheme); err != nil {
return nil, err
}
return obj, nil
}
// ReadYAML reads content of a reader and returns the runtime.Object that matches it. It chooses the match from
// the scheme installation that you provide. It does not convert and it does not default.
func ReadYAML(reader io.Reader, schemeFns ...InstallFunc) (runtime.Object, error) {
if reader == nil || reflect.ValueOf(reader).IsNil() {
return nil, nil
}
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
jsonData, err := kyaml.ToJSON(data)
if err != nil {
// maybe we were already json
jsonData = data
}
scheme := runtime.NewScheme()
for _, schemeFn := range schemeFns {
err := schemeFn(scheme)
if err != nil {
return nil, err
}
}
codec := serializer.NewCodecFactory(scheme).UniversalDeserializer()
obj, err := runtime.Decode(codec, jsonData)
if err != nil {
return nil, captureSurroundingJSONForError("error reading config: ", jsonData, err)
}
// make sure there are no extra fields in jsonData
if err := strictDecodeCheck(jsonData, obj, scheme); err != nil {
return nil, err
}
return obj, nil
}
// TODO: we ultimately want a better decoder for JSON that allows us exact line numbers and better
// surrounding text description. This should be removed / replaced when that happens.
func captureSurroundingJSONForError(prefix string, data []byte, err error) error {
if syntaxErr, ok := err.(*json.SyntaxError); err != nil && ok {
offset := syntaxErr.Offset
begin := offset - 20
if begin < 0 {
begin = 0
}
end := offset + 20
if end > int64(len(data)) {
end = int64(len(data))
}
return fmt.Errorf("%s%v (found near '%s')", prefix, err, string(data[begin:end]))
}
if err != nil {
return fmt.Errorf("%s%v", prefix, err)
}
return err
}
// strictDecodeCheck fails decodes when jsonData contains fields not included in the external version of obj
func strictDecodeCheck(jsonData []byte, obj runtime.Object, scheme *runtime.Scheme) error {
out, err := getExternalZeroValue(obj, scheme) // we need the external version of obj as that has the correct JSON struct tags
if err != nil {
klog.Errorf("Encountered config error %v in object %T, raw JSON:\n%s", err, obj, string(jsonData)) // TODO just return the error and die
// never error for now, we need to determine a safe way to make this check fatal
return nil
}
d := json.NewDecoder(bytes.NewReader(jsonData))
d.DisallowUnknownFields()
// note that we only care about the error, out is discarded
if err := d.Decode(out); err != nil {
klog.Errorf("Encountered config error %v in object %T, raw JSON:\n%s", err, obj, string(jsonData)) // TODO just return the error and die
}
// never error for now, we need to determine a safe way to make this check fatal
return nil
}
// getExternalZeroValue returns the zero value of the external version of obj
func getExternalZeroValue(obj runtime.Object, scheme *runtime.Scheme) (runtime.Object, error) {
gvks, _, err := scheme.ObjectKinds(obj)
if err != nil {
return nil, err
}
if len(gvks) == 0 { // should never happen
return nil, fmt.Errorf("no gvks found for %#v", obj)
}
return scheme.New(gvks[0])
}
// WriteYAML serializes a yaml file based on the scheme functions provided
func WriteYAML(obj runtime.Object, schemeFns ...InstallFunc) ([]byte, error) {
scheme := runtime.NewScheme()
for _, schemeFn := range schemeFns {
err := schemeFn(scheme)
if err != nil {
return nil, err
}
}
codec := serializer.NewCodecFactory(scheme).LegacyCodec(scheme.PrioritizedVersionsAllGroups()...)
json, err := runtime.Encode(codec, obj)
if err != nil {
return nil, err
}
content, err := yaml.JSONToYAML(json)
if err != nil {
return nil, err
}
return content, err
}