Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lauralorenz/0b3423366f8df1cabf29d348f44bd084 to your computer and use it in GitHub Desktop.
Save lauralorenz/0b3423366f8df1cabf29d348f44bd084 to your computer and use it in GitHub Desktop.
OpenAPIv3 maxLength tests with Kubernetes CRDs
apiVersion: about.x-k8s.io/v1alpha1
kind: ClusterProperty
metadata:
name: clusterproperty-doesnotfit
spec:
value: "🇺🇸🇺🇸"
apiVersion: about.x-k8s.io/v1alpha1
kind: ClusterProperty
metadata:
name: clusterproperty-fits
spec:
value: "🇺🇸"
lauralorenz@lauralorenz:samples$ kubectl config current-context
kind-kind
lauralorenz@lauralorenz:samples$ kubectl get crds
NAME CREATED AT
clusterproperties.about.x-k8s.io 2022-03-07T22:58:19Z
lauralorenz@lauralorenz:samples$ kubectl describe crds clusterproperties.about.x-k8s.io
Name: clusterproperties.about.x-k8s.io
Namespace:
Labels: <none>
Annotations: controller-gen.kubebuilder.io/version: v0.8.0
API Version: apiextensions.k8s.io/v1
Kind: CustomResourceDefinition
Metadata:
Creation Timestamp: 2022-03-07T22:58:19Z
Generation: 1
Managed Fields:
API Version: apiextensions.k8s.io/v1
Fields Type: FieldsV1
fieldsV1:
f:status:
f:acceptedNames:
f:kind:
f:listKind:
f:plural:
f:singular:
f:conditions:
k:{"type":"Established"}:
.:
f:lastTransitionTime:
f:message:
f:reason:
f:status:
f:type:
k:{"type":"NamesAccepted"}:
.:
f:lastTransitionTime:
f:message:
f:reason:
f:status:
f:type:
Manager: Go-http-client
Operation: Update
Subresource: status
Time: 2022-03-07T22:58:19Z
API Version: apiextensions.k8s.io/v1
Fields Type: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
.:
f:controller-gen.kubebuilder.io/version:
f:kubectl.kubernetes.io/last-applied-configuration:
f:spec:
f:conversion:
.:
f:strategy:
f:group:
f:names:
f:kind:
f:listKind:
f:plural:
f:singular:
f:scope:
f:versions:
Manager: kubectl-client-side-apply
Operation: Update
Time: 2022-03-07T22:58:19Z
Resource Version: 4990
UID: 14c3df78-a2bb-49df-9168-bc5a48c6c27d
Spec:
Conversion:
Strategy: None
Group: about.x-k8s.io
Names:
Kind: ClusterProperty
List Kind: ClusterPropertyList
Plural: clusterproperties
Singular: clusterproperty
Scope: Cluster
Versions:
Name: v1alpha1
Schema:
openAPIV3Schema:
Description: ClusterProperty is the Schema for the clusterproperties API
Properties:
API Version:
Description: APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
Type: string
Kind:
Description: Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
Type: string
Metadata:
Type: object
Spec:
Description: ClusterPropertySpec defines the desired state of ClusterProperty
Properties:
Name:
Description: ClusterProperty Name
Type: string
Value:
Description: ClusterProperty value
Max Length: 3
Min Length: 1
Type: string
Required:
value
Type: object
Status:
Description: ClusterPropertyStatus defines the observed state of ClusterProperty
Type: object
Type: object
Served: true
Storage: true
Subresources:
Status:
Status:
Accepted Names:
Kind: ClusterProperty
List Kind: ClusterPropertyList
Plural: clusterproperties
Singular: clusterproperty
Conditions:
Last Transition Time: 2022-03-07T22:58:19Z
Message: no conflicts found
Reason: NoConflicts
Status: True
Type: NamesAccepted
Last Transition Time: 2022-03-07T22:58:19Z
Message: the initial names have been accepted
Reason: InitialNamesAccepted
Status: True
Type: Established
Stored Versions:
v1alpha1
Events: <none>
lauralorenz@lauralorenz:samples$ python3
Python 3.9.9 (main, Jan 12 2022, 16:10:51)
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> src = open("about_v1alpha1_clusterproperty-fits.yaml", "r", encoding="utf-8") # text mode is implicit, will decode against `encoding` when it needs to
>>> src_s = src.read()
>>> print(src_s)
apiVersion: about.x-k8s.io/v1alpha1
kind: ClusterProperty
metadata:
name: clusterproperty-fits
spec:
value: "🇺🇸"
>>> dest = open("about_v1alpha1_clusterproperty-fits-utf32.yaml", "wb") # b = binary mode, will not encode for you
>>> dest.write(src_s.encode("utf-32"))
472
>>> dest.close()
>>> exit()
lauralorenz@lauralorenz:samples$ ls
about_v1alpha1_clusterproperty-doesnotfit-utf32.yaml about_v1alpha1_clusterproperty-fits-utf16.yaml about_v1alpha1_clusterproperty-fits.yaml
about_v1alpha1_clusterproperty-doesnotfit.yaml about_v1alpha1_clusterproperty-fits-utf32.yaml
lauralorenz@lauralorenz:samples$ kubectl apply -f about_v1alpha1_clusterproperty-fits.yaml
clusterproperty.about.x-k8s.io/clusterproperty-fits created
lauralorenz@lauralorenz:samples$ kubectl apply -f about_v1alpha1_clusterproperty-doesnotfit.yaml
The ClusterProperty "clusterproperty-doesnotfit" is invalid: spec.value: Invalid value: "🇺🇸🇺🇸": spec.value in body should be at most 3 chars long
lauralorenz@lauralorenz:samples$ kubectl apply -f about_v1alpha1_clusterproperty-fits-utf16.yaml
clusterproperty.about.x-k8s.io/clusterproperty-fits unchanged
lauralorenz@lauralorenz:samples$ kubectl apply -f about_v1alpha1_clusterproperty-fits-utf32.yaml
error: error parsing about_v1alpha1_clusterproperty-fits-utf32.yaml: error converting YAML to JSON: yaml: control characters are not allowed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment