validation.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. "encoding/json"
  16. "fmt"
  17. "math"
  18. apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  19. "sigs.k8s.io/controller-tools/pkg/markers"
  20. )
  21. const (
  22. SchemalessName = "kubebuilder:validation:Schemaless"
  23. )
  24. // ValidationMarkers lists all available markers that affect CRD schema generation,
  25. // except for the few that don't make sense as type-level markers (see FieldOnlyMarkers).
  26. // All markers start with `+kubebuilder:validation:`, and continue with their type name.
  27. // A copy is produced of all markers that describes types as well, for making types
  28. // reusable and writing complex validations on slice items.
  29. var ValidationMarkers = mustMakeAllWithPrefix("kubebuilder:validation", markers.DescribesField,
  30. // numeric markers
  31. Maximum(0),
  32. Minimum(0),
  33. ExclusiveMaximum(false),
  34. ExclusiveMinimum(false),
  35. MultipleOf(0),
  36. MinProperties(0),
  37. MaxProperties(0),
  38. // string markers
  39. MaxLength(0),
  40. MinLength(0),
  41. Pattern(""),
  42. // slice markers
  43. MaxItems(0),
  44. MinItems(0),
  45. UniqueItems(false),
  46. // general markers
  47. Enum(nil),
  48. Format(""),
  49. Type(""),
  50. XPreserveUnknownFields{},
  51. XEmbeddedResource{},
  52. XIntOrString{},
  53. XValidation{},
  54. )
  55. // FieldOnlyMarkers list field-specific validation markers (i.e. those markers that don't make
  56. // sense on a type, and thus aren't in ValidationMarkers).
  57. var FieldOnlyMarkers = []*definitionWithHelp{
  58. must(markers.MakeDefinition("kubebuilder:validation:Required", markers.DescribesField, struct{}{})).
  59. WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is required, if fields are optional by default.")),
  60. must(markers.MakeDefinition("kubebuilder:validation:Optional", markers.DescribesField, struct{}{})).
  61. WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is optional, if fields are required by default.")),
  62. must(markers.MakeDefinition("optional", markers.DescribesField, struct{}{})).
  63. WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is optional, if fields are required by default.")),
  64. must(markers.MakeDefinition("nullable", markers.DescribesField, Nullable{})).
  65. WithHelp(Nullable{}.Help()),
  66. must(markers.MakeAnyTypeDefinition("kubebuilder:default", markers.DescribesField, Default{})).
  67. WithHelp(Default{}.Help()),
  68. must(markers.MakeAnyTypeDefinition("kubebuilder:example", markers.DescribesField, Example{})).
  69. WithHelp(Example{}.Help()),
  70. must(markers.MakeDefinition("kubebuilder:validation:EmbeddedResource", markers.DescribesField, XEmbeddedResource{})).
  71. WithHelp(XEmbeddedResource{}.Help()),
  72. must(markers.MakeDefinition(SchemalessName, markers.DescribesField, Schemaless{})).
  73. WithHelp(Schemaless{}.Help()),
  74. }
  75. // ValidationIshMarkers are field-and-type markers that don't fall under the
  76. // :validation: prefix, and/or don't have a name that directly matches their
  77. // type.
  78. var ValidationIshMarkers = []*definitionWithHelp{
  79. must(markers.MakeDefinition("kubebuilder:pruning:PreserveUnknownFields", markers.DescribesField, XPreserveUnknownFields{})).
  80. WithHelp(XPreserveUnknownFields{}.Help()),
  81. must(markers.MakeDefinition("kubebuilder:pruning:PreserveUnknownFields", markers.DescribesType, XPreserveUnknownFields{})).
  82. WithHelp(XPreserveUnknownFields{}.Help()),
  83. }
  84. func init() {
  85. AllDefinitions = append(AllDefinitions, ValidationMarkers...)
  86. for _, def := range ValidationMarkers {
  87. newDef := *def.Definition
  88. // copy both parts so we don't change the definition
  89. typDef := definitionWithHelp{
  90. Definition: &newDef,
  91. Help: def.Help,
  92. }
  93. typDef.Target = markers.DescribesType
  94. AllDefinitions = append(AllDefinitions, &typDef)
  95. }
  96. AllDefinitions = append(AllDefinitions, FieldOnlyMarkers...)
  97. AllDefinitions = append(AllDefinitions, ValidationIshMarkers...)
  98. }
  99. // +controllertools:marker:generateHelp:category="CRD validation"
  100. // Maximum specifies the maximum numeric value that this field can have.
  101. type Maximum float64
  102. func (m Maximum) Value() float64 {
  103. return float64(m)
  104. }
  105. // +controllertools:marker:generateHelp:category="CRD validation"
  106. // Minimum specifies the minimum numeric value that this field can have. Negative numbers are supported.
  107. type Minimum float64
  108. func (m Minimum) Value() float64 {
  109. return float64(m)
  110. }
  111. // +controllertools:marker:generateHelp:category="CRD validation"
  112. // ExclusiveMinimum indicates that the minimum is "up to" but not including that value.
  113. type ExclusiveMinimum bool
  114. // +controllertools:marker:generateHelp:category="CRD validation"
  115. // ExclusiveMaximum indicates that the maximum is "up to" but not including that value.
  116. type ExclusiveMaximum bool
  117. // +controllertools:marker:generateHelp:category="CRD validation"
  118. // MultipleOf specifies that this field must have a numeric value that's a multiple of this one.
  119. type MultipleOf float64
  120. func (m MultipleOf) Value() float64 {
  121. return float64(m)
  122. }
  123. // +controllertools:marker:generateHelp:category="CRD validation"
  124. // MaxLength specifies the maximum length for this string.
  125. type MaxLength int
  126. // +controllertools:marker:generateHelp:category="CRD validation"
  127. // MinLength specifies the minimum length for this string.
  128. type MinLength int
  129. // +controllertools:marker:generateHelp:category="CRD validation"
  130. // Pattern specifies that this string must match the given regular expression.
  131. type Pattern string
  132. // +controllertools:marker:generateHelp:category="CRD validation"
  133. // MaxItems specifies the maximum length for this list.
  134. type MaxItems int
  135. // +controllertools:marker:generateHelp:category="CRD validation"
  136. // MinItems specifies the minimum length for this list.
  137. type MinItems int
  138. // +controllertools:marker:generateHelp:category="CRD validation"
  139. // UniqueItems specifies that all items in this list must be unique.
  140. type UniqueItems bool
  141. // +controllertools:marker:generateHelp:category="CRD validation"
  142. // MaxProperties restricts the number of keys in an object
  143. type MaxProperties int
  144. // +controllertools:marker:generateHelp:category="CRD validation"
  145. // MinProperties restricts the number of keys in an object
  146. type MinProperties int
  147. // +controllertools:marker:generateHelp:category="CRD validation"
  148. // Enum specifies that this (scalar) field is restricted to the *exact* values specified here.
  149. type Enum []interface{}
  150. // +controllertools:marker:generateHelp:category="CRD validation"
  151. // Format specifies additional "complex" formatting for this field.
  152. //
  153. // For example, a date-time field would be marked as "type: string" and
  154. // "format: date-time".
  155. type Format string
  156. // +controllertools:marker:generateHelp:category="CRD validation"
  157. // Type overrides the type for this field (which defaults to the equivalent of the Go type).
  158. //
  159. // This generally must be paired with custom serialization. For example, the
  160. // metav1.Time field would be marked as "type: string" and "format: date-time".
  161. type Type string
  162. // +controllertools:marker:generateHelp:category="CRD validation"
  163. // Nullable marks this field as allowing the "null" value.
  164. //
  165. // This is often not necessary, but may be helpful with custom serialization.
  166. type Nullable struct{}
  167. // +controllertools:marker:generateHelp:category="CRD validation"
  168. // Default sets the default value for this field.
  169. //
  170. // A default value will be accepted as any value valid for the
  171. // field. Formatting for common types include: boolean: `true`, string:
  172. // `Cluster`, numerical: `1.24`, array: `{1,2}`, object: `{policy:
  173. // "delete"}`). Defaults should be defined in pruned form, and only best-effort
  174. // validation will be performed. Full validation of a default requires
  175. // submission of the containing CRD to an apiserver.
  176. type Default struct {
  177. Value interface{}
  178. }
  179. // +controllertools:marker:generateHelp:category="CRD validation"
  180. // Example sets the example value for this field.
  181. //
  182. // An example value will be accepted as any value valid for the
  183. // field. Formatting for common types include: boolean: `true`, string:
  184. // `Cluster`, numerical: `1.24`, array: `{1,2}`, object: `{policy:
  185. // "delete"}`). Examples should be defined in pruned form, and only best-effort
  186. // validation will be performed. Full validation of an example requires
  187. // submission of the containing CRD to an apiserver.
  188. type Example struct {
  189. Value interface{}
  190. }
  191. // +controllertools:marker:generateHelp:category="CRD processing"
  192. // PreserveUnknownFields stops the apiserver from pruning fields which are not specified.
  193. //
  194. // By default the apiserver drops unknown fields from the request payload
  195. // during the decoding step. This marker stops the API server from doing so.
  196. // It affects fields recursively, but switches back to normal pruning behaviour
  197. // if nested properties or additionalProperties are specified in the schema.
  198. // This can either be true or undefined. False
  199. // is forbidden.
  200. //
  201. // NB: The kubebuilder:validation:XPreserveUnknownFields variant is deprecated
  202. // in favor of the kubebuilder:pruning:PreserveUnknownFields variant. They function
  203. // identically.
  204. type XPreserveUnknownFields struct{}
  205. // +controllertools:marker:generateHelp:category="CRD validation"
  206. // EmbeddedResource marks a fields as an embedded resource with apiVersion, kind and metadata fields.
  207. //
  208. // An embedded resource is a value that has apiVersion, kind and metadata fields.
  209. // They are validated implicitly according to the semantics of the currently
  210. // running apiserver. It is not necessary to add any additional schema for these
  211. // field, yet it is possible. This can be combined with PreserveUnknownFields.
  212. type XEmbeddedResource struct{}
  213. // +controllertools:marker:generateHelp:category="CRD validation"
  214. // IntOrString marks a fields as an IntOrString.
  215. //
  216. // This is required when applying patterns or other validations to an IntOrString
  217. // field. Knwon information about the type is applied during the collapse phase
  218. // and as such is not normally available during marker application.
  219. type XIntOrString struct{}
  220. // +controllertools:marker:generateHelp:category="CRD validation"
  221. // Schemaless marks a field as being a schemaless object.
  222. //
  223. // Schemaless objects are not introspected, so you must provide
  224. // any type and validation information yourself. One use for this
  225. // tag is for embedding fields that hold JSONSchema typed objects.
  226. // Because this field disables all type checking, it is recommended
  227. // to be used only as a last resort.
  228. type Schemaless struct{}
  229. func hasNumericType(schema *apiext.JSONSchemaProps) bool {
  230. return schema.Type == "integer" || schema.Type == "number"
  231. }
  232. func isIntegral(value float64) bool {
  233. return value == math.Trunc(value) && !math.IsNaN(value) && !math.IsInf(value, 0)
  234. }
  235. // +controllertools:marker:generateHelp:category="CRD validation"
  236. // XValidation marks a field as requiring a value for which a given
  237. // expression evaluates to true.
  238. //
  239. // This marker may be repeated to specify multiple expressions, all of
  240. // which must evaluate to true.
  241. type XValidation struct {
  242. Rule string
  243. Message string `marker:",optional"`
  244. }
  245. func (m Maximum) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  246. if !hasNumericType(schema) {
  247. return fmt.Errorf("must apply maximum to a numeric value, found %s", schema.Type)
  248. }
  249. if schema.Type == "integer" && !isIntegral(m.Value()) {
  250. return fmt.Errorf("cannot apply non-integral maximum validation (%v) to integer value", m.Value())
  251. }
  252. val := m.Value()
  253. schema.Maximum = &val
  254. return nil
  255. }
  256. func (m Minimum) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  257. if !hasNumericType(schema) {
  258. return fmt.Errorf("must apply minimum to a numeric value, found %s", schema.Type)
  259. }
  260. if schema.Type == "integer" && !isIntegral(m.Value()) {
  261. return fmt.Errorf("cannot apply non-integral minimum validation (%v) to integer value", m.Value())
  262. }
  263. val := m.Value()
  264. schema.Minimum = &val
  265. return nil
  266. }
  267. func (m ExclusiveMaximum) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  268. if !hasNumericType(schema) {
  269. return fmt.Errorf("must apply exclusivemaximum to a numeric value, found %s", schema.Type)
  270. }
  271. schema.ExclusiveMaximum = bool(m)
  272. return nil
  273. }
  274. func (m ExclusiveMinimum) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  275. if !hasNumericType(schema) {
  276. return fmt.Errorf("must apply exclusiveminimum to a numeric value, found %s", schema.Type)
  277. }
  278. schema.ExclusiveMinimum = bool(m)
  279. return nil
  280. }
  281. func (m MultipleOf) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  282. if !hasNumericType(schema) {
  283. return fmt.Errorf("must apply multipleof to a numeric value, found %s", schema.Type)
  284. }
  285. if schema.Type == "integer" && !isIntegral(m.Value()) {
  286. return fmt.Errorf("cannot apply non-integral multipleof validation (%v) to integer value", m.Value())
  287. }
  288. val := m.Value()
  289. schema.MultipleOf = &val
  290. return nil
  291. }
  292. func (m MaxLength) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  293. if schema.Type != "string" {
  294. return fmt.Errorf("must apply maxlength to a string")
  295. }
  296. val := int64(m)
  297. schema.MaxLength = &val
  298. return nil
  299. }
  300. func (m MinLength) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  301. if schema.Type != "string" {
  302. return fmt.Errorf("must apply minlength to a string")
  303. }
  304. val := int64(m)
  305. schema.MinLength = &val
  306. return nil
  307. }
  308. func (m Pattern) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  309. // Allow string types or IntOrStrings. An IntOrString will still
  310. // apply the pattern validation when a string is detected, the pattern
  311. // will not apply to ints though.
  312. if schema.Type != "string" && !schema.XIntOrString {
  313. return fmt.Errorf("must apply pattern to a `string` or `IntOrString`")
  314. }
  315. schema.Pattern = string(m)
  316. return nil
  317. }
  318. func (m MaxItems) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  319. if schema.Type != "array" {
  320. return fmt.Errorf("must apply maxitem to an array")
  321. }
  322. val := int64(m)
  323. schema.MaxItems = &val
  324. return nil
  325. }
  326. func (m MinItems) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  327. if schema.Type != "array" {
  328. return fmt.Errorf("must apply minitems to an array")
  329. }
  330. val := int64(m)
  331. schema.MinItems = &val
  332. return nil
  333. }
  334. func (m UniqueItems) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  335. if schema.Type != "array" {
  336. return fmt.Errorf("must apply uniqueitems to an array")
  337. }
  338. schema.UniqueItems = bool(m)
  339. return nil
  340. }
  341. func (m MinProperties) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  342. if schema.Type != "object" {
  343. return fmt.Errorf("must apply minproperties to an object")
  344. }
  345. val := int64(m)
  346. schema.MinProperties = &val
  347. return nil
  348. }
  349. func (m MaxProperties) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  350. if schema.Type != "object" {
  351. return fmt.Errorf("must apply maxproperties to an object")
  352. }
  353. val := int64(m)
  354. schema.MaxProperties = &val
  355. return nil
  356. }
  357. func (m Enum) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  358. // TODO(directxman12): this is a bit hacky -- we should
  359. // probably support AnyType better + using the schema structure
  360. vals := make([]apiext.JSON, len(m))
  361. for i, val := range m {
  362. // TODO(directxman12): check actual type with schema type?
  363. // if we're expecting a string, marshal the string properly...
  364. // NB(directxman12): we use json.Marshal to ensure we handle JSON escaping properly
  365. valMarshalled, err := json.Marshal(val)
  366. if err != nil {
  367. return err
  368. }
  369. vals[i] = apiext.JSON{Raw: valMarshalled}
  370. }
  371. schema.Enum = vals
  372. return nil
  373. }
  374. func (m Format) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  375. schema.Format = string(m)
  376. return nil
  377. }
  378. // NB(directxman12): we "typecheck" on target schema properties here,
  379. // which means the "Type" marker *must* be applied first.
  380. // TODO(directxman12): find a less hacky way to do this
  381. // (we could preserve ordering of markers, but that feels bad in its own right).
  382. func (m Type) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  383. schema.Type = string(m)
  384. return nil
  385. }
  386. func (m Type) ApplyFirst() {}
  387. func (m Nullable) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  388. schema.Nullable = true
  389. return nil
  390. }
  391. // Defaults are only valid CRDs created with the v1 API
  392. func (m Default) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  393. marshalledDefault, err := json.Marshal(m.Value)
  394. if err != nil {
  395. return err
  396. }
  397. if schema.Type == "array" && string(marshalledDefault) == "{}" {
  398. marshalledDefault = []byte("[]")
  399. }
  400. schema.Default = &apiext.JSON{Raw: marshalledDefault}
  401. return nil
  402. }
  403. func (m Example) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  404. marshalledExample, err := json.Marshal(m.Value)
  405. if err != nil {
  406. return err
  407. }
  408. schema.Example = &apiext.JSON{Raw: marshalledExample}
  409. return nil
  410. }
  411. func (m XPreserveUnknownFields) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  412. defTrue := true
  413. schema.XPreserveUnknownFields = &defTrue
  414. return nil
  415. }
  416. func (m XEmbeddedResource) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  417. schema.XEmbeddedResource = true
  418. return nil
  419. }
  420. // NB(JoelSpeed): we use this property in other markers here,
  421. // which means the "XIntOrString" marker *must* be applied first.
  422. func (m XIntOrString) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  423. schema.XIntOrString = true
  424. return nil
  425. }
  426. func (m XIntOrString) ApplyFirst() {}
  427. func (m XValidation) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
  428. schema.XValidations = append(schema.XValidations, apiext.ValidationRule{
  429. Rule: m.Rule,
  430. Message: m.Message,
  431. })
  432. return nil
  433. }