Skip to content

Commit

Permalink
wip to get everything working
Browse files Browse the repository at this point in the history
  • Loading branch information
rquitales committed Sep 12, 2024
1 parent 870be09 commit 873d042
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/pulumi/pulumi/pkg/v3 v3.130.0
github.com/pulumi/pulumi/sdk/v3 v3.130.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
golang.org/x/text v0.17.0
k8s.io/apiextensions-apiserver v0.31.0
k8s.io/apimachinery v0.31.0
Expand Down Expand Up @@ -196,6 +195,7 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/texttheater/golang-levenshtein v1.0.1 // indirect
github.com/tweekmonster/luser v0.0.0-20161003172636-3fa38070dbd7 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
Expand Down
14 changes: 11 additions & 3 deletions pkg/codegen/customresourcegenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,20 @@ func flattenRecursively(sw *spec.Swagger, parentName string, currSpec spec.Schem

// Recurse through the properties of the object.
for nestedPropertyName, nestedProperty := range currSpec.Properties {
// VistoriaMetrics has some weird fields - likely a typegen issue on their end, so let's skip them.
if nestedPropertyName == "-" {
delete(currSpec.Properties, nestedPropertyName)
continue
}
// Create a new definition for the nested object by joining the parent definition name and the property name.
// This is to ensure that the nested object is unique and does not conflict with other definitions.
nestedDefinitionName := parentName + sanitizeFieldName(nestedPropertyName)

// if nestedPropertyName == "enforcers" {
// log.Println("enforcers found: ", nestedDefinitionName)
// if nestedPropertyName == "bearerTokenSecret" {
// log.Println("bearerTokenSecret found: ", nestedDefinitionName)
// g := json.NewEncoder(os.Stdout)
// g.SetIndent("", " ")
// g.Encode(nestedProperty)
// }

s, err := flattenRecursively(sw, nestedDefinitionName, nestedProperty)
Expand Down Expand Up @@ -189,7 +197,7 @@ func crdToOpenAPI(crd *extensionv1.CustomResourceDefinition) ([]*spec.Swagger, e
continue
}
// Defaults are not pruned here, but before being served.
sw, err := builder.BuildOpenAPIV2(crd, v.Name, builder.Options{V2: true, StripValueValidation: false, StripNullable: false, AllowNonStructural: true})
sw, err := builder.BuildOpenAPIV2(crd, v.Name, builder.Options{V2: true, StripValueValidation: true, StripNullable: true, AllowNonStructural: true, IncludeSelectableFields: true})
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/codegen/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"path"
"path/filepath"
"runtime/debug"
"strings"

ijson "github.com/pulumi/crd2pulumi/internal/json"
Expand All @@ -44,7 +45,7 @@ var UnneededGoFiles = codegen.NewStringSet(
func GenerateGo(pg *PackageGenerator, name string) (buffers map[string]*bytes.Buffer, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
err = fmt.Errorf("%v\n%s", r, string(debug.Stack()))
}
}()

Expand Down
15 changes: 1 addition & 14 deletions pkg/codegen/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"fmt"

ijson "github.com/pulumi/crd2pulumi/internal/json"
"github.com/pulumi/pulumi/pkg/v3/codegen/nodejs"
)

Expand All @@ -27,25 +26,13 @@ const nodejsMetaPath = "meta/v1.ts"
const nodejsMetaFile = `import * as k8s from "@pulumi/kubernetes";
export type ObjectMeta = k8s.types.input.meta.v1.ObjectMeta;
export type ObjectMetaPatch = k8s.types.input.meta.v1.ObjectMetaPatch;
`

func GenerateNodeJS(pg *PackageGenerator, name string) (map[string]*bytes.Buffer, error) {
pkg := pg.SchemaPackage()
oldName := pkg.Name
pkg.Name = name
moduleToPackage, err := pg.ModuleToPackage()
if err != nil {
return nil, fmt.Errorf("%w", err)
}
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
}

files, err := nodejs.GeneratePackage(PulumiToolName, pkg, nil, nil, true)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -128,8 +129,24 @@ func genPackage(version string, crgenerators []CustomResourceGenerator, includeO
// See: https://github.com/pulumi/crd2pulumi/issues/43
pkgSpec := gen.PulumiSchema(unstructuredOpenAPISchema, gen.WithAllowHyphens(true))

// Populate the package spec with information used in previous versions of crd2pulumi to maintain backwards
// compatibility.
pkgSpec.Name = DefaultName
pkgSpec.Version = version
pkgSpec.Config = pschema.ConfigSpec{}
pkgSpec.Provider = pschema.ResourceSpec{}

// Save a serialized version of the package for debugging purposes.
// Save the unflattened OpenAPI spec for debugging purposes.
f := "/Users/rquitales/code/environs/pulumi-gen-sdk/new/pulumi-new-defaults.json"
file, err := os.Create(f)
if err != nil {
return nil, fmt.Errorf("error opening file: %w", err)
}
defer file.Close()
en := json.NewEncoder(file)
en.SetIndent("", " ")
en.Encode(pkgSpec)

if !includeObjectMetaType {
delete(pkgSpec.Types, objectMetaToken)
Expand Down
2 changes: 1 addition & 1 deletion tests/crds/k8sversion/mock_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ spec:
openAPIV3Schema:
properties:
testProperty:
type: string`
type: string

0 comments on commit 873d042

Please sign in to comment.