mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Add image transformer
This commit is contained in:
@@ -25,11 +25,11 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
|
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
"sigs.k8s.io/kustomize/pkg/types"
|
"sigs.k8s.io/kustomize/pkg/image"
|
||||||
)
|
)
|
||||||
|
|
||||||
type setImageTagOptions struct {
|
type setImageTagOptions struct {
|
||||||
imageTagMap map[string]types.ImageTag
|
imageTagMap map[string]image.Tag
|
||||||
}
|
}
|
||||||
|
|
||||||
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]types.ImageTag)
|
o.imageTagMap = make(map[string]image.Tag)
|
||||||
|
|
||||||
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]] = types.ImageTag{
|
o.imageTagMap[s[0]] = image.Tag{
|
||||||
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]] = types.ImageTag{
|
o.imageTagMap[s[1]] = image.Tag{
|
||||||
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 []types.ImageTag
|
var imageTags []image.Tag
|
||||||
for _, v := range o.imageTagMap {
|
for _, v := range o.imageTagMap {
|
||||||
imageTags = append(imageTags, v)
|
imageTags = append(imageTags, v)
|
||||||
}
|
}
|
||||||
|
|||||||
33
pkg/image/append.go
Normal file
33
pkg/image/append.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
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 image
|
||||||
|
|
||||||
|
// Append appends a slice of type Tag to slice of type Image
|
||||||
|
func Append(images []Image, tags ...Tag) []Image {
|
||||||
|
for _, tag := range tags {
|
||||||
|
images = append(images, toImage(tag))
|
||||||
|
}
|
||||||
|
return images
|
||||||
|
}
|
||||||
|
|
||||||
|
func toImage(tag Tag) Image {
|
||||||
|
return Image{
|
||||||
|
Name: tag.Name,
|
||||||
|
NewTag: tag.NewTag,
|
||||||
|
Digest: tag.Digest,
|
||||||
|
}
|
||||||
|
}
|
||||||
34
pkg/image/image.go
Normal file
34
pkg/image/image.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
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 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
31
pkg/image/imagetag.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
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 image
|
||||||
|
|
||||||
|
// Tag contains an image and a new tag, which will replace the original tag.
|
||||||
|
// Tag usage is deprecated, instead use ImageTag.
|
||||||
|
type Tag 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"`
|
||||||
|
}
|
||||||
@@ -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.NewImageTagTransformer(kt.kustomization.ImageTags)
|
t, err = transformers.NewImageTransformer(kt.kustomization.Image)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
159
pkg/transformers/image.go
Normal file
159
pkg/transformers/image.go
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
/*
|
||||||
|
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"
|
||||||
|
"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 and/or tag
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -22,13 +22,13 @@ import (
|
|||||||
|
|
||||||
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
|
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
|
||||||
"sigs.k8s.io/kustomize/pkg/gvk"
|
"sigs.k8s.io/kustomize/pkg/gvk"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/image"
|
||||||
"sigs.k8s.io/kustomize/pkg/resid"
|
"sigs.k8s.io/kustomize/pkg/resid"
|
||||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||||
"sigs.k8s.io/kustomize/pkg/resource"
|
"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(
|
var rf = resource.NewFactory(
|
||||||
kunstruct.NewKunstructuredFactoryImpl())
|
kunstruct.NewKunstructuredFactoryImpl())
|
||||||
|
|
||||||
@@ -49,6 +49,10 @@ func TestImageTagTransformer(t *testing.T) {
|
|||||||
"name": "nginx2",
|
"name": "nginx2",
|
||||||
"image": "my-nginx:1.8.0",
|
"image": "my-nginx:1.8.0",
|
||||||
},
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"name": "init-alpine",
|
||||||
|
"image": "alpine:1.8.0",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"containers": []interface{}{
|
"containers": []interface{}{
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
@@ -59,6 +63,10 @@ func TestImageTagTransformer(t *testing.T) {
|
|||||||
"name": "replaced-with-digest",
|
"name": "replaced-with-digest",
|
||||||
"image": "foobar:1",
|
"image": "foobar:1",
|
||||||
},
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"name": "postgresdb",
|
||||||
|
"image": "postgres:1.8.0",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -98,6 +106,22 @@ 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",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
expected := resmap.ResMap{
|
expected := resmap.ResMap{
|
||||||
@@ -117,6 +141,10 @@ func TestImageTagTransformer(t *testing.T) {
|
|||||||
"name": "nginx2",
|
"name": "nginx2",
|
||||||
"image": "my-nginx:previous",
|
"image": "my-nginx:previous",
|
||||||
},
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"name": "init-alpine",
|
||||||
|
"image": "myprivaterepohostname:1234/my/cool-alpine:1.8.0",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"containers": []interface{}{
|
"containers": []interface{}{
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
@@ -127,6 +155,10 @@ func TestImageTagTransformer(t *testing.T) {
|
|||||||
"name": "replaced-with-digest",
|
"name": "replaced-with-digest",
|
||||||
"image": "foobar@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3",
|
"image": "foobar@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3",
|
||||||
},
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"name": "postgresdb",
|
||||||
|
"image": "my-postgres:v3",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -166,14 +198,33 @@ 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",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
it, err := NewImageTagTransformer([]types.ImageTag{
|
it, err := NewImageTransformer([]image.Image{
|
||||||
{Name: "nginx", NewTag: "v2"},
|
{Name: "nginx", NewTag: "v2"},
|
||||||
{Name: "my-nginx", NewTag: "previous"},
|
{Name: "my-nginx", NewTag: "previous"},
|
||||||
{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: "postgres", NewName: "my-postgres", NewTag: "v3"},
|
||||||
|
{Name: "docker", NewName: "my-docker", Digest: "sha256:25a0d4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
@@ -184,6 +235,6 @@ func TestImageTagTransformer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
if !reflect.DeepEqual(m, expected) {
|
if !reflect.DeepEqual(m, expected) {
|
||||||
err = expected.ErrorIfNotEqual(m)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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)
|
|
||||||
}
|
|
||||||
@@ -18,6 +18,7 @@ limitations under the License.
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sigs.k8s.io/kustomize/pkg/image"
|
||||||
"sigs.k8s.io/kustomize/pkg/patch"
|
"sigs.k8s.io/kustomize/pkg/patch"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -71,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"`
|
||||||
|
|
||||||
// ImageTags is a list of (image name, new tag) pairs for simply
|
// Image is a list of (image name, new name, new tag or digest)
|
||||||
// changing a an image tag. 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.
|
||||||
ImageTags []ImageTag `json:"imageTags,omitempty" yaml:"imageTags,omitempty"`
|
Image []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
|
||||||
@@ -134,6 +135,9 @@ type Kustomization struct {
|
|||||||
|
|
||||||
// Deprecated.
|
// Deprecated.
|
||||||
Patches []string `json:"patches,omitempty" yaml:"patches,omitempty"`
|
Patches []string `json:"patches,omitempty" yaml:"patches,omitempty"`
|
||||||
|
|
||||||
|
// Deprecated. Use `Image`
|
||||||
|
ImageTags []image.Tag `json:"imageTags,omitempty" yaml:"imageTags,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DealWithDeprecatedFields should be called immediately after
|
// DealWithDeprecatedFields should be called immediately after
|
||||||
@@ -149,6 +153,13 @@ func (k *Kustomization) DealWithDeprecatedFields() {
|
|||||||
k.PatchesStrategicMerge, k.Patches...)
|
k.PatchesStrategicMerge, k.Patches...)
|
||||||
k.Patches = []string{}
|
k.Patches = []string{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(k.ImageTags) > 0 {
|
||||||
|
// Transform `image.Tag` to `image.Image`
|
||||||
|
// for backwards compatibility
|
||||||
|
k.Image = image.Append(
|
||||||
|
k.Image, k.ImageTags...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DealWithMissingFields fills the missing fields
|
// DealWithMissingFields fills the missing fields
|
||||||
@@ -261,19 +272,6 @@ type DataSources struct {
|
|||||||
EnvSource string `json:"env,omitempty" yaml:"env,omitempty"`
|
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.
|
// GeneratorOptions modify behavior of all ConfigMap and Secret generators.
|
||||||
type GeneratorOptions struct {
|
type GeneratorOptions struct {
|
||||||
// Labels to add to all generated resources.
|
// Labels to add to all generated resources.
|
||||||
|
|||||||
Reference in New Issue
Block a user