Skip to content

Commit

Permalink
Specify provider version for invokes (#982)
Browse files Browse the repository at this point in the history
The SDK previously used invoke for Helm and YAML support,
but was not specifying the parent or version explicitly. As a result,
the provider plugin to use was nondeterministic, and could select
an incompatible version that lead to an error.
  • Loading branch information
lblackstone committed Feb 7, 2020
1 parent 6077980 commit 4b1bc4f
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## HEAD (Unreleased)

### Bug fixes

- Specify provider version for invokes. (https://github.com/pulumi/pulumi-kubernetes/pull/982).

## 1.5.0 (February 4, 2020)

### Improvements
Expand Down
17 changes: 14 additions & 3 deletions pkg/gen/nodejs-templates/helm/v2/helm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import * as pulumi from "@pulumi/pulumi";
import { execSync } from "child_process";
import * as fs from "fs";
import * as jsyaml from "js-yaml";
import * as nodepath from "path";
import * as shell from "shell-quote";
import * as tmp from "tmp";
import * as path from "../../path";
import { getVersion } from "../../version";
import * as yaml from "../../yaml/index";

interface BaseChartOpts {
Expand Down Expand Up @@ -218,7 +218,7 @@ export class Chart extends yaml.CollectionComponentResource {
},
).toString();
return this.parseTemplate(
yamlStream, cfg.transformations, cfg.resourcePrefix, configDeps, cfg.namespace);
yamlStream, cfg.transformations, cfg.resourcePrefix, configDeps, cfg.namespace, opts);
} catch (e) {
// Shed stack trace, only emit the error.
throw new pulumi.RunError(e.toString());
Expand All @@ -236,9 +236,20 @@ export class Chart extends yaml.CollectionComponentResource {
resourcePrefix: string | undefined,
dependsOn: pulumi.Resource[],
defaultNamespace: string | undefined,
opts?: pulumi.ComponentResourceOptions,
): pulumi.Output<{ [key: string]: pulumi.CustomResource }> {
// Rather than using the default provider for the following invoke call, determine the
// provider from the parent if specified, or fallback to using the version specified
// in package.json.
let invokeOpts: pulumi.InvokeOptions = {async: true};
if (opts?.parent) {
invokeOpts = {...invokeOpts, parent: opts.parent};
} else {
invokeOpts = {...invokeOpts, version: getVersion()};
}

const promise = pulumi.runtime.invoke(
"kubernetes:yaml:decode", {text, defaultNamespace}, {async: true});
"kubernetes:yaml:decode", {text, defaultNamespace}, invokeOpts);
return pulumi.output(promise).apply<{[key: string]: pulumi.CustomResource}>(p => yaml.parse(
{
resourcePrefix: resourcePrefix,
Expand Down
2 changes: 0 additions & 2 deletions pkg/gen/nodejs-templates/package.json.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
},
"dependencies": {
"@pulumi/pulumi": "^1.8.1",
"@types/js-yaml": "^3.11.2",
"js-yaml": "^3.12.0",
"shell-quote": "^1.6.1",
"tmp": "^0.0.33",
"@types/tmp": "^0.0.33",
Expand Down
21 changes: 16 additions & 5 deletions pkg/gen/nodejs-templates/yaml.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as fs from "fs";
import * as glob from "glob";
import fetch from "node-fetch";
import * as k8s from "../index";
import { getVersion } from "../version";
import * as outputs from "../types/output";

export interface ConfigGroupOpts {
Expand Down Expand Up @@ -120,9 +121,19 @@ import * as outputs from "../types/output";
resourcePrefix?: string;
}

function yamlLoadAll(text: string): Promise<any[]> {
const promise = pulumi.runtime.invoke("kubernetes:yaml:decode", {text}, {async: true});
return promise.then(p => p.result);
function yamlLoadAll(text: string, opts?: pulumi.CustomResourceOptions): Promise<any[]> {
// Rather than using the default provider for the following invoke call, determine the
// provider from the parent if specified, or fallback to using the version specified
// in package.json.
let invokeOpts: pulumi.InvokeOptions = {async: true};
if (opts?.parent) {
invokeOpts = {...invokeOpts, parent: opts.parent};
} else {
invokeOpts = {...invokeOpts, version: getVersion()};
}

return pulumi.runtime.invoke("kubernetes:yaml:decode", {text}, invokeOpts)
.then((p => p.result));
}

/** @ignore */ export function parse(
Expand Down Expand Up @@ -172,7 +183,7 @@ import * as outputs from "../types/output";

for (const text of yamlTexts) {
const docResources = parseYamlDocument({
objs: yamlLoadAll(text),
objs: yamlLoadAll(text, opts),
transformations: config.transformations,
resourcePrefix: config.resourcePrefix
},
Expand Down Expand Up @@ -320,7 +331,7 @@ import * as outputs from "../types/output";
this.resources = pulumi.output(text.then(t => {
try {
return parseYamlDocument({
objs: yamlLoadAll(t),
objs: yamlLoadAll(t, opts),
transformations: config && config.transformations || [],
resourcePrefix: config && config.resourcePrefix || undefined
}, {parent: this})
Expand Down
13 changes: 12 additions & 1 deletion pkg/gen/python-templates/helm/v2/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any, Callable, List, Optional, TextIO, Tuple, Union

import pulumi.runtime
from ...version import get_version
from pulumi_kubernetes.yaml import _parse_yaml_document


Expand Down Expand Up @@ -347,9 +348,19 @@ def _parse_chart(all_config: Tuple[str, Union[ChartOpts, LocalChartOpts], pulumi
cmd.extend(home_arg)

chart_resources = pulumi.Output.all(cmd, data).apply(_run_helm_cmd)

# Rather than using the default provider for the following invoke call, determine the
# provider from the parent if specified, or fallback to using the version specified
# in package.json.
invoke_opts = pulumi.InvokeOptions()
if opts.parent is not None:
invoke_opts.parent = opts.parent
else:
invoke_opts.version = get_version()

objects = chart_resources.apply(
lambda text: pulumi.runtime.invoke('kubernetes:yaml:decode', {
'text': text, 'defaultNamespace': config.namespace}).value['result'])
'text': text, 'defaultNamespace': config.namespace}, invoke_opts).value['result'])

# Parse the manifest and create the specified resources.
resources = objects.apply(
Expand Down
12 changes: 11 additions & 1 deletion pkg/gen/python-templates/yaml.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,20 @@ class ConfigFile(pulumi.ComponentResource):

opts = pulumi.ResourceOptions.merge(opts, pulumi.ResourceOptions(parent=self))

# Rather than using the default provider for the following invoke call, determine the
# provider from the parent if specified, or fallback to using the version specified
# in package.json.
invoke_opts = pulumi.InvokeOptions()
if opts.parent is not None:
invoke_opts.parent = opts.parent
else:
invoke_opts.version = get_version()

__ret__ = pulumi.runtime.invoke('kubernetes:yaml:decode', {'text': text}, invoke_opts).value['result']

# 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.
__ret__ = pulumi.runtime.invoke('kubernetes:yaml:decode', {'text': text}).value['result']
self.resources = _parse_yaml_document(__ret__, opts, transformations, resource_prefix)
self.register_outputs({"resources": self.resources})

Expand Down
17 changes: 14 additions & 3 deletions sdk/nodejs/helm/v2/helm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import * as pulumi from "@pulumi/pulumi";
import { execSync } from "child_process";
import * as fs from "fs";
import * as jsyaml from "js-yaml";
import * as nodepath from "path";
import * as shell from "shell-quote";
import * as tmp from "tmp";
import * as path from "../../path";
import { getVersion } from "../../version";
import * as yaml from "../../yaml/index";

interface BaseChartOpts {
Expand Down Expand Up @@ -218,7 +218,7 @@ export class Chart extends yaml.CollectionComponentResource {
},
).toString();
return this.parseTemplate(
yamlStream, cfg.transformations, cfg.resourcePrefix, configDeps, cfg.namespace);
yamlStream, cfg.transformations, cfg.resourcePrefix, configDeps, cfg.namespace, opts);
} catch (e) {
// Shed stack trace, only emit the error.
throw new pulumi.RunError(e.toString());
Expand All @@ -236,9 +236,20 @@ export class Chart extends yaml.CollectionComponentResource {
resourcePrefix: string | undefined,
dependsOn: pulumi.Resource[],
defaultNamespace: string | undefined,
opts?: pulumi.ComponentResourceOptions,
): pulumi.Output<{ [key: string]: pulumi.CustomResource }> {
// Rather than using the default provider for the following invoke call, determine the
// provider from the parent if specified, or fallback to using the version specified
// in package.json.
let invokeOpts: pulumi.InvokeOptions = {async: true};
if (opts?.parent) {
invokeOpts = {...invokeOpts, parent: opts.parent};
} else {
invokeOpts = {...invokeOpts, version: getVersion()};
}

const promise = pulumi.runtime.invoke(
"kubernetes:yaml:decode", {text, defaultNamespace}, {async: true});
"kubernetes:yaml:decode", {text, defaultNamespace}, invokeOpts);
return pulumi.output(promise).apply<{[key: string]: pulumi.CustomResource}>(p => yaml.parse(
{
resourcePrefix: resourcePrefix,
Expand Down
2 changes: 0 additions & 2 deletions sdk/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
},
"dependencies": {
"@pulumi/pulumi": "^1.8.1",
"@types/js-yaml": "^3.11.2",
"js-yaml": "^3.12.0",
"shell-quote": "^1.6.1",
"tmp": "^0.0.33",
"@types/tmp": "^0.0.33",
Expand Down
21 changes: 16 additions & 5 deletions sdk/nodejs/yaml/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as fs from "fs";
import * as glob from "glob";
import fetch from "node-fetch";
import * as k8s from "../index";
import { getVersion } from "../version";
import * as outputs from "../types/output";

export interface ConfigGroupOpts {
Expand Down Expand Up @@ -120,9 +121,19 @@ import * as outputs from "../types/output";
resourcePrefix?: string;
}

function yamlLoadAll(text: string): Promise<any[]> {
const promise = pulumi.runtime.invoke("kubernetes:yaml:decode", {text}, {async: true});
return promise.then(p => p.result);
function yamlLoadAll(text: string, opts?: pulumi.CustomResourceOptions): Promise<any[]> {
// Rather than using the default provider for the following invoke call, determine the
// provider from the parent if specified, or fallback to using the version specified
// in package.json.
let invokeOpts: pulumi.InvokeOptions = {async: true};
if (opts?.parent) {
invokeOpts = {...invokeOpts, parent: opts.parent};
} else {
invokeOpts = {...invokeOpts, version: getVersion()};
}

return pulumi.runtime.invoke("kubernetes:yaml:decode", {text}, invokeOpts)
.then((p => p.result));
}

/** @ignore */ export function parse(
Expand Down Expand Up @@ -172,7 +183,7 @@ import * as outputs from "../types/output";

for (const text of yamlTexts) {
const docResources = parseYamlDocument({
objs: yamlLoadAll(text),
objs: yamlLoadAll(text, opts),
transformations: config.transformations,
resourcePrefix: config.resourcePrefix
},
Expand Down Expand Up @@ -2462,7 +2473,7 @@ import * as outputs from "../types/output";
this.resources = pulumi.output(text.then(t => {
try {
return parseYamlDocument({
objs: yamlLoadAll(t),
objs: yamlLoadAll(t, opts),
transformations: config && config.transformations || [],
resourcePrefix: config && config.resourcePrefix || undefined
}, {parent: this})
Expand Down
13 changes: 12 additions & 1 deletion sdk/python/pulumi_kubernetes/helm/v2/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any, Callable, List, Optional, TextIO, Tuple, Union

import pulumi.runtime
from ...version import get_version
from pulumi_kubernetes.yaml import _parse_yaml_document


Expand Down Expand Up @@ -347,9 +348,19 @@ def _parse_chart(all_config: Tuple[str, Union[ChartOpts, LocalChartOpts], pulumi
cmd.extend(home_arg)

chart_resources = pulumi.Output.all(cmd, data).apply(_run_helm_cmd)

# Rather than using the default provider for the following invoke call, determine the
# provider from the parent if specified, or fallback to using the version specified
# in package.json.
invoke_opts = pulumi.InvokeOptions()
if opts.parent is not None:
invoke_opts.parent = opts.parent
else:
invoke_opts.version = get_version()

objects = chart_resources.apply(
lambda text: pulumi.runtime.invoke('kubernetes:yaml:decode', {
'text': text, 'defaultNamespace': config.namespace}).value['result'])
'text': text, 'defaultNamespace': config.namespace}, invoke_opts).value['result'])

# Parse the manifest and create the specified resources.
resources = objects.apply(
Expand Down
12 changes: 11 additions & 1 deletion sdk/python/pulumi_kubernetes/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,20 @@ def __init__(self, name, file_id, opts=None, transformations=None, resource_pref

opts = pulumi.ResourceOptions.merge(opts, pulumi.ResourceOptions(parent=self))

# Rather than using the default provider for the following invoke call, determine the
# provider from the parent if specified, or fallback to using the version specified
# in package.json.
invoke_opts = pulumi.InvokeOptions()
if opts.parent is not None:
invoke_opts.parent = opts.parent
else:
invoke_opts.version = get_version()

__ret__ = pulumi.runtime.invoke('kubernetes:yaml:decode', {'text': text}, invoke_opts).value['result']

# 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.
__ret__ = pulumi.runtime.invoke('kubernetes:yaml:decode', {'text': text}).value['result']
self.resources = _parse_yaml_document(__ret__, opts, transformations, resource_prefix)
self.register_outputs({"resources": self.resources})

Expand Down
4 changes: 2 additions & 2 deletions tests/examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestAccHelmApiVersions(t *testing.T) {
t *testing.T, stackInfo integration.RuntimeValidationStackInfo,
) {
assert.NotNil(t, stackInfo.Deployment)
assert.Equal(t, 7, len(stackInfo.Deployment.Resources))
assert.Equal(t, 6, len(stackInfo.Deployment.Resources))
},
})

Expand All @@ -181,7 +181,7 @@ func TestAccHelmLocal(t *testing.T) {
t *testing.T, stackInfo integration.RuntimeValidationStackInfo,
) {
assert.NotNil(t, stackInfo.Deployment)
assert.Equal(t, 16, len(stackInfo.Deployment.Resources))
assert.Equal(t, 15, len(stackInfo.Deployment.Resources))
},
})

Expand Down
6 changes: 3 additions & 3 deletions tests/integration/aliases/aliases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestAliases(t *testing.T) {
Quick: true,
ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) {
assert.NotNil(t, stackInfo.Deployment)
assert.Equal(t, 5, len(stackInfo.Deployment.Resources))
assert.Equal(t, 4, len(stackInfo.Deployment.Resources))

tests.SortResourcesByURN(stackInfo)

Expand All @@ -51,7 +51,7 @@ func TestAliases(t *testing.T) {
Additive: true,
ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) {
assert.NotNil(t, stackInfo.Deployment)
assert.Equal(t, 5, len(stackInfo.Deployment.Resources))
assert.Equal(t, 4, len(stackInfo.Deployment.Resources))

tests.SortResourcesByURN(stackInfo)

Expand All @@ -69,7 +69,7 @@ func TestAliases(t *testing.T) {
Additive: true,
ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) {
assert.NotNil(t, stackInfo.Deployment)
assert.Equal(t, 5, len(stackInfo.Deployment.Resources))
assert.Equal(t, 4, len(stackInfo.Deployment.Resources))

tests.SortResourcesByURN(stackInfo)

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/yaml-url/yaml_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestYAMLURL(t *testing.T) {
assert.NotNil(t, stackInfo.Deployment)

// Assert that we've retrieved the YAML from the URL and provisioned them.
assert.Equal(t, 19, len(stackInfo.Deployment.Resources))
assert.Equal(t, 18, len(stackInfo.Deployment.Resources))
},
})
}

0 comments on commit 4b1bc4f

Please sign in to comment.