From c461f1f76677098daf83e4036a227f540fdd4329 Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Fri, 25 Jan 2019 14:26:17 -0800 Subject: [PATCH] remove patches and imageTags from kustomization.yaml --- docs/kustomization.yaml | 23 +-- pkg/commands/edit/set/all.go | 1 - pkg/commands/edit/set/setimage.go | 3 + pkg/commands/edit/set/setimagetag.go | 132 ------------------ pkg/commands/edit/set/setimagetag_test.go | 101 -------------- pkg/commands/kustfile/kustomizationfile.go | 4 +- .../kustfile/kustomizationfile_test.go | 47 ------- pkg/target/baseandoverlaymedium_test.go | 2 +- pkg/target/baseandoverlaysmall_test.go | 2 +- pkg/target/kusttarget.go | 3 +- pkg/types/kustomization.go | 50 +++---- 11 files changed, 24 insertions(+), 344 deletions(-) delete mode 100644 pkg/commands/edit/set/setimagetag.go delete mode 100644 pkg/commands/edit/set/setimagetag_test.go diff --git a/docs/kustomization.yaml b/docs/kustomization.yaml index 377d7837c..15f03864c 100644 --- a/docs/kustomization.yaml +++ b/docs/kustomization.yaml @@ -156,7 +156,7 @@ bases: # a memory request/limit, change an env var in a # ConfigMap, etc. Small patches are easy to review and # easy to mix together in overlays. -patches: +patchesStrategicMerge: - service_port_8888.yaml - deployment_increase_replicas.yaml - deployment_increase_memory.yaml @@ -311,24 +311,3 @@ images: newName: my-app - name: alpine digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3 - -# ImageTags is deprecated, instead use Image. -# ImageTags modify the tags for images without creating patches. -# E.g. Given this fragment of a Deployment: -# ``` -# containers: -# - name: myapp -# image: mycontainerregistry/myimage:v0 -# - name: nginxapp -# image: nginx:1.7.9 -#``` -# one can change the tag of myimage to v1 and the tag of nginx to 1.8.0 with the following: -# -# It also supports digests. If digest is present newTag is ignored. -imageTags: - - name: mycontainerregistry/myimage - newTag: v1 - - name: nginx - newTag: 1.8.0 - - name: alpine - digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3 diff --git a/pkg/commands/edit/set/all.go b/pkg/commands/edit/set/all.go index ed78b06ff..0ac085711 100644 --- a/pkg/commands/edit/set/all.go +++ b/pkg/commands/edit/set/all.go @@ -42,7 +42,6 @@ func NewCmdSet(fsys fs.FileSystem, v ifc.Validator) *cobra.Command { newCmdSetNamePrefix(fsys), newCmdSetNameSuffix(fsys), newCmdSetNamespace(fsys, v), - newCmdSetImageTag(fsys), newCmdSetImage(fsys), ) return c diff --git a/pkg/commands/edit/set/setimage.go b/pkg/commands/edit/set/setimage.go index 8454ab0f8..e861504f5 100644 --- a/pkg/commands/edit/set/setimage.go +++ b/pkg/commands/edit/set/setimage.go @@ -18,6 +18,7 @@ package set import ( "errors" + "regexp" "sort" "strings" @@ -31,6 +32,8 @@ type setImageOptions struct { imageMap map[string]image.Image } +var pattern = regexp.MustCompile("^(.*):([a-zA-Z0-9._-]*)$") + // errors var ( diff --git a/pkg/commands/edit/set/setimagetag.go b/pkg/commands/edit/set/setimagetag.go deleted file mode 100644 index 2c8e5db32..000000000 --- a/pkg/commands/edit/set/setimagetag.go +++ /dev/null @@ -1,132 +0,0 @@ -/* -Copyright 2018 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 set - -import ( - "errors" - "log" - "regexp" - "sort" - "strings" - - "github.com/spf13/cobra" - "sigs.k8s.io/kustomize/pkg/commands/kustfile" - "sigs.k8s.io/kustomize/pkg/fs" - "sigs.k8s.io/kustomize/pkg/image" -) - -type setImageTagOptions struct { - imageTagMap map[string]image.ImageTag -} - -var pattern = regexp.MustCompile("^(.*):([a-zA-Z0-9._-]*)$") - -// newCmdSetImageTag sets the new tags for images in the kustomization. -func newCmdSetImageTag(fsys fs.FileSystem) *cobra.Command { - var o setImageTagOptions - - cmd := &cobra.Command{ - Use: "imagetag", - Short: "The `imagetag` command is deprecated, instead use `edit set image`.", - Example: ` -The command - set imagetag nginx:1.8.0 my-app:latest alpine@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3 -will add - -imageTags: -- name: nginx - newTag: 1.8.0 -- name: my-app - newTag: latest -- name: alpine - digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3 - -to the kustomization file if it doesn't exist, -and overwrite the previous ones if the image tag exists. -`, - RunE: func(cmd *cobra.Command, args []string) error { - log.Print(cmd.Short) - err := o.Validate(args) - if err != nil { - return err - } - return o.RunSetImageTags(fsys) - }, - } - return cmd -} - -// Validate validates setImageTag command. -func (o *setImageTagOptions) Validate(args []string) error { - if len(args) == 0 { - return errors.New("no image specified") - } - - o.imageTagMap = make(map[string]image.ImageTag) - - for _, arg := range args { - if s := strings.Split(arg, "@"); len(s) > 1 { - o.imageTagMap[s[0]] = image.ImageTag{ - Name: s[0], - Digest: s[1], - } - continue - } - - s := pattern.FindStringSubmatch(arg) - if len(s) != 3 { - return errors.New("invalid format of imagetag, must specify it as : or @") - } - o.imageTagMap[s[1]] = image.ImageTag{ - Name: s[1], - NewTag: s[2], - } - } - return nil -} - -// RunSetImageTags runs setImageTags command (does real work). -func (o *setImageTagOptions) RunSetImageTags(fSys fs.FileSystem) error { - mf, err := kustfile.NewKustomizationFile(fSys) - if err != nil { - return err - } - m, err := mf.Read() - if err != nil { - return err - } - - for _, it := range m.ImageTags { - if _, ok := o.imageTagMap[it.Name]; ok { - continue - } - - o.imageTagMap[it.Name] = it - } - - var imageTags []image.ImageTag - for _, v := range o.imageTagMap { - imageTags = append(imageTags, v) - } - sort.Slice(imageTags, func(i, j int) bool { - return imageTags[i].Name < imageTags[j].Name - }) - - m.ImageTags = imageTags - - return mf.Write(m) -} diff --git a/pkg/commands/edit/set/setimagetag_test.go b/pkg/commands/edit/set/setimagetag_test.go deleted file mode 100644 index b70d57c3c..000000000 --- a/pkg/commands/edit/set/setimagetag_test.go +++ /dev/null @@ -1,101 +0,0 @@ -/* -Copyright 2018 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 set - -import ( - "strings" - "testing" - - "sigs.k8s.io/kustomize/pkg/fs" -) - -func TestSetImageTagsHappyPath(t *testing.T) { - fakeFS := fs.MakeFakeFS() - fakeFS.WriteTestKustomization() - - cmd := newCmdSetImageTag(fakeFS) - args := []string{"image1:tag1", "image2:tag2", "localhost:5000/operator:1.0.0", - "foo.bar.baz:5000/one/two@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"} - err := cmd.RunE(cmd, args) - if err != nil { - t.Errorf("unexpected cmd error: %v", err) - } - content, err := fakeFS.ReadTestKustomization() - if err != nil { - t.Errorf("unexpected read error: %v", err) - } - expected := []byte(` -imageTags: -- digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3 - name: foo.bar.baz:5000/one/two -- name: image1 - newTag: tag1 -- name: image2 - newTag: tag2 -- name: localhost:5000/operator - newTag: 1.0.0 -`) - if !strings.Contains(string(content), string(expected)) { - t.Errorf("expected imageTags in kustomization file") - } -} - -func TestSetImageTagsOverride(t *testing.T) { - fakeFS := fs.MakeFakeFS() - fakeFS.WriteTestKustomization() - - cmd := newCmdSetImageTag(fakeFS) - args := []string{"image1:tag1", "image2:tag1"} - err := cmd.RunE(cmd, args) - if err != nil { - t.Errorf("unexpected cmd error: %v", err) - } - args = []string{"image2:tag2", "image3:tag3"} - err = cmd.RunE(cmd, args) - if err != nil { - t.Errorf("unexpected cmd error: %v", err) - } - content, err := fakeFS.ReadTestKustomization() - if err != nil { - t.Errorf("unexpected read error: %v", err) - } - expected := []byte(` -imageTags: -- name: image1 - newTag: tag1 -- name: image2 - newTag: tag2 -- name: image3 - newTag: tag3 -`) - if !strings.Contains(string(content), string(expected)) { - t.Errorf("expected imageTags in kustomization file %s", string(content)) - } -} - -func TestSetImageTagsNoArgs(t *testing.T) { - fakeFS := fs.MakeFakeFS() - - cmd := newCmdSetImageTag(fakeFS) - err := cmd.Execute() - if err == nil { - t.Errorf("expected error: %v", err) - } - if err.Error() != "no image specified" { - t.Errorf("incorrect error: %v", err.Error()) - } -} diff --git a/pkg/commands/kustfile/kustomizationfile.go b/pkg/commands/kustfile/kustomizationfile.go index 177ec604f..108170782 100644 --- a/pkg/commands/kustfile/kustomizationfile.go +++ b/pkg/commands/kustfile/kustomizationfile.go @@ -59,7 +59,6 @@ func determineFieldOrder() []string { "Crds", "CommonLabels", "CommonAnnotations", - "Patches", "PatchesStrategicMerge", "PatchesJson6902", "ConfigMapGenerator", @@ -67,7 +66,6 @@ func determineFieldOrder() []string { "GeneratorOptions", "Vars", "Images", - "ImageTags", "Configurations", } @@ -148,12 +146,12 @@ func (mf *kustomizationFile) Read() (*types.Kustomization, error) { if err != nil { return nil, err } + data = types.DealWithDeprecatedFields(data) var k types.Kustomization err = yaml.Unmarshal(data, &k) if err != nil { return nil, err } - k.DealWithDeprecatedFields() msgs := k.DealWithMissingFields() if len(msgs) > 0 { log.Printf(strings.Join(msgs, "\n")) diff --git a/pkg/commands/kustfile/kustomizationfile_test.go b/pkg/commands/kustfile/kustomizationfile_test.go index b1b1cc42a..80d9ca84f 100644 --- a/pkg/commands/kustfile/kustomizationfile_test.go +++ b/pkg/commands/kustfile/kustomizationfile_test.go @@ -45,7 +45,6 @@ func TestFieldOrder(t *testing.T) { "GeneratorOptions", "Vars", "Images", - "ImageTags", "Configurations", } actual := determineFieldOrder() @@ -85,52 +84,6 @@ func TestWriteAndRead(t *testing.T) { } } -// Deprecated fields should not survive being read. -func TestDeprecationOfPatches(t *testing.T) { - hasDeprecatedFields := []byte(` -namePrefix: acme -nameSuffix: emca -patches: -- alice -patchesStrategicMerge: -- bob -`) - fSys := fs.MakeFakeFS() - fSys.WriteTestKustomizationWith(hasDeprecatedFields) - mf, err := NewKustomizationFile(fSys) - if err != nil { - t.Fatalf("Unexpected Error: %v", err) - } - k, err := mf.Read() - if err != nil { - t.Fatalf("Couldn't read kustomization file: %v\n", err) - } - if k.NamePrefix != "acme" { - t.Fatalf("Unexpected name prefix") - } - if k.NameSuffix != "emca" { - t.Fatalf("Unexpected name suffix") - } - if len(k.Patches) > 0 { - t.Fatalf("Expected nothing in Patches.") - } - if len(k.PatchesStrategicMerge) != 2 { - t.Fatalf( - "Expected len(k.PatchesStrategicMerge) == 2, got %d", - len(k.PatchesStrategicMerge)) - } - m := make(map[string]bool) - for _, v := range k.PatchesStrategicMerge { - m[string(v)] = true - } - if _, f := m["alice"]; !f { - t.Fatalf("Expected alice in PatchesStrategicMerge") - } - if _, f := m["bob"]; !f { - t.Fatalf("Expected bob in PatchesStrategicMerge") - } -} - func TestNewNotExist(t *testing.T) { fakeFS := fs.MakeFakeFS() _, err := NewKustomizationFile(fakeFS) diff --git a/pkg/target/baseandoverlaymedium_test.go b/pkg/target/baseandoverlaymedium_test.go index fe2bf19bb..24f9558a4 100644 --- a/pkg/target/baseandoverlaymedium_test.go +++ b/pkg/target/baseandoverlaymedium_test.go @@ -162,7 +162,7 @@ configMapGenerator: - name: app-config files: - configmap/app-init.ini -imageTags: +images: - name: nginx newTag: 1.8.0`) diff --git a/pkg/target/baseandoverlaysmall_test.go b/pkg/target/baseandoverlaysmall_test.go index a8c72b7e6..d9cbdb3fe 100644 --- a/pkg/target/baseandoverlaysmall_test.go +++ b/pkg/target/baseandoverlaysmall_test.go @@ -111,7 +111,7 @@ bases: - ../base patchesStrategicMerge: - deployment/deployment.yaml -imageTags: +images: - name: whatever newTag: 1.8.0 `) diff --git a/pkg/target/kusttarget.go b/pkg/target/kusttarget.go index 2dd0b2f02..138ffeaa6 100644 --- a/pkg/target/kusttarget.go +++ b/pkg/target/kusttarget.go @@ -58,13 +58,12 @@ func NewKustTarget( if err != nil { return nil, err } - + content = types.DealWithDeprecatedFields(content) var k types.Kustomization err = unmarshal(content, &k) if err != nil { return nil, err } - k.DealWithDeprecatedFields() msgs, errs := k.EnforceFields() if len(errs) > 0 { return nil, fmt.Errorf(strings.Join(errs, "\n")) diff --git a/pkg/types/kustomization.go b/pkg/types/kustomization.go index c59f550e9..4c75b0a43 100644 --- a/pkg/types/kustomization.go +++ b/pkg/types/kustomization.go @@ -18,6 +18,8 @@ limitations under the License. package types import ( + "regexp" + "sigs.k8s.io/kustomize/pkg/image" "sigs.k8s.io/kustomize/pkg/patch" ) @@ -128,40 +130,6 @@ type Kustomization struct { // Configurations is a list of transformer configuration files Configurations []string `json:"configurations,omitempty" yaml:"configurations,omitempty"` - - // - // Deprecated fields - See DealWithDeprecatedFields - // - - // Deprecated. - Patches []string `json:"patches,omitempty" yaml:"patches,omitempty"` - - // Deprecated. Use `Image` - ImageTags []image.ImageTag `json:"imageTags,omitempty" yaml:"imageTags,omitempty"` -} - -// DealWithDeprecatedFields should be called immediately after -// loading from storage. -func (k *Kustomization) DealWithDeprecatedFields() { - if len(k.Patches) > 0 { - // The Patches field, meant to hold strategic merge - // patches, is deprecated. Append anything found - // there to the PatchesStrategicMerge field. - // This happened when the PatchesJson6902 field - // was introduced. - k.PatchesStrategicMerge = patch.Append( - k.PatchesStrategicMerge, k.Patches...) - k.Patches = []string{} - } - - if len(k.ImageTags) > 0 { - // Transform `ImageTag` to `Image` - // for backwards compatibility - // images are appended first to keep - // higher precedence - k.Images = image.Append( - k.Images, k.ImageTags...) - } } // DealWithMissingFields fills the missing fields @@ -193,6 +161,20 @@ func (k *Kustomization) EnforceFields() ([]string, []string) { return msgs, errs } +// DealWithDeprecatedFields should be called immediately after +// loading from storage. +func DealWithDeprecatedFields(data []byte) []byte { + deprecateFieldsMap := map[string]string{ + "patches:": "patchesStrategicMerge:", + "imageTags:": "images:", + } + for oldname, newname := range deprecateFieldsMap { + pattern := regexp.MustCompile(oldname) + data = pattern.ReplaceAll(data, []byte(newname)) + } + return data +} + // GeneratorArgs contains arguments common to generators. type GeneratorArgs struct { // Namespace for the configmap, optional