Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict yaml schema validation via openAPIV3Schema #1157

Closed
suchen-sci opened this issue Dec 4, 2023 · 1 comment
Closed

Strict yaml schema validation via openAPIV3Schema #1157

suchen-sci opened this issue Dec 4, 2023 · 1 comment

Comments

@suchen-sci
Copy link
Contributor

Is your feature request related to a problem? Please describe.
In k8s, if we create following yaml:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80
invalidPart: invalidPart

it will report error,

Error from server (BadRequest): error when creating "yamls/test.yaml": Pod in version "v1" cannot be handled as a Pod: strict decoding error: unknown field "invalidPart"

which mean invalidPart of yaml is unknown field.

But in easegress, if we do this:

➜  easegress  ✗ cat yamls/httpserver.yaml       
kind: HTTPServer
name: httpserver
port: 8088
keepAlive: true
https: false
rules:
  - paths:
      - backend: pipeline-demo
invalidPart: invalidPart

➜  easegress ✗ e apply -f yamls/httpserver.yaml 
create HTTPServer httpserver successfully

it actually works, it may cause potential bugs, for example, some users think their settings work, but actually not.

Describe the solution you'd like
I think k8s use the tech called openAPIV3Schema, generated from golang struct, to make sure yamls have exactly same schema of corresponding struct. Here is an example how they use it in CustomResourceDefinition.

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  # name must match the spec fields below, and be in the form: <plural>.<group>
  name: crontabs.stable.example.com
spec:
  # group name to use for REST API: /apis/<group>/<version>
  group: stable.example.com
  # list of versions supported by this CustomResourceDefinition
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                cronSpec:
                  type: string
                image:
                  type: string
                replicas:
                  type: integer

Thanks for contributing 🎉!

@xxx7xxxx
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants