prefixsuffixname.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 transformers
  14. import (
  15. "errors"
  16. "fmt"
  17. "sigs.k8s.io/kustomize/pkg/gvk"
  18. "sigs.k8s.io/kustomize/pkg/resmap"
  19. "sigs.k8s.io/kustomize/pkg/transformers/config"
  20. )
  21. // namePrefixSuffixTransformer contains the prefix, suffix, and the FieldSpecs
  22. // for each field needing a name prefix and suffix.
  23. type namePrefixSuffixTransformer struct {
  24. prefix string
  25. suffix string
  26. fieldSpecsToUse []config.FieldSpec
  27. fieldSpecsToSkip []config.FieldSpec
  28. }
  29. var _ Transformer = &namePrefixSuffixTransformer{}
  30. var prefixSuffixFieldSpecsToSkip = []config.FieldSpec{
  31. {
  32. Gvk: gvk.Gvk{Kind: "CustomResourceDefinition"},
  33. },
  34. }
  35. // NewNamePrefixSuffixTransformer makes a namePrefixSuffixTransformer.
  36. func NewNamePrefixSuffixTransformer(
  37. np, ns string, fieldSpecs []config.FieldSpec) (Transformer, error) {
  38. if len(np) == 0 && len(ns) == 0 {
  39. return NewNoOpTransformer(), nil
  40. }
  41. if fieldSpecs == nil {
  42. return nil, errors.New("fieldSpecs is not expected to be nil")
  43. }
  44. return &namePrefixSuffixTransformer{
  45. prefix: np,
  46. suffix: ns,
  47. fieldSpecsToUse: fieldSpecs,
  48. fieldSpecsToSkip: prefixSuffixFieldSpecsToSkip}, nil
  49. }
  50. // Transform prepends the name prefix and appends the name suffix.
  51. func (o *namePrefixSuffixTransformer) Transform(m resmap.ResMap) error {
  52. // Fill map "mf" with entries subject to name modification, and
  53. // delete these entries from "m", so that for now m retains only
  54. // the entries whose names will not be modified.
  55. mf := resmap.ResMap{}
  56. for id := range m {
  57. found := false
  58. for _, path := range o.fieldSpecsToSkip {
  59. if id.Gvk().IsSelected(&path.Gvk) {
  60. found = true
  61. break
  62. }
  63. }
  64. if !found {
  65. mf[id] = m[id]
  66. delete(m, id)
  67. }
  68. }
  69. for id := range mf {
  70. objMap := mf[id].Map()
  71. for _, path := range o.fieldSpecsToUse {
  72. if !id.Gvk().IsSelected(&path.Gvk) {
  73. continue
  74. }
  75. err := mutateField(
  76. objMap,
  77. path.PathSlice(),
  78. path.CreateIfNotPresent,
  79. o.addPrefixSuffix)
  80. if err != nil {
  81. return err
  82. }
  83. newId := id.CopyWithNewPrefixSuffix(o.prefix, o.suffix)
  84. m[newId] = mf[id]
  85. }
  86. }
  87. return nil
  88. }
  89. func (o *namePrefixSuffixTransformer) addPrefixSuffix(
  90. in interface{}) (interface{}, error) {
  91. s, ok := in.(string)
  92. if !ok {
  93. return nil, fmt.Errorf("%#v is expected to be %T", in, s)
  94. }
  95. return fmt.Sprintf("%s%s%s", o.prefix, s, o.suffix), nil
  96. }