Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate provider SDK from Kubernetes v1.12 #293

Merged
merged 3 commits into from
Nov 26, 2018
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
10 changes: 8 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PROVIDER := pulumi-resource-${PACK}
CODEGEN := pulumi-gen-${PACK}
VERSION ?= $(shell scripts/get-version)
PYPI_VERSION := $(shell scripts/get-py-version)
KUBE_VERSION ?= v1.9.7
KUBE_VERSION ?= v1.12.2
SWAGGER_URL ?= https://github.com/kubernetes/kubernetes/raw/${KUBE_VERSION}/api/openapi-spec/swagger.json
OPENAPI_DIR := pkg/gen/openapi-specs
OPENAPI_FILE := ${OPENAPI_DIR}/swagger-${KUBE_VERSION}.json
Expand All @@ -31,6 +31,8 @@ $(OPENAPI_FILE)::
build:: $(OPENAPI_FILE)
$(GO) install $(VERSION_FLAGS) $(PROJECT)/cmd/$(PROVIDER)
$(GO) install $(VERSION_FLAGS) $(PROJECT)/cmd/$(CODEGEN)
# Delete only files and folders that are generated.
rm -r sdk/python/pulumi_kubernetes/*/ sdk/python/pulumi_kubernetes/__init__.py
for LANGUAGE in "nodejs" "python" ; do \
$(CODEGEN) $$LANGUAGE $(OPENAPI_FILE) pkg/gen/$${LANGUAGE}-templates $(PACKDIR) || exit 3 ; \
done
Expand Down
14 changes: 7 additions & 7 deletions pkg/gen/nodejs-templates/provider.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ export namespace yaml {
*/
{{#Groups}}
{{#Versions}}
{{#Kinds}}
{{#KindsAndAliases}}
public getResource(groupVersionKind: "{{RawAPIVersion}}/{{Kind}}", name: string): pulumi.Output<{{Group}}.{{Version}}.{{Kind}}>;
public getResource(groupVersionKind: "{{RawAPIVersion}}/{{Kind}}", namespace: string, name: string): pulumi.Output<{{Group}}.{{Version}}.{{Kind}}>;
{{/Kinds}}
{{/KindsAndAliases}}
{{/Versions}}
{{/Groups}}
public getResource(groupVersionKind: string, namespaceOrName: string, name?: string): pulumi.Output<pulumi.CustomResource> {
Expand All @@ -107,12 +107,12 @@ export namespace yaml {
*/
{{#Groups}}
{{#Versions}}
{{#Kinds}}
{{#KindsAndAliases}}
{{#Properties}}
public getResourceProperty(groupVersionKind: "{{RawAPIVersion}}/{{Kind}}", name: string, property: "{{LanguageName}}"): pulumi.Output<{{{PropType}}}>;
public getResourceProperty(groupVersionKind: "{{RawAPIVersion}}/{{Kind}}", namespace: string, name: string, property: "{{LanguageName}}"): pulumi.Output<{{{PropType}}}>;
{{/Properties}}
{{/Kinds}}
{{/KindsAndAliases}}
{{/Versions}}
{{/Groups}}
public getResourceProperty(groupVersionKind: string, namespaceOrName: string, nameOrProperty: string, property?: string): pulumi.Output<any> {
Expand Down Expand Up @@ -241,13 +241,13 @@ export namespace yaml {
switch (`${apiVersion}/${kind}`) {
{{#Groups}}
{{#Versions}}
{{#Kinds}}
{{#KindsAndAliases}}
case "{{RawAPIVersion}}/{{Kind}}":
return [{
name: `{{RawAPIVersion}}/{{Kind}}::${id}`,
resource: new {{Group}}.{{Version}}.{{Kind}}(id, obj, opts),
}];
{{/Kinds}}
{{/KindsAndAliases}}
{{/Versions}}
{{/Groups}}
default:
Expand Down Expand Up @@ -414,7 +414,7 @@ export namespace {{Group}} {
{{#Properties}}
inputs["{{Name}}"] = {{{DefaultValue}}};
{{/Properties}}
super("kubernetes:{{APIVersion}}:{{Kind}}", name, inputs, opts);
super("kubernetes:{{URNAPIVersion}}:{{Kind}}", name, inputs, opts);
this.__inputs = <any>args;
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gen/python-templates/kind.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class {{Kind}}(pulumi.CustomResource):
{{/OptionalProperties}}

super({{Kind}}, self).__init__(
"kubernetes:{{APIVersion}}:{{Kind}}",
"kubernetes:{{URNAPIVersion}}:{{Kind}}",
__name__,
__props__,
__opts__)
Expand Down
100 changes: 61 additions & 39 deletions pkg/gen/typegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strings"

linq "github.com/ahmetb/go-linq"
"github.com/jinzhu/copier"
wordwrap "github.com/mitchellh/go-wordwrap"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
Expand All @@ -27,7 +28,10 @@ import (
const (
object = "object"
stringT = "string"
str = "str"
)

const (
apiRegistration = "apiregistration.k8s.io"
)

// --------------------------------------------------------------------------
Expand Down Expand Up @@ -68,6 +72,26 @@ func (vc *VersionConfig) Version() string { return vc.version }
// `apps/v1beta1` has the `Deployment` kind, etc.).
func (vc *VersionConfig) Kinds() []*KindConfig { return vc.kinds }

// KindsAndAliases will produce a list of kinds, including aliases (e.g., both `apiregistration` and
// `apiregistration.k8s.io`).
func (vc *VersionConfig) KindsAndAliases() []*KindConfig {
kindsAndAliases := []*KindConfig{}
for _, kind := range vc.kinds {
kindsAndAliases = append(kindsAndAliases, kind)
if strings.HasPrefix(kind.APIVersion(), apiRegistration) {
alias := KindConfig{}
err := copier.Copy(&alias, kind)
if err != nil {
panic(err)
}
rawAPIVersion := "apiregistration" + strings.TrimPrefix(kind.APIVersion(), apiRegistration)
alias.rawAPIVersion = rawAPIVersion
kindsAndAliases = append(kindsAndAliases, &alias)
}
}
return kindsAndAliases
}

// APIVersion returns the fully-qualified apiVersion (e.g., `storage.k8s.io/v1` for storage, etc.)
func (vc *VersionConfig) APIVersion() string { return vc.apiVersion }

Expand Down Expand Up @@ -116,6 +140,15 @@ func (kc *KindConfig) APIVersion() string { return kc.apiVersion }
// RawAPIVersion returns the "raw" apiVersion (e.g., `v1` rather than `core/v1`).
func (kc *KindConfig) RawAPIVersion() string { return kc.rawAPIVersion }

// URNAPIVersion returns API version that can be used in a URN (e.g., using the backwards-compatible
// alias `apiextensions` instead of `apiextensions.k8s.io`).
func (kc *KindConfig) URNAPIVersion() string {
if strings.HasPrefix(kc.apiVersion, apiRegistration) {
return "apiregistration" + strings.TrimPrefix(kc.apiVersion, apiRegistration)
}
return kc.apiVersion
}

// TypeGuard returns the text of a TypeScript type guard for the given kind.
func (kc *KindConfig) TypeGuard() string { return kc.typeGuard }

Expand Down Expand Up @@ -244,14 +277,32 @@ func makeTypescriptType(prop map[string]interface{}, opts groupOpts) string {
}

ref := stripPrefix(prop["$ref"].(string))
if ref == "io.k8s.apimachinery.pkg.api.resource.Quantity" {
const (
apiextensionsV1beta1 = "io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1"
quantity = "io.k8s.apimachinery.pkg.api.resource.Quantity"
intOrString = "io.k8s.apimachinery.pkg.util.intstr.IntOrString"
v1Time = "io.k8s.apimachinery.pkg.apis.meta.v1.Time"
v1MicroTime = "io.k8s.apimachinery.pkg.apis.meta.v1.MicroTime"
v1beta1JSONSchemaPropsOrBool = apiextensionsV1beta1 + ".JSONSchemaPropsOrBool"
v1beta1JSONSchemaPropsOrArray = apiextensionsV1beta1 + ".JSONSchemaPropsOrArray"
v1beta1JSON = apiextensionsV1beta1 + ".JSON"
v1beta1CRSubresourceStatus = apiextensionsV1beta1 + ".CustomResourceSubresourceStatus"
)

switch ref {
case quantity:
return stringT
} else if ref == "io.k8s.apimachinery.pkg.util.intstr.IntOrString" {
case intOrString:
return "number | string"
} else if ref == "io.k8s.apimachinery.pkg.apis.meta.v1.Time" ||
ref == "io.k8s.apimachinery.pkg.apis.meta.v1.MicroTime" {
case v1Time, v1MicroTime:
// TODO: Automatically deserialized with `DateConstructor`.
return stringT
case v1beta1JSONSchemaPropsOrBool:
return "apiextensions.v1beta1.JSONSchemaProps | boolean"
hausdorff marked this conversation as resolved.
Show resolved Hide resolved
case v1beta1JSONSchemaPropsOrArray:
return "apiextensions.v1beta1.JSONSchemaProps | any[]"
case v1beta1JSON, v1beta1CRSubresourceStatus:
return "any"
}

gvk := gvkFromRef(ref)
Expand All @@ -261,44 +312,12 @@ func makeTypescriptType(prop map[string]interface{}, opts groupOpts) string {
return fmt.Sprintf("%s.%s.%s.%s", refPrefix, gvk.Group, gvk.Version, gvk.Kind)
}

func makePythonType(prop map[string]interface{}, opts groupOpts) string {
if opts.generatorType != provider {
panic("Python does not support output or input types")
}

if t, exists := prop["type"]; exists {
tstr := t.(string)
if tstr == "array" {
return "list"
} else if tstr == "integer" {
return "int"
} else if tstr == object {
return "dict"
} else if tstr == stringT {
return str
}
return tstr
}

ref := stripPrefix(prop["$ref"].(string))
if ref == "io.k8s.apimachinery.pkg.api.resource.Quantity" {
return str
} else if ref == "io.k8s.apimachinery.pkg.util.intstr.IntOrString" {
return object
} else if ref == "io.k8s.apimachinery.pkg.apis.meta.v1.Time" ||
ref == "io.k8s.apimachinery.pkg.apis.meta.v1.MicroTime" {
// TODO: Automatically deserialized with `DateConstructor`.
return str
}
return "dict"
}

func makeType(prop map[string]interface{}, opts groupOpts) string {
switch opts.language {
case typescript:
return makeTypescriptType(prop, opts)
case python:
return makePythonType(prop, opts)
panic("Python does not support output or input types")
default:
panic("Unrecognized generator type")
}
Expand Down Expand Up @@ -429,16 +448,19 @@ func createGroups(definitionsJSON map[string]interface{}, opts groupOpts) []*Gro
}

var prefix string
var t string
switch opts.language {
case typescript:
prefix = " "
t = makeType(prop, opts)
case python:
prefix = " "
// Python currently does not emit types for use.
}

return &Property{
comment: fmtComment(prop["description"], prefix, opts),
propType: makeType(prop, opts),
propType: t,
name: propName,
languageName: pyName(propName),
defaultValue: defaultValue,
Expand Down
Loading