mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
Merge pull request #722 from Liujingfang1/removeDeprecates
remove patches and imageTags from kustomization.yaml
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 (
|
||||
|
||||
@@ -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 <image>:<newtag> or <image>@<digest>")
|
||||
}
|
||||
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)
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -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",
|
||||
}
|
||||
|
||||
@@ -158,12 +156,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"))
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user