Skip to content

Commit

Permalink
Fix a panic due to non-primitive defaults (#136)
Browse files Browse the repository at this point in the history
The Pulumi schema doesn't support default values for non-primitive types
-- objects and arrays, specifically.

This PR fixes the panic by only setting a default value when the object
is primitive.

Fixes #61.
Fixes #68.
Fixes #125.
Fixes #105.
  • Loading branch information
blampe authored May 14, 2024
1 parent 6d25f60 commit 13bd703
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
name: ci
on:
pull_request:
branches:
- master
push:
branches:
- master

jobs:
ci:
runs-on: macos-latest
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

## Unreleased

- Fix invalid generated code due to unnamed properties. [#135](https://github.com/pulumi/crd2pulumi/pull/135)
- Fix unpinned Kubernetes version in generated nodejs resources. [#121](https://github.com/pulumi/crd2pulumi/pull/121)
- Fix .NET generated code to use provider v4. [#134](https://github.com/pulumi/crd2pulumi/pull/134)
- Fix invalid generated code due to unnamed properties. [#135](https://github.com/pulumi/crd2pulumi/pull/135)
- Fix a panic when generating code with non-primitive defaults. [#136](https://github.com/pulumi/crd2pulumi/pull/136)
- Add Java generation support. [#129](https://github.com/pulumi/crd2pulumi/pull/129)

## 1.3.0 (2023-12-12)
Expand Down
9 changes: 7 additions & 2 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,14 @@ func AddType(schema map[string]any, name string, types map[string]pschema.Comple
}
propertySchema, _, _ := unstructured.NestedMap(properties, propertyName)
propertyDescription, _, _ := unstructured.NestedString(propertySchema, "description")
defaultValue, _, _ := unstructured.NestedFieldNoCopy(propertySchema, "default")
typeSpec := GetTypeSpec(propertySchema, name+strcase.ToCamel(propertyName), types)
// Pulumi's schema doesn't support defaults for objects, so ignore them.
var defaultValue any
if !(typeSpec.Type == "object" || typeSpec.Type == "array") {
defaultValue, _, _ = unstructured.NestedFieldNoCopy(propertySchema, "default")
}
propertySpecs[propertyName] = pschema.PropertySpec{
TypeSpec: GetTypeSpec(propertySchema, name+camelCase, types),
TypeSpec: typeSpec,
Description: propertyDescription,
Default: defaultValue,
}
Expand Down
8 changes: 8 additions & 0 deletions tests/crds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ func TestCRDsFromUrl(t *testing.T) {
name: "VictoriaMetrics",
url: "https://raw.githubusercontent.com/VictoriaMetrics/helm-charts/fdb7dfe/charts/victoria-metrics-operator/crd.yaml",
},
{
name: "GatewayClasses",
url: "https://raw.githubusercontent.com/kubernetes-sigs/gateway-api/v0.3.0/config/crd/bases/networking.x-k8s.io_gatewayclasses.yaml",
},
{
name: "Contours",
url: "https://raw.githubusercontent.com/projectcontour/contour-operator/f8c07498803d062e30c255976270cbc82cd619b0/config/crd/bases/operator.projectcontour.io_contours.yaml",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 13bd703

Please sign in to comment.