Merge pull request #704 from narg95/feature/add-image-transformer

Add image transformer
This commit is contained in:
Kubernetes Prow Robot
2019-01-18 09:17:06 -08:00
committed by GitHub
9 changed files with 362 additions and 162 deletions

View File

@@ -25,11 +25,11 @@ import (
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/types"
"sigs.k8s.io/kustomize/pkg/image"
)
type setImageTagOptions struct {
imageTagMap map[string]types.ImageTag
imageTagMap map[string]image.ImageTag
}
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")
}
o.imageTagMap = make(map[string]types.ImageTag)
o.imageTagMap = make(map[string]image.ImageTag)
for _, arg := range args {
if s := strings.Split(arg, "@"); len(s) > 1 {
o.imageTagMap[s[0]] = types.ImageTag{
o.imageTagMap[s[0]] = image.ImageTag{
Name: s[0],
Digest: s[1],
}
@@ -89,7 +89,7 @@ func (o *setImageTagOptions) Validate(args []string) error {
if len(s) != 3 {
return errors.New("invalid format of imagetag, must specify it as <image>:<newtag> or <image>@<digest>")
}
o.imageTagMap[s[1]] = types.ImageTag{
o.imageTagMap[s[1]] = image.ImageTag{
Name: s[1],
NewTag: s[2],
}
@@ -116,7 +116,7 @@ func (o *setImageTagOptions) RunSetImageTags(fSys fs.FileSystem) error {
o.imageTagMap[it.Name] = it
}
var imageTags []types.ImageTag
var imageTags []image.ImageTag
for _, v := range o.imageTagMap {
imageTags = append(imageTags, v)
}

33
pkg/image/append.go Normal file
View File

@@ -0,0 +1,33 @@
/*
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 image
// Append appends a slice of type Tag to slice of type Image
func Append(images []Image, tags ...ImageTag) []Image {
for _, tag := range tags {
images = append(images, toImage(tag))
}
return images
}
func toImage(tag ImageTag) Image {
return Image{
Name: tag.Name,
NewTag: tag.NewTag,
Digest: tag.Digest,
}
}

36
pkg/image/image.go Normal file
View File

@@ -0,0 +1,36 @@
/*
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 image provides struct definitions and libraries
// for image overwriting of names, tags and digest.
package image
// Image contains an image name, a new name, a new tag or digest,
// which will replace the original name and tag.
type Image struct {
// Name is a tag-less image name.
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// NewName is the value used to replace the original name.
NewName string `json:"newName,omitempty" yaml:"newName,omitempty"`
// NewTag is the value used to replace the original tag.
NewTag string `json:"newTag,omitempty" yaml:"newTag,omitempty"`
// Digest is the value used to replace the original image tag.
// If digest is present NewTag value is ignored.
Digest string `json:"digest,omitempty" yaml:"digest,omitempty"`
}

31
pkg/image/imagetag.go Normal file
View File

@@ -0,0 +1,31 @@
/*
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 image
// ImageTag contains an image and a new tag, which will replace the original tag.
// Deprecated, instead use Image.
type ImageTag struct {
// Name is a tag-less image name.
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// NewTag is the value to use in replacing the original tag.
NewTag string `json:"newTag,omitempty" yaml:"newTag,omitempty"`
// Digest is the value used to replace the original image tag.
// If digest is present NewTag value is ignored.
Digest string `json:"digest,omitempty" yaml:"digest,omitempty"`
}

View File

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

159
pkg/transformers/image.go Normal file
View File

@@ -0,0 +1,159 @@
/*
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 transformers
import (
"regexp"
"strings"
"sigs.k8s.io/kustomize/pkg/image"
"sigs.k8s.io/kustomize/pkg/resmap"
)
// imageTransformer replace image names and tags
type imageTransformer struct {
images []image.Image
}
var _ Transformer = &imageTransformer{}
// NewImageTransformer constructs an imageTransformer.
func NewImageTransformer(slice []image.Image) (Transformer, error) {
return &imageTransformer{slice}, nil
}
// Transform finds the matching images and replaces name, tag and/or digest
func (pt *imageTransformer) Transform(resources resmap.ResMap) error {
if len(pt.images) == 0 {
return nil
}
for _, res := range resources {
err := pt.findAndReplaceImage(res.Map())
if err != nil {
return err
}
}
return nil
}
/*
findAndReplaceImage replaces the image name and tags inside one object
It searches the object for container session
then loops though all images inside containers session,
finds matched ones and update the image name and tag name
*/
func (pt *imageTransformer) findAndReplaceImage(obj map[string]interface{}) error {
paths := []string{"containers", "initContainers"}
found := false
for _, path := range paths {
_, found = obj[path]
if found {
err := pt.updateContainers(obj, path)
if err != nil {
return err
}
}
}
if !found {
return pt.findContainers(obj)
}
return nil
}
func (pt *imageTransformer) updateContainers(obj map[string]interface{}, path string) error {
containers := obj[path].([]interface{})
for i := range containers {
container := containers[i].(map[string]interface{})
containerImage, found := container["image"]
if !found {
continue
}
imageName := containerImage.(string)
for _, image := range pt.images {
if isImageMatched(imageName, image.Name) {
name, tag := split(imageName)
if image.NewName != "" {
name = image.NewName
}
if image.NewTag != "" {
tag = ":" + image.NewTag
}
if image.Digest != "" {
tag = "@" + image.Digest
}
container["image"] = name + tag
break
}
}
}
return nil
}
func (pt *imageTransformer) findContainers(obj map[string]interface{}) error {
for key := range obj {
switch typedV := obj[key].(type) {
case map[string]interface{}:
err := pt.findAndReplaceImage(typedV)
if err != nil {
return err
}
case []interface{}:
for i := range typedV {
item := typedV[i]
typedItem, ok := item.(map[string]interface{})
if ok {
err := pt.findAndReplaceImage(typedItem)
if err != nil {
return err
}
}
}
}
}
return nil
}
func isImageMatched(s, t string) bool {
// Tag values are limited to [a-zA-Z0-9_.-].
pattern, _ := regexp.Compile("^" + t + "(:[a-zA-Z0-9_.-]*)?$")
return pattern.MatchString(s)
}
// split separates and returns the name and tag parts
// from the image string using either colon `:` or at `@` separators.
// Note that the returned tag keeps its separator.
func split(imageName string) (name string, tag string) {
ic := strings.LastIndex(imageName, ":")
ia := strings.LastIndex(imageName, "@")
if ic < 0 && ia < 0 {
return imageName, ""
}
i := ic
if ic < 0 {
i = ia
}
name = imageName[:i]
tag = imageName[i:]
return
}

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");
you may not use this file except in compliance with the License.
@@ -22,13 +22,13 @@ import (
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/image"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/types"
)
func TestImageTagTransformer(t *testing.T) {
func TestImageTransformer(t *testing.T) {
var rf = resource.NewFactory(
kunstruct.NewKunstructuredFactoryImpl())
@@ -49,6 +49,10 @@ func TestImageTagTransformer(t *testing.T) {
"name": "nginx2",
"image": "my-nginx:1.8.0",
},
map[string]interface{}{
"name": "init-alpine",
"image": "alpine:1.8.0",
},
},
"containers": []interface{}{
map[string]interface{}{
@@ -59,6 +63,10 @@ func TestImageTagTransformer(t *testing.T) {
"name": "replaced-with-digest",
"image": "foobar:1",
},
map[string]interface{}{
"name": "postgresdb",
"image": "postgres:1.8.0",
},
},
},
},
@@ -74,10 +82,6 @@ func TestImageTagTransformer(t *testing.T) {
"name": "nginx1",
"image": "nginx",
},
map[string]interface{}{
"name": "myimage",
"image": "myprivaterepohostname:1234/my/image:latest",
},
},
},
},
@@ -98,6 +102,34 @@ func TestImageTagTransformer(t *testing.T) {
},
},
},
"spec3": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"initContainers": []interface{}{
map[string]interface{}{
"name": "postgresdb",
"image": "postgres:alpine-9",
},
map[string]interface{}{
"name": "init-docker",
"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",
},
},
},
},
},
}),
}
expected := resmap.ResMap{
@@ -117,6 +149,10 @@ func TestImageTagTransformer(t *testing.T) {
"name": "nginx2",
"image": "my-nginx:previous",
},
map[string]interface{}{
"name": "init-alpine",
"image": "myprivaterepohostname:1234/my/cool-alpine:1.8.0",
},
},
"containers": []interface{}{
map[string]interface{}{
@@ -127,6 +163,10 @@ func TestImageTagTransformer(t *testing.T) {
"name": "replaced-with-digest",
"image": "foobar@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3",
},
map[string]interface{}{
"name": "postgresdb",
"image": "my-postgres:v3",
},
},
},
},
@@ -142,10 +182,6 @@ func TestImageTagTransformer(t *testing.T) {
"name": "nginx1",
"image": "nginx:v2",
},
map[string]interface{}{
"name": "myimage",
"image": "myprivaterepohostname:1234/my/image:v1.0.1",
},
},
},
},
@@ -166,14 +202,47 @@ func TestImageTagTransformer(t *testing.T) {
},
},
},
"spec3": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"initContainers": []interface{}{
map[string]interface{}{
"name": "postgresdb",
"image": "my-postgres:v3",
},
map[string]interface{}{
"name": "init-docker",
"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",
},
},
},
},
},
}),
}
it, err := NewImageTagTransformer([]types.ImageTag{
it, err := NewImageTransformer([]image.Image{
{Name: "nginx", NewTag: "v2"},
{Name: "my-nginx", NewTag: "previous"},
{Name: "myprivaterepohostname:1234/my/image", NewTag: "v1.0.1"},
{Name: "foobar", Digest: "sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"},
{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: "docker", NewName: "my-docker", Digest: "sha256:25a0d4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
@@ -184,6 +253,6 @@ func TestImageTagTransformer(t *testing.T) {
}
if !reflect.DeepEqual(m, expected) {
err = expected.ErrorIfNotEqual(m)
t.Fatalf("actual doesn't match expected: %v", err)
t.Fatalf("actual doesn't match expected: %v. Actual %+v", err, m)
}
}

View File

@@ -1,126 +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 transformers
import (
"regexp"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/types"
)
// imageTagTransformer replace image tags
type imageTagTransformer struct {
imageTags []types.ImageTag
}
var _ Transformer = &imageTagTransformer{}
// NewImageTagTransformer constructs a imageTagTransformer.
func NewImageTagTransformer(slice []types.ImageTag) (Transformer, error) {
return &imageTagTransformer{slice}, nil
}
// Transform finds the matching images and replace the tag
func (pt *imageTagTransformer) Transform(resources resmap.ResMap) error {
if len(pt.imageTags) == 0 {
return nil
}
for _, res := range resources {
err := pt.findAndReplaceTag(res.Map())
if err != nil {
return err
}
}
return nil
}
/*
findAndReplaceTag replaces the image tags inside one object
It searches the object for container session
then loops though all images inside containers session, finds matched ones and update the tag name
*/
func (pt *imageTagTransformer) findAndReplaceTag(obj map[string]interface{}) error {
paths := []string{"containers", "initContainers"}
found := false
for _, path := range paths {
_, found = obj[path]
if found {
err := pt.updateContainers(obj, path)
if err != nil {
return err
}
}
}
if !found {
return pt.findContainers(obj)
}
return nil
}
func (pt *imageTagTransformer) updateContainers(obj map[string]interface{}, path string) error {
containers := obj[path].([]interface{})
for i := range containers {
container := containers[i].(map[string]interface{})
image, found := container["image"]
if !found {
continue
}
for _, imagetag := range pt.imageTags {
if isImageMatched(image.(string), imagetag.Name) {
container["image"] = imagetag.Name + ":" + imagetag.NewTag
if imagetag.Digest != "" {
container["image"] = imagetag.Name + "@" + imagetag.Digest
}
break
}
}
}
return nil
}
func (pt *imageTagTransformer) findContainers(obj map[string]interface{}) error {
for key := range obj {
switch typedV := obj[key].(type) {
case map[string]interface{}:
err := pt.findAndReplaceTag(typedV)
if err != nil {
return err
}
case []interface{}:
for i := range typedV {
item := typedV[i]
typedItem, ok := item.(map[string]interface{})
if ok {
err := pt.findAndReplaceTag(typedItem)
if err != nil {
return err
}
}
}
}
}
return nil
}
func isImageMatched(s, t string) bool {
// Tag values are limited to [a-zA-Z0-9_.-].
pattern, _ := regexp.Compile("^" + t + "(:[a-zA-Z0-9_.-]*)?$")
return pattern.MatchString(s)
}

View File

@@ -18,6 +18,7 @@ limitations under the License.
package types
import (
"sigs.k8s.io/kustomize/pkg/image"
"sigs.k8s.io/kustomize/pkg/patch"
)
@@ -71,10 +72,10 @@ type Kustomization struct {
// and http://jsonpatch.com
PatchesJson6902 []patch.Json6902 `json:"patchesJson6902,omitempty" yaml:"patchesJson6902,omitempty"`
// ImageTags is a list of (image name, new tag) pairs for simply
// changing a an image tag. This can also be achieved with a
// 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
// patch, but this operator is simpler to specify.
ImageTags []ImageTag `json:"imageTags,omitempty" yaml:"imageTags,omitempty"`
Images []image.Image `json:"images,omitempty" yaml:"images,omitempty"`
// Vars allow things modified by kustomize to be injected into a
// container specification. A var is a name (e.g. FOO) associated
@@ -134,6 +135,9 @@ type Kustomization struct {
// 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
@@ -149,6 +153,13 @@ func (k *Kustomization) DealWithDeprecatedFields() {
k.PatchesStrategicMerge, k.Patches...)
k.Patches = []string{}
}
if len(k.ImageTags) > 0 {
// Transform `ImageTag` to `Image`
// for backwards compatibility
k.Images = image.Append(
k.Images, k.ImageTags...)
}
}
// DealWithMissingFields fills the missing fields
@@ -247,19 +258,6 @@ type DataSources struct {
EnvSource string `json:"env,omitempty" yaml:"env,omitempty"`
}
// ImageTag contains an image and a new tag, which will replace the original tag.
type ImageTag struct {
// Name is a tag-less image name.
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// NewTag is the value to use in replacing the original tag.
NewTag string `json:"newTag,omitempty" yaml:"newTag,omitempty"`
// Digest is the value used to replace the original image tag.
// If digest is present NewTag value is ignored.
Digest string `json:"digest,omitempty" yaml:"digest,omitempty"`
}
// GeneratorOptions modify behavior of all ConfigMap and Secret generators.
type GeneratorOptions struct {
// Labels to add to all generated resources.