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

Various Python codegen fixes #771

Merged
merged 2 commits into from
Aug 29, 2019
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

### Bug fixes

- Fix name collisions in the Charts/YAML Python packages
(https://github.com/pulumi/pulumi-kubernetes/pull/771).
- Implement `{ConfigFile, ConfigGroup, Chart}#get_resource`
(https://github.com/pulumi/pulumi-kubernetes/pull/771).

## 1.0.0-rc.1 (August 28, 2019)

### Supported Kubernetes versions
Expand Down
27 changes: 25 additions & 2 deletions pkg/gen/python-templates/helm/v2/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ class Chart(pulumi.ComponentResource):
none of Tiller's server-side validity testing is executed.
"""

resources: pulumi.Output[dict]
"""
Kubernetes resources contained in this Chart.
"""

def __init__(self, release_name, config, opts=None):
"""
Create an instance of the specified Helm chart.
Expand Down Expand Up @@ -440,5 +445,23 @@ def __init__(self, release_name, config, opts=None):
# Note: Unlike NodeJS, Python requires that we "pull" on our futures in order to get them scheduled for
# execution. In order to do this, we leverage the engine's RegisterResourceOutputs to wait for the
# resolution of all resources that this Helm chart created.
resources = all_config.apply(_parse_chart)
self.register_outputs({"output": resources})
self.resources = all_config.apply(_parse_chart)
self.register_outputs({"resources": self.resources})

def get_resource(self, group_version_kind, name, namespace=None) -> pulumi.Output[pulumi.CustomResource]:
"""
get_resource returns a resource defined by a built-in Kubernetes group/version/kind and
name. For example: `get_resource("apps/v1/Deployment", "nginx")`

:param str group_version_kind: Group/Version/Kind of the resource, e.g., `apps/v1/Deployment`
:param str name: Name of the resource to retrieve
:param str namespace: Optional namespace of the resource to retrieve
"""

# `id` will either be `${name}` or `${namespace}/${name}`.
id = pulumi.Output.from_input(name)
if namespace != None:
id = pulumi.Output.concat(namespace, '/', name)

resource_id = id.apply(lambda x: f'{group_version_kind}:{x}')
hausdorff marked this conversation as resolved.
Show resolved Hide resolved
return resource_id.apply(lambda x: self.resources[x])
35 changes: 28 additions & 7 deletions pkg/gen/python-templates/yaml.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ from typing import Callable, Dict, List, Optional
import pulumi.runtime
import requests
import yaml
{{#Groups}}
{{#Versions}}
from pulumi_kubernetes.{{Group}}.{{Version}} import *
{{/Versions}}
{{/Groups}}
import pulumi_kubernetes
from pulumi_kubernetes.apiextensions import CustomResource

from . import tables
Expand All @@ -24,6 +20,12 @@ class ConfigFile(pulumi.ComponentResource):
ConfigFile creates a set of Kubernetes resources from a Kubernetes YAML file.
"""

resources: pulumi.Output[dict]
"""
Kubernetes resources contained in this ConfigFile.
"""


def __init__(self, name, file_id, opts=None, transformations=None, resource_prefix=None):
"""
:param str name: A name for a resource.
Expand Down Expand Up @@ -61,15 +63,32 @@ class ConfigFile(pulumi.ComponentResource):
# Note: Unlike NodeJS, Python requires that we "pull" on our futures in order to get them scheduled for
# execution. In order to do this, we leverage the engine's RegisterResourceOutputs to wait for the
# resolution of all resources that this YAML document created.
output = _parse_yaml_document(yaml.safe_load_all(text), opts, transformations, resource_prefix)
self.register_outputs({"output": output})
self.resources = _parse_yaml_document(yaml.safe_load_all(text), opts, transformations, resource_prefix)
self.register_outputs({"resources": self.resources})

def translate_output_property(self, prop: str) -> str:
return tables._CASING_FORWARD_TABLE.get(prop) or prop

def translate_input_property(self, prop: str) -> str:
return tables._CASING_BACKWARD_TABLE.get(prop) or prop

def get_resource(self, group_version_kind, name, namespace=None) -> pulumi.Output[pulumi.CustomResource]:
"""
get_resource returns a resource defined by a built-in Kubernetes group/version/kind and
name. For example: `get_resource("apps/v1/Deployment", "nginx")`

:param str group_version_kind: Group/Version/Kind of the resource, e.g., `apps/v1/Deployment`
:param str name: Name of the resource to retrieve
:param str namespace: Optional namespace of the resource to retrieve
"""

# `id` will either be `${name}` or `${namespace}/${name}`.
id = pulumi.Output.from_input(name)
if namespace != None:
id = pulumi.Output.concat(namespace, '/', name)

resource_id = id.apply(lambda x: f'{group_version_kind}:{x}')
return resource_id.apply(lambda x: self.resources[x])

def _read_url(url: str) -> str:
response = requests.get(url)
Expand Down Expand Up @@ -171,6 +190,8 @@ def _parse_yaml_object(
{{#Versions}}
{{#Kinds}}
if gvk == "{{RawAPIVersion}}/{{Kind}}":
# Import locally to avoid name collisions.
from pulumi_kubernetes.{{Group}}.{{Version}} import {{Kind}}
lblackstone marked this conversation as resolved.
Show resolved Hide resolved
return [identifier.apply(
lambda x: (f"{{RawAPIVersion}}/{{Kind}}:{x}",
{{Kind}}(f"{x}", opts, **obj)))]
Expand Down
27 changes: 25 additions & 2 deletions sdk/python/pulumi_kubernetes/helm/v2/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ class Chart(pulumi.ComponentResource):
none of Tiller's server-side validity testing is executed.
"""

resources: pulumi.Output[dict]
"""
Kubernetes resources contained in this Chart.
"""

def __init__(self, release_name, config, opts=None):
"""
Create an instance of the specified Helm chart.
Expand Down Expand Up @@ -440,5 +445,23 @@ def __init__(self, release_name, config, opts=None):
# Note: Unlike NodeJS, Python requires that we "pull" on our futures in order to get them scheduled for
# execution. In order to do this, we leverage the engine's RegisterResourceOutputs to wait for the
# resolution of all resources that this Helm chart created.
resources = all_config.apply(_parse_chart)
self.register_outputs({"output": resources})
self.resources = all_config.apply(_parse_chart)
self.register_outputs({"resources": self.resources})

def get_resource(self, group_version_kind, name, namespace=None) -> pulumi.Output[pulumi.CustomResource]:
"""
get_resource returns a resource defined by a built-in Kubernetes group/version/kind and
name. For example: `get_resource("apps/v1/Deployment", "nginx")`

:param str group_version_kind: Group/Version/Kind of the resource, e.g., `apps/v1/Deployment`
:param str name: Name of the resource to retrieve
:param str namespace: Optional namespace of the resource to retrieve
"""

# `id` will either be `${name}` or `${namespace}/${name}`.
id = pulumi.Output.from_input(name)
if namespace != None:
id = pulumi.Output.concat(namespace, '/', name)

resource_id = id.apply(lambda x: f'{group_version_kind}:{x}')
return resource_id.apply(lambda x: self.resources[x])
Loading