Skip to content

Commit

Permalink
Add Python usage examples for Helm, YAML, and Kustomize (#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
lblackstone committed Jul 21, 2020
1 parent b452b8a commit 52a8169
Show file tree
Hide file tree
Showing 8 changed files with 540 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Improvements

- Add NodeJS usage examples for Helm, Kustomize, and YAML resources. (https://github.com/pulumi/pulumi-kubernetes/pull/1205)
- Add Python usage examples for Helm, Kustomize, and YAML resources. (https://github.com/pulumi/pulumi-kubernetes/pull/1209)
- Add v3 Helm package for Go SDK. (https://github.com/pulumi/pulumi-kubernetes/pull/1211)

## 2.4.0 (July 7, 2020)
Expand Down
111 changes: 111 additions & 0 deletions provider/pkg/gen/python-templates/helm/v2/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,117 @@ def __init__(self, release_name, config, opts=None):
manifests. Any values that would be retrieved in-cluster are assigned fake values, and
none of Tiller's server-side validity testing is executed.
## Example Usage
### Local Chart Directory
```python
from pulumi_kubernetes.helm.v3 import Chart, LocalChartOpts
nginx_ingress = Chart(
"nginx-ingress",
LocalChartOpts(
path="./nginx-ingress",
),
)
```
### Remote Chart
```python
from pulumi_kubernetes.helm.v3 import Chart, ChartOpts, FetchOpts
nginx_ingress = Chart(
"nginx-ingress",
ChartOpts(
chart="nginx-ingress",
version="1.24.4",
fetch_opts=FetchOpts(
repo="https://kubernetes-charts.storage.googleapis.com/",
),
),
)
```
### Set Chart Values
```python
from pulumi_kubernetes.helm.v3 import Chart, ChartOpts, FetchOpts
nginx_ingress = Chart(
"nginx-ingress",
ChartOpts(
chart="nginx-ingress",
version="1.24.4",
fetch_opts=FetchOpts(
repo="https://kubernetes-charts.storage.googleapis.com/",
),
values={
"controller": {
"metrics": {
"enabled": True,
},
},
},
),
)
```
### Deploy Chart into Namespace
```python
from pulumi_kubernetes.helm.v3 import Chart, ChartOpts, FetchOpts
nginx_ingress = Chart(
"nginx-ingress",
ChartOpts(
chart="nginx-ingress",
version="1.24.4",
namespace="test-namespace",
fetch_opts=FetchOpts(
repo="https://kubernetes-charts.storage.googleapis.com/",
),
),
)
```
### Chart with Transformations
```python
from pulumi_kubernetes.helm.v3 import Chart, ChartOpts, FetchOpts
# Make every service private to the cluster, i.e., turn all services into ClusterIP instead of LoadBalancer.
def make_service_private(obj, opts):
if obj["kind"] == "Service" and obj["apiVersion"] == "v1":
try:
t = obj["spec"]["type"]
if t == "LoadBalancer":
obj["spec"]["type"] = "ClusterIP"
except KeyError:
pass
# Set a resource alias for a previous name.
def alias(obj, opts):
if obj["kind"] == "Deployment":
opts.aliases = ["oldName"]
# Omit a resource from the Chart by transforming the specified resource definition to an empty List.
def omit_resource(obj, opts):
if obj["kind"] == "Pod" and obj["metadata"]["name"] == "test":
obj["apiVersion"] = "v1"
obj["kind"] = "List"
nginx_ingress = Chart(
"nginx-ingress",
ChartOpts(
chart="nginx-ingress",
version="1.24.4",
fetch_opts=FetchOpts(
repo="https://kubernetes-charts.storage.googleapis.com/",
),
transformations=[make_service_private, alias, omit_resource],
),
)
```
:param str release_name: Name of the Chart (e.g., nginx-ingress).
:param Union[ChartOpts, LocalChartOpts] config: Configuration options for the Chart.
:param Optional[pulumi.ResourceOptions] opts: A bag of options that control this
Expand Down
56 changes: 56 additions & 0 deletions provider/pkg/gen/python-templates/kustomize/kustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,62 @@ def __init__(self, name, directory, opts=None, transformations=None, resource_pr
Directory is a component representing a collection of resources described by a kustomize directory
(kustomization).
## Example Usage
### Local Kustomize Directory
```python
from pulumi_kubernetes.kustomize import Directory
hello_world = Directory(
"hello-world-local",
directory="./helloWorld",
)
```
### Kustomize Directory from a Git Repo
```python
from pulumi_kubernetes.kustomize import Directory
hello_world = Directory(
"hello-world-remote",
directory="https://github.com/kubernetes-sigs/kustomize/tree/v3.3.1/examples/helloWorld",
)
```
### Kustomize Directory with Transformations
```python
from pulumi_kubernetes.helm.v3 import Chart, ChartOpts, FetchOpts
# Make every service private to the cluster, i.e., turn all services into ClusterIP instead of LoadBalancer.
def make_service_private(obj, opts):
if obj["kind"] == "Service" and obj["apiVersion"] == "v1":
try:
t = obj["spec"]["type"]
if t == "LoadBalancer":
obj["spec"]["type"] = "ClusterIP"
except KeyError:
pass
# Set a resource alias for a previous name.
def alias(obj, opts):
if obj["kind"] == "Deployment":
opts.aliases = ["oldName"]
# Omit a resource from the Chart by transforming the specified resource definition to an empty List.
def omit_resource(obj, opts):
if obj["kind"] == "Pod" and obj["metadata"]["name"] == "test":
obj["apiVersion"] = "v1"
obj["kind"] = "List"
hello_world = Directory(
"hello-world-remote",
directory="https://github.com/kubernetes-sigs/kustomize/tree/v3.3.1/examples/helloWorld",
transformations=[make_service_private, alias, omit_resource],
)
```
:param str name: A name for a resource.
:param str directory: The directory containing the kustomization to apply. The value can be a local directory
or a folder in a git repository.
Expand Down
47 changes: 47 additions & 0 deletions provider/pkg/gen/python-templates/yaml/yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,53 @@ class ConfigFile(pulumi.ComponentResource):
"""
ConfigFile creates a set of Kubernetes resources from a Kubernetes YAML file.

## Example Usage
### Local File

```python
from pulumi_kubernetes.yaml import ConfigFile

example = ConfigFile(
"example",
file_id="foo.yaml",
)
```
### YAML with Transformations

```python
from pulumi_kubernetes.yaml import ConfigFile

# Make every service private to the cluster, i.e., turn all services into ClusterIP instead of LoadBalancer.
def make_service_private(obj, opts):
if obj["kind"] == "Service" and obj["apiVersion"] == "v1":
try:
t = obj["spec"]["type"]
if t == "LoadBalancer":
obj["spec"]["type"] = "ClusterIP"
except KeyError:
pass


# Set a resource alias for a previous name.
def alias(obj, opts):
if obj["kind"] == "Deployment":
opts.aliases = ["oldName"]


# Omit a resource from the Chart by transforming the specified resource definition to an empty List.
def omit_resource(obj, opts):
if obj["kind"] == "Pod" and obj["metadata"]["name"] == "test":
obj["apiVersion"] = "v1"
obj["kind"] = "List"


example = ConfigFile(
"example",
file_id="foo.yaml",
transformations=[make_service_private, alias, omit_resource],
)
```

:param str name: A name for a resource.
:param str file_id: Path or a URL that uniquely identifies a file.
:param Optional[pulumi.ResourceOptions] opts: A bag of optional settings that control a resource's behavior.
Expand Down
111 changes: 111 additions & 0 deletions sdk/python/pulumi_kubernetes/helm/v2/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,117 @@ def __init__(self, release_name, config, opts=None):
manifests. Any values that would be retrieved in-cluster are assigned fake values, and
none of Tiller's server-side validity testing is executed.
## Example Usage
### Local Chart Directory
```python
from pulumi_kubernetes.helm.v3 import Chart, LocalChartOpts
nginx_ingress = Chart(
"nginx-ingress",
LocalChartOpts(
path="./nginx-ingress",
),
)
```
### Remote Chart
```python
from pulumi_kubernetes.helm.v3 import Chart, ChartOpts, FetchOpts
nginx_ingress = Chart(
"nginx-ingress",
ChartOpts(
chart="nginx-ingress",
version="1.24.4",
fetch_opts=FetchOpts(
repo="https://kubernetes-charts.storage.googleapis.com/",
),
),
)
```
### Set Chart Values
```python
from pulumi_kubernetes.helm.v3 import Chart, ChartOpts, FetchOpts
nginx_ingress = Chart(
"nginx-ingress",
ChartOpts(
chart="nginx-ingress",
version="1.24.4",
fetch_opts=FetchOpts(
repo="https://kubernetes-charts.storage.googleapis.com/",
),
values={
"controller": {
"metrics": {
"enabled": True,
},
},
},
),
)
```
### Deploy Chart into Namespace
```python
from pulumi_kubernetes.helm.v3 import Chart, ChartOpts, FetchOpts
nginx_ingress = Chart(
"nginx-ingress",
ChartOpts(
chart="nginx-ingress",
version="1.24.4",
namespace="test-namespace",
fetch_opts=FetchOpts(
repo="https://kubernetes-charts.storage.googleapis.com/",
),
),
)
```
### Chart with Transformations
```python
from pulumi_kubernetes.helm.v3 import Chart, ChartOpts, FetchOpts
# Make every service private to the cluster, i.e., turn all services into ClusterIP instead of LoadBalancer.
def make_service_private(obj, opts):
if obj["kind"] == "Service" and obj["apiVersion"] == "v1":
try:
t = obj["spec"]["type"]
if t == "LoadBalancer":
obj["spec"]["type"] = "ClusterIP"
except KeyError:
pass
# Set a resource alias for a previous name.
def alias(obj, opts):
if obj["kind"] == "Deployment":
opts.aliases = ["oldName"]
# Omit a resource from the Chart by transforming the specified resource definition to an empty List.
def omit_resource(obj, opts):
if obj["kind"] == "Pod" and obj["metadata"]["name"] == "test":
obj["apiVersion"] = "v1"
obj["kind"] = "List"
nginx_ingress = Chart(
"nginx-ingress",
ChartOpts(
chart="nginx-ingress",
version="1.24.4",
fetch_opts=FetchOpts(
repo="https://kubernetes-charts.storage.googleapis.com/",
),
transformations=[make_service_private, alias, omit_resource],
),
)
```
:param str release_name: Name of the Chart (e.g., nginx-ingress).
:param Union[ChartOpts, LocalChartOpts] config: Configuration options for the Chart.
:param Optional[pulumi.ResourceOptions] opts: A bag of options that control this
Expand Down
Loading

0 comments on commit 52a8169

Please sign in to comment.