mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-10 08:20:59 +00:00
Merge pull request #722 from Liujingfang1/removeDeprecates
remove patches and imageTags from kustomization.yaml
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -162,7 +162,7 @@ configMapGenerator:
|
||||
- name: app-config
|
||||
files:
|
||||
- configmap/app-init.ini
|
||||
imageTags:
|
||||
images:
|
||||
- name: nginx
|
||||
newTag: 1.8.0`)
|
||||
|
||||
|
||||
@@ -111,7 +111,7 @@ bases:
|
||||
- ../base
|
||||
patchesStrategicMerge:
|
||||
- deployment/deployment.yaml
|
||||
imageTags:
|
||||
images:
|
||||
- name: whatever
|
||||
newTag: 1.8.0
|
||||
`)
|
||||
|
||||
@@ -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"))
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user