crd2pulumi icon indicating copy to clipboard operation
crd2pulumi copied to clipboard

cannot have a constant value; only booleans, integers, numbers and strings may have constant values

Open rawkode opened this issue 3 years ago • 11 comments

I'm trying to automate SDK generation for major Kubernetes resources, such as Istio, Crossplane, and others; but unfortunately I'm running into some problems.

Here's two examples of such behaviour:

crd2pulumi --nodejsName istio --nodejsPath ../output https://doc.crds.dev/raw/github.com/istio/[email protected]
crd2pulumi --nodejsName istio --nodejsPath ../output https://doc.crds.dev/raw/github.com/crossplane/[email protected]

and here's the stacktrace for the Istio attempt:

panic: fatal: error An assertion has failed: could not parse Pulumi package. source error: could not import spec: <nil>: #/types/kubernetes:networking.x-k8s.io%2Fv1alpha1:TLSRouteSpec/properties/gateways/default: type kubernetes:networking.x-k8s.io/v1alpha1:TLSRouteSpecGateways cannot have a constant value; only booleans, integers, numbers and strings may have constant values; , and 29 other diagnostic(s)

goroutine 1 [running]:
github.com/pulumi/pulumi/sdk/v3/go/common/util/contract.failfast(...)
	/home/runner/go/pkg/mod/github.com/pulumi/pulumi/sdk/[email protected]/go/common/util/contract/failfast.go:23
github.com/pulumi/pulumi/sdk/v3/go/common/util/contract.AssertNoErrorf(0x2041820, 0xc0003509c0, 0x1eac169, 0x1e, 0x0, 0x0, 0x0)
	/home/runner/go/pkg/mod/github.com/pulumi/pulumi/sdk/[email protected]/go/common/util/contract/assert.go:47 +0x1cd
github.com/pulumi/crd2pulumi/gen.(*PackageGenerator).SchemaPackage(0xc000c6ba90, 0xc000000180)
	/home/runner/work/crd2pulumi/crd2pulumi/gen/generate.go:237 +0xb6
github.com/pulumi/crd2pulumi/gen.(*PackageGenerator).genNodeJSFiles(0xc000c6ba90, 0x7ff7bfeff8e0, 0x5, 0xc000ca6cb0, 0x1, 0x1)
	/home/runner/work/crd2pulumi/crd2pulumi/gen/nodejs.go:39 +0x45
github.com/pulumi/crd2pulumi/gen.(*PackageGenerator).genNodeJS(0xc000c6ba90, 0x7ff7bfeff8f3, 0x9, 0x7ff7bfeff8e0, 0x5, 0x16, 0xc0005281c0)
	/home/runner/work/crd2pulumi/crd2pulumi/gen/nodejs.go:30 +0x45
github.com/pulumi/crd2pulumi/gen.Generate(0xc000138850, 0x0, 0x0, 0x0, 0x7ff7bfeff8e0, 0x5, 0x1e8ad7e, 0x4, 0x1e8ad7e, 0x4, ...)
	/home/runner/work/crd2pulumi/crd2pulumi/gen/generate.go:70 +0x2f7
github.com/pulumi/crd2pulumi/cmd.Execute.func2(0xc000334dc0, 0xc0000a09b0, 0x1, 0x5)
	/home/runner/work/crd2pulumi/crd2pulumi/cmd/root.go:158 +0x271
github.com/spf13/cobra.(*Command).execute(0xc000334dc0, 0xc00003a1f0, 0x5, 0x5, 0xc000334dc0, 0xc00003a1f0)
	/home/runner/go/pkg/mod/github.com/spf13/[email protected]/command.go:846 +0x2c2
github.com/spf13/cobra.(*Command).ExecuteC(0xc000334dc0, 0xc00052ff00, 0x1, 0x1)
	/home/runner/go/pkg/mod/github.com/spf13/[email protected]/command.go:950 +0x375
github.com/spf13/cobra.(*Command).Execute(...)
	/home/runner/go/pkg/mod/github.com/spf13/[email protected]/command.go:887
github.com/pulumi/crd2pulumi/cmd.Execute(0xc000084748, 0x27380e0)
	/home/runner/work/crd2pulumi/crd2pulumi/cmd/root.go:190 +0x666
main.main()
	/home/runner/work/crd2pulumi/crd2pulumi/main.go:25 +0x26

rawkode avatar Jan 06 '22 15:01 rawkode

I'm running into the same issue with trying to convert Contour using crd2pulumi. Is this something that the team is thinking about resolving?

itay avatar Feb 11 '22 22:02 itay

For reference, this is also mentioned here: https://github.com/pulumi/crd2pulumi/pull/63#issuecomment-1006926655

itay avatar Feb 11 '22 23:02 itay

Second on this PR, just run into the same issue but with the fluxcd source-controller crds.

 crd2pulumi --go  https://github.com/fluxcd/source-controller/releases/download/v0.23.0/source-controller.crds.yaml
panic: fatal: error An assertion has failed: could not parse Pulumi package. source error: could not import spec: <nil>: #/types/kubernetes:source.toolkit.fluxcd.io%2Fv1beta1:Bucket/properties/status/default: type kubernetes:source.toolkit.fluxcd.io/v1beta1:BucketStatus cannot have a constant value; only booleans, integers, numbers and strings may have constant values; , and 31 other diagnostic(s)

goroutine 1 [running]:
github.com/pulumi/pulumi/sdk/v3/go/common/util/contract.failfast(...)
        /home/runner/go/pkg/mod/github.com/pulumi/pulumi/sdk/[email protected]/go/common/util/contract/failfast.go:23
github.com/pulumi/pulumi/sdk/v3/go/common/util/contract.AssertNoErrorf(0x2041380, 0xc00040b278, 0x1eabe6f, 0x1e, 0x0, 0x0, 0x0)
        /home/runner/go/pkg/mod/github.com/pulumi/pulumi/sdk/[email protected]/go/common/util/contract/assert.go:47 +0x1cd
github.com/pulumi/crd2pulumi/gen.(*PackageGenerator).SchemaPackageWithObjectMetaType(0xc0008a1a90, 0x2)
        /home/runner/work/crd2pulumi/crd2pulumi/gen/generate.go:248 +0xb6
github.com/pulumi/crd2pulumi/gen.(*PackageGenerator).genGoFiles(0xc0008a1a90, 0x1e8aa9e, 0x4, 0xc0001f8ae0, 0x2, 0x2)
        /home/runner/work/crd2pulumi/crd2pulumi/gen/golang.go:45 +0x45
github.com/pulumi/crd2pulumi/gen.(*PackageGenerator).genGo(0xc0008a1a90, 0xc000597628, 0x7, 0x1e8aa9e, 0x4, 0x4, 0xc0005b1380)
        /home/runner/work/crd2pulumi/crd2pulumi/gen/golang.go:36 +0x45
github.com/pulumi/crd2pulumi/gen.Generate(0x0, 0x0, 0x0, 0xc000816c40, 0x1e8aa9e, 0x4, 0x1e8aa9e, 0x4, 0x1e8aa9e, 0x4, ...)
        /home/runner/work/crd2pulumi/crd2pulumi/gen/generate.go:80 +0x20c
github.com/pulumi/crd2pulumi/cmd.Execute.func2(0xc00014a2c0, 0xc00045b280, 0x1, 0x2)
        /home/runner/work/crd2pulumi/crd2pulumi/cmd/root.go:158 +0x271
github.com/spf13/cobra.(*Command).execute(0xc00014a2c0, 0xc00003a1c0, 0x2, 0x2, 0xc00014a2c0, 0xc00003a1c0)
        /home/runner/go/pkg/mod/github.com/spf13/[email protected]/command.go:846 +0x2c2
github.com/spf13/cobra.(*Command).ExecuteC(0xc00014a2c0, 0xc0005c5f00, 0x1, 0x1)
        /home/runner/go/pkg/mod/github.com/spf13/[email protected]/command.go:950 +0x375
github.com/spf13/cobra.(*Command).Execute(...)
        /home/runner/go/pkg/mod/github.com/spf13/[email protected]/command.go:887
github.com/pulumi/crd2pulumi/cmd.Execute(0xc00007c748, 0x27380e0)
        /home/runner/work/crd2pulumi/crd2pulumi/cmd/root.go:190 +0x666
main.main()
        /home/runner/work/crd2pulumi/crd2pulumi/main.go:25 +0x26

I am on the latest version:

❯ crd2pulumi version
v1.2.0

dirien avatar Apr 15 '22 18:04 dirien

FWIW, also seeing this error trying to convert the Cluster API CRDs. This is with crd2pulumi version 1.2.0.

scottslowe avatar May 10 '22 05:05 scottslowe

Seeing this also with FluxCD.

RealityAnomaly avatar Jun 08 '22 10:06 RealityAnomaly

Can confirm, same issue with CRD helm.toolkit.fluxcd.io/v2beta1.

It appears to happen on fields where the default contains a nested value, like: status: default: observedGeneration: -1

terrafying avatar Jul 15 '22 04:07 terrafying

Another example here: https://github.com/pulumi/crd2pulumi/issues/102#issue-1400949103

lblackstone avatar Oct 07 '22 16:10 lblackstone

Cross-posting.

A temporary workaround:

import yaml from 'yaml';

yaml.parseAllDocuments(crdYaml).forEach((parsedKubeResource, i) => {
      const data = yaml.parse(parsedKubeResource.toString(), (k, v) => {
              return typeof v === 'object' && k === 'default' ? undefined : v; 
      });
      fs.writeFileSync(path, yaml.stringify(data));
 });

Oyelowo avatar Oct 09 '22 04:10 Oyelowo

Any news on this one?

piclemx avatar Nov 29 '23 17:11 piclemx

The workaround above works- the issue specifically seems to be that crd2pulumi can't handle default: {} in the yaml definition. When they are stripped, it seems to be ok.

image

Here is a more complete script that can strip the default in nodejs repl:

var fs = require('fs'),
    path = require('path'),    
    yaml = require('yaml');

function fixYaml(fileName) {
    var split = fileName.split('.yaml');

    filePath = path.join(path.resolve(), fileName),
    filePath2 = path.join(path.resolve(), split[0] + '-fixed.yaml');

    fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
        if (!err) {
            yaml.parseAllDocuments(data).forEach((parsedKubeResource, i) => {
                const data = yaml.parse(parsedKubeResource.toString(), (k, v) => {
                        return typeof v === 'object' && k === 'default' ? undefined : v; 
                });
                fs.writeFileSync(filePath2, yaml.stringify(data));
        });
        } else {
            console.log(err);
        }
    });
}

Thanks @Oyelowo for the workaround!

automagic avatar Dec 14 '23 21:12 automagic

@automagic To verify then: we would run your script against the CRD manifests before trying to use them with crd2pulumi, is that correct?

scottslowe avatar Dec 15 '23 18:12 scottslowe