-
Notifications
You must be signed in to change notification settings - Fork 117
/
kubernetes.go
47 lines (43 loc) · 1.13 KB
/
kubernetes.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
package option
import (
"bytes"
"fmt"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/resource"
)
// ReadManifests reads Kubernetes manifests from local files or remote files
// via HTTP/S.
//
// WARNING: This function should not be used with untrusted input!
func ReadManifests(recursive bool, filenames ...string) ([]byte, error) {
buildRes, err := resource.NewBuilder(&genericclioptions.ConfigFlags{}).
Local().
Unstructured().
FilenameParam(false, &resource.FilenameOptions{
Filenames: filenames,
Recursive: recursive,
}).
Flatten().
Do().
Infos()
if err != nil {
return nil, fmt.Errorf("build resources: %w", err)
}
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
defer func() {
_ = enc.Close()
}()
for _, info := range buildRes {
u, ok := info.Object.(*unstructured.Unstructured)
if !ok {
return nil, fmt.Errorf("expected *unstructured.Unstructured, got %T", info.Object)
}
if err := enc.Encode(&u.Object); err != nil {
return nil, fmt.Errorf("encode object: %w", err)
}
}
return buf.Bytes(), nil
}