crd.go 12 KB

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