Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 94 additions & 39 deletions provider/cmd/pulumi-gen-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import (
"text/template"
"unicode"

"github.com/pulumi/pulumi-kubernetes/provider/v4/pkg/gen"
providerVersion "github.com/pulumi/pulumi-kubernetes/provider/v4/pkg/version"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/pulumi/pulumi/pkg/v3/codegen"
dotnetgen "github.com/pulumi/pulumi/pkg/v3/codegen/dotnet"
gogen "github.com/pulumi/pulumi/pkg/v3/codegen/go"
Expand All @@ -39,8 +40,9 @@ import (
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/pulumi/pulumi-kubernetes/provider/v4/pkg/gen"
providerVersion "github.com/pulumi/pulumi-kubernetes/provider/v4/pkg/version"
)

// TemplateDir is the path to the base directory for code generator templates.
Expand Down Expand Up @@ -174,7 +176,11 @@ func generateSchema(swaggerPath string) schema.PackageSpec {
}

// Generate schema
return gen.PulumiSchema(schemaMap, gen.WithResourceOverlays(gen.ResourceOverlays), gen.WithTypeOverlays(gen.TypeOverlays))
return gen.PulumiSchema(
schemaMap,
gen.WithResourceOverlays(gen.ResourceOverlays),
gen.WithTypeOverlays(gen.TypeOverlays),
)
}

// This is to mostly filter resources from the spec.
Expand Down Expand Up @@ -210,7 +216,8 @@ func writeNodeJSClient(pkg *schema.Package, outdir, templateDir string) {
Token: resource.Token,
}
for _, property := range resource.Properties {
// hack(levi): manually remove `| undefined` from the ConstValue and Package until https://github.com/pulumi/pulumi-kubernetes/issues/1650 is resolved.
// hack(levi): manually remove `| undefined` from the ConstValue and Package until
// https://github.com/pulumi/pulumi-kubernetes/issues/1650 is resolved.
cv := strings.TrimSuffix(property.ConstValue, " | undefined")
pkg := strings.TrimSuffix(property.Package, " | undefined")

Expand All @@ -231,11 +238,18 @@ func writeNodeJSClient(pkg *schema.Package, outdir, templateDir string) {
templateResources.Packages = packages.SortedValues()

overlays := map[string][]byte{
"apiextensions/customResource.ts": mustLoadFile(filepath.Join(templateDir, "apiextensions", "customResource.ts")),
"apiextensions/customResourcePatch.ts": mustLoadFile(filepath.Join(templateDir, "apiextensions", "customResourcePatch.ts")),
"helm/v3/helm.ts": mustLoadFile(filepath.Join(templateDir, "helm", "v3", "helm.ts")),
"kustomize/kustomize.ts": mustLoadFile(filepath.Join(templateDir, "kustomize", "kustomize.ts")),
"yaml/yaml.ts": mustRenderTemplate(filepath.Join(templateDir, "yaml", "yaml.tmpl"), templateResources),
"apiextensions/customResource.ts": mustLoadFile(
filepath.Join(templateDir, "apiextensions", "customResource.ts"),
),
"apiextensions/customResourcePatch.ts": mustLoadFile(
filepath.Join(templateDir, "apiextensions", "customResourcePatch.ts"),
),
"helm/v3/helm.ts": mustLoadFile(filepath.Join(templateDir, "helm", "v3", "helm.ts")),
"kustomize/kustomize.ts": mustLoadFile(filepath.Join(templateDir, "kustomize", "kustomize.ts")),
"yaml/yaml.ts": mustRenderTemplate(
filepath.Join(templateDir, "yaml", "yaml.tmpl"),
templateResources,
),
}
files, err := nodejsgen.GeneratePackage("pulumigen", pkg, overlays, nil, false, nil)
if err != nil {
Expand Down Expand Up @@ -278,11 +292,18 @@ func writePythonClient(pkg *schema.Package, outdir string, templateDir string) {
})

overlays := map[string][]byte{
"apiextensions/CustomResource.py": mustLoadFile(filepath.Join(templateDir, "apiextensions", "CustomResource.py")),
"apiextensions/CustomResourcePatch.py": mustLoadFile(filepath.Join(templateDir, "apiextensions", "CustomResourcePatch.py")),
"helm/v3/helm.py": mustLoadFile(filepath.Join(templateDir, "helm", "v3", "helm.py")),
"kustomize/kustomize.py": mustLoadFile(filepath.Join(templateDir, "kustomize", "kustomize.py")),
"yaml/yaml.py": mustRenderTemplate(filepath.Join(templateDir, "yaml", "yaml.tmpl"), templateResources),
"apiextensions/CustomResource.py": mustLoadFile(
filepath.Join(templateDir, "apiextensions", "CustomResource.py"),
),
"apiextensions/CustomResourcePatch.py": mustLoadFile(
filepath.Join(templateDir, "apiextensions", "CustomResourcePatch.py"),
),
"helm/v3/helm.py": mustLoadFile(filepath.Join(templateDir, "helm", "v3", "helm.py")),
"kustomize/kustomize.py": mustLoadFile(filepath.Join(templateDir, "kustomize", "kustomize.py")),
"yaml/yaml.py": mustRenderTemplate(
filepath.Join(templateDir, "yaml", "yaml.tmpl"),
templateResources,
),
}

files, err := pythongen.GeneratePackage("pulumigen", pkg, overlays, nil)
Expand Down Expand Up @@ -318,19 +339,28 @@ func writeDotnetClient(pkg *schema.Package, outdir, templateDir string) {
return templateResources.Resources[i].Token < templateResources.Resources[j].Token
})
overlays := map[string][]byte{
"ApiExtensions/CustomResource.cs": mustLoadFile(filepath.Join(templateDir, "apiextensions", "CustomResource.cs")),
"ApiExtensions/CustomResourcePatch.cs": mustLoadFile(filepath.Join(templateDir, "apiextensions", "CustomResourcePatch.cs")),
"Helm/ChartBase.cs": mustLoadFile(filepath.Join(templateDir, "helm", "ChartBase.cs")),
"Helm/Unwraps.cs": mustLoadFile(filepath.Join(templateDir, "helm", "Unwraps.cs")),
"Helm/V3/Chart.cs": mustLoadFile(filepath.Join(templateDir, "helm", "v3", "Chart.cs")),
"Helm/V3/Invokes.cs": mustLoadFile(filepath.Join(templateDir, "helm", "v3", "Invokes.cs")),
"Kustomize/Directory.cs": mustLoadFile(filepath.Join(templateDir, "kustomize", "Directory.cs")),
"Kustomize/Invokes.cs": mustLoadFile(filepath.Join(templateDir, "kustomize", "Invokes.cs")),
"Yaml/ConfigFile.cs": mustLoadFile(filepath.Join(templateDir, "yaml", "ConfigFile.cs")),
"Yaml/ConfigGroup.cs": mustLoadFile(filepath.Join(templateDir, "yaml", "ConfigGroup.cs")),
"Yaml/Invokes.cs": mustLoadFile(filepath.Join(templateDir, "yaml", "Invokes.cs")),
"Yaml/TransformationAction.cs": mustLoadFile(filepath.Join(templateDir, "yaml", "TransformationAction.cs")),
"Yaml/Yaml.cs": mustRenderTemplate(filepath.Join(templateDir, "yaml", "yaml.tmpl"), templateResources),
"ApiExtensions/CustomResource.cs": mustLoadFile(
filepath.Join(templateDir, "apiextensions", "CustomResource.cs"),
),
"ApiExtensions/CustomResourcePatch.cs": mustLoadFile(
filepath.Join(templateDir, "apiextensions", "CustomResourcePatch.cs"),
),
"Helm/ChartBase.cs": mustLoadFile(filepath.Join(templateDir, "helm", "ChartBase.cs")),
"Helm/Unwraps.cs": mustLoadFile(filepath.Join(templateDir, "helm", "Unwraps.cs")),
"Helm/V3/Chart.cs": mustLoadFile(filepath.Join(templateDir, "helm", "v3", "Chart.cs")),
"Helm/V3/Invokes.cs": mustLoadFile(filepath.Join(templateDir, "helm", "v3", "Invokes.cs")),
"Kustomize/Directory.cs": mustLoadFile(filepath.Join(templateDir, "kustomize", "Directory.cs")),
"Kustomize/Invokes.cs": mustLoadFile(filepath.Join(templateDir, "kustomize", "Invokes.cs")),
"Yaml/ConfigFile.cs": mustLoadFile(filepath.Join(templateDir, "yaml", "ConfigFile.cs")),
"Yaml/ConfigGroup.cs": mustLoadFile(filepath.Join(templateDir, "yaml", "ConfigGroup.cs")),
"Yaml/Invokes.cs": mustLoadFile(filepath.Join(templateDir, "yaml", "Invokes.cs")),
"Yaml/TransformationAction.cs": mustLoadFile(
filepath.Join(templateDir, "yaml", "TransformationAction.cs"),
),
"Yaml/Yaml.cs": mustRenderTemplate(
filepath.Join(templateDir, "yaml", "yaml.tmpl"),
templateResources,
),
}

files, err := dotnetgen.GeneratePackage("pulumigen", pkg, overlays, nil)
Expand Down Expand Up @@ -404,18 +434,32 @@ func writeGoClient(pkg *schema.Package, outdir string, templateDir string) {
})

files["kubernetes/customPulumiTypes.go"] = mustLoadGoFile(filepath.Join(templateDir, "customPulumiTypes.go"))
files["kubernetes/apiextensions/customResource.go"] = mustLoadGoFile(filepath.Join(templateDir, "apiextensions", "customResource.go"))
files["kubernetes/apiextensions/customResourcePatch.go"] = mustLoadGoFile(filepath.Join(templateDir, "apiextensions", "customResourcePatch.go"))
files["kubernetes/apiextensions/customResource.go"] = mustLoadGoFile(
filepath.Join(templateDir, "apiextensions", "customResource.go"),
)
files["kubernetes/apiextensions/customResourcePatch.go"] = mustLoadGoFile(
filepath.Join(templateDir, "apiextensions", "customResourcePatch.go"),
)
files["kubernetes/helm/v3/chart.go"] = mustLoadGoFile(filepath.Join(templateDir, "helm", "v3", "chart.go"))
// Rename pulumiTypes.go to avoid conflict with schema generated Helm Release types.
files["kubernetes/helm/v3/chartPulumiTypes.go"] = mustLoadGoFile(filepath.Join(templateDir, "helm", "v3", "pulumiTypes.go"))
files["kubernetes/helm/v3/chartPulumiTypes.go"] = mustLoadGoFile(
filepath.Join(templateDir, "helm", "v3", "pulumiTypes.go"),
)
files["kubernetes/kustomize/directory.go"] = mustLoadGoFile(filepath.Join(templateDir, "kustomize", "directory.go"))
files["kubernetes/kustomize/pulumiTypes.go"] = mustLoadGoFile(filepath.Join(templateDir, "kustomize", "pulumiTypes.go"))
files["kubernetes/kustomize/pulumiTypes.go"] = mustLoadGoFile(
filepath.Join(templateDir, "kustomize", "pulumiTypes.go"),
)
files["kubernetes/yaml/configFile.go"] = mustLoadGoFile(filepath.Join(templateDir, "yaml", "configFile.go"))
files["kubernetes/yaml/configGroup.go"] = mustLoadGoFile(filepath.Join(templateDir, "yaml", "configGroup.go"))
files["kubernetes/yaml/transformation.go"] = mustLoadGoFile(filepath.Join(templateDir, "yaml", "transformation.go"))
files["kubernetes/yaml/yaml.go"] = mustRenderGoTemplate(filepath.Join(templateDir, "yaml", "yaml.tmpl"), templateResources)
files["kubernetes/yaml/v2/kinds.go"] = mustRenderGoTemplate(filepath.Join(templateDir, "yaml", "v2", "kinds.tmpl"), templateResources)
files["kubernetes/yaml/yaml.go"] = mustRenderGoTemplate(
filepath.Join(templateDir, "yaml", "yaml.tmpl"),
templateResources,
)
files["kubernetes/yaml/v2/kinds.go"] = mustRenderGoTemplate(
filepath.Join(templateDir, "yaml", "v2", "kinds.tmpl"),
templateResources,
)

mustWriteFiles(outdir, files)
}
Expand Down Expand Up @@ -459,7 +503,8 @@ func mustRenderGoTemplate(path string, resources any) []byte {
}

func genK8sResourceTypes(pkg *schema.Package) {
groupVersions, kinds, patchKinds, listKinds := codegen.NewStringSet(), codegen.NewStringSet(), codegen.NewStringSet(), codegen.NewStringSet()
groupVersions, kinds, patchKinds, listKinds := codegen.NewStringSet(), codegen.NewStringSet(),
codegen.NewStringSet(), codegen.NewStringSet()
for _, resource := range pkg.Resources {
if resourcesToFilterFromTemplate.Has(resource.Token) {
continue
Expand All @@ -484,7 +529,11 @@ func genK8sResourceTypes(pkg *schema.Package) {
kinds.Add(kind)
}

gvk := gen.GVK{Kinds: kinds.SortedValues(), PatchKinds: patchKinds.SortedValues(), ListKinds: listKinds.SortedValues()}
gvk := gen.GVK{
Kinds: kinds.SortedValues(),
PatchKinds: patchKinds.SortedValues(),
ListKinds: listKinds.SortedValues(),
}
gvStrings := groupVersions.SortedValues()
for _, gvString := range gvStrings {
gvk.GroupVersions = append(gvk.GroupVersions, gen.GroupVersion(gvString))
Expand Down Expand Up @@ -743,7 +792,9 @@ func mustWriteTerraformMapping(pkgSpec schema.PackageSpec) {
// converter only cares about a few fields and is tolerant of missing fields. We get the terraform
// kubernetes schema by running `terraform providers schema -json` in a minimal terraform project that
// defines a kubernetes provider.
rawTerraformSchema := mustLoadFile(filepath.Join(BaseDir, "provider", "cmd", "pulumi-gen-kubernetes", "terraform.json"))
rawTerraformSchema := mustLoadFile(
filepath.Join(BaseDir, "provider", "cmd", "pulumi-gen-kubernetes", "terraform.json"),
)

var terraformSchema TerraformSchema
err := json.Unmarshal(rawTerraformSchema, &terraformSchema)
Expand Down Expand Up @@ -778,5 +829,9 @@ func mustWriteTerraformMapping(pkgSpec schema.PackageSpec) {
panic(err)
}

mustWriteFile(BaseDir, filepath.Join("provider", "cmd", "pulumi-resource-kubernetes", "terraform-mapping.json"), data)
mustWriteFile(
BaseDir,
filepath.Join("provider", "cmd", "pulumi-resource-kubernetes", "terraform-mapping.json"),
data,
)
}
Loading
Loading