resid.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 resid
  14. import (
  15. "strings"
  16. "sigs.k8s.io/kustomize/pkg/gvk"
  17. )
  18. // ResId is an immutable identifier of a k8s resource object.
  19. type ResId struct {
  20. // Gvk of the resource.
  21. gvKind gvk.Gvk
  22. // name of the resource before transformation.
  23. name string
  24. // namePrefix of the resource.
  25. // An untransformed resource has no prefix.
  26. // A fully transformed resource has an arbitrary
  27. // number of prefixes concatenated together.
  28. prefix string
  29. // nameSuffix of the resource.
  30. // An untransformed resource has no suffix.
  31. // A fully transformed resource has an arbitrary
  32. // number of suffixes concatenated together.
  33. suffix string
  34. // Namespace the resource belongs to.
  35. // An untransformed resource has no namespace.
  36. // A fully transformed resource has the namespace
  37. // from the top most overlay.
  38. namespace string
  39. }
  40. // NewResIdWithPrefixSuffixNamespace creates new resource identifier with a prefix, suffix and a namespace
  41. func NewResIdWithPrefixSuffixNamespace(k gvk.Gvk, n, p, s, ns string) ResId {
  42. return ResId{gvKind: k, name: n, prefix: p, suffix: s, namespace: ns}
  43. }
  44. // NewResIdWithPrefixNamespace creates new resource identifier with a prefix and a namespace
  45. func NewResIdWithPrefixNamespace(k gvk.Gvk, n, p, ns string) ResId {
  46. return ResId{gvKind: k, name: n, prefix: p, namespace: ns}
  47. }
  48. // NewResIdWithSuffixNamespace creates new resource identifier with a suffix and a namespace
  49. func NewResIdWithSuffixNamespace(k gvk.Gvk, n, s, ns string) ResId {
  50. return ResId{gvKind: k, name: n, suffix: s, namespace: ns}
  51. }
  52. // NewResIdWithPrefixSuffix creates new resource identifier with a prefix and suffix
  53. func NewResIdWithPrefixSuffix(k gvk.Gvk, n, p, s string) ResId {
  54. return ResId{gvKind: k, name: n, prefix: p, suffix: s}
  55. }
  56. // NewResId creates new resource identifier
  57. func NewResId(k gvk.Gvk, n string) ResId {
  58. return ResId{gvKind: k, name: n}
  59. }
  60. // NewResIdKindOnly creates new resource identifier
  61. func NewResIdKindOnly(k string, n string) ResId {
  62. return ResId{gvKind: gvk.FromKind(k), name: n}
  63. }
  64. const (
  65. noNamespace = "~X"
  66. noPrefix = "~P"
  67. noName = "~N"
  68. noSuffix = "~S"
  69. separator = "|"
  70. )
  71. // String of ResId based on GVK, name and prefix
  72. func (n ResId) String() string {
  73. ns := n.namespace
  74. if ns == "" {
  75. ns = noNamespace
  76. }
  77. p := n.prefix
  78. if p == "" {
  79. p = noPrefix
  80. }
  81. nm := n.name
  82. if nm == "" {
  83. nm = noName
  84. }
  85. s := n.suffix
  86. if s == "" {
  87. s = noSuffix
  88. }
  89. return strings.Join(
  90. []string{n.gvKind.String(), ns, p, nm, s}, separator)
  91. }
  92. // GvknString of ResId based on GVK and name
  93. func (n ResId) GvknString() string {
  94. return n.gvKind.String() + separator + n.name
  95. }
  96. // GvknEquals returns true if the other id matches
  97. // Group/Version/Kind/name.
  98. func (n ResId) GvknEquals(id ResId) bool {
  99. return n.name == id.name && n.gvKind.Equals(id.gvKind)
  100. }
  101. // NsGvknEquals returns true if the other id matches
  102. // namespace/Group/Version/Kind/name.
  103. func (n ResId) NsGvknEquals(id ResId) bool {
  104. return n.namespace == id.namespace && n.GvknEquals(id)
  105. }
  106. // Gvk returns Group/Version/Kind of the resource.
  107. func (n ResId) Gvk() gvk.Gvk {
  108. return n.gvKind
  109. }
  110. // Name returns resource name.
  111. func (n ResId) Name() string {
  112. return n.name
  113. }
  114. // Namespace returns resource namespace.
  115. func (n ResId) Namespace() string {
  116. return n.namespace
  117. }
  118. // CopyWithNewPrefixSuffix make a new copy from current ResId
  119. // and append a new prefix and suffix
  120. func (n ResId) CopyWithNewPrefixSuffix(p, s string) ResId {
  121. result := n
  122. if p != "" {
  123. result.prefix = n.concatPrefix(p)
  124. }
  125. if s != "" {
  126. result.suffix = n.concatSuffix(s)
  127. }
  128. return result
  129. }
  130. // CopyWithNewNamespace make a new copy from current ResId and set a new namespace
  131. func (n ResId) CopyWithNewNamespace(ns string) ResId {
  132. result := n
  133. result.namespace = ns
  134. return result
  135. }
  136. // HasSameLeftmostPrefix check if two ResIds have the same
  137. // left most prefix.
  138. func (n ResId) HasSameLeftmostPrefix(id ResId) bool {
  139. prefixes1 := n.prefixList()
  140. prefixes2 := id.prefixList()
  141. return prefixes1[0] == prefixes2[0]
  142. }
  143. // HasSameRightmostSuffix check if two ResIds have the same
  144. // right most suffix.
  145. func (n ResId) HasSameRightmostSuffix(id ResId) bool {
  146. suffixes1 := n.suffixList()
  147. suffixes2 := id.suffixList()
  148. return suffixes1[len(suffixes1)-1] == suffixes2[len(suffixes2)-1]
  149. }
  150. func (n ResId) concatPrefix(p string) string {
  151. if p == "" {
  152. return n.prefix
  153. }
  154. if n.prefix == "" {
  155. return p
  156. }
  157. return p + ":" + n.prefix
  158. }
  159. func (n ResId) concatSuffix(s string) string {
  160. if s == "" {
  161. return n.suffix
  162. }
  163. if n.suffix == "" {
  164. return s
  165. }
  166. return n.suffix + ":" + s
  167. }
  168. func (n ResId) prefixList() []string {
  169. return strings.Split(n.prefix, ":")
  170. }
  171. func (n ResId) suffixList() []string {
  172. return strings.Split(n.suffix, ":")
  173. }