fix issues from code review

This commit is contained in:
Nestor
2019-01-17 15:49:35 +01:00
parent 6dd599a983
commit 6d56c1750f
8 changed files with 50 additions and 30 deletions

View File

@@ -29,7 +29,7 @@ import (
) )
type setImageTagOptions struct { type setImageTagOptions struct {
imageTagMap map[string]image.Tag imageTagMap map[string]image.ImageTag
} }
var pattern = regexp.MustCompile("^(.*):([a-zA-Z0-9._-]*)$") var pattern = regexp.MustCompile("^(.*):([a-zA-Z0-9._-]*)$")
@@ -74,11 +74,11 @@ func (o *setImageTagOptions) Validate(args []string) error {
return errors.New("no image specified") return errors.New("no image specified")
} }
o.imageTagMap = make(map[string]image.Tag) o.imageTagMap = make(map[string]image.ImageTag)
for _, arg := range args { for _, arg := range args {
if s := strings.Split(arg, "@"); len(s) > 1 { if s := strings.Split(arg, "@"); len(s) > 1 {
o.imageTagMap[s[0]] = image.Tag{ o.imageTagMap[s[0]] = image.ImageTag{
Name: s[0], Name: s[0],
Digest: s[1], Digest: s[1],
} }
@@ -89,7 +89,7 @@ func (o *setImageTagOptions) Validate(args []string) error {
if len(s) != 3 { if len(s) != 3 {
return errors.New("invalid format of imagetag, must specify it as <image>:<newtag> or <image>@<digest>") return errors.New("invalid format of imagetag, must specify it as <image>:<newtag> or <image>@<digest>")
} }
o.imageTagMap[s[1]] = image.Tag{ o.imageTagMap[s[1]] = image.ImageTag{
Name: s[1], Name: s[1],
NewTag: s[2], NewTag: s[2],
} }
@@ -116,7 +116,7 @@ func (o *setImageTagOptions) RunSetImageTags(fSys fs.FileSystem) error {
o.imageTagMap[it.Name] = it o.imageTagMap[it.Name] = it
} }
var imageTags []image.Tag var imageTags []image.ImageTag
for _, v := range o.imageTagMap { for _, v := range o.imageTagMap {
imageTags = append(imageTags, v) imageTags = append(imageTags, v)
} }

View File

@@ -1,5 +1,5 @@
/* /*
Copyright 2018 The Kubernetes Authors. Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -17,14 +17,14 @@ limitations under the License.
package image package image
// Append appends a slice of type Tag to slice of type Image // Append appends a slice of type Tag to slice of type Image
func Append(images []Image, tags ...Tag) []Image { func Append(images []Image, tags ...ImageTag) []Image {
for _, tag := range tags { for _, tag := range tags {
images = append(images, toImage(tag)) images = append(images, toImage(tag))
} }
return images return images
} }
func toImage(tag Tag) Image { func toImage(tag ImageTag) Image {
return Image{ return Image{
Name: tag.Name, Name: tag.Name,
NewTag: tag.NewTag, NewTag: tag.NewTag,

View File

@@ -1,5 +1,5 @@
/* /*
Copyright 2018 The Kubernetes Authors. Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
// Package image provides struct definitions and libraries
// for image overwriting of names, tags and digest.
package image package image
// Image contains an image name, a new name, a new tag or digest, // Image contains an image name, a new name, a new tag or digest,

View File

@@ -1,5 +1,5 @@
/* /*
Copyright 2018 The Kubernetes Authors. Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -16,9 +16,9 @@ limitations under the License.
package image package image
// Tag contains an image and a new tag, which will replace the original tag. // ImageTag contains an image and a new tag, which will replace the original tag.
// Tag usage is deprecated, instead use ImageTag. // Deprecated, instead use Image.
type Tag struct { type ImageTag struct {
// Name is a tag-less image name. // Name is a tag-less image name.
Name string `json:"name,omitempty" yaml:"name,omitempty"` Name string `json:"name,omitempty" yaml:"name,omitempty"`

View File

@@ -302,7 +302,7 @@ func (kt *KustTarget) newTransformer(
return nil, err return nil, err
} }
r = append(r, t) r = append(r, t)
t, err = transformers.NewImageTransformer(kt.kustomization.Image) t, err = transformers.NewImageTransformer(kt.kustomization.Images)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -1,5 +1,5 @@
/* /*
Copyright 2018 The Kubernetes Authors. Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@ func NewImageTransformer(slice []image.Image) (Transformer, error) {
return &imageTransformer{slice}, nil return &imageTransformer{slice}, nil
} }
// Transform finds the matching images and replaces name and/or tag // Transform finds the matching images and replaces name, tag and/or digest
func (pt *imageTransformer) Transform(resources resmap.ResMap) error { func (pt *imageTransformer) Transform(resources resmap.ResMap) error {
if len(pt.images) == 0 { if len(pt.images) == 0 {
return nil return nil

View File

@@ -1,5 +1,5 @@
/* /*
Copyright 2018 The Kubernetes Authors. Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -82,10 +82,6 @@ func TestImageTransformer(t *testing.T) {
"name": "nginx1", "name": "nginx1",
"image": "nginx", "image": "nginx",
}, },
map[string]interface{}{
"name": "myimage",
"image": "myprivaterepohostname:1234/my/image:latest",
},
}, },
}, },
}, },
@@ -118,6 +114,18 @@ func TestImageTransformer(t *testing.T) {
"name": "init-docker", "name": "init-docker",
"image": "docker:17-git", "image": "docker:17-git",
}, },
map[string]interface{}{
"name": "myimage",
"image": "myprivaterepohostname:1234/my/image:latest",
},
map[string]interface{}{
"name": "my-app",
"image": "my-app-image:v1",
},
map[string]interface{}{
"name": "my-cool-app",
"image": "gcr.io:8080/my-project/my-cool-app:latest",
},
}, },
}, },
}, },
@@ -174,10 +182,6 @@ func TestImageTransformer(t *testing.T) {
"name": "nginx1", "name": "nginx1",
"image": "nginx:v2", "image": "nginx:v2",
}, },
map[string]interface{}{
"name": "myimage",
"image": "myprivaterepohostname:1234/my/image:v1.0.1",
},
}, },
}, },
}, },
@@ -210,6 +214,18 @@ func TestImageTransformer(t *testing.T) {
"name": "init-docker", "name": "init-docker",
"image": "my-docker@sha256:25a0d4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3", "image": "my-docker@sha256:25a0d4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3",
}, },
map[string]interface{}{
"name": "myimage",
"image": "myprivaterepohostname:1234/my/image:v1.0.1",
},
map[string]interface{}{
"name": "my-app",
"image": "gcr.io/my-project/my-app-image:v1",
},
map[string]interface{}{
"name": "my-cool-app",
"image": "my-cool-app:latest",
},
}, },
}, },
}, },
@@ -223,6 +239,8 @@ func TestImageTransformer(t *testing.T) {
{Name: "myprivaterepohostname:1234/my/image", NewTag: "v1.0.1"}, {Name: "myprivaterepohostname:1234/my/image", NewTag: "v1.0.1"},
{Name: "foobar", Digest: "sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"}, {Name: "foobar", Digest: "sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"},
{Name: "alpine", NewName: "myprivaterepohostname:1234/my/cool-alpine"}, {Name: "alpine", NewName: "myprivaterepohostname:1234/my/cool-alpine"},
{Name: "my-app-image", NewName: "gcr.io/my-project/my-app-image"},
{Name: "gcr.io:8080/my-project/my-cool-app", NewName: "my-cool-app"},
{Name: "postgres", NewName: "my-postgres", NewTag: "v3"}, {Name: "postgres", NewName: "my-postgres", NewTag: "v3"},
{Name: "docker", NewName: "my-docker", Digest: "sha256:25a0d4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"}, {Name: "docker", NewName: "my-docker", Digest: "sha256:25a0d4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"},
}) })

View File

@@ -72,10 +72,10 @@ type Kustomization struct {
// and http://jsonpatch.com // and http://jsonpatch.com
PatchesJson6902 []patch.Json6902 `json:"patchesJson6902,omitempty" yaml:"patchesJson6902,omitempty"` PatchesJson6902 []patch.Json6902 `json:"patchesJson6902,omitempty" yaml:"patchesJson6902,omitempty"`
// Image is a list of (image name, new name, new tag or digest) // Images is a list of (image name, new name, new tag or digest)
// for changing image names, tags or digests. This can also be achieved with a // for changing image names, tags or digests. This can also be achieved with a
// patch, but this operator is simpler to specify. // patch, but this operator is simpler to specify.
Image []image.Image `json:"images,omitempty" yaml:"images,omitempty"` Images []image.Image `json:"images,omitempty" yaml:"images,omitempty"`
// Vars allow things modified by kustomize to be injected into a // Vars allow things modified by kustomize to be injected into a
// container specification. A var is a name (e.g. FOO) associated // container specification. A var is a name (e.g. FOO) associated
@@ -137,7 +137,7 @@ type Kustomization struct {
Patches []string `json:"patches,omitempty" yaml:"patches,omitempty"` Patches []string `json:"patches,omitempty" yaml:"patches,omitempty"`
// Deprecated. Use `Image` // Deprecated. Use `Image`
ImageTags []image.Tag `json:"imageTags,omitempty" yaml:"imageTags,omitempty"` ImageTags []image.ImageTag `json:"imageTags,omitempty" yaml:"imageTags,omitempty"`
} }
// DealWithDeprecatedFields should be called immediately after // DealWithDeprecatedFields should be called immediately after
@@ -157,8 +157,8 @@ func (k *Kustomization) DealWithDeprecatedFields() {
if len(k.ImageTags) > 0 { if len(k.ImageTags) > 0 {
// Transform `image.Tag` to `image.Image` // Transform `image.Tag` to `image.Image`
// for backwards compatibility // for backwards compatibility
k.Image = image.Append( k.Images = image.Append(
k.Image, k.ImageTags...) k.Images, k.ImageTags...)
} }
} }