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

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)
}