kubernetes-client / gen

Common generator scripts for all client libraries
Apache License 2.0
148 stars 145 forks source link

crd-model-gen should enforce that enums are UPPERCASE and don't contain special characters #196

Closed ksumit closed 2 years ago

ksumit commented 3 years ago

While working on a CRD, i had two enum patterns that didn't work out:

  1. Enums with lowercase or mixed case names
  2. Enums with special characters in the names

Example:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
...
spec:
...
  versions:
    - name: v1alpha1
...
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
...
                type:
                  type: string
                  # First pattern with lowercase or mixed case names
                  enum:
                    - apple
                    - banana
                    - WaterMelon
                  default: apple
                  description: "Type of fruit"
...
...
                fileName:
                  type: string
                  # Second pattern with names containing special characters
                  enum:
                    - log4j.properties
                    - web.xml
                    - server.xml
...

Technically these are not valid patterns for enum names in Java (atleast the second pattern wouldn't even compile) and crd-model-gen happily does the right thing by transforming these names to UPPERCASE and replacing special characters with "_" putting the values in the enum like below:

// for the first pattern
  public static enum TypeEnum {
    APPLE("apple"),
    BANANA("banana"),
    WATERMELON("WaterMelon");

// for the second pattern
  public static enum FileNameEnum {
    LOG4J_PROPERTIES("log4j.properties"),
    WEB_XML("web.xml"),
    SERVER_XML("server.xml");

The problem however happens when loading a CR with such enum values using Yaml class that is provided with java implementation of kubernetes-client.

SnakeYaml started supporting a loader option to ignore case while loading enums starting version 1.28 and I have a corresponding change here to consume the same. However i am not able to justify that the second pattern should even be supported. And that brings to me to the thought, should crd-model-gen enforce that enum names are always UPPERCASE and don't container special characters other than "_".

Thoughts?

brendandburns commented 3 years ago

If we wanted to do this, we should check in a "validate_crd.py" or somesuch script that can validate the CRD definition before we try to generate code for it.

We'd be happy to have a PR for that.

yue9944882 commented 3 years ago

added a new constructor by which we can pass our custom loader/dumper options, but calling the native constructor should also work anyway. https://github.com/kubernetes-client/java/pull/1808

k8s-triage-robot commented 2 years ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot commented 2 years ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

k8s-triage-robot commented 2 years ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

k8s-ci-robot commented 2 years ago

@k8s-triage-robot: Closing this issue.

In response to [this](https://github.com/kubernetes-client/gen/issues/196#issuecomment-1001457498): >The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs. > >This bot triages issues and PRs according to the following rules: >- After 90d of inactivity, `lifecycle/stale` is applied >- After 30d of inactivity since `lifecycle/stale` was applied, `lifecycle/rotten` is applied >- After 30d of inactivity since `lifecycle/rotten` was applied, the issue is closed > >You can: >- Reopen this issue or PR with `/reopen` >- Mark this issue or PR as fresh with `/remove-lifecycle rotten` >- Offer to help out with [Issue Triage][1] > >Please send feedback to sig-contributor-experience at [kubernetes/community](https://github.com/kubernetes/community). > >/close > >[1]: https://www.kubernetes.dev/docs/guide/issue-triage/ Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.