forked from kptdev/kpt
/
rgstream.go
104 lines (91 loc) · 3.03 KB
/
rgstream.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
// Copyright 2020 The kpt 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 live
import (
"bytes"
"io"
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
rgfilev1alpha1 "github.com/GoogleContainerTools/kpt/pkg/api/resourcegroup/v1alpha1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/resid"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
var (
excludedGKs = []schema.GroupKind{
kptfilev1.KptFileGVK().GroupKind(),
rgfilev1alpha1.ResourceGroupGVK().GroupKind(),
}
)
// ResourceGroupStreamManifestReader encapsulates the default stream
// manifest reader.
type ResourceGroupStreamManifestReader struct {
ReaderName string
Reader io.Reader
manifestreader.ReaderOptions
}
// Read reads the manifests and returns them as Info objects.
// Transforms the Kptfile into the ResourceGroup inventory object,
// and appends it to the rest of the standard StreamManifestReader
// generated objects. Returns an error if one occurs. If the
// ResourceGroup inventory object does not exist, it is NOT an error.
func (p *ResourceGroupStreamManifestReader) Read() ([]*unstructured.Unstructured, error) {
var objs []*unstructured.Unstructured
nodes, err := (&kio.ByteReader{
Reader: p.Reader,
WrapBareSeqNode: true,
}).Read()
if err != nil {
return objs, err
}
for _, n := range nodes {
if isExcluded(n) {
continue
}
err = removeAnnotations(n, kioutil.IndexAnnotation, kioutil.LegacyIndexAnnotation) // nolint:staticcheck
if err != nil {
return objs, err
}
u, err := kyamlNodeToUnstructured(n)
if err != nil {
return objs, err
}
objs = append(objs, u)
}
err = manifestreader.SetNamespaces(p.Mapper, objs, p.Namespace, p.EnforceNamespace)
return objs, err
}
func isExcluded(n *yaml.RNode) bool {
kind := n.GetKind()
group, _ := resid.ParseGroupVersion(n.GetApiVersion())
for _, gk := range excludedGKs {
if kind == gk.Kind && group == gk.Group {
return true
}
}
return false
}
var kptFileTemplate = kptfilev1.KptFile{ResourceMeta: kptfilev1.TypeMeta}
// isKptfile returns true if the passed resource config is a Kptfile; false otherwise
func isKptfile(resource []byte) bool {
d := yaml.NewDecoder(bytes.NewReader(resource))
d.KnownFields(true)
if err := d.Decode(&kptFileTemplate); err == nil {
return kptFileTemplate.ResourceMeta.TypeMeta == kptfilev1.TypeMeta.TypeMeta
}
return false
}