mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Add glob support in edit add resource
This commit is contained in:
@@ -18,7 +18,7 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"log"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type addResourceOptions struct {
|
type addResourceOptions struct {
|
||||||
resourceFilePath string
|
resourceFilePaths []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// newCmdAddResource adds the name of a file containing a resource to the kustomization file.
|
// newCmdAddResource adds the name of a file containing a resource to the kustomization file.
|
||||||
@@ -56,10 +56,10 @@ func newCmdAddResource(fsys fs.FileSystem) *cobra.Command {
|
|||||||
|
|
||||||
// Validate validates addResource command.
|
// Validate validates addResource command.
|
||||||
func (o *addResourceOptions) Validate(args []string) error {
|
func (o *addResourceOptions) Validate(args []string) error {
|
||||||
if len(args) != 1 {
|
if len(args) == 0 {
|
||||||
return errors.New("must specify a resource file")
|
return errors.New("must specify a resource file")
|
||||||
}
|
}
|
||||||
o.resourceFilePath = args[0]
|
o.resourceFilePaths = args
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,9 +70,24 @@ func (o *addResourceOptions) Complete(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
// RunAddResource runs addResource command (do real work).
|
// RunAddResource runs addResource command (do real work).
|
||||||
func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error {
|
func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error {
|
||||||
if !fsys.Exists(o.resourceFilePath) {
|
var resources []string
|
||||||
return errors.New(o.resourceFilePath + " does not exist")
|
|
||||||
|
for _, pattern := range o.resourceFilePaths {
|
||||||
|
files, err := fsys.Glob(pattern)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(files) == 0 {
|
||||||
|
log.Printf("%s has no match", pattern)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
resources = append(resources, files...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(resources) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -83,11 +98,13 @@ func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if stringInSlice(o.resourceFilePath, m.Resources) {
|
for _, resource := range resources {
|
||||||
return fmt.Errorf("resource %s already in kustomization file", o.resourceFilePath)
|
if stringInSlice(resource, m.Resources) {
|
||||||
|
log.Printf("resource %s already in kustomization file", resource)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
m.Resources = append(m.Resources, resource)
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Resources = append(m.Resources, o.resourceFilePath)
|
|
||||||
|
|
||||||
return mf.write(m)
|
return mf.write(m)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,8 @@ limitations under the License.
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
|
||||||
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
"github.com/kubernetes-sigs/kustomize/pkg/constants"
|
"github.com/kubernetes-sigs/kustomize/pkg/constants"
|
||||||
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
||||||
@@ -52,10 +51,11 @@ secretGenerator: []
|
|||||||
func TestAddResourceHappyPath(t *testing.T) {
|
func TestAddResourceHappyPath(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
|
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
|
||||||
|
fakeFS.WriteFile(resourceFileName+"another", []byte(resourceFileContent))
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
||||||
|
|
||||||
cmd := newCmdAddResource(fakeFS)
|
cmd := newCmdAddResource(fakeFS)
|
||||||
args := []string{resourceFileName}
|
args := []string{resourceFileName + "*"}
|
||||||
err := cmd.RunE(cmd, args)
|
err := cmd.RunE(cmd, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected cmd error: %v", err)
|
t.Errorf("unexpected cmd error: %v", err)
|
||||||
@@ -67,6 +67,9 @@ func TestAddResourceHappyPath(t *testing.T) {
|
|||||||
if !strings.Contains(string(content), resourceFileName) {
|
if !strings.Contains(string(content), resourceFileName) {
|
||||||
t.Errorf("expected resource name in kustomization")
|
t.Errorf("expected resource name in kustomization")
|
||||||
}
|
}
|
||||||
|
if !strings.Contains(string(content), resourceFileName+"another") {
|
||||||
|
t.Errorf("expected resource name in kustomization")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddResourceAlreadyThere(t *testing.T) {
|
func TestAddResourceAlreadyThere(t *testing.T) {
|
||||||
@@ -81,13 +84,10 @@ func TestAddResourceAlreadyThere(t *testing.T) {
|
|||||||
t.Fatalf("unexpected cmd error: %v", err)
|
t.Fatalf("unexpected cmd error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// adding an existing resource should return an error
|
// adding an existing resource doesn't return an error
|
||||||
err = cmd.RunE(cmd, args)
|
err = cmd.RunE(cmd, args)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
t.Errorf("expected already there problem")
|
t.Errorf("unexpected cmd error :%v", err)
|
||||||
}
|
|
||||||
if err.Error() != "resource "+resourceFileName+" already in kustomization file" {
|
|
||||||
t.Errorf("unexpected error %v", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ package fs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ FileSystem = &FakeFS{}
|
var _ FileSystem = &FakeFS{}
|
||||||
@@ -60,6 +62,18 @@ func (fs *FakeFS) Exists(name string) bool {
|
|||||||
return found
|
return found
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Glob returns the list of matching files
|
||||||
|
func (fs *FakeFS) Glob(pattern string) ([]string, error) {
|
||||||
|
var result []string
|
||||||
|
for p := range fs.m {
|
||||||
|
if fs.pathMatch(p, pattern) {
|
||||||
|
result = append(result, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Strings(result)
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
// IsDir returns true if the file exists and is a directory.
|
// IsDir returns true if the file exists and is a directory.
|
||||||
func (fs *FakeFS) IsDir(name string) bool {
|
func (fs *FakeFS) IsDir(name string) bool {
|
||||||
f, found := fs.m[name]
|
f, found := fs.m[name]
|
||||||
@@ -77,18 +91,6 @@ func (fs *FakeFS) ReadFile(name string) ([]byte, error) {
|
|||||||
return nil, fmt.Errorf("cannot read file %q", name)
|
return nil, fmt.Errorf("cannot read file %q", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadFiles looks through all files in the fake filesystem
|
|
||||||
// and find the matching files and then read content from all of them
|
|
||||||
func (fs *FakeFS) ReadFiles(name string) (map[string][]byte, error) {
|
|
||||||
result := map[string][]byte{}
|
|
||||||
for p, f := range fs.m {
|
|
||||||
if fs.pathMatch(p, name) {
|
|
||||||
result[p] = f.content
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteFile always succeeds and does nothing.
|
// WriteFile always succeeds and does nothing.
|
||||||
func (fs *FakeFS) WriteFile(name string, c []byte) error {
|
func (fs *FakeFS) WriteFile(name string, c []byte) error {
|
||||||
ff := &FakeFile{}
|
ff := &FakeFile{}
|
||||||
@@ -98,8 +100,6 @@ func (fs *FakeFS) WriteFile(name string, c []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (fs *FakeFS) pathMatch(path, pattern string) bool {
|
func (fs *FakeFS) pathMatch(path, pattern string) bool {
|
||||||
if path == pattern {
|
match, _ := filepath.Match(pattern, path)
|
||||||
return true
|
return match
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package fs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -90,3 +91,20 @@ func TestWriteFile(t *testing.T) {
|
|||||||
t.Fatalf("incorrect content: %v", content)
|
t.Fatalf("incorrect content: %v", content)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGlob(t *testing.T) {
|
||||||
|
x := MakeFakeFS()
|
||||||
|
x.Create("dir/foo")
|
||||||
|
x.Create("dir/bar")
|
||||||
|
files, err := x.Glob("dir/*")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected no error")
|
||||||
|
}
|
||||||
|
expected := []string{
|
||||||
|
"dir/bar",
|
||||||
|
"dir/foo",
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(files, expected) {
|
||||||
|
t.Fatalf("incorrect files found by glob: %v", files)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ type FileSystem interface {
|
|||||||
Open(name string) (File, error)
|
Open(name string) (File, error)
|
||||||
IsDir(name string) bool
|
IsDir(name string) bool
|
||||||
Exists(name string) bool
|
Exists(name string) bool
|
||||||
|
Glob(pattern string) ([]string, error)
|
||||||
ReadFile(name string) ([]byte, error)
|
ReadFile(name string) ([]byte, error)
|
||||||
ReadFiles(name string) (map[string][]byte, error)
|
|
||||||
WriteFile(name string, data []byte) error
|
WriteFile(name string, data []byte) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,6 +49,11 @@ func (realFS) Exists(name string) bool {
|
|||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Glob returns the list of matching files
|
||||||
|
func (realFS) Glob(pattern string) ([]string, error) {
|
||||||
|
return filepath.Glob(pattern)
|
||||||
|
}
|
||||||
|
|
||||||
// IsDir delegates to os.Stat and FileInfo.IsDir
|
// IsDir delegates to os.Stat and FileInfo.IsDir
|
||||||
func (realFS) IsDir(name string) bool {
|
func (realFS) IsDir(name string) bool {
|
||||||
info, err := os.Stat(name)
|
info, err := os.Stat(name)
|
||||||
@@ -61,26 +66,6 @@ func (realFS) IsDir(name string) bool {
|
|||||||
// ReadFile delegates to ioutil.ReadFile.
|
// ReadFile delegates to ioutil.ReadFile.
|
||||||
func (realFS) ReadFile(name string) ([]byte, error) { return ioutil.ReadFile(name) }
|
func (realFS) ReadFile(name string) ([]byte, error) { return ioutil.ReadFile(name) }
|
||||||
|
|
||||||
// ReadFiles use glob to find the matching files and then read content from all of them
|
|
||||||
func (realFS) ReadFiles(name string) (map[string][]byte, error) {
|
|
||||||
files, err := filepath.Glob(name)
|
|
||||||
if err != nil || len(files) == 0 {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
output := map[string][]byte{}
|
|
||||||
for _, file := range files {
|
|
||||||
bytes, err := ioutil.ReadFile(file)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if bytes != nil {
|
|
||||||
output[file] = bytes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteFile delegates to ioutil.WriteFile with read/write permissions.
|
// WriteFile delegates to ioutil.WriteFile with read/write permissions.
|
||||||
func (realFS) WriteFile(name string, c []byte) error {
|
func (realFS) WriteFile(name string, c []byte) error {
|
||||||
return ioutil.WriteFile(name, c, 0666)
|
return ioutil.WriteFile(name, c, 0666)
|
||||||
|
|||||||
@@ -55,17 +55,15 @@ func TestReadFilesRealFS(t *testing.T) {
|
|||||||
t.Fatalf("unexpected error %s", err)
|
t.Fatalf("unexpected error %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := map[string][]byte{
|
files, err := x.Glob(path.Join("testDir", "*"))
|
||||||
path.Join(testDir, "foo"): []byte(`foo`),
|
expected := []string{
|
||||||
path.Join(testDir, "bar"): []byte(`bar`),
|
path.Join(testDir, "bar"),
|
||||||
}
|
path.Join(testDir, "foo"),
|
||||||
|
|
||||||
content, err := x.ReadFiles("kustomize_testing_dir/*")
|
|
||||||
if !reflect.DeepEqual(content, expected) {
|
|
||||||
t.Fatalf("actual: %+v doesn't match expected: %+v", content, expected)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error %s", err)
|
t.Fatalf("expected no error")
|
||||||
|
}
|
||||||
|
if reflect.DeepEqual(files, expected) {
|
||||||
|
t.Fatalf("incorrect files found by glob: %v", files)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,8 +63,3 @@ func (f FakeLoader) New(newRoot string) (loader.Loader, error) {
|
|||||||
func (f FakeLoader) Load(location string) ([]byte, error) {
|
func (f FakeLoader) Load(location string) ([]byte, error) {
|
||||||
return f.delegate.Load(location)
|
return f.delegate.Load(location)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GlobLoad performs load from a given location.
|
|
||||||
func (f FakeLoader) GlobLoad(location string) (map[string][]byte, error) {
|
|
||||||
return f.delegate.GlobLoad(location)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -109,13 +109,3 @@ func (l *fileLoader) Load(location string) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
return l.fSys.ReadFile(fullLocation)
|
return l.fSys.ReadFile(fullLocation)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GlobLoad returns the map from path to bytes from reading a glob path.
|
|
||||||
// Implements the Loader interface.
|
|
||||||
func (l *fileLoader) GlobLoad(location string) (map[string][]byte, error) {
|
|
||||||
fullLocation, err := l.fullLocation(l.root, location)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return l.fSys.ReadFiles(fullLocation)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -25,6 +25,4 @@ type Loader interface {
|
|||||||
New(newRoot string) (Loader, error)
|
New(newRoot string) (Loader, error)
|
||||||
// Load returns the bytes read from the location or an error.
|
// Load returns the bytes read from the location or an error.
|
||||||
Load(location string) ([]byte, error)
|
Load(location string) ([]byte, error)
|
||||||
// GlobLoad returns the bytes read from a glob path or an error.
|
|
||||||
GlobLoad(location string) (map[string][]byte, error)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user