Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Fix: Pin the Kubernetes version for generated nodejs resources(<https://github.com/pulumi/crd2pulumi/pull/121>)

## 1.3.0 (2023-12-12)

- Fix: excluding files from unneededGoFiles was not working (<https://github.com/pulumi/crd2pulumi/pull/120>)
Expand Down
15 changes: 15 additions & 0 deletions pkg/codegen/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func GenerateNodeJS(pg *PackageGenerator, name string) (map[string]*bytes.Buffer
}
pkg.Language[nodejsName], err = ijson.RawMessage(map[string]any{
"moduleToPackage": moduleToPackage,
"dependencies": map[string]string{
"@pulumi/kubernetes": "^4.0.0",
},
})
if err != nil {
return nil, err
Expand All @@ -59,6 +62,18 @@ func GenerateNodeJS(pg *PackageGenerator, name string) (map[string]*bytes.Buffer
}
files["package.json"] = bytes.ReplaceAll(packageJSON, []byte("${VERSION}"), []byte(""))

// Pin the kubernetes provider version used
utilities, ok := files["utilities.ts"]
if !ok {
return nil, fmt.Errorf("cannot find generated utilities.ts")
}
files["utilities.ts"] = bytes.ReplaceAll(utilities,
[]byte("export function getVersion(): string {"),
[]byte(`export const getVersion: () => string = () => "4.5.5"

function unusedGetVersion(): string {`),
)

// Create a helper `meta/v1.ts` script that exports the ObjectMeta class from the SDK. If there happens to already
// be a `meta/v1.ts` file, then just append the script.
if code, ok := files[nodejsMetaPath]; !ok {
Expand Down
17 changes: 17 additions & 0 deletions tests/crds/k8sversion/mock_crd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
spec:
group: k8sversion.pulumi.com
names:
plural: testresources
singular: testresource
kind: TestResource
scope: Namespaced
versions:
- test:
name: test
schema:
openAPIV3Schema:
properties:
testProperty:
type: string`
40 changes: 40 additions & 0 deletions tests/crds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var languages = []string{"dotnet", "go", "nodejs", "python"}
Expand Down Expand Up @@ -117,3 +118,42 @@ func TestCRDsWithUnderscore(t *testing.T) {

execCrd2Pulumi(t, "python", "crds/underscored-types/networkpolicy.yaml", validateUnderscore)
}

func TestKubernetesVersionNodeJs(t *testing.T) {
validateVersion := func(t *testing.T, path string) {
// enter and build the generated package
withDir(t, path, func() {

require.NoError(t, exec.Command("npm", "i").Run())
require.NoError(t, exec.Command("npm", "run", "build").Run())

// extract the version returned by a resource
appendFile(t, "bin/index.js", "\nconsole.log((new k8sversion.test.TestResource('test')).__version)")

version, err := exec.Command("node", "bin/index.js").Output()
require.NoError(t, err)
assert.Equal(t, "4.5.5\n", string(version))
})
}

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)
defer os.Chdir(pwd)

require.NoError(t, os.Chdir(dir))

f()
}

func appendFile(t *testing.T, filename, content string) {
// extract the version returned by a resource
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0600)
require.NoError(t, err)
defer f.Close()

_, err = f.WriteString(content)
}