-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8sparser.go
57 lines (50 loc) · 1.28 KB
/
k8sparser.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
// Package k8sparser is used to parse a k8s json/yaml and allow you to quickly convert them to the proper type
package k8sparser
import (
"bufio"
"encoding/json"
"io"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
k8sYaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/kubernetes/scheme"
)
// Parse return the parsed k8s items
func Parse(reader io.Reader) (items Items, err error) {
var (
doc []byte
kindDecoder runtime.Decoder
docReader *k8sYaml.YAMLReader
obj runtime.Object
)
kindDecoder = scheme.Codecs.UniversalDeserializer()
docReader = k8sYaml.NewYAMLReader(bufio.NewReader(reader))
for {
doc, err = docReader.Read()
if err == io.EOF {
break
}
if err != nil {
err = errors.Wrap(err, "error reading documents")
return nil, err
}
if obj, _, err = kindDecoder.Decode(doc, nil, nil); err != nil {
err = errors.Wrap(err, "error decoding documents")
return nil, err
}
doc, err = json.Marshal(obj)
if err != nil {
err = errors.Wrap(err, "error decoding document")
return nil, err
}
var item *Item
err = json.Unmarshal(doc, &item)
if err != nil {
err = errors.Wrap(err, "error reading doc metadata")
return nil, err
}
item.Raw = obj
items = append(items, item)
}
return items, nil
}