Skip to content

Commit

Permalink
Keep backward compatibility for image transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
yujunz committed Mar 17, 2019
1 parent f311ba8 commit abf538d
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions pkg/transformers/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,37 @@ func (pt *imageTransformer) Transform(m resmap.ResMap) error {
if err != nil {
return err
}
}
// Keep for backward compatibility
err := pt.findAndReplaceImage(objMap)
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 {
containers, found := obj[path]
if found {
_, err := pt.updateContainers(containers)
if err != nil {
return err
}
}
}
if !found {
return pt.findContainers(obj)
}
return nil
}

Expand Down Expand Up @@ -94,6 +122,30 @@ func (pt *imageTransformer) updateContainers(in interface{}) (interface{}, error
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 {
// Tag values are limited to [a-zA-Z0-9_.-].
pattern, _ := regexp.Compile("^" + t + "(:[a-zA-Z0-9_.-]*)?$")
Expand Down

0 comments on commit abf538d

Please sign in to comment.