-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add remove resource subcommand #1057
Merged
monopole
merged 2 commits into
kubernetes-sigs:master
from
narg95:feature/remove_resource
May 13, 2019
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package remove | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"sigs.k8s.io/kustomize/pkg/fs" | ||
) | ||
|
||
// NewCmdRemove returns an instance of 'remove' subcommand. | ||
func NewCmdRemove(fsys fs.FileSystem) *cobra.Command { | ||
c := &cobra.Command{ | ||
Use: "remove", | ||
Short: "Removes items to the kustomization file.", | ||
Long: "", | ||
Example: ` | ||
# Removes resources to the kustomization file | ||
kustomize edit remove resource {filepath} {filepath} | ||
kustomize edit remove resource {pattern} | ||
`, | ||
Args: cobra.MinimumNArgs(1), | ||
} | ||
c.AddCommand( | ||
newCmdRemoveResource(fsys), | ||
) | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package remove | ||
|
||
import ( | ||
"errors" | ||
"path/filepath" | ||
|
||
"github.com/spf13/cobra" | ||
"sigs.k8s.io/kustomize/pkg/commands/kustfile" | ||
"sigs.k8s.io/kustomize/pkg/fs" | ||
) | ||
|
||
type removeResourceOptions struct { | ||
resourceFilePaths []string | ||
} | ||
|
||
// newCmdRemoveResource remove the name of a file containing a resource to the kustomization file. | ||
func newCmdRemoveResource(fsys fs.FileSystem) *cobra.Command { | ||
var o removeResourceOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "resource", | ||
Short: "Remove resource file paths to the kustomization file.", | ||
Example: ` | ||
remove resource my-resource.yml | ||
remove resource resource1.yml resource2.yml resource3.yml | ||
remove resource resources/*.yml | ||
`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
err := o.Validate(args) | ||
if err != nil { | ||
return err | ||
} | ||
err = o.Complete(cmd, args) | ||
if err != nil { | ||
return err | ||
} | ||
return o.RunRemoveResource(fsys) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
// Validate validates removeResource command. | ||
func (o *removeResourceOptions) Validate(args []string) error { | ||
if len(args) == 0 { | ||
return errors.New("must specify a resource file") | ||
} | ||
o.resourceFilePaths = args | ||
return nil | ||
} | ||
|
||
// Complete completes removeResource command. | ||
func (o *removeResourceOptions) Complete(cmd *cobra.Command, args []string) error { | ||
return nil | ||
} | ||
|
||
// RunRemoveResource runs Resource command (do real work). | ||
func (o *removeResourceOptions) RunRemoveResource(fSys fs.FileSystem) error { | ||
|
||
mf, err := kustfile.NewKustomizationFile(fSys) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
m, err := mf.Read() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resources, err := globPatterns(m.Resources, o.resourceFilePaths) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(resources) == 0 { | ||
return nil | ||
} | ||
|
||
newResources := make([]string, 0, len(m.Resources)) | ||
for _, resource := range m.Resources { | ||
if kustfile.StringInSlice(resource, resources) { | ||
continue | ||
} | ||
newResources = append(newResources, resource) | ||
} | ||
|
||
m.Resources = newResources | ||
return mf.Write(m) | ||
} | ||
|
||
func globPatterns(resources []string, patterns []string) ([]string, error) { | ||
var result []string | ||
for _, pattern := range patterns { | ||
for _, resource := range resources { | ||
match, err := filepath.Match(pattern, resource) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !match { | ||
continue | ||
} | ||
result = append(result, resource) | ||
} | ||
} | ||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package remove | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"sigs.k8s.io/kustomize/pkg/fs" | ||
) | ||
|
||
func TestRemoveResources(t *testing.T) { | ||
type given struct { | ||
resources []string | ||
removeArgs []string | ||
} | ||
type expected struct { | ||
resources []string | ||
deleted []string | ||
err error | ||
} | ||
testCases := []struct { | ||
description string | ||
given given | ||
expected expected | ||
}{ | ||
{ | ||
description: "remove resource", | ||
given: given{ | ||
resources: []string{ | ||
"resource1.yaml", | ||
"resource2.yaml", | ||
"resource3.yaml", | ||
}, | ||
removeArgs: []string{"resource1.yaml"}, | ||
}, | ||
expected: expected{ | ||
resources: []string{ | ||
"resource2.yaml", | ||
"resource3.yaml", | ||
}, | ||
deleted: []string{ | ||
"resource1.yaml", | ||
}, | ||
}, | ||
}, | ||
{ | ||
description: "remove resources with pattern", | ||
given: given{ | ||
resources: []string{ | ||
"foo/resource1.yaml", | ||
"foo/resource2.yaml", | ||
"foo/resource3.yaml", | ||
"do/not/deleteme/please.yaml", | ||
}, | ||
removeArgs: []string{"foo/resource*.yaml"}, | ||
}, | ||
expected: expected{ | ||
resources: []string{ | ||
"do/not/deleteme/please.yaml", | ||
}, | ||
deleted: []string{ | ||
"foo/resource1.yaml", | ||
"foo/resource2.yaml", | ||
"foo/resource3.yaml", | ||
}, | ||
}, | ||
}, | ||
{ | ||
description: "nothing found to remove", | ||
given: given{ | ||
resources: []string{ | ||
"resource1.yaml", | ||
"resource2.yaml", | ||
"resource3.yaml", | ||
}, | ||
removeArgs: []string{"foo"}, | ||
}, | ||
expected: expected{ | ||
resources: []string{ | ||
"resource2.yaml", | ||
"resource3.yaml", | ||
"resource1.yaml", | ||
}, | ||
}, | ||
}, | ||
{ | ||
description: "no arguments", | ||
given: given{}, | ||
expected: expected{ | ||
err: errors.New("must specify a resource file"), | ||
}, | ||
}, | ||
{ | ||
description: "remove with multiple pattern arguments", | ||
given: given{ | ||
resources: []string{ | ||
"foo/foo.yaml", | ||
"bar/bar.yaml", | ||
"resource3.yaml", | ||
"do/not/deleteme/please.yaml", | ||
}, | ||
removeArgs: []string{ | ||
"foo/*.*", | ||
"bar/*.*", | ||
"res*.yaml", | ||
}, | ||
}, | ||
expected: expected{ | ||
resources: []string{ | ||
"do/not/deleteme/please.yaml", | ||
}, | ||
deleted: []string{ | ||
"foo/foo.yaml", | ||
"bar/bar.yaml", | ||
"resource3.yaml", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.description, func(t *testing.T) { | ||
fakeFS := fs.MakeFakeFS() | ||
fakeFS.WriteTestKustomizationWith([]byte(fmt.Sprintf("resources:\n - %s", strings.Join(tc.given.resources, "\n - ")))) | ||
cmd := newCmdRemoveResource(fakeFS) | ||
err := cmd.RunE(cmd, tc.given.removeArgs) | ||
if err != nil && tc.expected.err == nil { | ||
|
||
t.Errorf("unexpected cmd error: %v", err) | ||
} else if tc.expected.err != nil { | ||
if err.Error() != tc.expected.err.Error() { | ||
t.Errorf("expected error did not occurred. Expected: %v. Actual: %v", tc.expected.err, err) | ||
} | ||
return | ||
} | ||
content, err := fakeFS.ReadTestKustomization() | ||
if err != nil { | ||
t.Errorf("unexpected read error: %v", err) | ||
} | ||
|
||
for _, resourceFileName := range tc.expected.resources { | ||
if !strings.Contains(string(content), resourceFileName) { | ||
t.Errorf("expected resource not found in kustomization file.\nResource: %s\nKustomization file:\n%s", resourceFileName, content) | ||
} | ||
} | ||
for _, resourceFileName := range tc.expected.deleted { | ||
if strings.Contains(string(content), resourceFileName) { | ||
t.Errorf("expected deleted resource found in kustomization file. Resource: %s\nKustomization file:\n%s", resourceFileName, content) | ||
} | ||
} | ||
|
||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi. After a quick read of the tests, is
remove
, removing itemsfrom
the kustomization file?See lines 28 and 38.