-
Notifications
You must be signed in to change notification settings - Fork 95
/
parser.go
315 lines (276 loc) · 8.02 KB
/
parser.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
Copyright 2020 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package parser implements a parser for Crossplane packages.
package parser
import (
"bufio"
"context"
"io"
"strings"
"unicode"
"github.com/spf13/afero"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/kubernetes"
"github.com/crossplane/crossplane-runtime/pkg/errors"
)
// Lintable defines the common API for lintable packages.
type Lintable interface {
// GetMeta returns metadata objects of the lintable package, such as
// Provider, Configuration or Function.
GetMeta() []runtime.Object
// GetObjects returns objects of the lintable package.
GetObjects() []runtime.Object
}
// AnnotatedReadCloser is a wrapper around io.ReadCloser that allows
// implementations to supply additional information about data that is read.
type AnnotatedReadCloser interface {
io.ReadCloser
Annotate() any
}
// ObjectCreaterTyper know how to create and determine the type of objects.
type ObjectCreaterTyper interface {
runtime.ObjectCreater
runtime.ObjectTyper
}
// Package is the set of metadata and objects in a package.
type Package struct {
meta []runtime.Object
objects []runtime.Object
}
// NewPackage creates a new Package.
func NewPackage() *Package {
return &Package{}
}
// GetMeta gets metadata from the package.
func (p *Package) GetMeta() []runtime.Object {
return p.meta
}
// GetObjects gets objects from the package.
func (p *Package) GetObjects() []runtime.Object {
return p.objects
}
// Parser is a package parser.
type Parser interface {
Parse(ctx context.Context, rc io.ReadCloser) (*Package, error)
}
// PackageParser is a Parser implementation for parsing packages.
type PackageParser struct {
metaScheme ObjectCreaterTyper
objScheme ObjectCreaterTyper
}
// New returns a new PackageParser.
func New(meta, obj ObjectCreaterTyper) *PackageParser {
return &PackageParser{
metaScheme: meta,
objScheme: obj,
}
}
// Parse is the underlying logic for parsing packages. It first attempts to
// decode objects recognized by the meta scheme, then attempts to decode objects
// recognized by the object scheme. Objects not recognized by either scheme
// return an error rather than being skipped.
func (p *PackageParser) Parse(_ context.Context, reader io.ReadCloser) (*Package, error) {
pkg := NewPackage()
if reader == nil {
return pkg, nil
}
defer func() { _ = reader.Close() }()
yr := yaml.NewYAMLReader(bufio.NewReader(reader))
dm := json.NewSerializerWithOptions(json.DefaultMetaFactory, p.metaScheme, p.metaScheme, json.SerializerOptions{Yaml: true})
do := json.NewSerializerWithOptions(json.DefaultMetaFactory, p.objScheme, p.objScheme, json.SerializerOptions{Yaml: true})
for {
content, err := yr.Read()
if err != nil && !errors.Is(err, io.EOF) {
return pkg, err
}
if errors.Is(err, io.EOF) {
break
}
if isEmptyYAML(content) {
continue
}
m, _, err := dm.Decode(content, nil, nil)
if err != nil {
// NOTE(hasheddan): we only try to decode with object scheme if the
// error is due the object not being registered in the meta scheme.
if !runtime.IsNotRegisteredError(err) {
return pkg, annotateErr(err, reader)
}
o, _, err := do.Decode(content, nil, nil)
if err != nil {
return pkg, annotateErr(err, reader)
}
pkg.objects = append(pkg.objects, o)
continue
}
pkg.meta = append(pkg.meta, m)
}
return pkg, nil
}
// isEmptyYAML checks whether the provided YAML can be considered empty. This
// is useful for filtering out empty YAML documents that would otherwise
// cause issues when decoded.
func isEmptyYAML(y []byte) bool {
for _, line := range strings.Split(string(y), "\n") {
trimmed := strings.TrimLeftFunc(line, unicode.IsSpace)
// We don't want to return an empty document with only separators that
// have nothing in-between.
if trimmed != "" && trimmed != "---" && trimmed != "..." && !strings.HasPrefix(trimmed, "#") {
return false
}
}
return true
}
// annotateErr annotates an error if the reader is an AnnotatedReadCloser.
func annotateErr(err error, reader io.ReadCloser) error {
if anno, ok := reader.(AnnotatedReadCloser); ok {
return errors.Wrapf(err, "%+v", anno.Annotate())
}
return err
}
// BackendOption modifies the parser backend. Backends may accept options at
// creation time, but must accept them at initialization.
type BackendOption func(Backend)
// Backend provides a source for a parser.
type Backend interface {
Init(ctx context.Context, o ...BackendOption) (io.ReadCloser, error)
}
// PodLogBackend is a parser backend that uses Kubernetes pod logs as source.
type PodLogBackend struct {
client kubernetes.Interface
name string
namespace string
}
// NewPodLogBackend returns a new PodLogBackend.
func NewPodLogBackend(bo ...BackendOption) *PodLogBackend {
p := &PodLogBackend{}
for _, o := range bo {
o(p)
}
return p
}
// Init initializes a PodLogBackend.
func (p *PodLogBackend) Init(ctx context.Context, bo ...BackendOption) (io.ReadCloser, error) {
for _, o := range bo {
o(p)
}
logs := p.client.CoreV1().Pods(p.namespace).GetLogs(p.name, &corev1.PodLogOptions{})
reader, err := logs.Stream(ctx)
if err != nil {
return nil, err
}
return reader, nil
}
// PodName sets the pod name of a PodLogBackend.
func PodName(name string) BackendOption {
return func(p Backend) {
pl, ok := p.(*PodLogBackend)
if !ok {
return
}
pl.name = name
}
}
// PodNamespace sets the pod namespace of a PodLogBackend.
func PodNamespace(namespace string) BackendOption {
return func(p Backend) {
pl, ok := p.(*PodLogBackend)
if !ok {
return
}
pl.namespace = namespace
}
}
// PodClient sets the pod client of a PodLogBackend.
func PodClient(client kubernetes.Interface) BackendOption {
return func(p Backend) {
pl, ok := p.(*PodLogBackend)
if !ok {
return
}
pl.client = client
}
}
// NopBackend is a parser backend with empty source.
type NopBackend struct{}
// NewNopBackend returns a new NopBackend.
func NewNopBackend(...BackendOption) *NopBackend {
return &NopBackend{}
}
// Init initializes a NopBackend.
func (p *NopBackend) Init(_ context.Context, _ ...BackendOption) (io.ReadCloser, error) {
return nil, nil
}
// FsBackend is a parser backend that uses a filestystem as source.
type FsBackend struct {
fs afero.Fs
dir string
skips []FilterFn
}
// NewFsBackend returns an FsBackend.
func NewFsBackend(fs afero.Fs, bo ...BackendOption) *FsBackend {
f := &FsBackend{
fs: fs,
}
for _, o := range bo {
o(f)
}
return f
}
// Init initializes an FsBackend.
func (p *FsBackend) Init(_ context.Context, bo ...BackendOption) (io.ReadCloser, error) {
for _, o := range bo {
o(p)
}
return NewFsReadCloser(p.fs, p.dir, p.skips...)
}
// FsDir sets the directory of an FsBackend.
func FsDir(dir string) BackendOption {
return func(p Backend) {
f, ok := p.(*FsBackend)
if !ok {
return
}
f.dir = dir
}
}
// FsFilters adds FilterFns to an FsBackend.
func FsFilters(skips ...FilterFn) BackendOption {
return func(p Backend) {
f, ok := p.(*FsBackend)
if !ok {
return
}
f.skips = skips
}
}
// EchoBackend is a parser backend that uses string input as source.
type EchoBackend struct {
echo string
}
// NewEchoBackend returns a new EchoBackend.
func NewEchoBackend(echo string) Backend {
return &EchoBackend{
echo: echo,
}
}
// Init initializes an EchoBackend.
func (p *EchoBackend) Init(_ context.Context, bo ...BackendOption) (io.ReadCloser, error) {
for _, o := range bo {
o(p)
}
return io.NopCloser(strings.NewReader(p.echo)), nil
}