crossplane

Форк
0
77 строк · 1.9 Кб
1
/*
2
Copyright 2023 The Crossplane Authors.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
// Package examples contains utilities for parsing examples.
18
package examples
19

20
import (
21
	"bufio"
22
	"context"
23
	"io"
24

25
	"github.com/crossplane/crossplane-runtime/pkg/parser"
26
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27
	"k8s.io/apimachinery/pkg/util/yaml"
28
	k8syaml "sigs.k8s.io/yaml"
29
)
30

31
// Examples is the set of metadata and objects in a package.
32
type Examples struct {
33
	objects []unstructured.Unstructured
34
}
35

36
// Parser is a Parser implementation for parsing examples.
37
type Parser struct {
38
	objScheme parser.ObjectCreaterTyper
39
}
40

41
// NewExamples creates a new Examples object.
42
func NewExamples() *Examples {
43
	return &Examples{}
44
}
45

46
// New creates a new Package.
47
func New() *Parser {
48
	return &Parser{}
49
}
50

51
// Parse is the underlying logic for parsing examples.
52
func (p *Parser) Parse(ctx context.Context, reader io.ReadCloser) (*Examples, error) {
53
	ex := NewExamples()
54
	if reader == nil {
55
		return ex, nil
56
	}
57
	defer func() { _ = reader.Close() }()
58
	yr := yaml.NewYAMLReader(bufio.NewReader(reader))
59
	for {
60
		bytes, err := yr.Read()
61
		if err != nil && err != io.EOF {
62
			return ex, err
63
		}
64
		if err == io.EOF {
65
			break
66
		}
67
		if len(bytes) == 0 {
68
			continue
69
		}
70
		var obj unstructured.Unstructured
71
		if err := k8syaml.Unmarshal(bytes, &obj); err != nil {
72
			return ex, err
73
		}
74
		ex.objects = append(ex.objects, obj)
75
	}
76
	return ex, nil
77
}
78

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.