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

Fix YAML parsing for computed namespaces #483

Merged
merged 4 commits into from
Mar 15, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

- Check for unexpected RPC ID and return an error (https://github.com/pulumi/pulumi-kubernetes/pull/475)
- Fix an issue where the Python `pulumi_kubernetes` package was depending on an older `pulumi` package.
- Fix YAML parsing for computed namespaces (https://github.com/pulumi/pulumi-kubernetes/pull/483)

## 0.21.0 (Released March 6, 2019)

Expand Down
40 changes: 23 additions & 17 deletions pkg/gen/nodejs-templates/provider.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as glob from "glob";
import * as jsyaml from "js-yaml";
import fetch from "node-fetch";
import * as k8s from "./index";
import * as inputApi from "./types/input";
import * as outputApi from "./types/output";

export namespace yaml {
Expand All @@ -18,7 +17,7 @@ export namespace yaml {
/** YAML text containing Kubernetes resource definitions. */
yaml?: string[] | string;

/** JavaScript objects represeting Kubernetes resources. */
/** JavaScript objects representing Kubernetes resources. */
objs?: any[] | any;

/**
Expand Down Expand Up @@ -163,7 +162,7 @@ export namespace yaml {
id = `${namespaceOrName}/${name}`;
}

return this.resources[`${groupVersionKind}::${id}`];
return this.resources.apply(r => r[`${groupVersionKind}::${id}`]);
}
}

Expand Down Expand Up @@ -205,31 +204,38 @@ export namespace yaml {
text = Promise.resolve(fs.readFileSync(fileId).toString());
}

this.resources = pulumi.output(text).apply(t => parseYamlDocument({
this.resources = pulumi.output(text.then(t => parseYamlDocument({
objs: jsyaml.safeLoadAll(t),
transformations: config && config.transformations || []
}, {parent: this}));
}, {parent: this})));
}
}

function parseYamlDocument(
config: ConfigOpts, opts?: pulumi.CustomResourceOptions,
): {[key: string]: pulumi.CustomResource} {
let resources: {[key: string]: pulumi.CustomResource} = {};
): pulumi.Output<{[key: string]: pulumi.CustomResource}> {
const objs: pulumi.Output<{name: string, resource: pulumi.CustomResource}>[] = [];

for (const obj of config.objs) {
const fileObjects = parseYamlObject(obj, config.transformations, opts);
const fileObjects: pulumi.Output<{name: string, resource: pulumi.CustomResource}>[] =
parseYamlObject(obj, config.transformations, opts);
for (const fileObject of fileObjects) {
resources[fileObject.name] = fileObject.resource;
objs.push(fileObject);
}
}
return pulumi.all(objs).apply(xs => {
let resources: {[key: string]: pulumi.CustomResource} = {};
for (const x of xs) {
resources[x.name] = x.resource
}

return resources;
return resources;
});
}

function parseYamlObject(
obj: any, transformations?: ((o: any) => void)[], opts?: pulumi.CustomResourceOptions,
): {name: string, resource: pulumi.CustomResource}[] {
): pulumi.Output<{name: string, resource: pulumi.CustomResource}>[] {
if (obj == null || Object.keys(obj).length == 0) {
return [];
}
Expand Down Expand Up @@ -281,28 +287,28 @@ export namespace yaml {
}

const meta = obj["metadata"];
let id: string = meta["name"];
let id: pulumi.Output<any> = pulumi.output(meta["name"]);
const namespace = meta["namespace"] || undefined;
if (namespace !== undefined) {
id = `${namespace}/${id}`;
id = pulumi.concat(namespace, "/", id);
}
switch (`${apiVersion}/${kind}`) {
{{#Groups}}
{{#Versions}}
{{#KindsAndAliases}}
case "{{RawAPIVersion}}/{{Kind}}":
return [{
return [id.apply(id => ({
name: `{{RawAPIVersion}}/{{Kind}}::${id}`,
resource: new k8s.{{Group}}.{{Version}}.{{Kind}}(id, obj, opts),
}];
}))];
{{/KindsAndAliases}}
{{/Versions}}
{{/Groups}}
default:
return [{
return [id.apply(id => ({
name: `${apiVersion}/${kind}::${id}`,
resource: new k8s.apiextensions.CustomResource(id, obj, opts),
}];
}))];
}
}
}
Expand Down
Loading