pulumi / crd2pulumi

Generate typed CustomResources from a Kubernetes CustomResourceDefinition
Apache License 2.0
98 stars 17 forks source link

Can't Import Two Generated Typescript Class that Belong to the Same "Resource Module" #111

Closed blaise-bradley closed 1 month ago

blaise-bradley commented 1 year ago

What happened?

I’m trying to generate code for the IngressRoute and Middleware CRDs from the Traefik Helm Chart. Based on the usage described in the README, it seems like you can only point crd2pulumi to a single file. My approach has been to generate code for each CRD yml file and then move the generated code into separate directories. If I import just one of the generated classes, my program works as expected. When I import both of the generated classes into the same Pulumi program, the planning step fails with the following error:

    TypeError: Invalid Version:
        at new SemVer (/Users/blaisebradley/Code/rollout-embedded-poc/node_modules/@pulumi/pulumi/node_modules/semver/semver.js:332:11)
        at compare (/Users/blaisebradley/Code/rollout-embedded-poc/node_modules/@pulumi/pulumi/node_modules/semver/semver.js:647:10)
        at Object.eq (/Users/blaisebradley/Code/rollout-embedded-poc/node_modules/@pulumi/pulumi/node_modules/semver/semver.js:693:10)
        at sameVersion (/Users/blaisebradley/Code/rollout-embedded-poc/node_modules/@pulumi/runtime/rpc.ts:701:57)
        at register (/Users/blaisebradley/Code/rollout-embedded-poc/node_modules/@pulumi/runtime/rpc.ts:716:17)
        at Object.registerResourceModule (/Users/blaisebradley/Code/rollout-embedded-poc/node_modules/@pulumi/runtime/rpc.ts:810:5)
        at Object.<anonymous> (/Users/blaisebradley/Code/rollout-embedded-poc/pulumi-crd/helm/middlewares/traefik/v1alpha1/index.ts:25:16)
        at Module._compile (node:internal/modules/cjs/loader:1159:14)
        at Module.m._compile (/Users/blaisebradley/Code/rollout-embedded-poc/node_modules/@pulumi/pulumi/node_modules/ts-node/src/index.ts:439:23)
        at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)

My best guess is that there is some kind of conflict since we are trying to register the "resource module" traefik.containo.us/v1alpha1 twice with the same version number.

Expected Behavior

It's possible to import and use both classes in the same program.

Steps to reproduce

  1. Run crd2pulumi against two Traefik CRD files such as ingressroute.yaml and middlewares.yaml.

  2. Put the generated code in sibling directories. A screenshot of my directory structure is below: Screen Shot 2023-04-19 at 4 38 43 PM

  3. Import both classes in the same Pulumi program:

    import { IngressRoute as TraefikIngressRoute } from "../pulumi-crd/traefik/ingress-route/traefik/v1alpha1";
    import { Middleware as TraefikMiddleware } from "../pulumi-crd/traefik/middlewares/traefik/v1alpha1";
  4. Attempt to create an instance of each resource:

  const traefikMiddleware = new TraefikMiddleware(
      "traefik-dashboard-auth",
      {
        apiVersion: "traefik.containo.us/v1alpha1",
        kind: "Middleware",
        metadata: {
          name: "basic-auth",
          namespace: traefikNamespace.metadata.name,
        },
        spec: {
          basicAuth: { secret: traefikDashboardCreds.metadata.name },
        },
      },
      { provider: k8sProvider }
    );

    new TraefikIngressRoute(
      "traefik-dashboard-route",
      {
        metadata: {
          name: "dashboard",
          namespace: traefikNamespace.metadata.name,
        },
        spec: {
          entryPoints: ["web"],
          routes: [
            {
              match:
                "PathPrefix(`/dashboard`, `/dashboard/`) || PathPrefix(`/api`, `/api/`)",
              kind: "Rule",
              services: [{ name: "api@internal", kind: "TraefikService" }],
              middlewares: [
                {
                  name: traefikMiddleware.metadata.name as Output<string>,
                  namespace: traefikNamespace.metadata.name,
                },
              ],
            },
          ],
        },
      },
      { provider: k8sProvider }
  );

Output of pulumi about

CLI          
Version      3.60.1
Go Version   go1.20.2
Go Compiler  gc

Plugins
NAME    VERSION
nodejs  unknown
random  4.12.1

Host     
OS       darwin
Version  12.4
Arch     arm64

This project is written in nodejs: executable='/Users/blaisebradley/.nvm/versions/node/v18.12.1/bin/node' version='v18.12.1'

Backend        
Name           pulumi.com
URL            https://app.pulumi.com/blaiseb
User           blaiseb
Organizations  blaiseb, rollout

Additional context

No response

Contributing

Vote on this issue by adding a 👍 reaction. To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).

iwahbe commented 1 year ago

Hi @blaise-bradley. Thanks for raising the issue.

As a work-around, I think you can change one of the generated packages to have a different name. This way Pulumi can distinguish between them and won't error because two modules with the same name and version were registered at the same time.

crd2pulumi wasn't design to generate side-by-side compatible packages. We will consider that for the future.

cleverguy25 commented 3 months ago

Added to epic https://github.com/pulumi/home/issues/3431

rquitales commented 1 month ago

crd2pulumi allows you to generate a single package from multiple input files. Usage example: crd2pulumi --go file1.yaml file2.yaml.

pulumi-bot commented 3 weeks ago

This issue has been addressed in PR #143 and shipped in release v1.5.0.