github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/deploy/helm/parse.go (about) 1 /* 2 Copyright 2020 The Skaffold 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 helm 18 19 import ( 20 "bufio" 21 "bytes" 22 "context" 23 "fmt" 24 "io" 25 26 k8syaml "k8s.io/apimachinery/pkg/util/yaml" 27 "k8s.io/client-go/kubernetes/scheme" 28 29 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/types" 30 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/manifest" 31 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log" 32 ) 33 34 // parseReleaseManifests parses a set of Kubernetes manifests extracting a set of 35 // objects with their corresponding namespace. If no namespace is specified, 36 // then assume `namespace`. 37 func parseReleaseManifests(namespace string, b *bufio.Reader) []types.Artifact { 38 var results []types.Artifact 39 40 r := k8syaml.NewYAMLReader(b) 41 for i := 0; ; i++ { 42 doc, err := r.Read() 43 if err == io.EOF { 44 break 45 } 46 if err != nil { 47 log.Entry(context.TODO()).Infof("error parsing object %d from string: %s", i, err.Error()) 48 continue 49 } 50 objNamespace, err := getObjectNamespaceIfDefined(doc, namespace) 51 if err != nil { 52 log.Entry(context.TODO()).Infof("error parsing object %d from string: %s", i, err.Error()) 53 continue 54 } 55 obj, err := parseRuntimeObject(objNamespace, doc) 56 if err != nil { 57 log.Entry(context.TODO()).Infof("error parsing object %d from string: %s", i, err.Error()) 58 } else { 59 results = append(results, *obj) 60 log.Entry(context.TODO()).Debugf("found deployed object %d: %+v", i, obj.Obj) 61 } 62 } 63 64 return results 65 } 66 67 func parseRuntimeObject(namespace string, b []byte) (*types.Artifact, error) { 68 d := scheme.Codecs.UniversalDeserializer() 69 obj, _, err := d.Decode(b, nil, nil) 70 if err != nil { 71 return nil, fmt.Errorf("error decoding parsed yaml: %s", err.Error()) 72 } 73 return &types.Artifact{ 74 Obj: obj, 75 Namespace: namespace, 76 }, nil 77 } 78 79 func getObjectNamespaceIfDefined(doc []byte, ns string) (string, error) { 80 if i := bytes.Index(doc, []byte("apiVersion")); i >= 0 { 81 manifests := manifest.ManifestList{doc[i:]} 82 namespaces, err := manifests.CollectNamespaces() 83 if err != nil { 84 return ns, err 85 } 86 if len(namespaces) > 0 { 87 return namespaces[0], nil 88 } 89 } 90 return ns, nil 91 }