Skip to content

Commit

Permalink
more wip
Browse files Browse the repository at this point in the history
  • Loading branch information
blampe committed Jul 10, 2024
1 parent ea4e1a6 commit 715a515
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 130 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/pulumi/pulumi/sdk/v3 v3.121.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8
golang.org/x/text v0.16.0
k8s.io/apimachinery v0.30.0
)
Expand Down Expand Up @@ -76,6 +77,7 @@ require (
github.com/edsrzf/mmap-go v1.1.0 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/ettle/strcase v0.1.1 // indirect
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
Expand Down Expand Up @@ -206,7 +208,6 @@ require (
gocloud.dev v0.37.0 // indirect
gocloud.dev/secrets/hashivault v0.37.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/ettle/strcase v0.1.1 h1:htFueZyVeE1XNnMEfbqp5r67qAN/4r6ya1ysq8Q+Zcw=
github.com/ettle/strcase v0.1.1/go.mod h1:hzDLsPC7/lwKyBOywSHEP89nt2pDgdy+No1NBA9o9VY=
github.com/evanphx/json-patch v5.7.0+incompatible h1:vgGkfT/9f8zE6tvSCe74nfpAVDQ2tG6yudJd8LBksgI=
github.com/evanphx/json-patch v5.7.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
Expand Down
25 changes: 0 additions & 25 deletions pkg/codegen/dotnet.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/crd2pulumi/internal/versions"
"github.com/pulumi/pulumi/pkg/v3/codegen/dotnet"
"golang.org/x/text/cases"
Expand Down Expand Up @@ -51,35 +50,11 @@ func GenerateDotNet(pg *PackageGenerator, name string) (map[string]*bytes.Buffer
}
namespaces["meta/v1"] = "Meta.V1"

// Configure C# language-specific settings. Notice that we set
// `compatibility` to `kubernetes20`. This is because the actual ObjectMeta
// class defined in the .NET SDK is located at
// `Pulumi.Kubernetes.Types.Outputs.Meta.V1.ObjectMeta`. This path would
// only get generated properly if `compatibility` was `kubernetes20`.
oldName := pkg.Name
pkg.Name = name
var err error
pkg.Language["csharp"], err = ijson.RawMessage(map[string]any{
"packageReferences": map[string]string{
"Pulumi": "3.*",
"Pulumi.Kubernetes": "4.*",
},
"compatibility": "kubernetes20",
"dictionaryConstructors": true,
"namespaces": namespaces,
})
if err != nil {
return nil, fmt.Errorf("failed to marshal JSON message: %w", err)
}

files, err := dotnet.GeneratePackage(PulumiToolName, pkg, nil, nil)
if err != nil {
return nil, fmt.Errorf("could not generate .NET package: %w", err)
}

pkg.Name = oldName
delete(pkg.Language, "csharp")

namespaceName := dotnet.Title(name)
files["KubernetesResource.cs"] = []byte(kubernetesResource(namespaceName))
files["Utilities.cs"] = []byte(dotNetUtilities(namespaceName))
Expand Down
25 changes: 0 additions & 25 deletions pkg/codegen/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"path/filepath"
"strings"

ijson "github.com/pulumi/crd2pulumi/internal/json"
"github.com/pulumi/pulumi/pkg/v3/codegen"
goGen "github.com/pulumi/pulumi/pkg/v3/codegen/go"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
Expand Down Expand Up @@ -49,27 +48,6 @@ func GenerateGo(pg *PackageGenerator, name string) (buffers map[string]*bytes.Bu
}()

pkg := pg.SchemaPackage(true)
langName := "go"
oldName := pkg.Name
pkg.Name = name
moduleToPackage, err := pg.ModuleToPackage()
if err != nil {
return nil, fmt.Errorf("%w", err)
}
moduleToPackage["meta/v1"] = "meta/v1"

jsonData, err := ijson.RawMessage(map[string]any{
"importBasePath": "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes",
"internalModuleName": "utilities",
"moduleToPackage": moduleToPackage,
"packageImportAliases": map[string]any{
"github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/meta/v1": "metav1",
},
})
if err != nil {
return nil, fmt.Errorf("failed to marshal language metadata: %w", err)
}
pkg.Language[langName] = jsonData

files, err := goGen.GeneratePackage("crd2pulumi", pkg)
if err != nil {
Expand All @@ -81,9 +59,6 @@ func GenerateGo(pg *PackageGenerator, name string) (buffers map[string]*bytes.Bu
return nil, fmt.Errorf("could not get package root: %w", err)
}

pkg.Name = oldName
delete(pkg.Language, langName)

buffers = map[string]*bytes.Buffer{}
for path, code := range files {
newPath, _ := filepath.Rel(name, path)
Expand Down
15 changes: 0 additions & 15 deletions pkg/codegen/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"regexp"

ijson "github.com/pulumi/crd2pulumi/internal/json"
"github.com/pulumi/crd2pulumi/internal/versions"
javaGen "github.com/pulumi/pulumi-java/pkg/codegen/java"
)
Expand All @@ -46,29 +45,15 @@ func GenerateJava(pg *PackageGenerator, name string) (map[string]*bytes.Buffer,
}
packages["meta/v1"] = "meta.v1"

langName := "java"
oldName := pkg.Name
pkg.Name = name

jsonData, err := ijson.RawMessage(map[string]any{
"buildFiles": "gradle",
"dependencies": map[string]string{
"com.pulumi:kubernetes": "4.9.0",
},
"packages": packages,
})
if err != nil {
return nil, fmt.Errorf("failed to marshal language metadata: %w", err)
}
pkg.Language[langName] = jsonData

files, err := javaGen.GeneratePackage("crd2pulumi", pkg, nil)
if err != nil {
return nil, fmt.Errorf("could not generate Java package: %w", err)
}

pkg.Name = oldName
delete(pkg.Language, langName)

// Pin the kubernetes provider version used
utilsPath := "src/main/java/com/pulumi/" + name + "/Utilities.java"
Expand Down
19 changes: 0 additions & 19 deletions pkg/codegen/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,12 @@ export type ObjectMeta = k8s.types.input.meta.v1.ObjectMeta;

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

pkg.Name = oldName
delete(pkg.Language, nodejsName)

// Remove ${VERSION} in package.json
packageJSON, ok := files["package.json"]
if !ok {
Expand Down
74 changes: 46 additions & 28 deletions pkg/codegen/packagegenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"github.com/pulumi/pulumi-kubernetes/provider/v4/pkg/gen"

Check failure on line 25 in pkg/codegen/packagegenerator.go

View workflow job for this annotation

GitHub Actions / ci

github.com/pulumi/pulumi-kubernetes/provider/[email protected]: replacement directory /Users/bryce/src/pulumi-kubernetes/provider does not exist
pschema "github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"golang.org/x/exp/maps"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

// PackageGenerator generates code for multiple CustomResources
Expand Down Expand Up @@ -108,6 +110,8 @@ func (pg *PackageGenerator) SchemaPackage(withObjectMeta bool) *pschema.Package
pkg, err := genPackage(pg.Version, pg.Types, pg.ResourceTokens, pg.language, withObjectMeta)
contract.AssertNoErrorf(err, "could not parse Pulumi package")
pg.schemaPackage = pkg

// TODO: dotnet := unstructured.NestedStringMap(pkg.Language, "csharp", "packageReferences")
}
return pg.schemaPackage
}
Expand Down Expand Up @@ -145,44 +149,58 @@ func (pg *PackageGenerator) GetTypes() map[string]pschema.ComplexTypeSpec {
definitions := map[string]any{"definitions": defs}

for _, crg := range pg.CustomResourceGenerators {
// schema := map[string]any{"definitions": map[string]any{}}
// for _, tok := range crg.ResourceTokens {
// schema["definitions"][tok] = crg.
// }

for version, spec := range crg.Schemas {
token := fmt.Sprintf("%s/%s:%s", crg.Group, version, crg.Kind)
spec["x-kubernetes-group-version-kind"] = []any{map[string]any{
"group": crg.Group,
"version": version,
"kind": crg.Kind,
}}

if meta, ok, _ := unstructured.NestedMap(spec, "properties", "metadata"); ok {
meta["$ref"] = "#/types/kubernetes:meta/v1:ObjectMeta"
delete(meta, "type")
unstructured.SetNestedMap(spec, meta, "properties", "metadata")
}

defs[token] = spec
}
}

pspec := gen.PulumiSchema(definitions)

for name, t := range pspec.Types {
pg.ResourceTokens = append(pg.ResourceTokens, name)
gvk := gen.GVKFromRef(name)
pg.ResourceTokens = maps.Keys(pspec.Types)

t.Properties["apiVersion"] = pschema.PropertySpec{
TypeSpec: pschema.TypeSpec{
Type: String,
},
Const: gvk.Group + "/" + gvk.Version,
}
t.Properties["kind"] = pschema.PropertySpec{
TypeSpec: pschema.TypeSpec{
Type: String,
},
Const: gvk.Kind,
}
t.Properties["metadata"] = pschema.PropertySpec{
TypeSpec: pschema.TypeSpec{
Ref: objectMetaRef,
},
}
t.Required = append(t.Required, "apiVersion", "kind", "metadata")
// for _, r := range pspec.Resources {
// for _, p := range r.Properties {
// pspec.Types p.TypeSpec
// }
// }
// for name, t := range pspec.Types {
// pg.ResourceTokens = append(pg.ResourceTokens, name)
// gvk := gen.GVKFromRef(name)

pspec.Types[name] = t
}
// t.Properties["apiVersion"] = pschema.PropertySpec{
// TypeSpec: pschema.TypeSpec{
// Type: String,
// },
// Const: gvk.Group + "/" + gvk.Version,
// }
// t.Properties["kind"] = pschema.PropertySpec{
// TypeSpec: pschema.TypeSpec{
// Type: String,
// },
// Const: gvk.Kind,
// }
// t.Properties["metadata"] = pschema.PropertySpec{
// TypeSpec: pschema.TypeSpec{
// Ref: objectMetaRef,
// },
// }
// t.Required = append(t.Required, "apiVersion", "kind", "metadata")

// pspec.Types[name] = t
// }

/*
fmt.Println(pspec)
Expand Down
17 changes: 0 additions & 17 deletions pkg/codegen/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ import pulumi_kubernetes.meta.v1.outputs
func GeneratePython(pg *PackageGenerator, name string) (map[string]*bytes.Buffer, error) {
pkg := pg.SchemaPackage(true)

langName := "python"
oldName := pkg.Name
pkg.Name = name

moduleToPackage, err := pg.ModuleToPackage()
if err != nil {
return nil, fmt.Errorf("%w", err)
Expand All @@ -45,16 +41,6 @@ func GeneratePython(pg *PackageGenerator, name string) (map[string]*bytes.Buffer
fmt.Println(l, string(b.(json.RawMessage)))
}
fmt.Println("moduleNameOverrides", moduleToPackage)
// pkg.Language[langName], err = ijson.RawMessage(map[string]any{
// "compatibility": "kubernetes20",
// "moduleNameOverrides": moduleToPackage,
// "requires": map[string]string{
// "pulumi": "\u003e=3.0.0,\u003c4.0.0",
// "pyyaml": "\u003e=5.3",
// "requests": "\u003e=2.21.0,\u003c2.22.0",
// },
// "ignorePyNamePanic": true,
// })
if err != nil {
return nil, fmt.Errorf("failed to marshal language metadata: %w", err)
}
Expand All @@ -64,9 +50,6 @@ func GeneratePython(pg *PackageGenerator, name string) (map[string]*bytes.Buffer
return nil, fmt.Errorf("could not generate Go package: %w", err)
}

pkg.Name = oldName
delete(pkg.Language, langName)

pythonPackageDir := "pulumi_" + name

// Remove unneeded files
Expand Down

0 comments on commit 715a515

Please sign in to comment.