This repository has been archived by the owner on Oct 13, 2023. It is now read-only.
/
load.go
141 lines (122 loc) · 4.28 KB
/
load.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
package main
import (
"bufio"
"io"
"os"
"path/filepath"
"k8s.io/apimachinery/pkg/util/yaml"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/composed"
"github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/composite"
apiextensionsv1 "github.com/crossplane/crossplane/apis/apiextensions/v1"
pkgv1beta1 "github.com/crossplane/crossplane/apis/pkg/v1beta1"
)
// LoadCompositeResource from a YAML manifest.
func LoadCompositeResource(file string) (*composite.Unstructured, error) {
y, err := os.ReadFile(file) //nolint:gosec // Taking this input is intentional.
if err != nil {
return nil, errors.Wrap(err, "cannot read composite resource file")
}
xr := composite.New()
return xr, errors.Wrap(yaml.Unmarshal(y, xr), "cannot unmarshal composite resource YAML")
}
// TODO(negz): What if we load a YAML stream of Compositions? We could then
// render out nested XRs too. What would that look like in our output? How would
// we match XRs to Compositions (e.g. selectors, refs etc)
// LoadComposition form a YAML manifest.
func LoadComposition(file string) (*apiextensionsv1.Composition, error) {
y, err := os.ReadFile(file) //nolint:gosec // Taking this as input is intentional.
if err != nil {
return nil, errors.Wrap(err, "cannot read composite resource file")
}
comp := &apiextensionsv1.Composition{}
return comp, errors.Wrap(yaml.Unmarshal(y, comp), "cannot unmarshal composite resource YAML")
}
// TODO(negz): Support optionally loading functions and observed resources from
// a directory of manifests instead of a single stream.
// LoadFunctions from a stream of YAML manifests.
func LoadFunctions(file string) ([]pkgv1beta1.Function, error) {
stream, err := LoadYAMLStream(file)
if err != nil {
return nil, errors.Wrap(err, "cannot load YAML stream from file")
}
functions := make([]pkgv1beta1.Function, 0, len(stream))
for _, y := range stream {
f := &pkgv1beta1.Function{}
if err := yaml.Unmarshal(y, f); err != nil {
return nil, errors.Wrap(err, "cannot parse YAML Function manifest")
}
functions = append(functions, *f)
}
return functions, nil
}
// LoadObservedResources from a stream of YAML manifests.
func LoadObservedResources(file string) ([]composed.Unstructured, error) {
stream, err := LoadYAMLStream(file)
if err != nil {
return nil, errors.Wrap(err, "cannot load YAML stream from file")
}
observed := make([]composed.Unstructured, 0, len(stream))
for _, y := range stream {
cd := composed.New()
if err := yaml.Unmarshal(y, cd); err != nil {
return nil, errors.Wrap(err, "cannot parse YAML composed resource manifest")
}
observed = append(observed, *cd)
}
return observed, nil
}
// LoadYAMLStream from the supplied file or directory. Returns an array of byte
// arrays, where each byte array is expected to be a YAML manifest.
func LoadYAMLStream(fileOrDir string) ([][]byte, error) {
var files []string
info, err := os.Stat(fileOrDir)
if err != nil {
return nil, errors.Wrap(err, "cannot stat file")
}
if !info.IsDir() {
files = append(files, fileOrDir)
} else {
files, err = filepath.Glob(filepath.Join(fileOrDir, "*.{yaml,yml}"))
if err != nil {
return nil, errors.Wrap(err, "cannot glob YAML files")
}
if len(files) == 0 {
return nil, errors.Errorf("no YAML files found in %q (.yaml or .yml)", fileOrDir)
}
}
out := make([][]byte, 0)
for i := range files {
o, err := LoadYAMLStreamFromFile(files[i])
if err != nil {
return nil, errors.Wrap(err, "cannot load YAML stream from file")
}
out = append(out, o...)
}
return out, nil
}
// LoadYAMLStreamFromFile from the supplied file. Returns an array of byte
// arrays, where each byte array is expected to be a YAML manifest.
func LoadYAMLStreamFromFile(file string) ([][]byte, error) {
out := make([][]byte, 0)
f, err := os.Open(file) //nolint:gosec // Taking this input is intentional.
if err != nil {
return nil, errors.Wrap(err, "cannot open file")
}
defer f.Close() //nolint:errcheck // Only open for reading.
yr := yaml.NewYAMLReader(bufio.NewReader(f))
for {
bytes, err := yr.Read()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, errors.Wrap(err, "cannot parse YAML stream")
}
if len(bytes) == 0 {
continue
}
out = append(out, bytes)
}
return out, nil
}