From 674cd89ac94ded8d5badc4054519077750db0996 Mon Sep 17 00:00:00 2001 From: Takuro Wada Date: Mon, 24 Jun 2019 21:15:02 +0900 Subject: [PATCH] Add patch remover --- pkg/commands/edit/remove/all.go | 4 + pkg/commands/edit/remove/removepatch.go | 102 +++++++++++++ pkg/commands/edit/remove/removepatch_test.go | 148 +++++++++++++++++++ pkg/commands/edit/remove/util.go | 38 +++++ 4 files changed, 292 insertions(+) create mode 100644 pkg/commands/edit/remove/removepatch.go create mode 100644 pkg/commands/edit/remove/removepatch_test.go create mode 100644 pkg/commands/edit/remove/util.go diff --git a/pkg/commands/edit/remove/all.go b/pkg/commands/edit/remove/all.go index eb258a3d4..e34b82e02 100644 --- a/pkg/commands/edit/remove/all.go +++ b/pkg/commands/edit/remove/all.go @@ -35,6 +35,9 @@ func NewCmdRemove( kustomize edit remove resource {filepath} {filepath} kustomize edit remove resource {pattern} + # Removes one or more patches from the kustomization file + kustomize edit remove patch + # Removes one or more commonLabels from the kustomization file kustomize edit remove label {labelKey1},{labelKey2} @@ -47,6 +50,7 @@ func NewCmdRemove( newCmdRemoveResource(fsys), newCmdRemoveLabel(fsys, ldr.Validator().MakeLabelNameValidator()), newCmdRemoveAnnotation(fsys, ldr.Validator().MakeAnnotationNameValidator()), + newCmdRemovePatch(fsys), ) return c } diff --git a/pkg/commands/edit/remove/removepatch.go b/pkg/commands/edit/remove/removepatch.go new file mode 100644 index 000000000..03ce83f6c --- /dev/null +++ b/pkg/commands/edit/remove/removepatch.go @@ -0,0 +1,102 @@ +/* +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/pkg/errors" + "github.com/spf13/cobra" + "log" + "sigs.k8s.io/kustomize/v3/pkg/commands/kustfile" + "sigs.k8s.io/kustomize/v3/pkg/fs" + "sigs.k8s.io/kustomize/v3/pkg/patch" + "sigs.k8s.io/kustomize/v3/pkg/pgmconfig" +) + +type removePatchOptions struct { + patchFilePaths []string +} + +// newCmdRemovePatch removes the name of a file containing a patch from the kustomization file. +func newCmdRemovePatch(fsys fs.FileSystem) *cobra.Command { + var o removePatchOptions + + cmd := &cobra.Command{ + Use: "patch", + Short: "Removes one or more patches from " + pgmconfig.KustomizationFileNames[0], + Example: ` + remove patch {filepath}`, + 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.RunRemovePatch(fsys) + }, + } + return cmd +} + +// Validate validates removePatch command. +func (o *removePatchOptions) Validate(args []string) error { + if len(args) == 0 { + return errors.New("must specify a patch file") + } + o.patchFilePaths = args + return nil +} + +// Complete completes removePatch command. +func (o *removePatchOptions) Complete(cmd *cobra.Command, args []string) error { + return nil +} + +// RunRemovePatch runs removePatch command (do real work). +func (o *removePatchOptions) RunRemovePatch(fSys fs.FileSystem) error { + patches, err := globPatternsFS(fSys, o.patchFilePaths) + if err != nil { + return err + } + if len(patches) == 0 { + return nil + } + + mf, err := kustfile.NewKustomizationFile(fSys) + if err != nil { + return err + } + + m, err := mf.Read() + if err != nil { + return err + } + + var removePatches []string + for _, p := range patches { + if !patch.Exist(m.PatchesStrategicMerge, p) { + log.Printf("patch %s doesn't exist in kustomization file", p) + continue + } + removePatches = append(removePatches, p) + } + m.PatchesStrategicMerge = patch.Delete(m.PatchesStrategicMerge, removePatches...) + + return mf.Write(m) +} diff --git a/pkg/commands/edit/remove/removepatch_test.go b/pkg/commands/edit/remove/removepatch_test.go new file mode 100644 index 000000000..42c9c25bd --- /dev/null +++ b/pkg/commands/edit/remove/removepatch_test.go @@ -0,0 +1,148 @@ +/* +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 ( + "fmt" + "sigs.k8s.io/kustomize/v3/pkg/fs" + "sigs.k8s.io/kustomize/v3/pkg/patch" + "strings" + "testing" +) + +const ( + patchFileContent = ` +Lorem ipsum dolor sit amet, consectetur adipiscing elit, +sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +` +) + +func makeKustomizationPatchFS() fs.FileSystem { + fakeFS := fs.MakeFakeFS() + patches := []string{"patch1.yaml", "patch2.yaml"} + + fakeFS.WriteTestKustomizationWith([]byte( + fmt.Sprintf("patchesStrategicMerge:\n - %s", + strings.Join(patches, "\n - ")))) + + for _, p := range patches { + fakeFS.WriteFile(p, []byte(patchFileContent)) + } + fakeFS.WriteFile("patch3.yaml", []byte(patchFileContent)) + return fakeFS +} + +func TestRemovePatch(t *testing.T) { + fakeFS := makeKustomizationPatchFS() + cmd := newCmdRemovePatch(fakeFS) + args := []string{"patch1.yaml"} + err := cmd.RunE(cmd, args) + + if err != nil { + t.Errorf("unexpected error %v", err) + } + + m := readKustomizationFS(t, fakeFS) + for _, k := range args { + if patch.Exist(m.PatchesStrategicMerge, k) { + t.Errorf("%s must be deleted", k) + } + } +} + +func TestRemovePatchMultipleArgs(t *testing.T) { + fakeFS := makeKustomizationPatchFS() + cmd := newCmdRemovePatch(fakeFS) + args := []string{"patch1.yaml", "patch2.yaml"} + err := cmd.RunE(cmd, args) + + if err != nil { + t.Errorf("unexpected error %v", err) + } + + m := readKustomizationFS(t, fakeFS) + for _, k := range args { + if patch.Exist(m.PatchesStrategicMerge, k) { + t.Errorf("%s must be deleted", k) + } + } +} + +func TestRemovePatchGlob(t *testing.T) { + fakeFS := makeKustomizationPatchFS() + cmd := newCmdRemovePatch(fakeFS) + args := []string{"patch*.yaml"} + err := cmd.RunE(cmd, args) + + if err != nil { + t.Errorf("unexpected error %v", err) + } + + m := readKustomizationFS(t, fakeFS) + if len(m.PatchesStrategicMerge) != 0 { + t.Errorf("all patch must be deleted") + } +} + +func TestRemovePatchNotDefinedInKustomization(t *testing.T) { + fakeFS := makeKustomizationPatchFS() + cmd := newCmdRemovePatch(fakeFS) + args := []string{"patch3.yaml"} + err := cmd.RunE(cmd, args) + + if err != nil { + t.Errorf("unexpected error %v", err) + } + + m := readKustomizationFS(t, fakeFS) + for _, k := range []string{"patch1.yaml", "patch2.yaml"} { + if !patch.Exist(m.PatchesStrategicMerge, k) { + t.Errorf("%s must exist", k) + } + } +} + +func TestRemovePatchNotExist(t *testing.T) { + fakeFS := makeKustomizationPatchFS() + cmd := newCmdRemovePatch(fakeFS) + args := []string{"patch4.yaml"} + err := cmd.RunE(cmd, args) + + if err != nil { + t.Errorf("unexpected error %v", err) + } + + m := readKustomizationFS(t, fakeFS) + for _, k := range []string{"patch1.yaml", "patch2.yaml"} { + if !patch.Exist(m.PatchesStrategicMerge, k) { + t.Errorf("%s must exist", k) + } + } +} + +func TestRemovePatchNoArgs(t *testing.T) { + fakeFS := makeKustomizationPatchFS() + cmd := newCmdRemovePatch(fakeFS) + err := cmd.RunE(cmd, nil) + + if err == nil { + t.Errorf("expected an error") + } + if err.Error() != "must specify a patch file" { + t.Errorf("incorrect error: %v", err.Error()) + } +} diff --git a/pkg/commands/edit/remove/util.go b/pkg/commands/edit/remove/util.go new file mode 100644 index 000000000..c35f63954 --- /dev/null +++ b/pkg/commands/edit/remove/util.go @@ -0,0 +1,38 @@ +/* +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 ( + "log" + "sigs.k8s.io/kustomize/v3/pkg/fs" +) + +func globPatternsFS(fsys fs.FileSystem, patterns []string) ([]string, error) { + var result []string + for _, pattern := range patterns { + files, err := fsys.Glob(pattern) + if err != nil { + return nil, err + } + if len(files) == 0 { + log.Printf("%s has no match", pattern) + continue + } + result = append(result, files...) + } + return result, nil +}