fieldspec.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 config
  14. import (
  15. "fmt"
  16. "strings"
  17. "sigs.k8s.io/kustomize/pkg/gvk"
  18. )
  19. // FieldSpec completely specifies a kustomizable field in
  20. // an unstructured representation of a k8s API object.
  21. // It helps define the operands of transformations.
  22. //
  23. // For example, a directive to add a common label to objects
  24. // will need to know that a 'Deployment' object (in API group
  25. // 'apps', any version) can have labels at field path
  26. // 'spec/template/metadata/labels', and further that it is OK
  27. // (or not OK) to add that field path to the object if the
  28. // field path doesn't exist already.
  29. //
  30. // This would look like
  31. // {
  32. // group: apps
  33. // kind: Deployment
  34. // path: spec/template/metadata/labels
  35. // create: true
  36. // }
  37. type FieldSpec struct {
  38. gvk.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"`
  39. Path string `json:"path,omitempty" yaml:"path,omitempty"`
  40. CreateIfNotPresent bool `json:"create,omitempty" yaml:"create,omitempty"`
  41. }
  42. const (
  43. escapedForwardSlash = "\\/"
  44. tempSlashReplacement = "???"
  45. )
  46. func (fs FieldSpec) String() string {
  47. return fmt.Sprintf(
  48. "%s:%v:%s", fs.Gvk.String(), fs.CreateIfNotPresent, fs.Path)
  49. }
  50. // If true, the primary key is the same, but other fields might not be.
  51. func (fs FieldSpec) effectivelyEquals(other FieldSpec) bool {
  52. return fs.IsSelected(&other.Gvk) && fs.Path == other.Path
  53. }
  54. // PathSlice converts the path string to a slice of strings,
  55. // separated by a '/'. Forward slash can be contained in a
  56. // fieldname. such as ingress.kubernetes.io/auth-secret in
  57. // Ingress annotations. To deal with this special case, the
  58. // path to this field should be formatted as
  59. //
  60. // metadata/annotations/ingress.kubernetes.io\/auth-secret
  61. //
  62. // Then PathSlice will return
  63. //
  64. // []string{
  65. // "metadata",
  66. // "annotations",
  67. // "ingress.auth-secretkubernetes.io/auth-secret"
  68. // }
  69. func (fs FieldSpec) PathSlice() []string {
  70. if !strings.Contains(fs.Path, escapedForwardSlash) {
  71. return strings.Split(fs.Path, "/")
  72. }
  73. s := strings.Replace(fs.Path, escapedForwardSlash, tempSlashReplacement, -1)
  74. paths := strings.Split(s, "/")
  75. var result []string
  76. for _, path := range paths {
  77. result = append(result, strings.Replace(path, tempSlashReplacement, "/", -1))
  78. }
  79. return result
  80. }
  81. type fsSlice []FieldSpec
  82. func (s fsSlice) Len() int { return len(s) }
  83. func (s fsSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  84. func (s fsSlice) Less(i, j int) bool {
  85. return s[i].Gvk.IsLessThan(s[j].Gvk)
  86. }
  87. // mergeAll merges the argument into this, returning the result.
  88. // Items already present are ignored.
  89. // Items that conflict (primary key matches, but remain data differs)
  90. // result in an error.
  91. func (s fsSlice) mergeAll(incoming fsSlice) (result fsSlice, err error) {
  92. result = s
  93. for _, x := range incoming {
  94. result, err = result.mergeOne(x)
  95. if err != nil {
  96. return nil, err
  97. }
  98. }
  99. return result, nil
  100. }
  101. // mergeOne merges the argument into this, returning the result.
  102. // If the item's primary key is already present, and there are no
  103. // conflicts, it is ignored (we don't want duplicates).
  104. // If there is a conflict, the merge fails.
  105. func (s fsSlice) mergeOne(x FieldSpec) (fsSlice, error) {
  106. i := s.index(x)
  107. if i > -1 {
  108. // It's already there.
  109. if s[i].CreateIfNotPresent != x.CreateIfNotPresent {
  110. return nil, fmt.Errorf("conflicting fieldspecs")
  111. }
  112. return s, nil
  113. }
  114. return append(s, x), nil
  115. }
  116. func (s fsSlice) index(fs FieldSpec) int {
  117. for i, x := range s {
  118. if x.effectivelyEquals(fs) {
  119. return i
  120. }
  121. }
  122. return -1
  123. }