Skip to content

Commit

Permalink
Merge pull request #38 from pulumi/upgrade-to-pulumiv3
Browse files Browse the repository at this point in the history
Upgrade to Pulumi v3.0
  • Loading branch information
stack72 authored Apr 21, 2021
2 parents e646398 + 8677c14 commit b20c611
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 36 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
PROJECT := github.com/pulumi/crd2pulumi

GO ?= go
GOMODULE = GO111MODULE=on

ensure::
$(GOMODULE) $(GO) mod tidy
$(GO) mod download

build::
$(GOMODULE) $(GO) build $(PROJECT)
$(GO) build $(PROJECT)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ import (
crontabs_v1 "crds-go-final/crontabs/stable/v1"

meta_v1 "github.com/pulumi/pulumi-kubernetes/sdk/v2/go/kubernetes/meta/v1"
"github.com/pulumi/pulumi/sdk/v2/go/pulumi"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion gen/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"strings"

"github.com/pkg/errors"
"github.com/pulumi/pulumi/pkg/v2/codegen/dotnet"
"github.com/pulumi/pulumi/pkg/v3/codegen/dotnet"
)

var unneededDotNetFiles = []string{
Expand Down
8 changes: 4 additions & 4 deletions gen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"unicode"

"github.com/pkg/errors"
pschema "github.com/pulumi/pulumi/pkg/v2/codegen/schema"
"github.com/pulumi/pulumi/sdk/v2/go/common/util/contract"
pschema "github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
unstruct "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

Expand Down Expand Up @@ -113,8 +113,8 @@ type PackageGenerator struct {
// GroupVersions is a slice of the names of every CustomResource's versions,
// in the format <group>/<version>
GroupVersions []string
// Types is a mapping from every type's token name to its ObjectTypeSpec
Types map[string]pschema.ObjectTypeSpec
// Types is a mapping from every type's token name to its ComplexTypeSpec
Types map[string]pschema.ComplexTypeSpec
// schemaPackage is the Pulumi schema package used to generate code for
// languages that do not need an ObjectMeta type (NodeJS)
schemaPackage *pschema.Package
Expand Down
4 changes: 2 additions & 2 deletions gen/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package gen
import (
"bytes"
"github.com/pkg/errors"
"github.com/pulumi/pulumi/pkg/v2/codegen"
go_gen "github.com/pulumi/pulumi/pkg/v2/codegen/go"
"github.com/pulumi/pulumi/pkg/v3/codegen"
go_gen "github.com/pulumi/pulumi/pkg/v3/codegen/go"
"path/filepath"
)

Expand Down
2 changes: 1 addition & 1 deletion gen/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package gen
import (
"bytes"
"github.com/pkg/errors"
"github.com/pulumi/pulumi/pkg/v2/codegen/nodejs"
"github.com/pulumi/pulumi/pkg/v3/codegen/nodejs"
)

const nodejsMetaPath = "meta/v1.ts"
Expand Down
4 changes: 2 additions & 2 deletions gen/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"path/filepath"

"github.com/pkg/errors"
"github.com/pulumi/pulumi/pkg/v2/codegen/python"
"github.com/pulumi/pulumi/sdk/v2/go/common/util/contract"
"github.com/pulumi/pulumi/pkg/v3/codegen/python"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)

const pythonMetaFile = `from pulumi_kubernetes.meta.v1._inputs import *
Expand Down
39 changes: 22 additions & 17 deletions gen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"strings"

"github.com/pkg/errors"
pschema "github.com/pulumi/pulumi/pkg/v2/codegen/schema"
pschema "github.com/pulumi/pulumi/pkg/v3/codegen/schema"
unstruct "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

Expand All @@ -46,9 +46,11 @@ var arbitraryJSONTypeSpec = pschema.TypeSpec{
AdditionalProperties: &anyTypeSpec,
}

var emptyObjectSpec = pschema.ObjectTypeSpec{
Type: Object,
Properties: map[string]pschema.PropertySpec{},
var emptySpec = pschema.ComplexTypeSpec{
ObjectTypeSpec: pschema.ObjectTypeSpec{
Type: Object,
Properties: map[string]pschema.PropertySpec{},
},
}

const objectMetaRef = "#/types/kubernetes:meta/v1:ObjectMeta"
Expand All @@ -66,8 +68,8 @@ var intOrStringTypeSpec = pschema.TypeSpec{
},
}

func (pg *PackageGenerator) GetTypes() map[string]pschema.ObjectTypeSpec {
types := map[string]pschema.ObjectTypeSpec{}
func (pg *PackageGenerator) GetTypes() map[string]pschema.ComplexTypeSpec {
types := map[string]pschema.ComplexTypeSpec{}
for _, crg := range pg.CustomResourceGenerators {
for version, schema := range crg.Schemas {
resourceToken := getToken(crg.Group, version, crg.Kind)
Expand All @@ -77,7 +79,7 @@ func (pg *PackageGenerator) GetTypes() map[string]pschema.ObjectTypeSpec {
}
preserveUnknownFields, _, _ := unstruct.NestedBool(schema, "x-kubernetes-preserve-unknown-fields")
if preserveUnknownFields {
types[resourceToken] = emptyObjectSpec
types[resourceToken] = emptySpec
}
if foundProperties || preserveUnknownFields {
types[resourceToken].Properties["apiVersion"] = pschema.PropertySpec{
Expand Down Expand Up @@ -106,19 +108,21 @@ func (pg *PackageGenerator) GetTypes() map[string]pschema.ObjectTypeSpec {
// Returns the Pulumi package given a types map and a slice of the token types
// of every CustomResource. If includeObjectMetaType is true, then a
// ObjectMetaType type is also generated.
func genPackage(types map[string]pschema.ObjectTypeSpec, resourceTokens []string, includeObjectMetaType bool) (*pschema.Package, error) {
func genPackage(types map[string]pschema.ComplexTypeSpec, resourceTokens []string, includeObjectMetaType bool) (*pschema.Package, error) {
if includeObjectMetaType {
types[objectMetaToken] = pschema.ObjectTypeSpec{
Type: "object",
types[objectMetaToken] = pschema.ComplexTypeSpec{
ObjectTypeSpec: pschema.ObjectTypeSpec{
Type: "object",
},
}
}

resources := map[string]pschema.ResourceSpec{}
for _, baseRef := range resourceTokens {
objectTypeSpec := types[baseRef]
complexTypeSpec := types[baseRef]
resources[baseRef] = pschema.ResourceSpec{
ObjectTypeSpec: objectTypeSpec,
InputProperties: objectTypeSpec.Properties,
ObjectTypeSpec: complexTypeSpec.ObjectTypeSpec,
InputProperties: complexTypeSpec.Properties,
}
}

Expand Down Expand Up @@ -149,7 +153,7 @@ func isAnyType(typeSpec pschema.TypeSpec) bool {
// AddType converts the given OpenAPI `schema` to a ObjectTypeSpec and adds it
// to the `types` map under the given `name`. Recursively converts and adds all
// nested schemas as well.
func AddType(schema map[string]interface{}, name string, types map[string]pschema.ObjectTypeSpec) {
func AddType(schema map[string]interface{}, name string, types map[string]pschema.ComplexTypeSpec) {
properties, foundProperties, _ := unstruct.NestedMap(schema, "properties")
description, _, _ := unstruct.NestedString(schema, "description")
schemaType, _, _ := unstruct.NestedString(schema, "type")
Expand All @@ -172,19 +176,20 @@ func AddType(schema map[string]interface{}, name string, types map[string]pschem
schemaType = Object
}

types[name] = pschema.ObjectTypeSpec{
types[name] = pschema.ComplexTypeSpec{
ObjectTypeSpec: pschema.ObjectTypeSpec{
Type: schemaType,
Properties: propertySpecs,
Required: required,
Description: description,
}
}}
}

// GetTypeSpec returns the corresponding pschema.TypeSpec for a OpenAPI v3
// schema. Handles nested pschema.TypeSpecs in case the schema type is an array,
// object, or "combined schema" (oneOf, allOf, anyOf). Also recursively converts
// and adds all schemas of type object to the types map.
func GetTypeSpec(schema map[string]interface{}, name string, types map[string]pschema.ObjectTypeSpec) pschema.TypeSpec {
func GetTypeSpec(schema map[string]interface{}, name string, types map[string]pschema.ComplexTypeSpec) pschema.TypeSpec {
if schema == nil {
return anyTypeSpec
}
Expand Down
2 changes: 1 addition & 1 deletion gen/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"unicode"

"github.com/pkg/errors"
"github.com/pulumi/pulumi/sdk/v2/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
unstruct "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.16

require (
github.com/pkg/errors v0.9.1
github.com/pulumi/pulumi/pkg/v2 v2.10.1
github.com/pulumi/pulumi/sdk/v2 v2.9.2
github.com/pulumi/pulumi/pkg/v3 v3.0.0
github.com/pulumi/pulumi/sdk/v3 v3.0.0
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.6.1
Expand Down
Loading

0 comments on commit b20c611

Please sign in to comment.