crd.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. Copyright 2019 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 markers
  14. import (
  15. "fmt"
  16. apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  17. "sigs.k8s.io/controller-tools/pkg/markers"
  18. )
  19. // CRDMarkers lists all markers that directly modify the CRD (not validation
  20. // schemas).
  21. var CRDMarkers = []*definitionWithHelp{
  22. // TODO(directxman12): more detailed help
  23. must(markers.MakeDefinition("kubebuilder:subresource:status", markers.DescribesType, SubresourceStatus{})).
  24. WithHelp(SubresourceStatus{}.Help()),
  25. must(markers.MakeDefinition("kubebuilder:subresource:scale", markers.DescribesType, SubresourceScale{})).
  26. WithHelp(SubresourceScale{}.Help()),
  27. must(markers.MakeDefinition("kubebuilder:printcolumn", markers.DescribesType, PrintColumn{})).
  28. WithHelp(PrintColumn{}.Help()),
  29. must(markers.MakeDefinition("kubebuilder:resource", markers.DescribesType, Resource{})).
  30. WithHelp(Resource{}.Help()),
  31. must(markers.MakeDefinition("kubebuilder:storageversion", markers.DescribesType, StorageVersion{})).
  32. WithHelp(StorageVersion{}.Help()),
  33. must(markers.MakeDefinition("kubebuilder:skipversion", markers.DescribesType, SkipVersion{})).
  34. WithHelp(SkipVersion{}.Help()),
  35. must(markers.MakeDefinition("kubebuilder:unservedversion", markers.DescribesType, UnservedVersion{})).
  36. WithHelp(UnservedVersion{}.Help()),
  37. must(markers.MakeDefinition("kubebuilder:deprecatedversion", markers.DescribesType, DeprecatedVersion{})).
  38. WithHelp(DeprecatedVersion{}.Help()),
  39. }
  40. // TODO: categories and singular used to be annotations types
  41. // TODO: doc
  42. func init() {
  43. AllDefinitions = append(AllDefinitions, CRDMarkers...)
  44. }
  45. // +controllertools:marker:generateHelp:category=CRD
  46. // SubresourceStatus enables the "/status" subresource on a CRD.
  47. type SubresourceStatus struct{}
  48. func (s SubresourceStatus) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
  49. var subresources *apiext.CustomResourceSubresources
  50. for i := range crd.Versions {
  51. ver := &crd.Versions[i]
  52. if ver.Name != version {
  53. continue
  54. }
  55. if ver.Subresources == nil {
  56. ver.Subresources = &apiext.CustomResourceSubresources{}
  57. }
  58. subresources = ver.Subresources
  59. break
  60. }
  61. if subresources == nil {
  62. return fmt.Errorf("status subresource applied to version %q not in CRD", version)
  63. }
  64. subresources.Status = &apiext.CustomResourceSubresourceStatus{}
  65. return nil
  66. }
  67. // +controllertools:marker:generateHelp:category=CRD
  68. // SubresourceScale enables the "/scale" subresource on a CRD.
  69. type SubresourceScale struct {
  70. // marker names are leftover legacy cruft
  71. // SpecPath specifies the jsonpath to the replicas field for the scale's spec.
  72. SpecPath string `marker:"specpath"`
  73. // StatusPath specifies the jsonpath to the replicas field for the scale's status.
  74. StatusPath string `marker:"statuspath"`
  75. // SelectorPath specifies the jsonpath to the pod label selector field for the scale's status.
  76. //
  77. // The selector field must be the *string* form (serialized form) of a selector.
  78. // Setting a pod label selector is necessary for your type to work with the HorizontalPodAutoscaler.
  79. SelectorPath *string `marker:"selectorpath"`
  80. }
  81. func (s SubresourceScale) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
  82. var subresources *apiext.CustomResourceSubresources
  83. for i := range crd.Versions {
  84. ver := &crd.Versions[i]
  85. if ver.Name != version {
  86. continue
  87. }
  88. if ver.Subresources == nil {
  89. ver.Subresources = &apiext.CustomResourceSubresources{}
  90. }
  91. subresources = ver.Subresources
  92. break
  93. }
  94. if subresources == nil {
  95. return fmt.Errorf("scale subresource applied to version %q not in CRD", version)
  96. }
  97. subresources.Scale = &apiext.CustomResourceSubresourceScale{
  98. SpecReplicasPath: s.SpecPath,
  99. StatusReplicasPath: s.StatusPath,
  100. LabelSelectorPath: s.SelectorPath,
  101. }
  102. return nil
  103. }
  104. // +controllertools:marker:generateHelp:category=CRD
  105. // StorageVersion marks this version as the "storage version" for the CRD for conversion.
  106. //
  107. // When conversion is enabled for a CRD (i.e. it's not a trivial-versions/single-version CRD),
  108. // one version is set as the "storage version" to be stored in etcd. Attempting to store any
  109. // other version will result in conversion to the storage version via a conversion webhook.
  110. type StorageVersion struct{}
  111. func (s StorageVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
  112. if version == "" {
  113. // single-version, do nothing
  114. return nil
  115. }
  116. // multi-version
  117. for i := range crd.Versions {
  118. ver := &crd.Versions[i]
  119. if ver.Name != version {
  120. continue
  121. }
  122. ver.Storage = true
  123. break
  124. }
  125. return nil
  126. }
  127. // +controllertools:marker:generateHelp:category=CRD
  128. // SkipVersion removes the particular version of the CRD from the CRDs spec.
  129. //
  130. // This is useful if you need to skip generating and listing version entries
  131. // for 'internal' resource versions, which typically exist if using the
  132. // Kubernetes upstream conversion-gen tool.
  133. type SkipVersion struct{}
  134. func (s SkipVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
  135. if version == "" {
  136. // single-version, this is an invalid state
  137. return fmt.Errorf("cannot skip a version if there is only a single version")
  138. }
  139. var versions []apiext.CustomResourceDefinitionVersion
  140. // multi-version
  141. for i := range crd.Versions {
  142. ver := crd.Versions[i]
  143. if ver.Name == version {
  144. // skip the skipped version
  145. continue
  146. }
  147. versions = append(versions, ver)
  148. }
  149. crd.Versions = versions
  150. return nil
  151. }
  152. // +controllertools:marker:generateHelp:category=CRD
  153. // PrintColumn adds a column to "kubectl get" output for this CRD.
  154. type PrintColumn struct {
  155. // Name specifies the name of the column.
  156. Name string
  157. // Type indicates the type of the column.
  158. //
  159. // It may be any OpenAPI data type listed at
  160. // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types.
  161. Type string
  162. // JSONPath specifies the jsonpath expression used to extract the value of the column.
  163. JSONPath string `marker:"JSONPath"` // legacy cruft
  164. // Description specifies the help/description for this column.
  165. Description string `marker:",optional"`
  166. // Format specifies the format of the column.
  167. //
  168. // It may be any OpenAPI data format corresponding to the type, listed at
  169. // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types.
  170. Format string `marker:",optional"`
  171. // Priority indicates how important it is that this column be displayed.
  172. //
  173. // Lower priority (*higher* numbered) columns will be hidden if the terminal
  174. // width is too small.
  175. Priority int32 `marker:",optional"`
  176. }
  177. func (s PrintColumn) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
  178. var columns *[]apiext.CustomResourceColumnDefinition
  179. for i := range crd.Versions {
  180. ver := &crd.Versions[i]
  181. if ver.Name != version {
  182. continue
  183. }
  184. if ver.Subresources == nil {
  185. ver.Subresources = &apiext.CustomResourceSubresources{}
  186. }
  187. columns = &ver.AdditionalPrinterColumns
  188. break
  189. }
  190. if columns == nil {
  191. return fmt.Errorf("printer columns applied to version %q not in CRD", version)
  192. }
  193. *columns = append(*columns, apiext.CustomResourceColumnDefinition{
  194. Name: s.Name,
  195. Type: s.Type,
  196. JSONPath: s.JSONPath,
  197. Description: s.Description,
  198. Format: s.Format,
  199. Priority: s.Priority,
  200. })
  201. return nil
  202. }
  203. // +controllertools:marker:generateHelp:category=CRD
  204. // Resource configures naming and scope for a CRD.
  205. type Resource struct {
  206. // Path specifies the plural "resource" for this CRD.
  207. //
  208. // It generally corresponds to a plural, lower-cased version of the Kind.
  209. // See https://book.kubebuilder.io/cronjob-tutorial/gvks.html.
  210. Path string `marker:",optional"`
  211. // ShortName specifies aliases for this CRD.
  212. //
  213. // Short names are often used when people have work with your resource
  214. // over and over again. For instance, "rs" for "replicaset" or
  215. // "crd" for customresourcedefinition.
  216. ShortName []string `marker:",optional"`
  217. // Categories specifies which group aliases this resource is part of.
  218. //
  219. // Group aliases are used to work with groups of resources at once.
  220. // The most common one is "all" which covers about a third of the base
  221. // resources in Kubernetes, and is generally used for "user-facing" resources.
  222. Categories []string `marker:",optional"`
  223. // Singular overrides the singular form of your resource.
  224. //
  225. // The singular form is otherwise defaulted off the plural (path).
  226. Singular string `marker:",optional"`
  227. // Scope overrides the scope of the CRD (Cluster vs Namespaced).
  228. //
  229. // Scope defaults to "Namespaced". Cluster-scoped ("Cluster") resources
  230. // don't exist in namespaces.
  231. Scope string `marker:",optional"`
  232. }
  233. func (s Resource) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
  234. if s.Path != "" {
  235. crd.Names.Plural = s.Path
  236. }
  237. if s.Singular != "" {
  238. crd.Names.Singular = s.Singular
  239. }
  240. crd.Names.ShortNames = s.ShortName
  241. crd.Names.Categories = s.Categories
  242. switch s.Scope {
  243. case "":
  244. crd.Scope = apiext.NamespaceScoped
  245. default:
  246. crd.Scope = apiext.ResourceScope(s.Scope)
  247. }
  248. return nil
  249. }
  250. // +controllertools:marker:generateHelp:category=CRD
  251. // UnservedVersion does not serve this version.
  252. //
  253. // This is useful if you need to drop support for a version in favor of a newer version.
  254. type UnservedVersion struct{}
  255. func (s UnservedVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
  256. for i := range crd.Versions {
  257. ver := &crd.Versions[i]
  258. if ver.Name != version {
  259. continue
  260. }
  261. ver.Served = false
  262. break
  263. }
  264. return nil
  265. }
  266. // NB(directxman12): singular was historically distinct, so we keep it here for backwards compat
  267. // +controllertools:marker:generateHelp:category=CRD
  268. // DeprecatedVersion marks this version as deprecated.
  269. type DeprecatedVersion struct {
  270. // Warning message to be shown on the deprecated version
  271. Warning *string `marker:",optional"`
  272. }
  273. func (s DeprecatedVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
  274. if version == "" {
  275. // single-version, do nothing
  276. return nil
  277. }
  278. // multi-version
  279. for i := range crd.Versions {
  280. ver := &crd.Versions[i]
  281. if ver.Name != version {
  282. continue
  283. }
  284. ver.Deprecated = true
  285. ver.DeprecationWarning = s.Warning
  286. break
  287. }
  288. return nil
  289. }