Skip to content

Commit

Permalink
Mark apiType, kind, and metadata as required
Browse files Browse the repository at this point in the history
  • Loading branch information
blampe committed Jul 1, 2024
1 parent c334f10 commit d31256b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 35 deletions.
4 changes: 2 additions & 2 deletions pkg/codegen/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var unneededDotNetFiles = []string{
}

func GenerateDotNet(pg *PackageGenerator, name string) (map[string]*bytes.Buffer, error) {
pkg := pg.SchemaPackageWithObjectMetaType()
pkg := pg.SchemaPackage(true)

// Set up C# namespaces
namespaces := map[string]string{}
Expand All @@ -61,7 +61,7 @@ func GenerateDotNet(pg *PackageGenerator, name string) (map[string]*bytes.Buffer
var err error
pkg.Language["csharp"], err = ijson.RawMessage(map[string]any{
"packageReferences": map[string]string{
"Pulumi": "3.*",
"Pulumi": "3.*",
"Pulumi.Kubernetes": "4.*",
},
"compatibility": "kubernetes20",
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func GenerateGo(pg *PackageGenerator, name string) (buffers map[string]*bytes.Bu
}
}()

pkg := pg.SchemaPackageWithObjectMetaType()
pkg := pg.SchemaPackage(true)
langName := "go"
oldName := pkg.Name
pkg.Name = name
Expand Down
7 changes: 4 additions & 3 deletions pkg/codegen/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ package codegen
import (
"bytes"
"fmt"
"regexp"

ijson "github.com/pulumi/crd2pulumi/internal/json"
"github.com/pulumi/crd2pulumi/internal/versions"
javaGen "github.com/pulumi/pulumi-java/pkg/codegen/java"
"regexp"
)

func GenerateJava(pg *PackageGenerator, name string) (map[string]*bytes.Buffer, error) {
pkg := pg.SchemaPackageWithObjectMetaType()
pkg := pg.SchemaPackage(true)

// These fields are required for the Java code generation
pkg.Description = "Generated Java SDK via crd2pulumi"
Expand Down Expand Up @@ -80,7 +81,7 @@ func GenerateJava(pg *PackageGenerator, name string) (map[string]*bytes.Buffer,
version = "4.9.0";
}`))

var unneededJavaFiles = []string{
unneededJavaFiles := []string{
"src/main/java/com/pulumi/" + name + "/Provider.java",
"src/main/java/com/pulumi/" + name + "/ProviderArgs.java",
"src/main/java/com/pulumi/kubernetes/meta/v1/inputs/ObjectMetaArgs.java",
Expand Down
10 changes: 6 additions & 4 deletions pkg/codegen/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ import (
"github.com/pulumi/pulumi/pkg/v3/codegen/nodejs"
)

const nodejsName = "nodejs"
const nodejsMetaPath = "meta/v1.ts"
const nodejsMetaFile = `import * as k8s from "@pulumi/kubernetes";
const (
nodejsName = "nodejs"
nodejsMetaPath = "meta/v1.ts"
nodejsMetaFile = `import * as k8s from "@pulumi/kubernetes";
export type ObjectMeta = k8s.types.input.meta.v1.ObjectMeta;
`
)

func GenerateNodeJS(pg *PackageGenerator, name string) (map[string]*bytes.Buffer, error) {
pkg := pg.SchemaPackage()
pkg := pg.SchemaPackage(false)
oldName := pkg.Name
pkg.Name = name
moduleToPackage, err := pg.ModuleToPackage()
Expand Down
35 changes: 12 additions & 23 deletions pkg/codegen/packagegenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ type PackageGenerator struct {
Types map[string]pschema.ComplexTypeSpec
// Version is the semver that will be stamped into the generated package
Version string
// schemaPackage is the Pulumi schema package used to generate code for
// languages that do not need an ObjectMeta type (NodeJS)
// schemaPackage is the cached Pulumi schema package used to generate code.
schemaPackage *pschema.Package
// schemaPackageWithObjectMetaType is the Pulumi schema package used to
// generate code for languages that need an ObjectMeta type (Python, Go, and .NET)
schemaPackageWithObjectMetaType *pschema.Package
}

// ReadPackagesFromSource reads one or more documents and returns a PackageGenerator that can be used to generate Pulumi code.
Expand Down Expand Up @@ -100,28 +96,17 @@ func ReadPackagesFromSource(version string, yamlSources []io.ReadCloser) (*Packa
return pg, nil
}

// SchemaPackage returns the Pulumi schema package with no ObjectMeta type.
// This is only necessary for NodeJS and Python.
func (pg *PackageGenerator) SchemaPackage() *pschema.Package {
// SchemaPackage returns the Pulumi schema package with
// an ObjectMeta type. This is only necessary for Go, .NET, Java and Python.
func (pg *PackageGenerator) SchemaPackage(withObjectMeta bool) *pschema.Package {
if pg.schemaPackage == nil {
pkg, err := genPackage(pg.Version, pg.Types, pg.ResourceTokens, false)
pkg, err := genPackage(pg.Version, pg.Types, pg.ResourceTokens, withObjectMeta)
contract.AssertNoErrorf(err, "could not parse Pulumi package")
pg.schemaPackage = pkg
}
return pg.schemaPackage
}

// SchemaPackageWithObjectMetaType returns the Pulumi schema package with
// an ObjectMeta type. This is only necessary for Go and .NET.
func (pg *PackageGenerator) SchemaPackageWithObjectMetaType() *pschema.Package {
if pg.schemaPackageWithObjectMetaType == nil {
pkg, err := genPackage(pg.Version, pg.Types, pg.ResourceTokens, true)
contract.AssertNoErrorf(err, "could not parse Pulumi package")
pg.schemaPackageWithObjectMetaType = pkg
}
return pg.schemaPackageWithObjectMetaType
}

// Returns language-specific 'ModuleToPackage' map. Creates a mapping from
// every groupVersion string <group>/<version> to <groupPrefix>/<version>.
func (pg *PackageGenerator) ModuleToPackage() (map[string]string, error) {
Expand Down Expand Up @@ -164,23 +149,27 @@ func (pg *PackageGenerator) GetTypes() map[string]pschema.ComplexTypeSpec {
types[resourceToken] = emptySpec
}
if foundProperties || preserveUnknownFields {
types[resourceToken].Properties["apiVersion"] = pschema.PropertySpec{
typ := types[resourceToken]
typ.Properties["apiVersion"] = pschema.PropertySpec{
TypeSpec: pschema.TypeSpec{
Type: String,
},
Const: crg.Group + "/" + version,
}
types[resourceToken].Properties["kind"] = pschema.PropertySpec{
typ.Properties["kind"] = pschema.PropertySpec{
TypeSpec: pschema.TypeSpec{
Type: String,
},
Const: crg.Kind,
}
types[resourceToken].Properties["metadata"] = pschema.PropertySpec{
typ.Properties["metadata"] = pschema.PropertySpec{
TypeSpec: pschema.TypeSpec{
Ref: objectMetaRef,
},
}
typ.Required = append(typ.Required, "apiVersion", "kind", "metadata")

types[resourceToken] = typ
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/codegen/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import pulumi_kubernetes.meta.v1.outputs
`

func GeneratePython(pg *PackageGenerator, name string) (map[string]*bytes.Buffer, error) {
pkg := pg.SchemaPackageWithObjectMetaType()
pkg := pg.SchemaPackage(true)

langName := "python"
oldName := pkg.Name
Expand Down Expand Up @@ -64,7 +64,7 @@ func GeneratePython(pg *PackageGenerator, name string) (map[string]*bytes.Buffer
pythonPackageDir := "pulumi_" + name

// Remove unneeded files
var unneededPythonFiles = []string{
unneededPythonFiles := []string{
filepath.Join(pythonPackageDir, "README.md"),
}
for _, unneededFile := range unneededPythonFiles {
Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func AddType(schema map[string]any, name string, types map[string]pschema.Comple
Properties: propertySpecs,
Required: required,
Description: description,
Language: make(map[string]pschema.RawMessage),
},
}
}
Expand Down

0 comments on commit d31256b

Please sign in to comment.