Skip to content

Commit

Permalink
Add support for Python first-class providers (#350)
Browse files Browse the repository at this point in the history
* Add support for Python first-class providers

Adds support for referring to the Kubernetes provider in a first-class
manner in Python. This is accomplished using a similar method to Node -
the provider is explicitly projected as a resource and users are free to
instantiate it at-will and use it to create other Kubernetes resources.

This commit also adds a smoke test for the feature.

* Fix a few package issues

1. Fix the minimum version of the pulumi package to 0.16.10, the minimum
for first-class providers
2. Hook up the smoke test to the integration test framework
3. Update the referenced version of pulumi in the provider test's
requirements.txt

* CR feedback: remove dead code
  • Loading branch information
swgillespie authored Jan 17, 2019
1 parent 2d39bf3 commit 1bd96ed
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 1 deletion.
3 changes: 3 additions & 0 deletions examples/python/provider/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: provider
description: A program that smoke-tests Kubernetes first-class providers
runtime: python
32 changes: 32 additions & 0 deletions examples/python/provider/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2016-2018, Pulumi Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from os import path
from pulumi import ResourceOptions
from pulumi_kubernetes import Provider
from pulumi_kubernetes.core.v1 import Pod

kubeconfig_file = path.join(path.expanduser("~"), ".kube", "config")
with open(kubeconfig_file) as f:
kubeconfig = f.read()

my_k8s = Provider("myk8s", kubeconfig=kubeconfig)
nginx = Pod("nginx", spec={
"containers": [{
"image": "nginx:1.7.9",
"name": "nginx",
"ports": [{
"container_port": 80,
}],
}],
}, __opts__=ResourceOptions(provider=my_k8s))
1 change: 1 addition & 0 deletions examples/python/provider/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pulumi>=0.16.10,<0.17.0
17 changes: 17 additions & 0 deletions examples/python/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,20 @@ func TestGuestbook(t *testing.T) {
})
integration.ProgramTest(t, &options)
}

// Smoke test for first-class Kubernetes providers.
func TestProvider(t *testing.T) {
kubectx := os.Getenv("KUBERNETES_CONTEXT")
if kubectx == "" {
t.Skipf("Skipping test due to missing KUBERNETES_CONTEXT variable")
}

cwd, err := os.Getwd()
if !assert.NoError(t, err) {
t.FailNow()
}
options := baseOptions.With(integration.ProgramTestOptions{
Dir: filepath.Join(cwd, "provider"),
})
integration.ProgramTest(t, &options)
}
3 changes: 3 additions & 0 deletions pkg/gen/python-templates/root__init__.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ __all__ = [
"{{Group}}",
{{/Groups}}
]

# Expose the provider directly.
from .provider import Provider
3 changes: 3 additions & 0 deletions sdk/python/pulumi_kubernetes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@
"settings",
"storage",
]

# Expose the provider directly.
from .provider import Provider
32 changes: 32 additions & 0 deletions sdk/python/pulumi_kubernetes/provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pulumi
from . import tables

class Provider(pulumi.ProviderResource):
"""
The provider type for the kubernetes package.
"""
def __init__(self,
__name__,
__opts__=None,
cluster=None,
context=None,
kubeconfig=None,
namespace=None):
"""
Create a Provider resource with the given unique name, arguments, and options.
:param str __name__: The unique name of the resource.
:param pulumi.ResourceOptions __opts__: An optional bag of options that controls this resource's behavior.
:param str cluster: If present, the name of the kubeconfig cluster to use.
:param str context: If present, the name of the kubeconfig context to use.
:param str kubeconfig: The contents of a kubeconfig file. If this is set, this config will be used instead
of $KUBECONFIG.
:param str namespace: If present, the namespace scope to use.
"""
__props__ = {
"cluster": cluster,
"context": context,
"kubeconfig": kubeconfig,
"namespace": namespace,
}
super(Provider, self).__init__("kubernetes", __name__, __props__, __opts__)
2 changes: 1 addition & 1 deletion sdk/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ def readme():
license='Apache-2.0',
packages=find_packages(),
install_requires=[
'pulumi>=0.16.4,<0.17.0'
'pulumi>=0.16.10,<0.17.0'
],
zip_safe=False)

0 comments on commit 1bd96ed

Please sign in to comment.