Skip to content

Commit ace2d53

Browse files
authored
Merge pull request #5333 from hailkomputer/add-skip-validation-flag-to-edit-add-resource
Add --no-verify flag to edit add resource command
2 parents aeb7cb2 + 7138423 commit ace2d53

File tree

5 files changed

+60
-59
lines changed

5 files changed

+60
-59
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, ldrhelper.NewFileLoaderAtCwd(fSys), strings.Split(opts.resources, ","))
102+
resources, err = util.GlobPatternsWithLoader(fSys, ldrhelper.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+
noVerify 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.noVerify, "no-verify", 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, ldrhelper.NewFileLoaderAtCwd(fSys), o.resourceFilePaths)
56+
resources, err := util.GlobPatternsWithLoader(fSys, ldrhelper.NewFileLoaderAtCwd(fSys), o.resourceFilePaths, o.noVerify)
5357
if err != nil {
5458
return err
5559
}

kustomize/commands/internal/util/util.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,40 @@ 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 {
41+
if skipValidation {
42+
result = append(result, pattern)
43+
continue
44+
}
45+
3846
files, err := fSys.Glob(pattern)
3947
if err != nil {
40-
return nil, err
48+
return nil, fmt.Errorf("error checking the filesystem: %w", err)
4149
}
42-
if len(files) == 0 {
43-
loader, err := ldr.New(pattern)
44-
if err != nil {
45-
return nil, fmt.Errorf("%s has no match: %w", pattern, err)
46-
} else {
47-
result = append(result, pattern)
48-
if loader != nil {
49-
loader.Cleanup()
50-
}
51-
}
50+
51+
if len(files) != 0 {
52+
result = append(result, files...)
5253
continue
5354
}
54-
result = append(result, files...)
55+
56+
loader, err := ldr.New(pattern)
57+
if err != nil {
58+
return nil, fmt.Errorf("%s has no match: %w", pattern, err)
59+
}
60+
61+
result = append(result, pattern)
62+
if loader != nil {
63+
if err = loader.Cleanup(); err != nil {
64+
return nil, fmt.Errorf("error cleaning up loader: %w", err)
65+
}
66+
}
5567
}
5668
return result, nil
5769
}

kustomize/commands/internal/util/util_test.go

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"reflect"
99
"testing"
1010

11+
"github.com/stretchr/testify/require"
1112
"sigs.k8s.io/kustomize/api/ifc"
1213
"sigs.k8s.io/kustomize/kyaml/filesys"
1314
)
@@ -21,26 +22,18 @@ func TestConvertToMap(t *testing.T) {
2122
expected["g"] = "h:k"
2223

2324
result, err := ConvertToMap(args, "annotation")
24-
if err != nil {
25-
t.Errorf("unexpected error: %v", err.Error())
26-
}
25+
require.NoError(t, err, "unexpected error")
2726

2827
eq := reflect.DeepEqual(expected, result)
29-
if !eq {
30-
t.Errorf("Converted map does not match expected, expected: %v, result: %v\n", expected, result)
31-
}
28+
require.True(t, eq, "Converted map does not match expected")
3229
}
3330

3431
func TestConvertToMapError(t *testing.T) {
3532
args := "a:b,c:\"d\",:f:g"
3633

3734
_, err := ConvertToMap(args, "annotation")
38-
if err == nil {
39-
t.Errorf("expected an error")
40-
}
41-
if err.Error() != "invalid annotation: ':f:g' (need k:v pair where v may be quoted)" {
42-
t.Errorf("incorrect error: %v", err.Error())
43-
}
35+
require.Error(t, err, "expected error but did not receive one")
36+
require.Equal(t, "invalid annotation: ':f:g' (need k:v pair where v may be quoted)", err.Error(), "incorrect error")
4437
}
4538

4639
func TestConvertSliceToMap(t *testing.T) {
@@ -52,14 +45,10 @@ func TestConvertSliceToMap(t *testing.T) {
5245
expected["g"] = "h:k"
5346

5447
result, err := ConvertSliceToMap(args, "annotation")
55-
if err != nil {
56-
t.Errorf("unexpected error: %v", err.Error())
57-
}
48+
require.NoError(t, err, "unexpected error")
5849

5950
eq := reflect.DeepEqual(expected, result)
60-
if !eq {
61-
t.Errorf("Converted map does not match expected, expected: %v, result: %v\n", expected, result)
62-
}
51+
require.True(t, eq, "Converted map does not match expected")
6352
}
6453

6554
func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) {
@@ -71,34 +60,30 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) {
7160
}
7261

7362
// test load remote file
74-
resources, err := GlobPatternsWithLoader(fSys, ldr, []string{httpPath})
75-
if err != nil {
76-
t.Fatalf("unexpected load error: %v", err)
77-
}
78-
if len(resources) != 1 || resources[0] != httpPath {
79-
t.Fatalf("incorrect resources: %v", resources)
80-
}
63+
resources, err := GlobPatternsWithLoader(fSys, ldr, []string{httpPath}, false)
64+
require.NoError(t, err, "unexpected load error")
65+
require.Equal(t, 1, len(resources), "incorrect resources")
66+
require.Equal(t, httpPath, resources[0], "incorrect resources")
8167

8268
// test load local and remote file
83-
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{httpPath, "/test.yml"})
84-
if err != nil {
85-
t.Fatalf("unexpected load error: %v", err)
86-
}
87-
if len(resources) != 2 || resources[0] != httpPath || resources[1] != "/test.yml" {
88-
t.Fatalf("incorrect resources: %v", resources)
89-
}
69+
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{httpPath, "/test.yml"}, false)
70+
require.NoError(t, err, "unexpected load error")
71+
require.Equal(t, 2, len(resources), "incorrect resources")
72+
require.Equal(t, httpPath, resources[0], "incorrect resources")
73+
require.Equal(t, "/test.yml", resources[1], "incorrect resources")
9074

9175
// test load invalid file
9276
invalidURL := "http://invalid"
93-
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL})
94-
if err == nil {
95-
t.Fatalf("expected error but did not receive one")
96-
} else if err.Error() != invalidURL+" has no match: "+invalidURL+" not exist" {
97-
t.Fatalf("unexpected load error: %v", err)
98-
}
99-
if len(resources) > 0 {
100-
t.Fatalf("incorrect resources: %v", resources)
101-
}
77+
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL}, false)
78+
require.Error(t, err, "expected error but did not receive one")
79+
require.Equal(t, invalidURL+" has no match: "+invalidURL+" not exist", err.Error(), "unexpected load error")
80+
require.Equal(t, 0, len(resources), "incorrect resources")
81+
82+
// test load unreachable remote file with skipped verification
83+
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL}, true)
84+
require.NoError(t, err, "unexpected load error")
85+
require.Equal(t, 1, len(resources), "incorrect resources")
86+
require.Equal(t, invalidURL, resources[0], "incorrect resources")
10287
}
10388

10489
type fakeLoader struct {

0 commit comments

Comments
 (0)