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

[crd-gen] Enum fields written in generated crd yaml #4225

Closed
leoandrea7 opened this issue Jun 22, 2022 · 8 comments · Fixed by #4230 or #5323
Closed

[crd-gen] Enum fields written in generated crd yaml #4225

leoandrea7 opened this issue Jun 22, 2022 · 8 comments · Fixed by #4230 or #5323
Assignees
Milestone

Comments

@leoandrea7
Copy link
Contributor

leoandrea7 commented Jun 22, 2022

Describe the bug

When the crd-gen tool tries to generate schema properties from an enum, both values and fields are written in the generated crd yaml.

Fabric8 Kubernetes Client version

5.12.2

Steps to reproduce

Create an enum with values and at least a field and put it into Spec.
Example:

@Group("com.example")
@Version("mycrd")
@Plural("mycrds")
public class MyCRD extends CustomResource<MyCRDSpec, Void> implements Namespaced {

  public class MyCRDSpec {

    private Test test;

    public enum Test {
      A("test-a"),
      B("test-b");

      private final String field1;

      Test(String field1) {
        this.field1 = field1;
      }

      public String getField1() {
        return field1;
      }
    }
  }
}


Exec mvn clean compile

the generated file target/classes/META-INF/fabric8/mycrds.com.example-v1.yml will contain:

# Generated by Fabric8 CRDGenerator, manual edits might get overwritten!
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: mycrds.com.example
spec:
  group: com.example
  names:
    kind: MyCRD
    plural: mycrds
    singular: mycrd
  scope: Namespaced
  versions:
  - name: mycrd
    schema:
      openAPIV3Schema:
        properties:
          spec:
            properties:
              test:
                enum:
                - A
                - B
                - field1
                type: string
            type: object
          status:
            type: object
        type: object
    served: true
    storage: true

Expected behavior

A crd with just enum values and without enum fields:


# Generated by Fabric8 CRDGenerator, manual edits might get overwritten!
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: mycrds.com.example
spec:
  group: com.example
  names:
    kind: MyCRD
    plural: mycrds
    singular: mycrd
  scope: Namespaced
  versions:
  - name: mycrd
    schema:
      openAPIV3Schema:
        properties:
          spec:
            properties:
              test:
                enum:
                - A
                - B
                type: string
            type: object
          status:
            type: object
        type: object
    served: true
    storage: true

Runtime

Kubernetes (vanilla)

Kubernetes API Server version

1.22.3@latest

Environment

Windows

Fabric8 Kubernetes Client Logs

No response

Additional context

No response

@leoandrea7 leoandrea7 changed the title [crd-gen] Enum fileds written in generated crd yaml [crd-gen] Enum fields written in generated crd yaml Jun 22, 2022
@leoandrea7
Copy link
Contributor Author

I think that the problem is here:

final JsonNode[] enumValues = def.getProperties().stream()

def.getProperties() return both fields and values...We are need of something like getValues().

@andreaTP
Copy link
Member

Can reproduce the bug and is valid indeed.

@stale
Copy link

stale bot commented Oct 17, 2022

This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!

@stale stale bot added the status/stale label Oct 17, 2022
@andreaTP
Copy link
Member

this issue is not stale, the underlying bug is still valid.

@andreaTP
Copy link
Member

ping to @iocanel on this.

@bachmanity1
Copy link
Contributor

bachmanity1 commented Nov 9, 2022

@andreaTP
is it possible to put enum field's value in the generated crd? Like in the example above, instead of this

enum:
    - A
    - B
    type: string

generate this

enum:
    - test-a
    - test-b
    type: string

I need this because I have field in CR file that has hyphens in it and I'm not able to use enums for that field because enums can't use hyphens, so I was looking for a work around.

@iocanel
Copy link
Member

iocanel commented Nov 10, 2022

Sundrio does not treat enums differently. This means for sundrio enums are regular classes with kind == ENUM.
This means that if you want to just grab the values of the enum, you'll have to chech if kind == ENUM and then return static final fields that have the same type as the declaring class.

@andreaTP
Copy link
Member

you'll have to chech if kind == ENUM and then return static final fields that have the same type as the declaring class.

This is only a partial solution, as it leaves space for other issues as demonstrated here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment