Skip to content

When using cdk8s import, provide an option to express simple enums as union typesΒ #2805

@benfiola

Description

@benfiola

Description of the feature or enhancement:

Provide an option to cdk8s import to express simple enums as union types.

(btw, thank you for cdk8s - it's an excellent and well-rounded piece of software!)

Use Case:

While cdk8s import is consistently awesome, I often run into snags when dealing with enums that have simple values. For example, here's how I'd create an operator.tigera.io/Installation custom resource:

new Installation(construct, ..., {
  metadata: {...},
  spec: {
    calicoNetwork: {
      bgp: InstallationSpecCalicoNetworkBgp.ENABLED,
      bpfNetworkBootstrap: InstallationSpecCalicoNetworkBpfNetworkBootstrap.ENABLED,
    },
  },
});

where:

export enum InstallationSpecCalicoNetworkBpfNetworkBootstrap {
  /** Disabled */
  DISABLED = "Disabled",
  /** Enabled */
  ENABLED = "Enabled",
}

export enum InstallationSpecCalicoNetworkBgp {
  /** Enabled */
  ENABLED = "Enabled",
  /** Disabled */
  DISABLED = "Disabled",
}

It's a bit cumbersome to determine the name of the proper enum field to use, and sometimes the names of these fields can get a bit unwieldy. Plus, because they're enums - it makes it hard to write common routines capable of working with several structurally similar subfields (e.g., ingress and egress rules for a CiliumNetworkPolicy).

After enough friction, I usually end up doing the following as an escape hatch:

new Installation(construct, ..., {
  metadata: {...},
  spec: {
    calicoNetwork: {
      bgp: "Enabled" as any,
      bpfNetworkBootstrap: "Enabled" as any,
    },
  },
});

but this seems counter-intuitive to the selling point of having strongly typed resource definitions.

Proposed Solution:

(Apologies for looking at this problem through a narrow, typescript lens. I understand that cdk8s import needs to support a handful of languages.)

The above enums could be expressed as a simple union type:

export type InstallationSpecCalicoNetworkBpfNetworkBootstrap = "Enabled" | "Disabled";

export type InstallationSpecCalicoNetworkBgp = "Enabled"  | "Disabled";

At least within typescript for these simple cases, resources will continue to be strictly typed without needing to import and specify field-specific enum types.

Other:

  • πŸ‘‹ I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a πŸš€ Feature Request

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestNew/Enhanced functionality wantedneeds-triagePriority and effort undetermined yet

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions