Skip to content

refactor: improve overlayFS errors #17808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 14, 2025
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
69 changes: 17 additions & 52 deletions coderd/files/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import (
"io/fs"
"path"
"strings"

"golang.org/x/xerrors"
)

// overlayFS allows you to "join" together the template files tar file fs.FS
// with the Terraform modules tar file fs.FS. We could potentially turn this
// into something more parameterized/configurable, but the requirements here are
// a _bit_ odd, because every file in the modulesFS includes the
// .terraform/modules/ folder at the beginning of it's path.
// overlayFS allows you to "join" together multiple fs.FS. Files in any specific
// overlay will only be accessible if their path starts with the base path
// provided for the overlay. eg. An overlay at the path .terraform/modules
// should contain files with paths inside the .terraform/modules folder.
type overlayFS struct {
baseFS fs.FS
overlays []Overlay
Expand All @@ -23,64 +20,32 @@ type Overlay struct {
fs.FS
}

func NewOverlayFS(baseFS fs.FS, overlays []Overlay) (fs.FS, error) {
if err := valid(baseFS); err != nil {
return nil, xerrors.Errorf("baseFS: %w", err)
}

for _, overlay := range overlays {
if err := valid(overlay.FS); err != nil {
return nil, xerrors.Errorf("overlayFS: %w", err)
}
}

func NewOverlayFS(baseFS fs.FS, overlays []Overlay) fs.FS {
return overlayFS{
baseFS: baseFS,
overlays: overlays,
}, nil
}
}

func (f overlayFS) Open(p string) (fs.File, error) {
func (f overlayFS) target(p string) fs.FS {
target := f.baseFS
for _, overlay := range f.overlays {
if strings.HasPrefix(path.Clean(p), overlay.Path) {
return overlay.FS.Open(p)
target = overlay.FS
break
}
}
return f.baseFS.Open(p)
return target
}

func (f overlayFS) ReadDir(p string) ([]fs.DirEntry, error) {
for _, overlay := range f.overlays {
if strings.HasPrefix(path.Clean(p), overlay.Path) {
//nolint:forcetypeassert
return overlay.FS.(fs.ReadDirFS).ReadDir(p)
}
}
//nolint:forcetypeassert
return f.baseFS.(fs.ReadDirFS).ReadDir(p)
func (f overlayFS) Open(p string) (fs.File, error) {
return f.target(p).Open(p)
}

func (f overlayFS) ReadFile(p string) ([]byte, error) {
for _, overlay := range f.overlays {
if strings.HasPrefix(path.Clean(p), overlay.Path) {
//nolint:forcetypeassert
return overlay.FS.(fs.ReadFileFS).ReadFile(p)
}
}
//nolint:forcetypeassert
return f.baseFS.(fs.ReadFileFS).ReadFile(p)
func (f overlayFS) ReadDir(p string) ([]fs.DirEntry, error) {
return fs.ReadDir(f.target(p), p)
}

// valid checks that the fs.FS implements the required interfaces.
// The fs.FS interface is not sufficient.
func valid(fsys fs.FS) error {
_, ok := fsys.(fs.ReadDirFS)
if !ok {
return xerrors.New("overlayFS does not implement ReadDirFS")
}
_, ok = fsys.(fs.ReadFileFS)
if !ok {
return xerrors.New("overlayFS does not implement ReadFileFS")
}
return nil
func (f overlayFS) ReadFile(p string) ([]byte, error) {
return fs.ReadFile(f.target(p), p)
}
3 changes: 1 addition & 2 deletions coderd/files/overlay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ func TestOverlayFS(t *testing.T) {
afero.WriteFile(b, ".terraform/modules/modules.json", []byte("{}"), 0o644)
afero.WriteFile(b, ".terraform/modules/example_module/main.tf", []byte("terraform {}"), 0o644)

it, err := files.NewOverlayFS(afero.NewIOFS(a), []files.Overlay{{
it := files.NewOverlayFS(afero.NewIOFS(a), []files.Overlay{{
Path: ".terraform/modules",
FS: afero.NewIOFS(b),
}})
require.NoError(t, err)

content, err := fs.ReadFile(it, "main.tf")
require.NoError(t, err)
Expand Down
9 changes: 1 addition & 8 deletions coderd/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,7 @@ func (api *API) templateVersionDynamicParameters(rw http.ResponseWriter, r *http
return
}
defer api.FileCache.Release(tf.CachedModuleFiles.UUID)
templateFS, err = files.NewOverlayFS(templateFS, []files.Overlay{{Path: ".terraform/modules", FS: moduleFilesFS}})
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error creating overlay filesystem.",
Detail: err.Error(),
})
return
}
templateFS = files.NewOverlayFS(templateFS, []files.Overlay{{Path: ".terraform/modules", FS: moduleFilesFS}})
}
} else if !xerrors.Is(err, sql.ErrNoRows) {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Expand Down
Loading