1 /* 2 Copyright 2019 The Kubernetes 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 resource 18 19 import ( 20 "bytes" 21 22 "sigs.k8s.io/kustomize/api/krusty" 23 "sigs.k8s.io/kustomize/kyaml/filesys" 24 ) 25 26 // KustomizeVisitor handles kustomization.yaml files. 27 type KustomizeVisitor struct { 28 mapper *mapper 29 schema ContentValidator 30 // Directory expected to contain a kustomization file. 31 dirPath string 32 // File system containing dirPath. 33 fSys filesys.FileSystem 34 // Holds result of kustomize build, retained for tests. 35 yml []byte 36 } 37 38 // Visit passes the result of a kustomize build to a StreamVisitor. 39 func (v *KustomizeVisitor) Visit(fn VisitorFunc) error { 40 kOpts := krusty.MakeDefaultOptions() 41 kOpts.Reorder = krusty.ReorderOptionLegacy 42 k := krusty.MakeKustomizer(kOpts) 43 m, err := k.Run(v.fSys, v.dirPath) 44 if err != nil { 45 return err 46 } 47 v.yml, err = m.AsYaml() 48 if err != nil { 49 return err 50 } 51 sv := NewStreamVisitor( 52 bytes.NewReader(v.yml), v.mapper, v.dirPath, v.schema) 53 return sv.Visit(fn) 54 } 55