image.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package transformers
  14. import (
  15. "fmt"
  16. "regexp"
  17. "strings"
  18. "sigs.k8s.io/kustomize/pkg/image"
  19. "sigs.k8s.io/kustomize/pkg/resmap"
  20. )
  21. // imageTransformer replace image names and tags
  22. type imageTransformer struct {
  23. images []image.Image
  24. }
  25. var _ Transformer = &imageTransformer{}
  26. // NewImageTransformer constructs an imageTransformer.
  27. func NewImageTransformer(slice []image.Image) (Transformer, error) {
  28. return &imageTransformer{slice}, nil
  29. }
  30. // Transform finds the matching images and replaces name, tag and/or digest
  31. func (pt *imageTransformer) Transform(resources resmap.ResMap) error {
  32. if len(pt.images) == 0 {
  33. return nil
  34. }
  35. for _, res := range resources {
  36. err := pt.findAndReplaceImage(res.Map())
  37. if err != nil {
  38. return err
  39. }
  40. }
  41. return nil
  42. }
  43. /*
  44. findAndReplaceImage replaces the image name and tags inside one object
  45. It searches the object for container session
  46. then loops though all images inside containers session,
  47. finds matched ones and update the image name and tag name
  48. */
  49. func (pt *imageTransformer) findAndReplaceImage(obj map[string]interface{}) error {
  50. paths := []string{"containers", "initContainers"}
  51. found := false
  52. for _, path := range paths {
  53. _, found = obj[path]
  54. if found {
  55. err := pt.updateContainers(obj, path)
  56. if err != nil {
  57. return err
  58. }
  59. }
  60. }
  61. if !found {
  62. return pt.findContainers(obj)
  63. }
  64. return nil
  65. }
  66. func (pt *imageTransformer) updateContainers(obj map[string]interface{}, path string) error {
  67. containers, ok := obj[path].([]interface{})
  68. if !ok {
  69. return fmt.Errorf("containers path is not of type []interface{} but %T", obj[path])
  70. }
  71. for i := range containers {
  72. container := containers[i].(map[string]interface{})
  73. containerImage, found := container["image"]
  74. if !found {
  75. continue
  76. }
  77. imageName := containerImage.(string)
  78. for _, img := range pt.images {
  79. if !isImageMatched(imageName, img.Name) {
  80. continue
  81. }
  82. name, tag := split(imageName)
  83. if img.NewName != "" {
  84. name = img.NewName
  85. }
  86. if img.NewTag != "" {
  87. tag = ":" + img.NewTag
  88. }
  89. if img.Digest != "" {
  90. tag = "@" + img.Digest
  91. }
  92. container["image"] = name + tag
  93. break
  94. }
  95. }
  96. return nil
  97. }
  98. func (pt *imageTransformer) findContainers(obj map[string]interface{}) error {
  99. for key := range obj {
  100. switch typedV := obj[key].(type) {
  101. case map[string]interface{}:
  102. err := pt.findAndReplaceImage(typedV)
  103. if err != nil {
  104. return err
  105. }
  106. case []interface{}:
  107. for i := range typedV {
  108. item := typedV[i]
  109. typedItem, ok := item.(map[string]interface{})
  110. if ok {
  111. err := pt.findAndReplaceImage(typedItem)
  112. if err != nil {
  113. return err
  114. }
  115. }
  116. }
  117. }
  118. }
  119. return nil
  120. }
  121. func isImageMatched(s, t string) bool {
  122. // Tag values are limited to [a-zA-Z0-9_.-].
  123. pattern, _ := regexp.Compile("^" + t + "(:[a-zA-Z0-9_.-]*)?$")
  124. return pattern.MatchString(s)
  125. }
  126. // split separates and returns the name and tag parts
  127. // from the image string using either colon `:` or at `@` separators.
  128. // Note that the returned tag keeps its separator.
  129. func split(imageName string) (name string, tag string) {
  130. // check if image name contains a domain
  131. // if domain is present, ignore domain and check for `:`
  132. ic := -1
  133. if slashIndex := strings.Index(imageName, "/"); slashIndex < 0 {
  134. ic = strings.LastIndex(imageName, ":")
  135. } else {
  136. lastIc := strings.LastIndex(imageName[slashIndex:], ":")
  137. // set ic only if `:` is present
  138. if lastIc > 0 {
  139. ic = slashIndex + lastIc
  140. }
  141. }
  142. ia := strings.LastIndex(imageName, "@")
  143. if ic < 0 && ia < 0 {
  144. return imageName, ""
  145. }
  146. i := ic
  147. if ic < 0 {
  148. i = ia
  149. }
  150. name = imageName[:i]
  151. tag = imageName[i:]
  152. return
  153. }