gvk.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 gvk
  14. import (
  15. "strings"
  16. )
  17. // Gvk identifies a Kubernetes API type.
  18. // https://github.com/kubernetes/community/blob/master/contributors/design-proposals/api-machinery/api-group.md
  19. type Gvk struct {
  20. Group string `json:"group,omitempty" yaml:"group,omitempty"`
  21. Version string `json:"version,omitempty" yaml:"version,omitempty"`
  22. Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
  23. }
  24. // FromKind makes a Gvk with only the kind specified.
  25. func FromKind(k string) Gvk {
  26. return Gvk{
  27. Kind: k,
  28. }
  29. }
  30. // Values that are brief but meaningful in logs.
  31. const (
  32. noGroup = "~G"
  33. noVersion = "~V"
  34. noKind = "~K"
  35. separator = "_"
  36. )
  37. // String returns a string representation of the GVK.
  38. func (x Gvk) String() string {
  39. g := x.Group
  40. if g == "" {
  41. g = noGroup
  42. }
  43. v := x.Version
  44. if v == "" {
  45. v = noVersion
  46. }
  47. k := x.Kind
  48. if k == "" {
  49. k = noKind
  50. }
  51. return strings.Join([]string{g, v, k}, separator)
  52. }
  53. // Equals returns true if the Gvk's have equal fields.
  54. func (x Gvk) Equals(o Gvk) bool {
  55. return x.Group == o.Group && x.Version == o.Version && x.Kind == o.Kind
  56. }
  57. // An attempt to order things to help k8s, e.g.
  58. // a Service should come before things that refer to it.
  59. // Namespace should be first.
  60. // In some cases order just specified to provide determinism.
  61. var order = []string{
  62. "Namespace",
  63. "StorageClass",
  64. "CustomResourceDefinition",
  65. "MutatingWebhookConfiguration",
  66. "ValidatingWebhookConfiguration",
  67. "ServiceAccount",
  68. "Role",
  69. "ClusterRole",
  70. "RoleBinding",
  71. "ClusterRoleBinding",
  72. "ConfigMap",
  73. "Secret",
  74. "Service",
  75. "Deployment",
  76. "StatefulSet",
  77. "CronJob",
  78. "PodDisruptionBudget",
  79. }
  80. var typeOrders = func() map[string]int {
  81. m := map[string]int{}
  82. for i, n := range order {
  83. m[n] = i
  84. }
  85. return m
  86. }()
  87. // IsLessThan returns true if self is less than the argument.
  88. func (x Gvk) IsLessThan(o Gvk) bool {
  89. indexI, foundI := typeOrders[x.Kind]
  90. indexJ, foundJ := typeOrders[o.Kind]
  91. if foundI && foundJ {
  92. if indexI != indexJ {
  93. return indexI < indexJ
  94. }
  95. }
  96. if foundI && !foundJ {
  97. return true
  98. }
  99. if !foundI && foundJ {
  100. return false
  101. }
  102. return x.String() < o.String()
  103. }
  104. // IsSelected returns true if `selector` selects `x`; otherwise, false.
  105. // If `selector` and `x` are the same, return true.
  106. // If `selector` is nil, it is considered a wildcard match, returning true.
  107. // If selector fields are empty, they are considered wildcards matching
  108. // anything in the corresponding fields, e.g.
  109. //
  110. // this item:
  111. // <Group: "extensions", Version: "v1beta1", Kind: "Deployment">
  112. //
  113. // is selected by
  114. // <Group: "", Version: "", Kind: "Deployment">
  115. //
  116. // but rejected by
  117. // <Group: "apps", Version: "", Kind: "Deployment">
  118. //
  119. func (x Gvk) IsSelected(selector *Gvk) bool {
  120. if selector == nil {
  121. return true
  122. }
  123. if len(selector.Group) > 0 {
  124. if x.Group != selector.Group {
  125. return false
  126. }
  127. }
  128. if len(selector.Version) > 0 {
  129. if x.Version != selector.Version {
  130. return false
  131. }
  132. }
  133. if len(selector.Kind) > 0 {
  134. if x.Kind != selector.Kind {
  135. return false
  136. }
  137. }
  138. return true
  139. }
  140. var clusterLevelKinds = []string{
  141. "APIService",
  142. "ClusterRoleBinding",
  143. "ClusterRole",
  144. "CustomResourceDefinition",
  145. "Namespace",
  146. "PersistentVolume",
  147. }
  148. // IsClusterKind returns true if x is a cluster-level Gvk
  149. func (x Gvk) IsClusterKind() bool {
  150. for _, k := range clusterLevelKinds {
  151. if k == x.Kind {
  152. return true
  153. }
  154. }
  155. return false
  156. }
  157. // ClusterLevelGvks returns a slice of cluster-level Gvks
  158. func ClusterLevelGvks() []Gvk {
  159. var result []Gvk
  160. for _, k := range clusterLevelKinds {
  161. result = append(result, Gvk{Kind: k})
  162. }
  163. return result
  164. }