cdk8s-team / cdk8s

Define Kubernetes native apps and abstractions using object-oriented programming
https://cdk8s.io
Apache License 2.0
4.29k stars 290 forks source link

[BUG] importing a CRD with complex union types fails #171

Closed julienperignon closed 4 years ago

julienperignon commented 4 years ago

Describe the bug When trying to import the virtualservice CRD from Istio the import fails midway

To Reproduce

npm run import https://gist.githubusercontent.com/julienperignon/bc2ca51e90408a8e3d5cf632900f5267/raw/b0fa0cc5bfef428b0c6bb3894adf25f8be686ac9/virtualservice.yaml

Expected behavior CLI should not fail imports/virtualservice.ts should be complete

Additional context

tsc -v 
Version 3.9.2

cdk8s --version
0.21.0

package.json


{
  "name": "kubernetes",
  "version": "1.0.0",
  "main": "main.js",
  "types": "main.ts",
  "license": "Apache-2.0",
  "private": true,
  "scripts": {
    "import": "cdk8s import",
    "synth": "cdk8s synth",
    "compile": "tsc",
    "watch": "tsc -w",
    "test": "echo ok",
    "build": "npm run compile && npm run test && npm run synth",
    "upgrade": "npm i cdk8s@latest cdk8s-cli@latest",
    "upgrade:next": "npm i cdk8s@next cdk8s-cli@next"
  },
  "dependencies": {
    "cdk8s": "^0.21.0",
    "constructs": "^2.0.1",
    "dotenv": "^8.2.0"
  },
  "devDependencies": {
    "@types/node": "^14.0.1",
    "cdk8s-cli": "^0.21.0",
    "typescript": "^3.9.2"
  }
}

Error :

(node:12792) UnhandledPromiseRejectionWarning: Error: unexpected union type undefined
    at C:\Source\kubernetes\node_modules\cdk8s-cli\lib\import\type-generator.js:141:27
    at TypeGenerator.generate (C:\Source\kubernetes\node_modules\cdk8s-cli\lib\import\type-generator.js:123:13)
    at CustomResourceDefinition.generateTypeScript (C:\Source\kubernetes\node_modules\cdk8s-cli\lib\import\crd.js:48:15)
    at C:\Source\kubernetes\node_modules\cdk8s-cli\lib\import\crd.js:84:100
    at Array.map (<anonymous>)
    at ImportCustomResourceDefinition.generateTypeScript (C:\Source\kubernetes\node_modules\cdk8s-cli\lib\import\crd.js:84:85)
    at ImportCustomResourceDefinition.import (C:\Source\kubernetes\node_modules\cdk8s-cli\lib\import\base.js:26:24)
    at async Object.importDispatch (C:\Source\kubernetes\node_modules\cdk8s-cli\lib\import\dispatch.js:11:9)
    at async Object.handler (C:\Source\kubernetes\node_modules\cdk8s-cli\bin\cmds\import.js:27:9)
(node:12792) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:12792) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Encryptic commented 4 years ago

I've also encountered this with the istio manifest, located here: https://raw.githubusercontent.com/istio/istio/1.5.4/manifests/base/files/crd-all.gen.yaml

I believe it's related to how the union code expects to handle these objects assuming they're all going to have type properties, not other sub objects.

Here's an example of the spec that causes a validation failure:

http:
              description: An ordered list of route rules for HTTP traffic.
              items:
                properties:
                  fault:
                    description: Fault injection policy to apply on HTTP traffic at
                      the client side.
                    properties:
                      delay:
                        oneOf:
                        - anyOf:
                            - required:
                              - fixedDelay
                            - required:
                              - exponentialDelay
                        - required:
                          - fixedDelay
                        - required:
                          - exponentialDelay
                        properties:
                          exponentialDelay:
                            type: string
                          fixedDelay:
                            description: Add a fixed delay before forwarding the request.
                            type: string
                          percent:
                            description: Percentage of requests on which the delay
                              will be injected (0-100).
                            format: int32
                            type: integer
                          percentage:
                            description: Percentage of requests on which the delay
                              will be injected.
                            properties:
                              value:
                                format: double
                                type: number
                            type: object
                        type: object
                    type: object
                type: object
              type: array