Skip to content
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

Add FileSystem interface to LocalPackageWriter #4086

Merged
merged 4 commits into from
Aug 19, 2021
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
3 changes: 2 additions & 1 deletion kyaml/kio/pkgio_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,14 @@ func (r *LocalPackageReadWriter) Write(nodes []*yaml.RNode) error {
PackagePath: r.PackagePath,
ClearAnnotations: clear,
KeepReaderAnnotations: r.KeepReaderAnnotations,
FileSystem: r.FileSystem,
}.Write(nodes)
if err != nil {
return errors.Wrap(err)
}
deleteFiles := r.files.Difference(newFiles)
for f := range deleteFiles {
if err = os.Remove(filepath.Join(r.PackagePath, f)); err != nil {
if err = r.FileSystem.RemoveAll(filepath.Join(r.PackagePath, f)); err != nil {
return errors.Wrap(err)
}
}
Expand Down
49 changes: 23 additions & 26 deletions kyaml/kio/pkgio_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
package kio

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"

"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
Expand All @@ -26,6 +28,9 @@ type LocalPackageWriter struct {

// ClearAnnotations will clear annotations before writing the resources
ClearAnnotations []string `yaml:"clearAnnotations,omitempty"`

// FileSystem can be used to mock the disk file system.
FileSystem filesys.FileSystemOrOnDisk
}

var _ Writer = LocalPackageWriter{}
Expand All @@ -36,9 +41,10 @@ func (r LocalPackageWriter) Write(nodes []*yaml.RNode) error {
return err
}

if s, err := os.Stat(r.PackagePath); err != nil {
return err
} else if !s.IsDir() {
if !r.FileSystem.Exists(r.PackagePath) {
return errors.WrapPrefixf(os.ErrNotExist, "could not write to %q", r.PackagePath)
}
if !r.FileSystem.IsDir(r.PackagePath) {
// if the user specified input isn't a directory, the package is the directory of the
// target
r.PackagePath = filepath.Dir(r.PackagePath)
Expand All @@ -65,45 +71,36 @@ func (r LocalPackageWriter) Write(nodes []*yaml.RNode) error {
// validate outputs before writing any
for path := range outputFiles {
outputPath := filepath.Join(r.PackagePath, path)
if st, err := os.Stat(outputPath); !os.IsNotExist(err) {
if err != nil {
return errors.Wrap(err)
}
if st.IsDir() {
return fmt.Errorf("config.kubernetes.io/path cannot be a directory: %s", path)
}
if r.FileSystem.IsDir(outputPath) {
return fmt.Errorf("config.kubernetes.io/path cannot be a directory: %s", path)
}

err = os.MkdirAll(filepath.Dir(outputPath), 0700)
err = r.FileSystem.MkdirAll(filepath.Dir(outputPath))
if err != nil {
return errors.Wrap(err)
}
}

// write files
buf := bytes.NewBuffer(nil)
for path := range outputFiles {
outputPath := filepath.Join(r.PackagePath, path)
err = os.MkdirAll(filepath.Dir(filepath.Join(r.PackagePath, path)), 0700)
err = r.FileSystem.MkdirAll(filepath.Dir(filepath.Join(r.PackagePath, path)))
if err != nil {
return errors.Wrap(err)
}

f, err := os.OpenFile(outputPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(0600))
if err != nil {
buf.Reset()
w := ByteWriter{
Writer: buf,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than writing to a buffer and then to the fs, would it work to do r.FileSystem.Create(path) and pass the result to the ByteWriter here, more similarly to what was being done before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is that there's no easy way with the current filesystem abstraction to open a file with O_TRUNC

KeepReaderAnnotations: r.KeepReaderAnnotations,
ClearAnnotations: r.ClearAnnotations,
}
if err = w.Write(outputFiles[path]); err != nil {
return errors.Wrap(err)
}
if err := func() error {
defer f.Close()
w := ByteWriter{
Writer: f,
KeepReaderAnnotations: r.KeepReaderAnnotations,
ClearAnnotations: r.ClearAnnotations,
}
if err = w.Write(outputFiles[path]); err != nil {
return errors.Wrap(err)
}
return nil
}(); err != nil {

if err := r.FileSystem.WriteFile(outputPath, buf.Bytes()); err != nil {
return errors.Wrap(err)
}
}
Expand Down
Loading