@ -20,20 +20,23 @@ import (
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						"fmt" 
 
					 
					 
					 
						"fmt" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						"os" 
 
					 
					 
					 
						"os" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						"path/filepath" 
 
					 
					 
					 
						"path/filepath" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
						"strings" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						"sync" 
 
					 
					 
					 
						"sync" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						"sigs.k8s.io/kustomize/api/filesys" 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						"sigs.k8s.io/kustomize/api/konfig" 
 
					 
					 
					 
						"sigs.k8s.io/kustomize/api/konfig" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						"sigs.k8s.io/kustomize/api/krusty" 
 
					 
					 
					 
						"sigs.k8s.io/kustomize/api/krusty" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						"sigs.k8s.io/kustomize/api/provider" 
 
					 
					 
					 
						"sigs.k8s.io/kustomize/api/provider" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						kustypes  "sigs.k8s.io/kustomize/api/types" 
 
					 
					 
					 
						kustypes  "sigs.k8s.io/kustomize/api/types" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						"sigs.k8s.io/yaml" 
 
					 
					 
					 
						"sigs.k8s.io/yaml" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
						"github.com/fluxcd/pkg/kustomize/filesys" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						"github.com/fluxcd/flux2/pkg/manifestgen" 
 
					 
					 
					 
						"github.com/fluxcd/flux2/pkg/manifestgen" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					) 
 
					 
					 
					 
					) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					// Generate scans the given directory for Kubernetes manifests and creates a kustomization.yaml
 
 
					 
					 
					 
					// Generate scans the given directory for Kubernetes manifests and creates a
 
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					// including all discovered manifests as resources.
 
 
					 
					 
					 
					// konfig.DefaultKustomizationFileName file, including all discovered manifests
 
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					// as resources.
 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					func  Generate ( options  Options )  ( * manifestgen . Manifest ,  error )  { 
 
					 
					 
					 
					func  Generate ( options  Options )  ( * manifestgen . Manifest ,  error )  { 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						kfile  :=  filepath . Join ( options . TargetPath ,  konfig . DefaultKustomizationFileName ( ) ) 
 
					 
					 
					 
						kfile  :=  filepath . Join ( options . TargetPath ,  konfig . DefaultKustomizationFileName ( ) ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						abskfile  :=  filepath . Join ( options . BaseDir ,  kfile ) 
 
					 
					 
					 
						abskfile  :=  filepath . Join ( options . BaseDir ,  kfile ) 
 
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -50,7 +53,7 @@ func Generate(options Options) (*manifestgen.Manifest, error) {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
									return  nil 
 
					 
					 
					 
									return  nil 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								} 
 
					 
					 
					 
								} 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								if  info . IsDir ( )  { 
 
					 
					 
					 
								if  info . IsDir ( )  { 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
									// If a sub-directory contains an existing Kustomization file  add the
 
					 
					 
					 
									// If a sub-directory contains an existing Kustomization file ,  add the
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
									// directory as a resource and do not decent into it.
 
 
					 
					 
					 
									// directory as a resource and do not decent into it.
 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
									for  _ ,  kfilename  :=  range  konfig . RecognizedKustomizationFileNames ( )  { 
 
					 
					 
					 
									for  _ ,  kfilename  :=  range  konfig . RecognizedKustomizationFileNames ( )  { 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
										if  options . FileSystem . Exists ( filepath . Join ( path ,  kfilename ) )  { 
 
					 
					 
					 
										if  options . FileSystem . Exists ( filepath . Join ( path ,  kfilename ) )  { 
 
				
			 
			
		
	
	
		
		
			
				
					
						
							
								 
							 
						
						
							
								 
							 
						
						
					 
					 
					@ -88,7 +91,9 @@ func Generate(options Options) (*manifestgen.Manifest, error) {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							if  err  !=  nil  { 
 
					 
					 
					 
							if  err  !=  nil  { 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								return  nil ,  err 
 
					 
					 
					 
								return  nil ,  err 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							} 
 
					 
					 
					 
							} 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							f . Close ( ) 
 
					 
					 
					 
							if  err  =  f . Close ( ) ;  err  !=  nil  { 
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								return  nil ,  err 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							} 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							kus  :=  kustypes . Kustomization { 
 
					 
					 
					 
							kus  :=  kustypes . Kustomization { 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								TypeMeta :  kustypes . TypeMeta { 
 
					 
					 
					 
								TypeMeta :  kustypes . TypeMeta { 
 
				
			 
			
		
	
	
		
		
			
				
					
						
							
								 
							 
						
						
							
								 
							 
						
						
					 
					 
					@ -128,20 +133,32 @@ func Generate(options Options) (*manifestgen.Manifest, error) {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						} ,  nil 
 
					 
					 
					 
						} ,  nil 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					} 
 
					 
					 
					 
					} 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					// kustomizeBuildMutex is a workaround for a concurrent map read and map write bug.
 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					// TODO(stefan): https://github.com/kubernetes-sigs/kustomize/issues/3659
 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					var  kustomizeBuildMutex  sync . Mutex 
 
					 
					 
					 
					var  kustomizeBuildMutex  sync . Mutex 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					// Build takes a Kustomize overlays and returns the resulting manifests as multi-doc YAML.
 
 
					 
					 
					 
					// Build takes the path to a directory with a konfig.RecognizedKustomizationFileNames,
 
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					// builds it, and returns the resulting manifests as multi-doc YAML.
 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					func  Build ( base  string )  ( [ ] byte ,  error )  { 
 
					 
					 
					 
					func  Build ( base  string )  ( [ ] byte ,  error )  { 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						// TODO(stefan): temporary workaround for concurrent map read and map write bug
 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						// https://github.com/kubernetes-sigs/kustomize/issues/3659
 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						kustomizeBuildMutex . Lock ( ) 
 
					 
					 
					 
						kustomizeBuildMutex . Lock ( ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						defer  kustomizeBuildMutex . Unlock ( ) 
 
					 
					 
					 
						defer  kustomizeBuildMutex . Unlock ( ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						kfile  :=  filepath . Join ( base ,  konfig . DefaultKustomizationFileName ( ) ) 
 
					 
					 
					 
						// TODO(hidde): make this configurable to a specific root (relative to base)
 
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
						parent  :=  filepath . Dir ( strings . TrimSuffix ( base ,  string ( filepath . Separator ) ) ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
						fs ,  err  :=  filesys . MakeFsOnDiskSecureBuild ( parent ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
						if  err  !=  nil  { 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							return  nil ,  err 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
						} 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						fs  :=  filesys . MakeFsOnDisk ( ) 
 
					 
					 
					 
						var  kfile  string 
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						if  ! fs . Exists ( kfile )  { 
 
					 
					 
					 
						for  _ ,  f  :=  range  konfig . RecognizedKustomizationFileNames ( )  { 
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							return  nil ,  fmt . Errorf ( "%s not found" ,  kfile ) 
 
					 
					 
					 
							if  kf  :=  filepath . Join ( base ,  f ) ;  fs . Exists ( kf )  { 
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								kfile  =  kf 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								break 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							} 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
						} 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
						if  kfile  ==  ""  { 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							return  nil ,  fmt . Errorf ( "%s not found" ,  konfig . DefaultKustomizationFileName ( ) ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						} 
 
					 
					 
					 
						} 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						// TODO(hidde): work around for a bug in kustomize causing it to
 
 
					 
					 
					 
						// TODO(hidde): work around for a bug in kustomize causing it to