From d77b74b5d6bce8679ebd65dc7de8b70aaefa22e9 Mon Sep 17 00:00:00 2001 From: Bryce Lampe Date: Wed, 13 Nov 2024 11:26:21 -0800 Subject: [PATCH] Fix NodeJS metadata type (#159) All of our languages except NodeJS use `SchemaPackageWithObjectMetaType`. As a result, Node exposes only input types for object metadata which makes it awkward to consume downstream. I'm assuming https://github.com/pulumi/crd2pulumi/pull/143 didn't change this for backwards-compatibility reasons, or maybe it was overlooked. I _think_ it is safe to continue generating the `ObjectMeta` type alias (in case users are importing it) but use the input/output types (matching the upstream k8s types) on resources. Edit: > // schemaPackageWithObjectMetaType is the Pulumi schema package used to > // generate code for languages that need an ObjectMeta type (Python, Go, and .NET) So this seems intentional. For whatever reason, using this with Node has the effect of generating input/output types matching upstream's types, which seems desirable. Fixes https://github.com/pulumi/crd2pulumi/issues/158 --- CHANGELOG.md | 4 +++- pkg/codegen/nodejs.go | 2 +- tests/crds_test.go | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d36a5fd..e9981a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # CHANGELOG -## Unreleased +## 1.5.4 (2024-11-13) + +- NodeJS now uses correct input/output types for object metadata. (https://github.com/pulumi/crd2pulumi/issues/158) ## 1.5.3 (2024-09-30) diff --git a/pkg/codegen/nodejs.go b/pkg/codegen/nodejs.go index cce2c26..c82306b 100644 --- a/pkg/codegen/nodejs.go +++ b/pkg/codegen/nodejs.go @@ -30,7 +30,7 @@ export type ObjectMetaPatch = k8s.types.input.meta.v1.ObjectMetaPatch; ` func GenerateNodeJS(pg *PackageGenerator, name string) (map[string]*bytes.Buffer, error) { - pkg := pg.SchemaPackage() + pkg := pg.SchemaPackageWithObjectMetaType() oldName := pkg.Name pkg.Name = name diff --git a/tests/crds_test.go b/tests/crds_test.go index f94129a..f2171e9 100644 --- a/tests/crds_test.go +++ b/tests/crds_test.go @@ -259,6 +259,29 @@ func TestKubernetesVersionNodeJs(t *testing.T) { execCrd2Pulumi(t, "nodejs", "crds/k8sversion/mock_crd.yaml", validateVersion) } +func TestNodeJsObjectMeta(t *testing.T) { + validateVersion := func(t *testing.T, path string) { + // enter and build the generated package + withDir(t, path, func() { + runRequireNoError(t, exec.Command("npm", "install")) + runRequireNoError(t, exec.Command("npm", "run", "build")) + + filename := filepath.Join(path, "k8sversion", "test", "testResource.ts") + t.Logf("validating objectmeta type in %s", filename) + + testResource, err := os.ReadFile(filename) + if err != nil { + t.Fatalf("expected to read generated NodeJS code: %s", err) + } + + assert.Contains(t, string(testResource), "public readonly metadata!: pulumi.Output;", "expected metadata output type") + assert.Contains(t, string(testResource), "metadata?: pulumi.Input;", "expected metadata input type") + }) + } + + execCrd2Pulumi(t, "nodejs", "crds/k8sversion/mock_crd.yaml", validateVersion) +} + func withDir(t *testing.T, dir string, f func()) { pwd, err := os.Getwd() require.NoError(t, err)