resaccumulator.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright 2018 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 target
  14. import (
  15. "fmt"
  16. "log"
  17. "strings"
  18. "sigs.k8s.io/kustomize/pkg/resid"
  19. "sigs.k8s.io/kustomize/pkg/resmap"
  20. "sigs.k8s.io/kustomize/pkg/transformers"
  21. "sigs.k8s.io/kustomize/pkg/transformers/config"
  22. "sigs.k8s.io/kustomize/pkg/types"
  23. )
  24. // ResAccumulator accumulates resources and the rules
  25. // used to customize those resources.
  26. // TODO(monopole): Move to "accumulator" package and make members private.
  27. // This will make a better separation between KustTarget, which should
  28. // be mainly concerned with data loading, and this class, which could
  29. // become the home of all transformation data and logic.
  30. type ResAccumulator struct {
  31. resMap resmap.ResMap
  32. tConfig *config.TransformerConfig
  33. varSet types.VarSet
  34. }
  35. func MakeEmptyAccumulator() *ResAccumulator {
  36. ra := &ResAccumulator{}
  37. ra.resMap = make(resmap.ResMap)
  38. ra.tConfig = &config.TransformerConfig{}
  39. ra.varSet = types.VarSet{}
  40. return ra
  41. }
  42. // ResMap returns a copy of the internal resMap.
  43. func (ra *ResAccumulator) ResMap() resmap.ResMap {
  44. result := make(resmap.ResMap)
  45. for k, v := range ra.resMap {
  46. result[k] = v
  47. }
  48. return result
  49. }
  50. // Vars returns a copy of underlying vars.
  51. func (ra *ResAccumulator) Vars() []types.Var {
  52. return ra.varSet.Set()
  53. }
  54. func (ra *ResAccumulator) MergeResourcesWithErrorOnIdCollision(
  55. resources resmap.ResMap) (err error) {
  56. ra.resMap, err = resmap.MergeWithErrorOnIdCollision(
  57. resources, ra.resMap)
  58. return err
  59. }
  60. func (ra *ResAccumulator) MergeResourcesWithOverride(
  61. resources resmap.ResMap) (err error) {
  62. ra.resMap, err = resmap.MergeWithOverride(
  63. ra.resMap, resources)
  64. return err
  65. }
  66. func (ra *ResAccumulator) MergeConfig(
  67. tConfig *config.TransformerConfig) (err error) {
  68. ra.tConfig, err = ra.tConfig.Merge(tConfig)
  69. return err
  70. }
  71. func (ra *ResAccumulator) MergeVars(incoming []types.Var) error {
  72. return ra.varSet.MergeSlice(incoming)
  73. }
  74. func (ra *ResAccumulator) MergeAccumulator(other *ResAccumulator) (err error) {
  75. err = ra.MergeResourcesWithErrorOnIdCollision(other.resMap)
  76. if err != nil {
  77. return err
  78. }
  79. err = ra.MergeConfig(other.tConfig)
  80. if err != nil {
  81. return err
  82. }
  83. return ra.varSet.MergeSet(&other.varSet)
  84. }
  85. // makeVarReplacementMap returns a map of Var names to
  86. // their final values. The values are strings intended
  87. // for substitution wherever the $(var.Name) occurs.
  88. func (ra *ResAccumulator) makeVarReplacementMap() (map[string]string, error) {
  89. result := map[string]string{}
  90. for _, v := range ra.Vars() {
  91. matched := ra.resMap.GetMatchingIds(
  92. resid.NewResId(v.ObjRef.GVK(), v.ObjRef.Name).GvknEquals)
  93. if len(matched) > 1 {
  94. return nil, fmt.Errorf(
  95. "found %d resId matches for var %s "+
  96. "(unable to disambiguate)",
  97. len(matched), v)
  98. }
  99. if len(matched) == 1 {
  100. s, err := ra.resMap[matched[0]].GetFieldValue(v.FieldRef.FieldPath)
  101. if err != nil {
  102. return nil, fmt.Errorf(
  103. "field specified in var '%v' "+
  104. "not found in corresponding resource", v)
  105. }
  106. result[v.Name] = s
  107. } else {
  108. return nil, fmt.Errorf(
  109. "var '%v' cannot be mapped to a field "+
  110. "in the set of known resources", v)
  111. }
  112. }
  113. return result, nil
  114. }
  115. func (ra *ResAccumulator) Transform(t transformers.Transformer) error {
  116. return t.Transform(ra.resMap)
  117. }
  118. func (ra *ResAccumulator) ResolveVars() error {
  119. replacementMap, err := ra.makeVarReplacementMap()
  120. if err != nil {
  121. return err
  122. }
  123. if len(replacementMap) == 0 {
  124. return nil
  125. }
  126. t := transformers.NewRefVarTransformer(
  127. replacementMap, ra.tConfig.VarReference)
  128. err = ra.Transform(t)
  129. if len(t.UnusedVars()) > 0 {
  130. log.Printf(
  131. "well-defined vars that were never replaced: %s\n",
  132. strings.Join(t.UnusedVars(), ","))
  133. }
  134. return err
  135. }
  136. func (ra *ResAccumulator) FixBackReferences() (err error) {
  137. if ra.tConfig.NameReference == nil {
  138. return nil
  139. }
  140. return ra.Transform(transformers.NewNameReferenceTransformer(
  141. ra.tConfig.NameReference))
  142. }