Skip to content

Commit f3b34c4

Browse files
committed
Add skip-validation flag to edit add resource cmd
1 parent 59696d1 commit f3b34c4

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

kustomize/commands/create/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func runCreate(opts createFlags, fSys filesys.FileSystem, rf *resource.Factory)
9999
var resources []string
100100
var err error
101101
if opts.resources != "" {
102-
resources, err = util.GlobPatternsWithLoader(fSys, loader.NewFileLoaderAtCwd(fSys), strings.Split(opts.resources, ","))
102+
resources, err = util.GlobPatternsWithLoader(fSys, loader.NewFileLoaderAtCwd(fSys), strings.Split(opts.resources, ","), false)
103103
if err != nil {
104104
return err
105105
}

kustomize/commands/edit/add/addcomponent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (o *addComponentOptions) Validate(args []string) error {
4949

5050
// RunAddComponent runs addComponent command (do real work).
5151
func (o *addComponentOptions) RunAddComponent(fSys filesys.FileSystem) error {
52-
components, err := util.GlobPatternsWithLoader(fSys, loader.NewFileLoaderAtCwd(fSys), o.componentFilePaths)
52+
components, err := util.GlobPatternsWithLoader(fSys, loader.NewFileLoaderAtCwd(fSys), o.componentFilePaths, false)
5353
if err != nil {
5454
return err
5555
}

kustomize/commands/edit/add/addresource.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
type addResourceOptions struct {
1818
resourceFilePaths []string
19+
skipValidation bool
1920
}
2021

2122
// 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 {
3536
return o.RunAddResource(fSys)
3637
},
3738
}
39+
cmd.Flags().BoolVar(&o.skipValidation, "skip-validation", false,
40+
"skip validation for resources",
41+
)
3842
return cmd
3943
}
4044

@@ -49,7 +53,7 @@ func (o *addResourceOptions) Validate(args []string) error {
4953

5054
// RunAddResource runs addResource command (do real work).
5155
func (o *addResourceOptions) RunAddResource(fSys filesys.FileSystem) error {
52-
resources, err := util.GlobPatternsWithLoader(fSys, loader.NewFileLoaderAtCwd(fSys), o.resourceFilePaths)
56+
resources, err := util.GlobPatternsWithLoader(fSys, loader.NewFileLoaderAtCwd(fSys), o.resourceFilePaths, o.skipValidation)
5357
if err != nil {
5458
return err
5559
}

kustomize/commands/internal/util/util.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,35 @@ func GlobPatterns(fSys filesys.FileSystem, patterns []string) ([]string, error)
3030
return result, nil
3131
}
3232

33-
// 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.
34-
// It returns an error if there are no matching files or it can't load from remote.
35-
func GlobPatternsWithLoader(fSys filesys.FileSystem, ldr ifc.Loader, patterns []string) ([]string, error) {
33+
// GlobPatterns accepts a slice of glob strings and returns the set of matching file paths.
34+
// If validation is skipped, then it will return the patterns as provided.
35+
// Otherwise, It will try to load the files from the filesystem.
36+
// If files are not found in the filesystem, it will try to load from remote.
37+
// It returns an error if validation is not skipped and there are no matching files or it can't load from remote.
38+
func GlobPatternsWithLoader(fSys filesys.FileSystem, ldr ifc.Loader, patterns []string, skipValidation bool) ([]string, error) {
3639
var result []string
3740
for _, pattern := range patterns {
38-
files, err := fSys.Glob(pattern)
39-
if err != nil {
40-
return nil, err
41-
}
42-
if len(files) == 0 {
43-
loader, err := ldr.New(pattern)
41+
if skipValidation {
42+
result = append(result, pattern)
43+
} else {
44+
files, err := fSys.Glob(pattern)
4445
if err != nil {
45-
return nil, fmt.Errorf("%s has no match: %w", pattern, err)
46+
return nil, err
47+
}
48+
if len(files) != 0 {
49+
result = append(result, files...)
4650
} else {
47-
result = append(result, pattern)
48-
if loader != nil {
49-
loader.Cleanup()
51+
loader, err := ldr.New(pattern)
52+
if err != nil {
53+
return nil, fmt.Errorf("%s has no match: %w", pattern, err)
54+
} else {
55+
result = append(result, pattern)
56+
if loader != nil {
57+
loader.Cleanup()
58+
}
5059
}
5160
}
52-
continue
5361
}
54-
result = append(result, files...)
5562
}
5663
return result, nil
5764
}

kustomize/commands/internal/util/util_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) {
7171
}
7272

7373
// test load remote file
74-
resources, err := GlobPatternsWithLoader(fSys, ldr, []string{httpPath})
74+
resources, err := GlobPatternsWithLoader(fSys, ldr, []string{httpPath}, false)
7575
if err != nil {
7676
t.Fatalf("unexpected load error: %v", err)
7777
}
@@ -80,7 +80,7 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) {
8080
}
8181

8282
// test load local and remote file
83-
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{httpPath, "/test.yml"})
83+
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{httpPath, "/test.yml"}, false)
8484
if err != nil {
8585
t.Fatalf("unexpected load error: %v", err)
8686
}
@@ -90,7 +90,7 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) {
9090

9191
// test load invalid file
9292
invalidURL := "http://invalid"
93-
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL})
93+
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL}, false)
9494
if err == nil {
9595
t.Fatalf("expected error but did not receive one")
9696
} else if err.Error() != invalidURL+" has no match: "+invalidURL+" not exist" {
@@ -99,6 +99,15 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) {
9999
if len(resources) > 0 {
100100
t.Fatalf("incorrect resources: %v", resources)
101101
}
102+
103+
// test load unreachable remote file with skipped verification
104+
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL}, true)
105+
if err != nil {
106+
t.Fatalf("unexpected load error: %v", err)
107+
}
108+
if len(resources) != 1 || resources[0] != invalidURL {
109+
t.Fatalf("incorrect resources: %v", resources)
110+
}
102111
}
103112

104113
type fakeLoader struct {

0 commit comments

Comments
 (0)