mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Support custom config for image transformer
This commit is contained in:
@@ -31,7 +31,7 @@ func GetDefaultFieldSpecs() []byte {
|
|||||||
[]byte(namespaceFieldSpecs),
|
[]byte(namespaceFieldSpecs),
|
||||||
[]byte(varReferenceFieldSpecs),
|
[]byte(varReferenceFieldSpecs),
|
||||||
[]byte(nameReferenceFieldSpecs),
|
[]byte(nameReferenceFieldSpecs),
|
||||||
[]byte(imageFieldSpecs),
|
[]byte(imagesFieldSpecs),
|
||||||
}
|
}
|
||||||
return bytes.Join(configData, []byte("\n"))
|
return bytes.Join(configData, []byte("\n"))
|
||||||
}
|
}
|
||||||
@@ -46,6 +46,6 @@ func GetDefaultFieldSpecsAsMap() map[string]string {
|
|||||||
result["namespace"] = namespaceFieldSpecs
|
result["namespace"] = namespaceFieldSpecs
|
||||||
result["varreference"] = varReferenceFieldSpecs
|
result["varreference"] = varReferenceFieldSpecs
|
||||||
result["namereference"] = nameReferenceFieldSpecs
|
result["namereference"] = nameReferenceFieldSpecs
|
||||||
result["image"] = imageFieldSpecs
|
result["images"] = imagesFieldSpecs
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,15 @@ limitations under the License.
|
|||||||
package defaultconfig
|
package defaultconfig
|
||||||
|
|
||||||
const (
|
const (
|
||||||
imageFieldSpecs = `
|
imagesFieldSpecs = `
|
||||||
image:
|
images:
|
||||||
- path: containers
|
- kind: Pod
|
||||||
- path: initContainers
|
path: spec/initContainers
|
||||||
|
- kind: Pod
|
||||||
|
path: spec/containers
|
||||||
|
- kind: Deployment
|
||||||
|
path: spec/template/spec/initContainers
|
||||||
|
- kind: Deployment
|
||||||
|
path: spec/template/spec/containers
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
@@ -34,7 +34,7 @@ type TransformerConfig struct {
|
|||||||
CommonAnnotations fsSlice `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"`
|
CommonAnnotations fsSlice `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"`
|
||||||
NameReference nbrSlice `json:"nameReference,omitempty" yaml:"nameReference,omitempty"`
|
NameReference nbrSlice `json:"nameReference,omitempty" yaml:"nameReference,omitempty"`
|
||||||
VarReference fsSlice `json:"varReference,omitempty" yaml:"varReference,omitempty"`
|
VarReference fsSlice `json:"varReference,omitempty" yaml:"varReference,omitempty"`
|
||||||
Images fsSlice `json:"image,omitempty" yaml:"image,omitempty"`
|
Images fsSlice `json:"images,omitempty" yaml:"images,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeEmptyConfig returns an empty TransformerConfig object
|
// MakeEmptyConfig returns an empty TransformerConfig object
|
||||||
|
|||||||
@@ -40,47 +40,30 @@ func NewImageTransformer(slice []image.Image, fs []config.FieldSpec) (Transforme
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Transform finds the matching images and replaces name, tag and/or digest
|
// Transform finds the matching images and replaces name, tag and/or digest
|
||||||
func (pt *imageTransformer) Transform(resources resmap.ResMap) error {
|
func (pt *imageTransformer) Transform(m resmap.ResMap) error {
|
||||||
if len(pt.images) == 0 {
|
if len(pt.images) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
for _, res := range resources {
|
for id := range m {
|
||||||
err := pt.findAndReplaceImage(res.Map())
|
objMap := m[id].Map()
|
||||||
if err != nil {
|
for _, path := range pt.fieldSpecs {
|
||||||
return err
|
if !id.Gvk().IsSelected(&path.Gvk) {
|
||||||
}
|
continue
|
||||||
}
|
}
|
||||||
return nil
|
err := mutateField(objMap, path.PathSlice(), false, pt.updateContainers)
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
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 {
|
|
||||||
found := false
|
|
||||||
for _, fs := range pt.fieldSpecs {
|
|
||||||
path := fs.Path
|
|
||||||
_, found = obj[path]
|
|
||||||
if found {
|
|
||||||
err := pt.updateContainers(obj, path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found {
|
|
||||||
return pt.findContainers(obj)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pt *imageTransformer) updateContainers(obj map[string]interface{}, path string) error {
|
func (pt *imageTransformer) updateContainers(in interface{}) (interface{}, error) {
|
||||||
containers, ok := obj[path].([]interface{})
|
containers, ok := in.([]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("containers path is not of type []interface{} but %T", obj[path])
|
return nil, fmt.Errorf("containers path is not of type []interface{} but %T", in)
|
||||||
}
|
}
|
||||||
for i := range containers {
|
for i := range containers {
|
||||||
container := containers[i].(map[string]interface{})
|
container := containers[i].(map[string]interface{})
|
||||||
@@ -108,31 +91,7 @@ func (pt *imageTransformer) updateContainers(obj map[string]interface{}, path st
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return containers, 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 {
|
func isImageMatched(s, t string) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user