This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreader.go
153 lines (117 loc) · 3.55 KB
/
reader.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
package manifest
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
)
type Reader struct {
fieldManager string
client dynamic.Interface
mapper meta.RESTMapper
}
func NewReader(fieldManager string, config *rest.Config) (*Reader, error) {
httpClient, err := rest.HTTPClientFor(config)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP client: %w", err)
}
return NewReaderForConfigAndClient(fieldManager, config, httpClient)
}
func NewReaderForConfigAndClient(fieldManager string, config *rest.Config, httpClient *http.Client) (*Reader, error) {
m, err := newDynamicRESTMapper(config, httpClient)
if err != nil {
return nil, fmt.Errorf("failed to initialize the manifest reader: %w", err)
}
c, err := dynamic.NewForConfigAndClient(config, httpClient)
if err != nil {
return nil, fmt.Errorf("failed to initialize the manifest reader: %w", err)
}
return &Reader{fieldManager: fieldManager, client: c, mapper: m}, nil
}
func (r *Reader) FromUnstructured(resources []*unstructured.Unstructured) (List, error) {
return &list{resources: resources, fieldManager: r.fieldManager, client: r.client, mapper: r.mapper}, nil
}
func (r *Reader) FromBytes(data []byte) (List, error) {
reader := bytes.NewReader(data)
decoder := yaml.NewYAMLToJSONDecoder(reader)
var resources []*unstructured.Unstructured
var err error
for {
out := &unstructured.Unstructured{}
err = decoder.Decode(out)
if errors.Is(err, io.EOF) {
break
}
if err != nil || len(out.Object) == 0 {
continue
}
resources = append(resources, out)
}
if !errors.Is(err, io.EOF) {
return &list{}, fmt.Errorf("unable to parse manifest from bytes: %w", err)
}
return r.FromUnstructured(resources)
}
func (r *Reader) FromURL(url string) (List, error) {
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to read manifests from URL %q: %w", url, err)
}
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read manifests from URL %q: %w", url, err)
}
return r.FromBytes(body)
}
func (r *Reader) FromPath(pathname string, recursive bool) (List, error) {
info, err := os.Stat(pathname)
if err != nil {
return nil, fmt.Errorf("failed to read manifests from path %q: %w", pathname, err)
}
if info.IsDir() {
return r.readDir(pathname, recursive)
}
return r.readFile(pathname)
}
func (r *Reader) readFile(pathname string) (List, error) {
file, err := os.ReadFile(pathname)
if err != nil {
return nil, fmt.Errorf("failed to read manifests from file %q: %w", pathname, err)
}
return r.FromBytes(file)
}
func (r *Reader) readDir(pathname string, recursive bool) (List, error) {
contents, err := os.ReadDir(pathname)
if err != nil {
return nil, fmt.Errorf("failed to read manifests from dir %q: %w", pathname, err)
}
resources := make([]*unstructured.Unstructured, 0)
for _, f := range contents {
name := path.Join(pathname, f.Name())
info, err := os.Stat(name)
if err != nil {
return nil, fmt.Errorf("failed to read manifests from dir %q: %w", pathname, err)
}
var els List
switch {
case info.IsDir() && recursive:
els, err = r.readDir(name, recursive)
case !info.IsDir():
els, err = r.readFile(name)
}
if err != nil {
return nil, err
}
resources = append(resources, els.Resources()...)
}
return r.FromUnstructured(resources)
}