/
unstructuredutils.go
114 lines (102 loc) · 3.38 KB
/
unstructuredutils.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
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
// Package unstructuredutils provides utilities working with the unstructured.Unstructured type.
package unstructuredutils
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// ReadFile reads unstructured objects from a file with the given name.
// For further reference, have a look at Read.
func ReadFile(filename string) ([]unstructured.Unstructured, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer func() {
utilruntime.HandleError(f.Close())
}()
return Read(f)
}
// ReadFiles reads unstructured objects from a folder with the given name (including sub folders)
// and file name matched with the pattern.
func ReadFiles(pattern string) ([]unstructured.Unstructured, error) {
var objs []unstructured.Unstructured
files, err := filepath.Glob(pattern)
if err != nil {
return nil, err
}
for _, file := range files {
uobjs, err := ReadFile(file)
if err != nil {
return nil, err
}
objs = append(objs, uobjs...)
}
return objs, nil
}
// Read treats io.Reader as an incoming YAML or JSON stream and reads all unstructured.Unstructured objects of it.
//
// The document has to be well-formed. For multi-doc YAMLs, '---' is used as separator.
// Empty sub-documents are filtered from the resulting list.
func Read(r io.Reader) ([]unstructured.Unstructured, error) {
d := yaml.NewYAMLOrJSONDecoder(bufio.NewReader(r), 4096)
var objs []unstructured.Unstructured
for {
ext := runtime.RawExtension{}
if err := d.Decode(&ext); err != nil {
if !errors.Is(io.EOF, err) {
return nil, fmt.Errorf("error parsing: %w", err)
}
return objs, nil
}
ext.Raw = bytes.TrimSpace(ext.Raw)
if len(ext.Raw) == 0 || bytes.Equal(ext.Raw, []byte("null")) {
continue
}
obj := &unstructured.Unstructured{}
if _, _, err := scheme.Codecs.UniversalDeserializer().Decode(ext.Raw, nil, obj); err != nil {
return nil, fmt.Errorf("invalid object: %w", err)
}
objs = append(objs, *obj)
}
}
// UnstructuredSliceToObjectSliceNoCopy transforms the given list of unstructured.Unstructured to a list of
// client.Object, performing no copy while doing so.
//
// When creating the list, the resulting client.Object objects are obtained from having a pointer to the original
// slice item.
func UnstructuredSliceToObjectSliceNoCopy(unstructureds []unstructured.Unstructured) []client.Object {
if unstructureds == nil {
return nil
}
res := make([]client.Object, 0, len(unstructureds))
for i := range unstructureds {
res = append(res, &unstructureds[i])
}
return res
}
// UnstructuredSliceToObjectSlice transforms the given list of unstructured.Unstructured to a list of
// client.Object, copying the unstructured.Unstructured and using the pointers of them for the resulting client.Object.
func UnstructuredSliceToObjectSlice(unstructureds []unstructured.Unstructured) []client.Object {
if unstructureds == nil {
return nil
}
res := make([]client.Object, 0, len(unstructureds))
for _, u := range unstructureds {
u := u
res = append(res, &u)
}
return res
}