Skip to content

Commit

Permalink
Merge pull request #3775 from Shell32-Natsu/kyaml-filter
Browse files Browse the repository at this point in the history
Enable LocalPackageReader to ignore specific path
  • Loading branch information
k8s-ci-robot authored Apr 8, 2021
2 parents 629d822 + 7825050 commit f61b075
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 15 deletions.
48 changes: 33 additions & 15 deletions kyaml/kio/pkgio_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ type LocalPackageReadWriter struct {
NoDeleteFiles bool `yaml:"noDeleteFiles,omitempty"`

files sets.String

// FileSkipFunc is a function which returns true if reader should ignore
// the file
FileSkipFunc LocalPackageSkipFileFunc
}

func (r *LocalPackageReadWriter) Read() ([]*yaml.RNode, error) {
Expand All @@ -81,6 +85,7 @@ func (r *LocalPackageReadWriter) Read() ([]*yaml.RNode, error) {
ErrorIfNonResources: r.ErrorIfNonResources,
SetAnnotations: r.SetAnnotations,
PackageFileName: r.PackageFileName,
FileSkipFunc: r.FileSkipFunc,
}.Read()
if err != nil {
return nil, errors.Wrap(err)
Expand Down Expand Up @@ -133,6 +138,11 @@ func (r *LocalPackageReadWriter) getFiles(nodes []*yaml.RNode) (sets.String, err
return val, nil
}

// LocalPackageSkipFileFunc is a function which returns true if the file
// in the package should be ignored by reader.
// relPath is an OS specific relative path
type LocalPackageSkipFileFunc func(relPath string) bool

// LocalPackageReader reads ResourceNodes from a local package.
type LocalPackageReader struct {
Kind string `yaml:"kind,omitempty"`
Expand Down Expand Up @@ -163,6 +173,10 @@ type LocalPackageReader struct {

// SetAnnotations are annotations to set on the Resources as they are read.
SetAnnotations map[string]string `yaml:"setAnnotations,omitempty"`

// FileSkipFunc is a function which returns true if reader should ignore
// the file
FileSkipFunc LocalPackageSkipFileFunc
}

var _ Reader = LocalPackageReader{}
Expand Down Expand Up @@ -215,24 +229,24 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) {
if info.IsDir() {
return r.shouldSkipDir(path, ignoreFilesMatcher)
}
if match, err := r.shouldSkipFile(path, ignoreFilesMatcher); err != nil {
return err
} else if !match {
// skip this file
return nil
}

// get the relative path to file within the package so we can write the files back out
// to another location.
path, err = filepath.Rel(pathRelativeTo, path)
relPath, err := filepath.Rel(pathRelativeTo, path)
if err != nil {
return errors.WrapPrefixf(err, pathRelativeTo)
}
if match, err := r.shouldSkipFile(path, relPath, ignoreFilesMatcher); err != nil {
return err
} else if match {
// skip this file
return nil
}

r.initReaderAnnotations(path, info)
nodes, err := r.readFile(filepath.Join(pathRelativeTo, path), info)
r.initReaderAnnotations(relPath, info)
nodes, err := r.readFile(path, info)
if err != nil {
return errors.WrapPrefixf(err, filepath.Join(pathRelativeTo, path))
return errors.WrapPrefixf(err, path)
}
operand = append(operand, nodes...)
return nil
Expand All @@ -258,21 +272,25 @@ func (r *LocalPackageReader) readFile(path string, _ os.FileInfo) ([]*yaml.RNode
}

// shouldSkipFile returns true if the file should be skipped
func (r *LocalPackageReader) shouldSkipFile(path string, matcher *ignoreFilesMatcher) (bool, error) {
func (r *LocalPackageReader) shouldSkipFile(path, relPath string, matcher *ignoreFilesMatcher) (bool, error) {
// check if the file is covered by a .krmignore file.
if matcher.matchFile(path) {
return false, nil
return true, nil
}

if r.FileSkipFunc != nil && r.FileSkipFunc(relPath) {
return true, nil
}

// check if the files are in scope
for _, g := range r.MatchFilesGlob {
if match, err := filepath.Match(g, filepath.Base(path)); err != nil {
return false, errors.Wrap(err)
return true, errors.Wrap(err)
} else if match {
return true, nil
return false, nil
}
}
return false, nil
return true, nil
}

// initReaderAnnotations adds the LocalPackageReader Annotations to r.SetAnnotations
Expand Down
72 changes: 72 additions & 0 deletions kyaml/kio/pkgio_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,78 @@ metadata:
}
}

func TestLocalPackageReader_Read_pkgAndSkipFile(t *testing.T) {
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
defer s.Clean()
s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA)
s.WriteFile(t, filepath.Join("b_test.yaml"), readFileB)
s.WriteFile(t, filepath.Join("c_test.yaml"), readFileC)
s.WriteFile(t, filepath.Join("d_test.yaml"), readFileD)

paths := []struct {
path string
}{
{path: "./"},
{path: s.Root},
}
for _, p := range paths {
rfr := LocalPackageReader{
PackagePath: p.path,
FileSkipFunc: func(relPath string) bool {
return relPath == "d_test.yaml"
},
}
nodes, err := rfr.Read()
if !assert.NoError(t, err) {
return
}

if !assert.Len(t, nodes, 4) {
return
}
expected := []string{
`a: b #first
metadata:
annotations:
config.kubernetes.io/index: '0'
config.kubernetes.io/path: 'a_test.yaml'
`,
`c: d # second
metadata:
annotations:
config.kubernetes.io/index: '1'
config.kubernetes.io/path: 'a_test.yaml'
`,
`# second thing
e: f
g:
h:
- i # has a list
- j
metadata:
annotations:
config.kubernetes.io/index: '0'
config.kubernetes.io/path: 'b_test.yaml'
`,
`a: b #third
metadata:
annotations:
config.kubernetes.io/index: '0'
config.kubernetes.io/path: 'c_test.yaml'
`,
}
for i := range nodes {
val, err := nodes[i].String()
if !assert.NoError(t, err) {
return
}
if !assert.Equal(t, expected[i], val) {
return
}
}
}
}

func TestLocalPackageReader_Read_JSON(t *testing.T) {
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
defer s.Clean()
Expand Down

0 comments on commit f61b075

Please sign in to comment.