Skip to content

Commit 8e290aa

Browse files
committed
refactor: use new filesys/fs_memory for in-memory layer
Signed-off-by: rycli <cyril@ryc.li>
1 parent cccf6d1 commit 8e290aa

File tree

5 files changed

+124
-101
lines changed

5 files changed

+124
-101
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,4 @@ require (
268268
sigs.k8s.io/structured-merge-diff/v6 v6.3.2-0.20260122202528-d9cc6641c482 // indirect
269269
)
270270

271-
replace github.com/fluxcd/pkg/kustomize => github.com/rycli/fluxcd-pkg/kustomize v0.0.0-20260329153243-0671cee33351
271+
replace github.com/fluxcd/pkg/kustomize => github.com/rycli/fluxcd-pkg/kustomize v0.0.0-20260329192052-94b031e1aca6

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t
502502
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
503503
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
504504
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
505-
github.com/rycli/fluxcd-pkg/kustomize v0.0.0-20260329153243-0671cee33351 h1:HqutfZNQ2KtDfrgryyjBOmVTRW8c4T2LXNxpDYOL2q8=
506-
github.com/rycli/fluxcd-pkg/kustomize v0.0.0-20260329153243-0671cee33351/go.mod h1:cW08mnngSP8MJYb6mDmMvxH8YjNATdiML0udb37dk+M=
505+
github.com/rycli/fluxcd-pkg/kustomize v0.0.0-20260329192052-94b031e1aca6 h1:a231NZKoN+nuPkqivk8u0kKA6OaWdB30CCurQigx/Tg=
506+
github.com/rycli/fluxcd-pkg/kustomize v0.0.0-20260329192052-94b031e1aca6/go.mod h1:cW08mnngSP8MJYb6mDmMvxH8YjNATdiML0udb37dk+M=
507507
github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw=
508508
github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
509509
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=

internal/build/build.go

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6667
var 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 {
249263
func 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

603595
func (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

830822
func (b *Builder) StartSpinner() error {

0 commit comments

Comments
 (0)