Merge pull request #5333 from hailkomputer/add-skip-validation-flag-to-edit-add-resource

Add --no-verify flag to edit add resource command
This commit is contained in:
Natasha Sarkar
2023-10-06 13:00:27 -05:00
committed by GitHub
5 changed files with 63 additions and 62 deletions

View File

@@ -99,7 +99,7 @@ func runCreate(opts createFlags, fSys filesys.FileSystem, rf *resource.Factory)
var resources []string
var err error
if opts.resources != "" {
resources, err = util.GlobPatternsWithLoader(fSys, ldrhelper.NewFileLoaderAtCwd(fSys), strings.Split(opts.resources, ","))
resources, err = util.GlobPatternsWithLoader(fSys, ldrhelper.NewFileLoaderAtCwd(fSys), strings.Split(opts.resources, ","), false)
if err != nil {
return err
}

View File

@@ -49,7 +49,7 @@ func (o *addComponentOptions) Validate(args []string) error {
// RunAddComponent runs addComponent command (do real work).
func (o *addComponentOptions) RunAddComponent(fSys filesys.FileSystem) error {
components, err := util.GlobPatternsWithLoader(fSys, loader.NewFileLoaderAtCwd(fSys), o.componentFilePaths)
components, err := util.GlobPatternsWithLoader(fSys, loader.NewFileLoaderAtCwd(fSys), o.componentFilePaths, false)
if err != nil {
return err
}

View File

@@ -16,6 +16,7 @@ import (
type addResourceOptions struct {
resourceFilePaths []string
noVerify bool
}
// newCmdAddResource adds the name of a file containing a resource to the kustomization file.
@@ -35,6 +36,9 @@ func newCmdAddResource(fSys filesys.FileSystem) *cobra.Command {
return o.RunAddResource(fSys)
},
}
cmd.Flags().BoolVar(&o.noVerify, "no-verify", false,
"skip validation for resources",
)
return cmd
}
@@ -49,7 +53,7 @@ func (o *addResourceOptions) Validate(args []string) error {
// RunAddResource runs addResource command (do real work).
func (o *addResourceOptions) RunAddResource(fSys filesys.FileSystem) error {
resources, err := util.GlobPatternsWithLoader(fSys, ldrhelper.NewFileLoaderAtCwd(fSys), o.resourceFilePaths)
resources, err := util.GlobPatternsWithLoader(fSys, ldrhelper.NewFileLoaderAtCwd(fSys), o.resourceFilePaths, o.noVerify)
if err != nil {
return err
}

View File

@@ -30,28 +30,40 @@ func GlobPatterns(fSys filesys.FileSystem, patterns []string) ([]string, error)
return result, nil
}
// GlobPatterns accepts a slice of glob strings and returns the set of matching file paths. If files are not found, will try load from remote.
// It returns an error if there are no matching files or it can't load from remote.
func GlobPatternsWithLoader(fSys filesys.FileSystem, ldr ifc.Loader, patterns []string) ([]string, error) {
// GlobPatterns accepts a slice of glob strings and returns the set of matching file paths.
// If validation is skipped, then it will return the patterns as provided.
// Otherwise, It will try to load the files from the filesystem.
// If files are not found in the filesystem, it will try to load from remote.
// It returns an error if validation is not skipped and there are no matching files or it can't load from remote.
func GlobPatternsWithLoader(fSys filesys.FileSystem, ldr ifc.Loader, patterns []string, skipValidation bool) ([]string, error) {
var result []string
for _, pattern := range patterns {
files, err := fSys.Glob(pattern)
if err != nil {
return nil, err
}
if len(files) == 0 {
loader, err := ldr.New(pattern)
if err != nil {
return nil, fmt.Errorf("%s has no match: %w", pattern, err)
} else {
result = append(result, pattern)
if loader != nil {
loader.Cleanup()
}
}
if skipValidation {
result = append(result, pattern)
continue
}
result = append(result, files...)
files, err := fSys.Glob(pattern)
if err != nil {
return nil, fmt.Errorf("error checking the filesystem: %w", err)
}
if len(files) != 0 {
result = append(result, files...)
continue
}
loader, err := ldr.New(pattern)
if err != nil {
return nil, fmt.Errorf("%s has no match: %w", pattern, err)
}
result = append(result, pattern)
if loader != nil {
if err = loader.Cleanup(); err != nil {
return nil, fmt.Errorf("error cleaning up loader: %w", err)
}
}
}
return result, nil
}

View File

@@ -8,6 +8,7 @@ import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
@@ -21,26 +22,18 @@ func TestConvertToMap(t *testing.T) {
expected["g"] = "h:k"
result, err := ConvertToMap(args, "annotation")
if err != nil {
t.Errorf("unexpected error: %v", err.Error())
}
require.NoError(t, err, "unexpected error")
eq := reflect.DeepEqual(expected, result)
if !eq {
t.Errorf("Converted map does not match expected, expected: %v, result: %v\n", expected, result)
}
require.True(t, eq, "Converted map does not match expected")
}
func TestConvertToMapError(t *testing.T) {
args := "a:b,c:\"d\",:f:g"
_, err := ConvertToMap(args, "annotation")
if err == nil {
t.Errorf("expected an error")
}
if err.Error() != "invalid annotation: ':f:g' (need k:v pair where v may be quoted)" {
t.Errorf("incorrect error: %v", err.Error())
}
require.Error(t, err, "expected error but did not receive one")
require.Equal(t, "invalid annotation: ':f:g' (need k:v pair where v may be quoted)", err.Error(), "incorrect error")
}
func TestConvertSliceToMap(t *testing.T) {
@@ -52,14 +45,10 @@ func TestConvertSliceToMap(t *testing.T) {
expected["g"] = "h:k"
result, err := ConvertSliceToMap(args, "annotation")
if err != nil {
t.Errorf("unexpected error: %v", err.Error())
}
require.NoError(t, err, "unexpected error")
eq := reflect.DeepEqual(expected, result)
if !eq {
t.Errorf("Converted map does not match expected, expected: %v, result: %v\n", expected, result)
}
require.True(t, eq, "Converted map does not match expected")
}
func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) {
@@ -71,34 +60,30 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) {
}
// test load remote file
resources, err := GlobPatternsWithLoader(fSys, ldr, []string{httpPath})
if err != nil {
t.Fatalf("unexpected load error: %v", err)
}
if len(resources) != 1 || resources[0] != httpPath {
t.Fatalf("incorrect resources: %v", resources)
}
resources, err := GlobPatternsWithLoader(fSys, ldr, []string{httpPath}, false)
require.NoError(t, err, "unexpected load error")
require.Equal(t, 1, len(resources), "incorrect resources")
require.Equal(t, httpPath, resources[0], "incorrect resources")
// test load local and remote file
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{httpPath, "/test.yml"})
if err != nil {
t.Fatalf("unexpected load error: %v", err)
}
if len(resources) != 2 || resources[0] != httpPath || resources[1] != "/test.yml" {
t.Fatalf("incorrect resources: %v", resources)
}
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{httpPath, "/test.yml"}, false)
require.NoError(t, err, "unexpected load error")
require.Equal(t, 2, len(resources), "incorrect resources")
require.Equal(t, httpPath, resources[0], "incorrect resources")
require.Equal(t, "/test.yml", resources[1], "incorrect resources")
// test load invalid file
invalidURL := "http://invalid"
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL})
if err == nil {
t.Fatalf("expected error but did not receive one")
} else if err.Error() != invalidURL+" has no match: "+invalidURL+" not exist" {
t.Fatalf("unexpected load error: %v", err)
}
if len(resources) > 0 {
t.Fatalf("incorrect resources: %v", resources)
}
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL}, false)
require.Error(t, err, "expected error but did not receive one")
require.Equal(t, invalidURL+" has no match: "+invalidURL+" not exist", err.Error(), "unexpected load error")
require.Equal(t, 0, len(resources), "incorrect resources")
// test load unreachable remote file with skipped verification
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL}, true)
require.NoError(t, err, "unexpected load error")
require.Equal(t, 1, len(resources), "incorrect resources")
require.Equal(t, invalidURL, resources[0], "incorrect resources")
}
type fakeLoader struct {