github.com/GoogleContainerTools/kpt@v1.0.0-beta.50.0.20240520170205-c25345ffcbee/pkg/live/rgstream.go (about) 1 // Copyright 2020 The kpt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package live 16 17 import ( 18 "bytes" 19 "io" 20 21 kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1" 22 rgfilev1alpha1 "github.com/GoogleContainerTools/kpt/pkg/api/resourcegroup/v1alpha1" 23 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 24 "k8s.io/apimachinery/pkg/runtime/schema" 25 "sigs.k8s.io/cli-utils/pkg/manifestreader" 26 "sigs.k8s.io/kustomize/kyaml/kio" 27 "sigs.k8s.io/kustomize/kyaml/kio/kioutil" 28 "sigs.k8s.io/kustomize/kyaml/resid" 29 "sigs.k8s.io/kustomize/kyaml/yaml" 30 ) 31 32 var ( 33 excludedGKs = []schema.GroupKind{ 34 kptfilev1.KptFileGVK().GroupKind(), 35 rgfilev1alpha1.ResourceGroupGVK().GroupKind(), 36 } 37 ) 38 39 // ResourceGroupStreamManifestReader encapsulates the default stream 40 // manifest reader. 41 type ResourceGroupStreamManifestReader struct { 42 ReaderName string 43 Reader io.Reader 44 45 manifestreader.ReaderOptions 46 } 47 48 // Read reads the manifests and returns them as Info objects. 49 // Transforms the Kptfile into the ResourceGroup inventory object, 50 // and appends it to the rest of the standard StreamManifestReader 51 // generated objects. Returns an error if one occurs. If the 52 // ResourceGroup inventory object does not exist, it is NOT an error. 53 func (p *ResourceGroupStreamManifestReader) Read() ([]*unstructured.Unstructured, error) { 54 var objs []*unstructured.Unstructured 55 nodes, err := (&kio.ByteReader{ 56 Reader: p.Reader, 57 WrapBareSeqNode: true, 58 }).Read() 59 if err != nil { 60 return objs, err 61 } 62 63 for _, n := range nodes { 64 if isExcluded(n) { 65 continue 66 } 67 68 err = removeAnnotations(n, kioutil.IndexAnnotation, kioutil.LegacyIndexAnnotation) // nolint:staticcheck 69 if err != nil { 70 return objs, err 71 } 72 u, err := kyamlNodeToUnstructured(n) 73 if err != nil { 74 return objs, err 75 } 76 objs = append(objs, u) 77 } 78 79 err = manifestreader.SetNamespaces(p.Mapper, objs, p.Namespace, p.EnforceNamespace) 80 return objs, err 81 } 82 83 func isExcluded(n *yaml.RNode) bool { 84 kind := n.GetKind() 85 group, _ := resid.ParseGroupVersion(n.GetApiVersion()) 86 for _, gk := range excludedGKs { 87 if kind == gk.Kind && group == gk.Group { 88 return true 89 } 90 } 91 return false 92 } 93 94 var kptFileTemplate = kptfilev1.KptFile{ResourceMeta: kptfilev1.TypeMeta} 95 96 // isKptfile returns true if the passed resource config is a Kptfile; false otherwise 97 func isKptfile(resource []byte) bool { 98 d := yaml.NewDecoder(bytes.NewReader(resource)) 99 d.KnownFields(true) 100 if err := d.Decode(&kptFileTemplate); err == nil { 101 return kptFileTemplate.ResourceMeta.TypeMeta == kptfilev1.TypeMeta.TypeMeta 102 } 103 return false 104 }