Skip to content

Commit

Permalink
[jvm/sdk] [WIP] Java codegen
Browse files Browse the repository at this point in the history
- add Java codegen support

Limitations:
- non-generated overlays are missing, e.g. Yaml and Helm support
  • Loading branch information
pawelprazak committed Jan 25, 2022
1 parent 241ddef commit 2e08c5e
Show file tree
Hide file tree
Showing 1,577 changed files with 183,813 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Pulumi.*.yaml
yarn.lock
ci-scripts
/nuget/
.gradle
32 changes: 30 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,19 @@ dotnet_sdk::
$(WORKING_DIR)/bin/$(CODEGEN) -version=${DOTNET_VERSION} dotnet $(SCHEMA_FILE) $(CURDIR)
rm -rf sdk/dotnet/bin/Debug
cd ${PACKDIR}/dotnet/&& \
echo "${DOTNET_VERSION}" >version.txt && \
echo "${DOTNET_VERSION}" > version.txt && \
dotnet build /p:Version=${DOTNET_VERSION}

go_sdk::
$(WORKING_DIR)/bin/$(CODEGEN) -version=${VERSION} go $(SCHEMA_FILE) $(CURDIR)

jvm_sdk::
$(WORKING_DIR)/bin/$(CODEGEN) -version=${VERSION} jvm $(SCHEMA_FILE) $(CURDIR)
cd $(WORKING_DIR)/sdk/jvm && \
mkdir -p src/main/resources/io/pulumi/$(PACK) && \
echo "${VERSION}" > src/main/resources/io/pulumi/$(PACK)/version.txt && \
gradle -Pversion=${VERSION} build

nodejs_sdk:: VERSION := $(shell pulumictl get version --language javascript)
nodejs_sdk::
$(WORKING_DIR)/bin/$(CODEGEN) -version=${VERSION} nodejs $(SCHEMA_FILE) $(CURDIR)
Expand Down Expand Up @@ -94,9 +101,21 @@ lint::
pushd $$DIR && golangci-lint run -c ../.golangci.yml --timeout 10m && popd ; \
done

install:: install_nodejs_sdk install_dotnet_sdk
install:: install_nodejs_sdk install_dotnet_sdk install_jvm_sdk
cp $(WORKING_DIR)/bin/${PROVIDER} ${GOPATH}/bin

manual_provider_install::
pulumi plugin install resource $(PACK) v$(VERSION) --server "$(PROJECT)/releases/download"

install_local::
mkdir -p $(HOME)/.pulumi/plugins/resource-$(PACK)-v$(VERSION)
touch $(HOME)/.pulumi/plugins/resource-$(PACK)-v$(VERSION).lock
cp -v $(WORKING_DIR)/bin/pulumi-resource-$(PACK) $(HOME)/.pulumi/plugins/resource-$(PACK)-v$(VERSION)/${PROVIDER}
pulumi plugin ls | grep $(PACK)

cleanup_local::
pulumi plugin rm resource $(PACK) $(VERSION) --yes

GO_TEST_FAST := go test -short -v -count=1 -cover -timeout 2h -parallel ${TESTPARALLELISM}
GO_TEST := go test -v -count=1 -cover -timeout 2h -parallel ${TESTPARALLELISM}

Expand All @@ -106,13 +125,15 @@ test_fast::
cd provider/pkg && $(GO_TEST_FAST) ./...
cd tests/sdk/nodejs && $(GO_TEST_FAST) ./...
cd tests/sdk/python && $(GO_TEST_FAST) ./...
#cd tests/sdk/jvm && $(GO_TEST_FAST) ./...
cd tests/sdk/dotnet && $(GO_TEST_FAST) ./...
cd tests/sdk/go && $(GO_TEST_FAST) ./...

test_all::
cd provider/pkg && $(GO_TEST) ./...
cd tests/sdk/nodejs && $(GO_TEST) ./...
cd tests/sdk/python && $(GO_TEST) ./...
#cd tests/sdk/jvm && $(GO_TEST) ./...
cd tests/sdk/dotnet && $(GO_TEST) ./...
cd tests/sdk/go && $(GO_TEST) ./...

Expand All @@ -132,3 +153,10 @@ install_go_sdk::
install_nodejs_sdk::
-yarn unlink --cwd $(WORKING_DIR)/sdk/nodejs/bin
yarn link --cwd $(WORKING_DIR)/sdk/nodejs/bin

install_jvm_sdk::
cd $(WORKING_DIR)/sdk/jvm && gradle -Pversion=${VERSION} publishToMavenLocal

hack_local_deps::
cd provider && go mod edit -replace github.com/pulumi/pulumi/pkg/v3=${HOME}/repos/vl/pulumi/pkg
cd provider && go mod edit -replace github.com/pulumi/pulumi/sdk/v3=${HOME}/repos/vl/pulumi/sdk
36 changes: 36 additions & 0 deletions provider/cmd/pulumi-gen-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"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"
jvmcodegen "github.com/pulumi/pulumi/pkg/v3/codegen/jvm"
nodejsgen "github.com/pulumi/pulumi/pkg/v3/codegen/nodejs"
pythongen "github.com/pulumi/pulumi/pkg/v3/codegen/python"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
Expand Down Expand Up @@ -63,6 +64,7 @@ const (
DotNet Language = "dotnet"
Go Language = "go"
NodeJS Language = "nodejs"
JVM Language = "jvm"
Kinds Language = "kinds"
Python Language = "python"
Schema Language = "schema"
Expand Down Expand Up @@ -99,6 +101,9 @@ func main() {
case Python:
templateDir := filepath.Join(TemplateDir, "python-templates")
writePythonClient(readSchema(inputFile, version), outdir, templateDir)
case JVM:
templateDir := filepath.Join(TemplateDir, "jvm-templates")
writeJVMClient(readSchema(inputFile, version), outdir, templateDir)
case DotNet:
templateDir := filepath.Join(TemplateDir, "dotnet-templates")
writeDotnetClient(readSchema(inputFile, version), outdir, templateDir)
Expand Down Expand Up @@ -248,6 +253,37 @@ func writePythonClient(pkg *schema.Package, outdir string, templateDir string) {
mustWriteFiles(outdir, files)
}

func writeJVMClient(pkg *schema.Package, outdir, templateDir string) {
resources, err := jvmcodegen.LanguageResources("pulumigen", pkg)
if err != nil {
panic(err)
}

templateResources := gen.TemplateResources{}
for _, resource := range resources {
r := gen.TemplateResource{
Name: resource.Name,
Package: resource.Package,
Token: resource.Token,
}
templateResources.Resources = append(templateResources.Resources, r)
}
sort.Slice(templateResources.Resources, func(i, j int) bool {
return templateResources.Resources[i].Token < templateResources.Resources[j].Token
})
// TODO: add missing overlays
overlays := map[string][]byte{
"src/main/java/io/pulumi/kubernetes/KubernetesResource.java": mustLoadFile(filepath.Join(templateDir, "KubernetesResource.java")),
"src/main/java/io/pulumi/kubernetes/yaml/Yaml.java": mustRenderTemplate(filepath.Join(templateDir, "yaml", "Yaml.tmpl"), templateResources),
}
files, err := jvmcodegen.GeneratePackage("pulumigen", pkg, overlays)
if err != nil {
panic(err)
}

mustWriteFiles(outdir, files)
}

func writeDotnetClient(pkg *schema.Package, outdir, templateDir string) {
resources, err := dotnetgen.LanguageResources("pulumigen", pkg)
if err != nil {
Expand Down
56 changes: 56 additions & 0 deletions provider/cmd/pulumi-resource-kubernetes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -42457,6 +42457,62 @@
"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/storage/v1beta1": "storagev1beta1"
}
},
"jvm": {
"compatibility": "kubernetes20",
"packages": {
"admissionregistration.k8s.io/v1": "admissionregistration.v1",
"admissionregistration.k8s.io/v1beta1": "admissionregistration.v1beta1",
"apiextensions.k8s.io/v1": "apiextensions.v1",
"apiextensions.k8s.io/v1beta1": "apiextensions.v1beta1",
"apiregistration.k8s.io/v1": "apiregistration.v1",
"apiregistration.k8s.io/v1beta1": "apiregistration.v1beta1",
"apps/v1": "apps.v1",
"apps/v1beta1": "apps.v1beta1",
"apps/v1beta2": "apps.v1beta2",
"auditregistration.k8s.io/v1alpha1": "auditregistration.v1alpha1",
"authentication.k8s.io/v1": "authentication.v1",
"authentication.k8s.io/v1beta1": "authentication.v1beta1",
"authorization.k8s.io/v1": "authorization.v1",
"authorization.k8s.io/v1beta1": "authorization.v1beta1",
"autoscaling/v1": "autoscaling.v1",
"autoscaling/v2beta1": "autoscaling.v2beta1",
"autoscaling/v2beta2": "autoscaling.v2beta2",
"batch/v1": "batch.v1",
"batch/v1beta1": "batch.v1beta1",
"batch/v2alpha1": "batch.v2alpha1",
"certificates.k8s.io/v1": "certificates.v1",
"certificates.k8s.io/v1beta1": "certificates.v1beta1",
"coordination.k8s.io/v1": "coordination.v1",
"coordination.k8s.io/v1beta1": "coordination.v1beta1",
"core/v1": "core.v1",
"discovery.k8s.io/v1": "discovery.v1",
"discovery.k8s.io/v1beta1": "discovery.v1beta1",
"events.k8s.io/v1": "events.v1",
"events.k8s.io/v1beta1": "events.v1beta1",
"extensions/v1beta1": "extensions.v1beta1",
"flowcontrol.apiserver.k8s.io/v1alpha1": "flowcontrol.v1alpha1",
"flowcontrol.apiserver.k8s.io/v1beta1": "flowcontrol.v1beta1",
"meta/v1": "meta.v1",
"networking.k8s.io/v1": "networking.v1",
"networking.k8s.io/v1beta1": "networking.v1beta1",
"node.k8s.io/v1": "node.v1",
"node.k8s.io/v1alpha1": "node.v1alpha1",
"node.k8s.io/v1beta1": "node.v1beta1",
"pkg/version": "pkg.version",
"policy/v1": "policy.v1",
"policy/v1beta1": "policy.v1beta1",
"rbac.authorization.k8s.io/v1": "rbac.v1",
"rbac.authorization.k8s.io/v1alpha1": "rbac.v1alpha1",
"rbac.authorization.k8s.io/v1beta1": "rbac.v1beta1",
"scheduling.k8s.io/v1": "scheduling.v1",
"scheduling.k8s.io/v1alpha1": "scheduling.v1alpha1",
"scheduling.k8s.io/v1beta1": "scheduling.v1beta1",
"settings.k8s.io/v1alpha1": "settings.v1alpha1",
"storage.k8s.io/v1": "storage.v1",
"storage.k8s.io/v1alpha1": "storage.v1alpha1",
"storage.k8s.io/v1beta1": "storage.v1beta1"
}
},
"nodejs": {
"compatibility": "kubernetes20",
"dependencies": {
Expand Down
2 changes: 2 additions & 0 deletions provider/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ replace (
github.com/docker/distribution => github.com/docker/distribution v0.0.0-20191216044856-a8371794149d
github.com/docker/docker => github.com/moby/moby v17.12.0-ce-rc1.0.20200618181300-9dc6525e6118+incompatible
github.com/evanphx/json-patch => github.com/evanphx/json-patch v4.9.0+incompatible
github.com/pulumi/pulumi/pkg/v3 => /Users/pprazak/repos/vl/pulumi/pkg
github.com/pulumi/pulumi/sdk/v3 => /Users/pprazak/repos/vl/pulumi/sdk
)
24 changes: 24 additions & 0 deletions provider/pkg/gen/jvm-templates/KubernetesResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.pulumi.kubernetes;

import io.pulumi.resources.CustomResource;
import io.pulumi.resources.CustomResourceOptions;
import io.pulumi.resources.ResourceArgs;

import javax.annotation.Nullable;

// TODO
public abstract class KubernetesResource extends CustomResource {
protected KubernetesResource(String type, String name, @Nullable ResourceArgs args, boolean dependency) {
super(type, name, args, dependency);
}

protected KubernetesResource(String type, String name, @Nullable ResourceArgs args, @Nullable CustomResourceOptions options) {
super(type, name, args, options);
}

protected KubernetesResource(String type, String name, @Nullable ResourceArgs args, @Nullable CustomResourceOptions options, boolean dependency) {
super(type, name, args, options, dependency);
}

// TODO
}
5 changes: 5 additions & 0 deletions provider/pkg/gen/jvm-templates/yaml/Yaml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.pulumi.kubernetes.yaml;

public final class Yaml {
// TODO
}
13 changes: 10 additions & 3 deletions provider/pkg/gen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func PulumiSchema(swagger map[string]interface{}) pschema.PackageSpec {
},
},
Description: "The contents of a kubeconfig file or the path to a kubeconfig file.",
TypeSpec: pschema.TypeSpec{Type: "string"},
TypeSpec: pschema.TypeSpec{Type: "string"},
Language: map[string]json.RawMessage{
"csharp": rawMessage(map[string]interface{}{
"name": "KubeConfig",
Expand Down Expand Up @@ -139,6 +139,7 @@ func PulumiSchema(swagger map[string]interface{}) pschema.PackageSpec {
goImportPath := "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes"

csharpNamespaces := map[string]string{}
jvmPackages := map[string]string{}
modToPkg := map[string]string{}
pkgImportAliases := map[string]string{}

Expand All @@ -154,6 +155,7 @@ func PulumiSchema(swagger map[string]interface{}) pschema.PackageSpec {
tok := fmt.Sprintf(`kubernetes:%s:%s`, kind.apiVersion, kind.kind)

csharpNamespaces[kind.apiVersion] = fmt.Sprintf("%s.%s", pascalCase(group.Group()), pascalCase(version.Version()))
jvmPackages[kind.apiVersion] = fmt.Sprintf("%s.%s", group.Group(), version.Version())
modToPkg[kind.apiVersion] = kind.schemaPkgName
pkgImportAliases[fmt.Sprintf("%s/%s", goImportPath, kind.schemaPkgName)] = strings.Replace(
kind.schemaPkgName, "/", "", -1)
Expand Down Expand Up @@ -250,8 +252,8 @@ func PulumiSchema(swagger map[string]interface{}) pschema.PackageSpec {

pkg.Language["csharp"] = rawMessage(map[string]interface{}{
"packageReferences": map[string]string{
"Glob": "1.1.5",
"Pulumi": "3.*",
"Glob": "1.1.5",
"Pulumi": "3.*",
},
"namespaces": csharpNamespaces,
"compatibility": kubernetes20,
Expand All @@ -263,6 +265,11 @@ func PulumiSchema(swagger map[string]interface{}) pschema.PackageSpec {
"packageImportAliases": pkgImportAliases,
"generateResourceContainerTypes": true,
})
pkg.Language["jvm"] = rawMessage(map[string]interface{}{
// TODO: add deps
"packages": jvmPackages,
"compatibility": kubernetes20,
})
pkg.Language["nodejs"] = rawMessage(map[string]interface{}{
"compatibility": kubernetes20,
"dependencies": map[string]string{
Expand Down
3 changes: 3 additions & 0 deletions sdk/jvm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
README.md
build
src/main/resources/io/pulumi/kubernetes/version.txt
51 changes: 51 additions & 0 deletions sdk/jvm/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// *** WARNING: this file was generated. ***
// *** Do not edit by hand unless you're certain you know what you are doing! ***

plugins {
id("java-library")
id("maven-publish")
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
}
}

repositories {
maven { // The google mirror is less flaky than mavenCentral()
url("https://maven-central.storage-download.googleapis.com/maven2/")
}
mavenCentral()
mavenLocal()
}

dependencies {
implementation("io.pulumi:pulumi:3.6.0+")
implementation("com.google.code.findbugs:jsr305:3.0.2")
api("com.google.guava:guava:30.1-jre") // FIXME: do we really want to expose this dep?
api("com.google.code.gson:gson:2.8.6") // make sure we don't clash with grpc deps

def junitVersion = "5.7.2"
testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
testImplementation("org.junit.jupiter:junit-jupiter-params:${junitVersion}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
testImplementation("org.assertj:assertj-core:3.20.2")
}

test {
useJUnitPlatform()
}

publishing {
publications {
mavenJava(MavenPublication) {
groupId = 'io.pulumi'
artifactId = 'kubernetes'
version = project.version

from components.java
}
// TODO pom
}
}
14 changes: 14 additions & 0 deletions sdk/jvm/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// *** WARNING: this file was generated. ***
// *** Do not edit by hand unless you're certain you know what you are doing! ***

pluginManagement {
repositories {
maven { // The google mirror is less flaky than mavenCentral()
url("https://maven-central.storage-download.googleapis.com/maven2/")
}
gradlePluginPortal()
}
}

rootProject.name = "io.pulumi.kubernetes"
include("lib")
32 changes: 32 additions & 0 deletions sdk/jvm/src/main/java/io/pulumi/kubernetes/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// *** WARNING: this file was generated by pulumigen. ***
// *** Do not edit by hand unless you're certain you know what you are doing! ***

package io.pulumi.kubernetes;

import java.util.Optional;

public final class Config {

private static final io.pulumi.Config config = io.pulumi.Config.of("kubernetes");
public Optional<String> cluster() {
return config.get("cluster");
}
public Optional<String> context() {
return config.get("context");
}
public Optional<Boolean> enableDryRun() {
return config.getBoolean("enableDryRun");
}
public Optional<String> kubeconfig() {
return config.get("kubeconfig");
}
public Optional<String> namespace() {
return config.get("namespace");
}
public Optional<String> renderYamlToDirectory() {
return config.get("renderYamlToDirectory");
}
public Optional<Boolean> suppressDeprecationWarnings() {
return config.getBoolean("suppressDeprecationWarnings");
}
}

0 comments on commit 2e08c5e

Please sign in to comment.