mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-17 17:52:12 +00:00
Merge pull request #3775 from Shell32-Natsu/kyaml-filter
Enable LocalPackageReader to ignore specific path
This commit is contained in:
@@ -71,6 +71,10 @@ type LocalPackageReadWriter struct {
|
|||||||
NoDeleteFiles bool `yaml:"noDeleteFiles,omitempty"`
|
NoDeleteFiles bool `yaml:"noDeleteFiles,omitempty"`
|
||||||
|
|
||||||
files sets.String
|
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) {
|
func (r *LocalPackageReadWriter) Read() ([]*yaml.RNode, error) {
|
||||||
@@ -81,6 +85,7 @@ func (r *LocalPackageReadWriter) Read() ([]*yaml.RNode, error) {
|
|||||||
ErrorIfNonResources: r.ErrorIfNonResources,
|
ErrorIfNonResources: r.ErrorIfNonResources,
|
||||||
SetAnnotations: r.SetAnnotations,
|
SetAnnotations: r.SetAnnotations,
|
||||||
PackageFileName: r.PackageFileName,
|
PackageFileName: r.PackageFileName,
|
||||||
|
FileSkipFunc: r.FileSkipFunc,
|
||||||
}.Read()
|
}.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err)
|
return nil, errors.Wrap(err)
|
||||||
@@ -133,6 +138,11 @@ func (r *LocalPackageReadWriter) getFiles(nodes []*yaml.RNode) (sets.String, err
|
|||||||
return val, nil
|
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.
|
// LocalPackageReader reads ResourceNodes from a local package.
|
||||||
type LocalPackageReader struct {
|
type LocalPackageReader struct {
|
||||||
Kind string `yaml:"kind,omitempty"`
|
Kind string `yaml:"kind,omitempty"`
|
||||||
@@ -163,6 +173,10 @@ type LocalPackageReader struct {
|
|||||||
|
|
||||||
// SetAnnotations are annotations to set on the Resources as they are read.
|
// SetAnnotations are annotations to set on the Resources as they are read.
|
||||||
SetAnnotations map[string]string `yaml:"setAnnotations,omitempty"`
|
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{}
|
var _ Reader = LocalPackageReader{}
|
||||||
@@ -215,24 +229,24 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) {
|
|||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
return r.shouldSkipDir(path, ignoreFilesMatcher)
|
return r.shouldSkipDir(path, ignoreFilesMatcher)
|
||||||
}
|
}
|
||||||
if match, err := r.shouldSkipFile(path, ignoreFilesMatcher); err != nil {
|
|
||||||
|
// get the relative path to file within the package so we can write the files back out
|
||||||
|
// to another location.
|
||||||
|
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
|
return err
|
||||||
} else if !match {
|
} else if match {
|
||||||
// skip this file
|
// skip this file
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the relative path to file within the package so we can write the files back out
|
r.initReaderAnnotations(relPath, info)
|
||||||
// to another location.
|
nodes, err := r.readFile(path, info)
|
||||||
path, err = filepath.Rel(pathRelativeTo, path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WrapPrefixf(err, pathRelativeTo)
|
return errors.WrapPrefixf(err, path)
|
||||||
}
|
|
||||||
|
|
||||||
r.initReaderAnnotations(path, info)
|
|
||||||
nodes, err := r.readFile(filepath.Join(pathRelativeTo, path), info)
|
|
||||||
if err != nil {
|
|
||||||
return errors.WrapPrefixf(err, filepath.Join(pathRelativeTo, path))
|
|
||||||
}
|
}
|
||||||
operand = append(operand, nodes...)
|
operand = append(operand, nodes...)
|
||||||
return nil
|
return nil
|
||||||
@@ -258,21 +272,25 @@ func (r *LocalPackageReader) readFile(path string, _ os.FileInfo) ([]*yaml.RNode
|
|||||||
}
|
}
|
||||||
|
|
||||||
// shouldSkipFile returns true if the file should be skipped
|
// 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.
|
// check if the file is covered by a .krmignore file.
|
||||||
if matcher.matchFile(path) {
|
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
|
// check if the files are in scope
|
||||||
for _, g := range r.MatchFilesGlob {
|
for _, g := range r.MatchFilesGlob {
|
||||||
if match, err := filepath.Match(g, filepath.Base(path)); err != nil {
|
if match, err := filepath.Match(g, filepath.Base(path)); err != nil {
|
||||||
return false, errors.Wrap(err)
|
return true, errors.Wrap(err)
|
||||||
} else if match {
|
} else if match {
|
||||||
return true, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// initReaderAnnotations adds the LocalPackageReader Annotations to r.SetAnnotations
|
// initReaderAnnotations adds the LocalPackageReader Annotations to r.SetAnnotations
|
||||||
|
|||||||
@@ -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) {
|
func TestLocalPackageReader_Read_JSON(t *testing.T) {
|
||||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||||
defer s.Clean()
|
defer s.Clean()
|
||||||
|
|||||||
Reference in New Issue
Block a user