@@ -45,6 +45,7 @@ import (
4545
4646 kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
4747 "github.com/fluxcd/pkg/kustomize"
48+ buildfs "github.com/fluxcd/pkg/kustomize/filesys"
4849 runclient "github.com/fluxcd/pkg/runtime/client"
4950 ssautil "github.com/fluxcd/pkg/ssa/utils"
5051 "sigs.k8s.io/kustomize/kyaml/filesys"
@@ -65,51 +66,64 @@ const (
6566
6667var defaultTimeout = 80 * time .Second
6768
68- // buildBackend controls how the kustomization manifest is generated
69+ // fsBackend controls how the kustomization manifest is generated
6970// and which filesystem is used for the kustomize build.
70- type buildBackend interface {
71+ type fsBackend interface {
7172 Generate (gen * kustomize.Generator , dirPath string ) (filesys.FileSystem , string , kustomize.Action , error )
7273 Cleanup (dirPath string , action kustomize.Action ) error
7374}
7475
75- // onDiskBackend writes to the source directory, matching upstream behaviour .
76- type onDiskBackend struct {}
76+ // onDiskFsBackend writes to the source directory.
77+ type onDiskFsBackend struct {}
7778
78- func (onDiskBackend ) Generate (gen * kustomize.Generator , dirPath string ) (filesys.FileSystem , string , kustomize.Action , error ) {
79+ func (onDiskFsBackend ) Generate (gen * kustomize.Generator , dirPath string ) (filesys.FileSystem , string , kustomize.Action , error ) {
7980 action , err := gen .WriteFile (dirPath , kustomize .WithSaveOriginalKustomization ())
8081 if err != nil {
8182 return nil , "" , action , err
8283 }
8384 return filesys .MakeFsOnDisk (), dirPath , action , nil
8485}
8586
86- func (onDiskBackend ) Cleanup (dirPath string , action kustomize.Action ) error {
87+ func (onDiskFsBackend ) Cleanup (dirPath string , action kustomize.Action ) error {
8788 return kustomize .CleanDirectory (dirPath , action )
8889}
8990
90- const memFSRoot = "/work"
91+ // inMemoryFsBackend builds in an in-memory filesystem without modifying the source directory.
92+ type inMemoryFsBackend struct {}
9193
92- // inMemoryBackend builds in an in-memory filesystem without modifying the source directory.
93- type inMemoryBackend struct {}
94-
95- func (inMemoryBackend ) Generate (gen * kustomize.Generator , dirPath string ) (filesys.FileSystem , string , kustomize.Action , error ) {
94+ func (inMemoryFsBackend ) Generate (gen * kustomize.Generator , dirPath string ) (filesys.FileSystem , string , kustomize.Action , error ) {
9695 manifest , kfilePath , action , err := gen .GenerateManifest (dirPath )
9796 if err != nil {
9897 return nil , "" , action , err
9998 }
10099
101- memFS := filesys .MakeFsInMemory ()
102- if err := loadDirToMemFS (dirPath , memFSRoot , memFS ); err != nil {
103- return nil , "" , action , fmt .Errorf ("failed to load source dir: %w" , err )
100+ absDirPath , err := filepath .Abs (dirPath )
101+ if err != nil {
102+ return nil , "" , action , fmt .Errorf ("failed to resolve dirPath: %w" , err )
103+ }
104+ absDirPath , err = filepath .EvalSymlinks (absDirPath )
105+ if err != nil {
106+ return nil , "" , action , fmt .Errorf ("failed to eval symlinks: %w" , err )
107+ }
108+
109+ cwd , err := os .Getwd ()
110+ if err != nil {
111+ return nil , "" , action , fmt .Errorf ("failed to get working directory: %w" , err )
112+ }
113+
114+ diskFS , err := buildfs .MakeFsOnDiskSecure (cwd )
115+ if err != nil {
116+ return nil , "" , action , fmt .Errorf ("failed to create secure filesystem: %w" , err )
104117 }
118+ fs := buildfs .MakeFsInMemory (diskFS )
105119
106- if err := memFS .WriteFile (filepath .Join (memFSRoot , filepath .Base (kfilePath )), manifest ); err != nil {
120+ if err := fs .WriteFile (filepath .Join (absDirPath , filepath .Base (kfilePath )), manifest ); err != nil {
107121 return nil , "" , action , err
108122 }
109- return memFS , memFSRoot , action , nil
123+ return fs , absDirPath , action , nil
110124}
111125
112- func (inMemoryBackend ) Cleanup (string , kustomize.Action ) error { return nil }
126+ func (inMemoryFsBackend ) Cleanup (string , kustomize.Action ) error { return nil }
113127
114128// Builder builds yaml manifests
115129// It retrieves the kustomization object from the k8s cluster
@@ -134,7 +148,7 @@ type Builder struct {
134148 localSources map [string ]string
135149 // diff needs to handle kustomizations one by one
136150 singleKustomization bool
137- backend buildBackend
151+ fsBackend fsBackend
138152}
139153
140154// BuilderOptionFunc is a function that configures a Builder
@@ -249,7 +263,7 @@ func WithLocalSources(localSources map[string]string) BuilderOptionFunc {
249263func WithInMemoryBuild (inMemoryBuild bool ) BuilderOptionFunc {
250264 return func (b * Builder ) error {
251265 if inMemoryBuild {
252- b .backend = inMemoryBackend {}
266+ b .fsBackend = inMemoryFsBackend {}
253267 }
254268 return nil
255269 }
@@ -280,10 +294,10 @@ func withSpinnerFrom(in *Builder) BuilderOptionFunc {
280294 }
281295}
282296
283- // withBackend sets the build backend
284- func withBackend (s buildBackend ) BuilderOptionFunc {
297+ // withFsBackend sets the build backend
298+ func withFsBackend (s fsBackend ) BuilderOptionFunc {
285299 return func (b * Builder ) error {
286- b .backend = s
300+ b .fsBackend = s
287301 return nil
288302 }
289303}
@@ -323,8 +337,8 @@ func NewBuilder(name, resources string, opts ...BuilderOptionFunc) (*Builder, er
323337 b .timeout = defaultTimeout
324338 }
325339
326- if b .backend == nil {
327- b .backend = onDiskBackend {}
340+ if b .fsBackend == nil {
341+ b .fsBackend = onDiskFsBackend {}
328342 }
329343
330344 if b .dryRun && b .kustomizationFile == "" && b .kustomization == nil {
@@ -449,15 +463,15 @@ func (b *Builder) build() (m resmap.ResMap, err error) {
449463 // generate kustomization.yaml if needed
450464 buildFS , buildDir , action , er := b .generate (* k , b .resourcesPath )
451465 if er != nil {
452- errf := b .backend .Cleanup (b .resourcesPath , action )
466+ errf := b .fsBackend .Cleanup (b .resourcesPath , action )
453467 err = fmt .Errorf ("failed to generate kustomization.yaml: %w" , fmt .Errorf ("%v %v" , er , errf ))
454468 return
455469 }
456470
457471 b .action = action
458472
459473 defer func () {
460- errf := b .backend .Cleanup (b .resourcesPath , b .action )
474+ errf := b .fsBackend .Cleanup (b .resourcesPath , b .action )
461475 if err == nil {
462476 err = errf
463477 }
@@ -505,7 +519,7 @@ func (b *Builder) kustomizationBuild(k *kustomizev1.Kustomization) ([]*unstructu
505519 WithRecursive (b .recursive ),
506520 WithLocalSources (b .localSources ),
507521 WithDryRun (b .dryRun ),
508- withBackend (b .backend ),
522+ withFsBackend (b .fsBackend ),
509523 )
510524 if err != nil {
511525 return nil , err
@@ -575,29 +589,7 @@ func (b *Builder) generate(kustomization kustomizev1.Kustomization, dirPath stri
575589 b .mu .Lock ()
576590 defer b .mu .Unlock ()
577591
578- return b .backend .Generate (gen , dirPath )
579- }
580-
581- // loadDirToMemFS copies srcDir into dstDir on the given filesystem.
582- func loadDirToMemFS (srcDir , dstDir string , fs filesys.FileSystem ) error {
583- return filepath .Walk (srcDir , func (p string , info os.FileInfo , err error ) error {
584- if err != nil {
585- return err
586- }
587- rel , err := filepath .Rel (srcDir , p )
588- if err != nil {
589- return err
590- }
591- target := filepath .Join (dstDir , rel )
592- if info .IsDir () {
593- return fs .MkdirAll (target )
594- }
595- data , err := os .ReadFile (p )
596- if err != nil {
597- return err
598- }
599- return fs .WriteFile (target , data )
600- })
592+ return b .fsBackend .Generate (gen , dirPath )
601593}
602594
603595func (b * Builder ) do (ctx context.Context , kustomization kustomizev1.Kustomization , fs filesys.FileSystem , dirPath string ) (resmap.ResMap , error ) {
@@ -824,7 +816,7 @@ func (b *Builder) Cancel() error {
824816 b .mu .Lock ()
825817 defer b .mu .Unlock ()
826818
827- return b .backend .Cleanup (b .resourcesPath , b .action )
819+ return b .fsBackend .Cleanup (b .resourcesPath , b .action )
828820}
829821
830822func (b * Builder ) StartSpinner () error {
0 commit comments