Skip to content

Instantly share code, notes, and snippets.

@liggitt
Last active November 15, 2021 22:07
Show Gist options
  • Save liggitt/5bb0ece637e560ec906ee6a0786eb3bd to your computer and use it in GitHub Desktop.
Save liggitt/5bb0ece637e560ec906ee6a0786eb3bd to your computer and use it in GitHub Desktop.
# Apply like this to bypass client-side validation and exercise server-side validation:
#
# kubectl apply -f cel-type-mismatch.yaml --validate=false
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: mismatches.example.com
spec:
group: example.com
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
a:
type: array
items:
type: string
x-kubernetes-validations:
- rule: self != []
s:
type: string
x-kubernetes-validations:
- rule: self != ""
i:
type: integer
x-kubernetes-validations:
- rule: self != 0
"n":
type: number
x-kubernetes-validations:
- rule: self != 0.0
b:
type: boolean
x-kubernetes-validations:
- rule: self != false
m:
type: object
additionalProperties:
type: string
x-kubernetes-validations:
- rule: self != {}
names:
singular: mismatch
plural: mismatches
kind: Mismatch
listKind: MismatchList
---
apiVersion: example.com/v1
kind: Mismatch
metadata:
name: valid1
a: ["a"]
s: "a"
i: 1
"n": 1.0 # this switches to 1 in yaml --> json conversion and gets parsed as an int64
b: true
m: {"a":"a"}
---
apiVersion: example.com/v1
kind: Mismatch
metadata:
name: valid2
a: ["a"]
s: "a"
i: 1
"n": 1.5
b: true
m: {"a":"a"}
---
apiVersion: example.com/v1
kind: Mismatch
metadata:
name: mismatch1
a: []
s: ""
i: 0
"n": 0.0 # this switches to 0 in yaml --> json conversion and gets parsed as an int64
b: false
m: {}
---
apiVersion: example.com/v1
kind: Mismatch
metadata:
name: array
a: ["a"]
s: ["a"]
i: ["a"]
"n": ["a"]
b: ["a"]
m: ["a"]
---
apiVersion: example.com/v1
kind: Mismatch
metadata:
name: string
a: "a"
s: "a"
i: "a"
"n": "a"
b: "a"
m: "a"
---
apiVersion: example.com/v1
kind: Mismatch
metadata:
name: int
a: 1
s: 1
i: 1
"n": 1
b: 1
m: 1
---
apiVersion: example.com/v1
kind: Mismatch
metadata:
name: number
a: 1.5
s: 1.5
i: 1.5
"n": 1.5
b: 1.5
m: 1.5
---
apiVersion: example.com/v1
kind: Mismatch
metadata:
name: bool
a: true
s: true
i: true
"n": true
b: true
m: true
---
apiVersion: example.com/v1
kind: Mismatch
metadata:
name: map
a: {"a":"a"}
s: {"a":"a"}
i: {"a":"a"}
"n": {"a":"a"}
b: {"a":"a"}
m: {"a":"a"}
customresourcedefinition.apiextensions.k8s.io/mismatches.example.com unchanged
Error from server (Invalid): error when creating "...": Mismatch.example.com "valid1" is invalid:
n: Invalid value: 1: invalid data, expected float, got int64 evaluating rule: self != 0.0
mismatch.example.com/valid2 created
Error from server (Invalid): error when creating "...": Mismatch.example.com "mismatch1" is invalid: [
a: Invalid value: []interface {}{}: failed rule: self != [],
b: Invalid value: false: failed rule: self != false,
n: Invalid value: 0: invalid data, expected float, got int64 evaluating rule: self != 0.0,
i: Invalid value: 0: failed rule: self != 0,
m: Invalid value: map[string]interface {}{}: failed rule: self != {},
s: Invalid value: "": failed rule: self != ""]
Error from server (Invalid): error when creating "...": Mismatch.example.com "array" is invalid: [
n: Invalid value: "array": n in body must be of type number: "array",
s: Invalid value: "array": s in body must be of type string: "array",
b: Invalid value: "array": b in body must be of type boolean: "array",
i: Invalid value: "array": i in body must be of type integer: "array",
m: Invalid value: "array": m in body must be of type object: "array",
n: Invalid value: []interface {}{"a"}: invalid data, expected float, got []interface {} evaluating rule: self != 0.0,
i: Invalid value: []interface {}{"a"}: invalid data, expected int, got []interface {} evaluating rule: self != 0,
m: Invalid value: []interface {}{"a"}: invalid data, expected map[string]interface{} to match the provided schema with type=object evaluating rule: self != {},
s: Invalid value: []interface {}{"a"}: invalid data, expected string, got []interface {} evaluating rule: self != "",
b: Invalid value: []interface {}{"a"}: invalid data, expected bool, got []interface {} evaluating rule: self != false]
Error from server (Invalid): error when creating "...": Mismatch.example.com "string" is invalid: [
i: Invalid value: "string": i in body must be of type integer: "string",
m: Invalid value: "string": m in body must be of type object: "string",
n: Invalid value: "string": n in body must be of type number: "string",
a: Invalid value: "string": a in body must be of type array: "string",
b: Invalid value: "string": b in body must be of type boolean: "string",
b: Invalid value: "a": invalid data, expected bool, got string evaluating rule: self != false,
n: Invalid value: "a": invalid data, expected float, got string evaluating rule: self != 0.0,
a: Invalid value: "a": invalid data, expected []interface{} to match the provided schema with type=array evaluating rule: self != [],
i: Invalid value: "a": invalid data, expected int, got string evaluating rule: self != 0,
m: Invalid value: "a": invalid data, expected map[string]interface{} to match the provided schema with type=object evaluating rule: self != {}]
Error from server (Invalid): error when creating "...": Mismatch.example.com "int" is invalid: [
b: Invalid value: "integer": b in body must be of type boolean: "integer",
m: Invalid value: "integer": m in body must be of type object: "integer",
s: Invalid value: "integer": s in body must be of type string: "integer",
a: Invalid value: "integer": a in body must be of type array: "integer",
n: Invalid value: 1: invalid data, expected float, got int64 evaluating rule: self != 0.0,
s: Invalid value: 1: invalid data, expected string, got int64 evaluating rule: self != "",
b: Invalid value: 1: invalid data, expected bool, got int64 evaluating rule: self != false,
m: Invalid value: 1: invalid data, expected map[string]interface{} to match the provided schema with type=object evaluating rule: self != {},
a: Invalid value: 1: invalid data, expected []interface{} to match the provided schema with type=array evaluating rule: self != []]
Error from server (Invalid): error when creating "...": Mismatch.example.com "number" is invalid: [
s: Invalid value: "number": s in body must be of type string: "number",
a: Invalid value: "number": a in body must be of type array: "number",
b: Invalid value: "number": b in body must be of type boolean: "number",
i: Invalid value: "number": i in body must be of type integer: "number",
m: Invalid value: "number": m in body must be of type object: "number",
b: Invalid value: 1.5: invalid data, expected bool, got float64 evaluating rule: self != false,
m: Invalid value: 1.5: invalid data, expected map[string]interface{} to match the provided schema with type=object evaluating rule: self != {},
a: Invalid value: 1.5: invalid data, expected []interface{} to match the provided schema with type=array evaluating rule: self != [],
s: Invalid value: 1.5: invalid data, expected string, got float64 evaluating rule: self != "",
i: Invalid value: 1.5: invalid data, expected int, got float64 evaluating rule: self != 0]
Error from server (Invalid): error when creating "...": Mismatch.example.com "bool" is invalid: [
s: Invalid value: "boolean": s in body must be of type string: "boolean",
a: Invalid value: "boolean": a in body must be of type array: "boolean",
i: Invalid value: "boolean": i in body must be of type integer: "boolean",
m: Invalid value: "boolean": m in body must be of type object: "boolean",
n: Invalid value: "boolean": n in body must be of type number: "boolean",
a: Invalid value: true: invalid data, expected []interface{} to match the provided schema with type=array evaluating rule: self != [],
i: Invalid value: true: invalid data, expected int, got bool evaluating rule: self != 0,
m: Invalid value: true: invalid data, expected map[string]interface{} to match the provided schema with type=object evaluating rule: self != {},
n: Invalid value: true: invalid data, expected float, got bool evaluating rule: self != 0.0,
s: Invalid value: true: invalid data, expected string, got bool evaluating rule: self != ""]
Error from server (Invalid): error when creating "...": Mismatch.example.com "map" is invalid: [
i: Invalid value: "object": i in body must be of type integer: "object",
n: Invalid value: "object": n in body must be of type number: "object",
s: Invalid value: "object": s in body must be of type string: "object",
a: Invalid value: "object": a in body must be of type array: "object",
b: Invalid value: "object": b in body must be of type boolean: "object",
s: Invalid value: map[string]interface {}{}: invalid data, expected string, got map[string]interface {} evaluating rule: self != "",
a: Invalid value: map[string]interface {}{}: invalid data, expected []interface{} to match the provided schema with type=array evaluating rule: self != [],
i: Invalid value: map[string]interface {}{}: invalid data, expected int, got map[string]interface {} evaluating rule: self != 0,
n: Invalid value: map[string]interface {}{}: invalid data, expected float, got map[string]interface {} evaluating rule: self != 0.0,
b: Invalid value: map[string]interface {}{}: invalid data, expected bool, got map[string]interface {} evaluating rule: self != false]
mismatch.example.com/iors-valid-string created
mismatch.example.com/iors-valid-int created
Error from server (Invalid): error when creating "...": Mismatch.example.com "iors-invalid-bool" is invalid: [
iors: Invalid value: "boolean": iors in body must be of type integer,string: "boolean",
iors: Invalid value: true: invalid data, expected XIntOrString value to be either a string or integer evaluating rule: type(self) == string ? self != "" : true,
iors: Invalid value: true: invalid data, expected XIntOrString value to be either a string or integer evaluating rule: type(self) == int ? self != 0 : true]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment