mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
Add skip-validation flag to edit add resource cmd
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user