Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions kustomize/filesys/fs_memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
Copyright 2026 The Flux authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package filesys

import (
"os"
"path/filepath"

"sigs.k8s.io/kustomize/kyaml/filesys"
)

// MakeFsInMemory returns a filesystem that reads from disk and writes to
// memory. Read operations check the memory layer first, then fall back to the
// disk layer. Write operations only go to the memory layer, so the on-disk
// files are never modified.
//
// The disk layer can be any filesys.FileSystem (e.g., an fsSecure instance to
// constrain reads to a root directory).
func MakeFsInMemory(disk filesys.FileSystem) filesys.FileSystem {
return fsMemory{disk: disk, memory: filesys.MakeFsInMemory()}
}

// fsMemory layers an in-memory filesystem on top of a read-only disk
// filesystem. Writes go to memory; reads check memory first, then disk.
type fsMemory struct {
disk filesys.FileSystem
memory filesys.FileSystem
}

// Write operations: memory only.

func (fs fsMemory) Create(path string) (filesys.File, error) {
return fs.memory.Create(path)
}
func (fs fsMemory) Mkdir(path string) error { return fs.memory.Mkdir(path) }
func (fs fsMemory) MkdirAll(path string) error { return fs.memory.MkdirAll(path) }
func (fs fsMemory) RemoveAll(path string) error { return fs.memory.RemoveAll(path) }
func (fs fsMemory) WriteFile(path string, data []byte) error { return fs.memory.WriteFile(path, data) }

// Read operations: memory first, then disk.

func (fs fsMemory) Exists(path string) bool { return fs.memory.Exists(path) || fs.disk.Exists(path) }
func (fs fsMemory) IsDir(path string) bool { return fs.memory.IsDir(path) || fs.disk.IsDir(path) }

func (fs fsMemory) Open(path string) (filesys.File, error) {
if fs.memory.Exists(path) {
return fs.memory.Open(path)
}
return fs.disk.Open(path)
}

func (fs fsMemory) ReadFile(path string) ([]byte, error) {
if fs.memory.Exists(path) {
return fs.memory.ReadFile(path)
}
return fs.disk.ReadFile(path)
}

func (fs fsMemory) CleanedAbs(path string) (filesys.ConfirmedDir, string, error) {
return fs.disk.CleanedAbs(path)
}

func (fs fsMemory) ReadDir(path string) ([]string, error) {
return mergeResults(fs.memory.ReadDir(path))(fs.disk.ReadDir(path))
}

func (fs fsMemory) Glob(pattern string) ([]string, error) {
return mergeResults(fs.memory.Glob(pattern))(fs.disk.Glob(pattern))
}

func (fs fsMemory) Walk(path string, walkFn filepath.WalkFunc) error {
visited := make(map[string]struct{})
if fs.memory.Exists(path) {
if err := fs.memory.Walk(path, func(p string, info os.FileInfo, err error) error {
visited[p] = struct{}{}
return walkFn(p, info, err)
}); err != nil {
return err
}
}
return fs.disk.Walk(path, func(p string, info os.FileInfo, err error) error {
if _, ok := visited[p]; ok {
return nil
}
return walkFn(p, info, err)
})
}

// mergeResults deduplicates two ([]string, error) results, preferring the
// first set. Returns a closure so both calls can be inlined at the call site.
func mergeResults(primary []string, pErr error) func([]string, error) ([]string, error) {
return func(secondary []string, sErr error) ([]string, error) {
if pErr != nil && sErr != nil {
return nil, sErr
}
seen := make(map[string]struct{}, len(primary))
merged := make([]string, 0, len(primary)+len(secondary))
for _, e := range primary {
seen[e] = struct{}{}
merged = append(merged, e)
}
for _, e := range secondary {
if _, ok := seen[e]; !ok {
merged = append(merged, e)
}
}
return merged, nil
}
}
245 changes: 245 additions & 0 deletions kustomize/filesys/fs_memory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
/*
Copyright 2026 The Flux authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package filesys

import (
"os"
"path/filepath"
"testing"

"sigs.k8s.io/kustomize/kyaml/filesys"
)

func testTemp(t *testing.T) string {
t.Helper()
tmp, err := testTempDir(t)
if err != nil {
t.Fatal(err)
}
return tmp
}

func Test_fsMemory_ReadFile_fromDisk(t *testing.T) {
tmp := testTemp(t)
if err := os.WriteFile(filepath.Join(tmp, "a.txt"), []byte("disk"), 0o644); err != nil {
t.Fatal(err)
}

diskFS, err := MakeFsOnDiskSecure(tmp)
if err != nil {
t.Fatal(err)
}
fs := MakeFsInMemory(diskFS)

data, err := fs.ReadFile(filepath.Join(tmp, "a.txt"))
if err != nil {
t.Fatalf("ReadFile: %v", err)
}
if string(data) != "disk" {
t.Errorf("got %q, want %q", data, "disk")
}
}

func Test_fsMemory_WriteFile_toMemory(t *testing.T) {
tmp := testTemp(t)

diskFS, err := MakeFsOnDiskSecure(tmp)
if err != nil {
t.Fatal(err)
}
fs := MakeFsInMemory(diskFS)

path := filepath.Join(tmp, "new.txt")
if err := fs.WriteFile(path, []byte("memory")); err != nil {
t.Fatalf("WriteFile: %v", err)
}

// Readable from memory fs.
data, err := fs.ReadFile(path)
if err != nil {
t.Fatalf("ReadFile: %v", err)
}
if string(data) != "memory" {
t.Errorf("got %q, want %q", data, "memory")
}

// Not written to disk.
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Errorf("file should not exist on disk, got err: %v", err)
}
}

func Test_fsMemory_memoryOverridesDisk(t *testing.T) {
tmp := testTemp(t)
path := filepath.Join(tmp, "f.txt")
if err := os.WriteFile(path, []byte("disk"), 0o644); err != nil {
t.Fatal(err)
}

diskFS, err := MakeFsOnDiskSecure(tmp)
if err != nil {
t.Fatal(err)
}
fs := MakeFsInMemory(diskFS)

if err := fs.WriteFile(path, []byte("memory")); err != nil {
t.Fatal(err)
}

data, err := fs.ReadFile(path)
if err != nil {
t.Fatalf("ReadFile: %v", err)
}
if string(data) != "memory" {
t.Errorf("got %q, want %q", data, "memory")
}
}

func Test_fsMemory_Exists(t *testing.T) {
tmp := testTemp(t)
if err := os.WriteFile(filepath.Join(tmp, "disk.txt"), []byte("d"), 0o644); err != nil {
t.Fatal(err)
}

diskFS, err := MakeFsOnDiskSecure(tmp)
if err != nil {
t.Fatal(err)
}
fs := MakeFsInMemory(diskFS)

if err := fs.WriteFile(filepath.Join(tmp, "mem.txt"), []byte("m")); err != nil {
t.Fatal(err)
}

if !fs.Exists(filepath.Join(tmp, "disk.txt")) {
t.Error("disk file should exist")
}
if !fs.Exists(filepath.Join(tmp, "mem.txt")) {
t.Error("memory file should exist")
}
if fs.Exists(filepath.Join(tmp, "nope.txt")) {
t.Error("non-existent file should not exist")
}
}

func Test_fsMemory_IsDir(t *testing.T) {
tmp := testTemp(t)
if err := os.MkdirAll(filepath.Join(tmp, "diskdir"), 0o755); err != nil {
t.Fatal(err)
}

diskFS, err := MakeFsOnDiskSecure(tmp)
if err != nil {
t.Fatal(err)
}
fs := MakeFsInMemory(diskFS)

if err := fs.MkdirAll(filepath.Join(tmp, "memdir")); err != nil {
t.Fatal(err)
}

if !fs.IsDir(filepath.Join(tmp, "diskdir")) {
t.Error("disk dir should be a dir")
}
if !fs.IsDir(filepath.Join(tmp, "memdir")) {
t.Error("memory dir should be a dir")
}
}

func Test_fsMemory_ReadDir_merged(t *testing.T) {
tmp := testTemp(t)
if err := os.WriteFile(filepath.Join(tmp, "a.txt"), []byte("a"), 0o644); err != nil {
t.Fatal(err)
}

diskFS, err := MakeFsOnDiskSecure(tmp)
if err != nil {
t.Fatal(err)
}
fs := MakeFsInMemory(diskFS)

if err := fs.WriteFile(filepath.Join(tmp, "b.txt"), []byte("b")); err != nil {
t.Fatal(err)
}

entries, err := fs.ReadDir(tmp)
if err != nil {
t.Fatalf("ReadDir: %v", err)
}

has := func(name string) bool {
for _, e := range entries {
if e == name {
return true
}
}
return false
}
if !has("a.txt") {
t.Error("should contain disk file a.txt")
}
if !has("b.txt") {
t.Error("should contain memory file b.txt")
}
}

func Test_fsMemory_diskSecurityConstraint(t *testing.T) {
tmp := testTemp(t)

diskFS, err := MakeFsOnDiskSecure(tmp)
if err != nil {
t.Fatal(err)
}
fs := MakeFsInMemory(diskFS)

// Reading outside the secure root should fail.
_, err = fs.ReadFile("/etc/passwd")
if err == nil {
t.Error("expected error reading outside secure root")
}
}

func Test_fsMemory_Walk(t *testing.T) {
tmp := testTemp(t)
if err := os.WriteFile(filepath.Join(tmp, "disk.txt"), []byte("d"), 0o644); err != nil {
t.Fatal(err)
}

fs := MakeFsInMemory(filesys.MakeFsOnDisk())
if err := fs.WriteFile(filepath.Join(tmp, "mem.txt"), []byte("m")); err != nil {
t.Fatal(err)
}

visited := map[string]bool{}
err := fs.Walk(tmp, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
rel, _ := filepath.Rel(tmp, p)
visited[rel] = true
return nil
})
if err != nil {
t.Fatalf("Walk: %v", err)
}
if !visited["disk.txt"] {
t.Error("should visit disk.txt")
}
if !visited["mem.txt"] {
t.Error("should visit mem.txt")
}
}
Loading
Loading