Skip to content

Commit 53b4b4e

Browse files
authored
Pin the k8s version in the generated resources for node (#121)
Fixes #28 crd2pulumi uses codegen in a kind of funny way to generate resources that act as resources of the kubernetes resource plugin. This means the resource version needs to match a kubernetes plugin version. Generated resources however, default to the version from the package.json, so if the user attempts to give the generated package a version, the engine starts complaining that it can't find a kubernetes plugin with that version. This change overrides the generated `getVersion()` helper to point to a recent kubernetes release so that the engine can find the plugin even if the user adds a version to the package.json
1 parent 78ee41f commit 53b4b4e

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Fix: Pin the Kubernetes version for generated nodejs resources(<https://github.com/pulumi/crd2pulumi/pull/121>)
6+
57
## 1.3.0 (2023-12-12)
68

79
- Fix: excluding files from unneededGoFiles was not working (<https://github.com/pulumi/crd2pulumi/pull/120>)

pkg/codegen/nodejs.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func GenerateNodeJS(pg *PackageGenerator, name string) (map[string]*bytes.Buffer
3939
}
4040
pkg.Language[nodejsName], err = ijson.RawMessage(map[string]any{
4141
"moduleToPackage": moduleToPackage,
42+
"dependencies": map[string]string{
43+
"@pulumi/kubernetes": "^4.0.0",
44+
},
4245
})
4346
if err != nil {
4447
return nil, err
@@ -59,6 +62,18 @@ func GenerateNodeJS(pg *PackageGenerator, name string) (map[string]*bytes.Buffer
5962
}
6063
files["package.json"] = bytes.ReplaceAll(packageJSON, []byte("${VERSION}"), []byte(""))
6164

65+
// Pin the kubernetes provider version used
66+
utilities, ok := files["utilities.ts"]
67+
if !ok {
68+
return nil, fmt.Errorf("cannot find generated utilities.ts")
69+
}
70+
files["utilities.ts"] = bytes.ReplaceAll(utilities,
71+
[]byte("export function getVersion(): string {"),
72+
[]byte(`export const getVersion: () => string = () => "4.5.5"
73+
74+
function unusedGetVersion(): string {`),
75+
)
76+
6277
// Create a helper `meta/v1.ts` script that exports the ObjectMeta class from the SDK. If there happens to already
6378
// be a `meta/v1.ts` file, then just append the script.
6479
if code, ok := files[nodejsMetaPath]; !ok {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: apiextensions.k8s.io/v1
2+
kind: CustomResourceDefinition
3+
spec:
4+
group: k8sversion.pulumi.com
5+
names:
6+
plural: testresources
7+
singular: testresource
8+
kind: TestResource
9+
scope: Namespaced
10+
versions:
11+
- test:
12+
name: test
13+
schema:
14+
openAPIV3Schema:
15+
properties:
16+
testProperty:
17+
type: string`

tests/crds_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"testing"
2424

2525
"github.com/stretchr/testify/assert"
26+
"github.com/stretchr/testify/require"
2627
)
2728

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

118119
execCrd2Pulumi(t, "python", "crds/underscored-types/networkpolicy.yaml", validateUnderscore)
119120
}
121+
122+
func TestKubernetesVersionNodeJs(t *testing.T) {
123+
validateVersion := func(t *testing.T, path string) {
124+
// enter and build the generated package
125+
withDir(t, path, func() {
126+
127+
runRequireNoError(t, exec.Command("npm", "install"))
128+
runRequireNoError(t, exec.Command("npm", "run", "build"))
129+
130+
// extract the version returned by a resource
131+
appendFile(t, "bin/index.js", "\nconsole.log((new k8sversion.test.TestResource('test')).__version)")
132+
133+
version, err := exec.Command("node", "bin/index.js").Output()
134+
require.NoError(t, err)
135+
assert.Equal(t, "4.5.5\n", string(version))
136+
})
137+
}
138+
139+
execCrd2Pulumi(t, "nodejs", "crds/k8sversion/mock_crd.yaml", validateVersion)
140+
}
141+
142+
func withDir(t *testing.T, dir string, f func()) {
143+
pwd, err := os.Getwd()
144+
require.NoError(t, err)
145+
defer os.Chdir(pwd)
146+
147+
require.NoError(t, os.Chdir(dir))
148+
149+
f()
150+
}
151+
152+
func appendFile(t *testing.T, filename, content string) {
153+
// extract the version returned by a resource
154+
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0600)
155+
require.NoError(t, err)
156+
defer f.Close()
157+
158+
_, err = f.WriteString(content)
159+
}
160+
161+
func runRequireNoError(t *testing.T, cmd *exec.Cmd) {
162+
bytes, err := cmd.CombinedOutput()
163+
if err != nil {
164+
t.Log(bytes)
165+
}
166+
require.NoError(t, err)
167+
}

0 commit comments

Comments
 (0)