diff --git a/kustomize/commands/create/create.go b/kustomize/commands/create/create.go index 4b42b740b..0f6fa8b14 100644 --- a/kustomize/commands/create/create.go +++ b/kustomize/commands/create/create.go @@ -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, loader.NewFileLoaderAtCwd(fSys), strings.Split(opts.resources, ",")) + resources, err = util.GlobPatternsWithLoader(fSys, loader.NewFileLoaderAtCwd(fSys), strings.Split(opts.resources, ","), false) if err != nil { return err } diff --git a/kustomize/commands/edit/add/addcomponent.go b/kustomize/commands/edit/add/addcomponent.go index 946b38ae9..9746d9300 100644 --- a/kustomize/commands/edit/add/addcomponent.go +++ b/kustomize/commands/edit/add/addcomponent.go @@ -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 } diff --git a/kustomize/commands/edit/add/addresource.go b/kustomize/commands/edit/add/addresource.go index 98e1adb28..0a1829d91 100644 --- a/kustomize/commands/edit/add/addresource.go +++ b/kustomize/commands/edit/add/addresource.go @@ -16,6 +16,7 @@ import ( type addResourceOptions struct { resourceFilePaths []string + skipValidation 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.skipValidation, "skip-validation", 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, loader.NewFileLoaderAtCwd(fSys), o.resourceFilePaths) + resources, err := util.GlobPatternsWithLoader(fSys, loader.NewFileLoaderAtCwd(fSys), o.resourceFilePaths, o.skipValidation) if err != nil { return err } diff --git a/kustomize/commands/internal/util/util.go b/kustomize/commands/internal/util/util.go index 94004e997..01c21473d 100644 --- a/kustomize/commands/internal/util/util.go +++ b/kustomize/commands/internal/util/util.go @@ -30,28 +30,35 @@ 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 skipValidation { + result = append(result, pattern) + } else { + files, err := fSys.Glob(pattern) if err != nil { - return nil, fmt.Errorf("%s has no match: %w", pattern, err) + return nil, err + } + if len(files) != 0 { + result = append(result, files...) } else { - result = append(result, pattern) - if loader != nil { - loader.Cleanup() + 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() + } } } - continue } - result = append(result, files...) } return result, nil } diff --git a/kustomize/commands/internal/util/util_test.go b/kustomize/commands/internal/util/util_test.go index 631e1145f..5e6a57e4c 100644 --- a/kustomize/commands/internal/util/util_test.go +++ b/kustomize/commands/internal/util/util_test.go @@ -71,7 +71,7 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) { } // test load remote file - resources, err := GlobPatternsWithLoader(fSys, ldr, []string{httpPath}) + resources, err := GlobPatternsWithLoader(fSys, ldr, []string{httpPath}, false) if err != nil { t.Fatalf("unexpected load error: %v", err) } @@ -80,7 +80,7 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) { } // test load local and remote file - resources, err = GlobPatternsWithLoader(fSys, ldr, []string{httpPath, "/test.yml"}) + resources, err = GlobPatternsWithLoader(fSys, ldr, []string{httpPath, "/test.yml"}, false) if err != nil { t.Fatalf("unexpected load error: %v", err) } @@ -90,7 +90,7 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) { // test load invalid file invalidURL := "http://invalid" - resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL}) + resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL}, false) if err == nil { t.Fatalf("expected error but did not receive one") } else if err.Error() != invalidURL+" has no match: "+invalidURL+" not exist" { @@ -99,6 +99,15 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) { if len(resources) > 0 { t.Fatalf("incorrect resources: %v", resources) } + + // test load unreachable remote file with skipped verification + resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL}, true) + if err != nil { + t.Fatalf("unexpected load error: %v", err) + } + if len(resources) != 1 || resources[0] != invalidURL { + t.Fatalf("incorrect resources: %v", resources) + } } type fakeLoader struct {