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

Revert invoke changes #941

Merged
merged 4 commits into from
Jan 8, 2020
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
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

- Revert invoke changes. (https://github.com/pulumi/pulumi-kubernetes/pull/941).

## 1.4.2 (January 7, 2020)

### Improvements
Expand Down
5 changes: 0 additions & 5 deletions pkg/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ func (dcs *DynamicClientSet) IsNamespacedKind(gvk schema.GroupVersionKind) (bool
gv = "v1"
}

if known, namespaced := kinds.Kind(gvk.Kind).Namespaced(); known {
return namespaced, nil
}

// If the Kind is not known, attempt to look up from the API server. This applies to Kinds defined using a CRD.
resourceList, err := dcs.DiscoveryClientCached.ServerResourcesForGroupVersion(gv)
if err != nil {
return false, &NoNamespaceInfoErr{gvk}
Expand Down
85 changes: 76 additions & 9 deletions pkg/gen/nodejs-templates/helm/v2/helm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ export class Chart extends yaml.CollectionComponentResource {
maxBuffer: 512 * 1024 * 1024 // 512 MB
},
).toString();
return this.parseTemplate(
yamlStream, cfg.transformations, cfg.resourcePrefix, configDeps, cfg.namespace);
return this.parseTemplate(yamlStream, cfg.transformations, cfg.resourcePrefix, configDeps);
} catch (e) {
// Shed stack trace, only emit the error.
throw new pulumi.RunError(e.toString());
Expand All @@ -220,25 +219,93 @@ export class Chart extends yaml.CollectionComponentResource {
}

parseTemplate(
text: string,
yamlStream: string,
transformations: ((o: any, opts: pulumi.CustomResourceOptions) => void)[] | undefined,
resourcePrefix: string | undefined,
dependsOn: pulumi.Resource[],
defaultNamespace: string | undefined,
): pulumi.Output<{ [key: string]: pulumi.CustomResource }> {
const promise = pulumi.runtime.invoke(
"kubernetes:yaml:decode", {text, defaultNamespace}, {async: true});
return pulumi.output(promise).apply(p => yaml.parse(
// NOTE: We must manually split the YAML stream because of js-yaml#456. Perusing the code
// and the spec, it looks like a YAML stream is delimited by `^---`, though it is difficult
// to know for sure.
//
// NOTE: We use `{json: true, schema: jsyaml.CORE_SCHEMA}` here so that we conform to Helm's
// YAML parsing semantics. Specifically, `json: true` to ensure that a duplicate key
// overrides its predecessory, rather than throwing an exception, and `schema:
// jsyaml.CORE_SCHEMA` to avoid using additional YAML parsing rules not supported by the
// YAML parser used by Kubernetes.
const objs = yamlStream.split(/^---/m)
.map(yaml => jsyaml.safeLoad(yaml, {json: true, schema: jsyaml.CORE_SCHEMA}))
.filter(a => a != null && "kind" in a)
.sort(helmSort);
return yaml.parse(
{
resourcePrefix: resourcePrefix,
objs: p.result,
yaml: objs.map(o => jsyaml.safeDump(o)),
transformations: transformations || [],
},
{ parent: this, dependsOn: dependsOn }
));
);
}
}

// helmSort is a JavaScript implementation of the Helm Kind sorter[1]. It provides a
// best-effort topology of Kubernetes kinds, which in most cases should ensure that resources
// that must be created first, are.
//
// [1]: https://github.com/helm/helm/blob/094b97ab5d7e2f6eda6d0ab0f2ede9cf578c003c/pkg/tiller/kind_sorter.go
/** @ignore */ export function helmSort(a: { kind: string }, b: { kind: string }): number {
const installOrder = [
"Namespace",
"ResourceQuota",
"LimitRange",
"PodSecurityPolicy",
"Secret",
"ConfigMap",
"StorageClass",
"PersistentVolume",
"PersistentVolumeClaim",
"ServiceAccount",
"CustomResourceDefinition",
"ClusterRole",
"ClusterRoleBinding",
"Role",
"RoleBinding",
"Service",
"DaemonSet",
"Pod",
"ReplicationController",
"ReplicaSet",
"Deployment",
"StatefulSet",
"Job",
"CronJob",
"Ingress",
"APIService"
];

const ordering: { [key: string]: number } = {};
installOrder.forEach((_, i) => {
ordering[installOrder[i]] = i;
});

const aKind = a["kind"];
const bKind = b["kind"];

if (!(aKind in ordering) && !(bKind in ordering)) {
return aKind.localeCompare(bKind);
}

if (!(aKind in ordering)) {
return 1;
}

if (!(bKind in ordering)) {
return -1;
}

return ordering[aKind] - ordering[bKind];
}

/**
* Additional options to customize the fetching of the Helm chart.
*/
Expand Down
35 changes: 24 additions & 11 deletions pkg/gen/nodejs-templates/yaml.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ import * as outputs from "../types/output";

export interface ConfigOpts {
/** JavaScript objects representing Kubernetes resources. */
objs: pulumi.Input<pulumi.Input<any>[]>;
objs: any[];

/**
* A set of transformations to apply to Kubernetes resource definitions before registering
Expand Down Expand Up @@ -120,10 +120,14 @@ 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): any[] {
// NOTE[pulumi-kubernetes#501]: Use `loadAll` with `JSON_SCHEMA` here instead of
// `safeLoadAll` because the latter is incompatible with `JSON_SCHEMA`. It is
// important to use `JSON_SCHEMA` here because the fields of the Kubernetes core
// API types are all tagged with `json:`, and they don't deal very well with things
// like dates.
const jsyaml = require("js-yaml");
return jsyaml.loadAll(text, undefined, {schema: jsyaml.JSON_SCHEMA});
}

/** @ignore */ export function parse(
Expand Down Expand Up @@ -336,13 +340,22 @@ import * as outputs from "../types/output";
config: ConfigOpts,
opts?: pulumi.CustomResourceOptions,
): pulumi.Output<{[key: string]: pulumi.CustomResource}> {
return pulumi.output(config.objs).apply(configObjs => {
const objs = configObjs
.map(obj => parseYamlObject(obj, config.transformations, config.resourcePrefix, opts))
.reduce((array, objs) => (array.concat(...objs)), []);
const objs: pulumi.Output<{name: string, resource: pulumi.CustomResource}>[] = [];

return pulumi.output(objs).apply(objs => objs
.reduce((map: {[key: string]: pulumi.CustomResource}, val) => (map[val.name] = val.resource, map), {}))
for (const obj of config.objs) {
const fileObjects: pulumi.Output<{name: string, resource: pulumi.CustomResource}>[] =
parseYamlObject(obj, config.transformations, config.resourcePrefix, opts);
for (const fileObject of fileObjects) {
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;
});
}

Expand Down
10 changes: 4 additions & 6 deletions 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
import yaml
from pulumi_kubernetes.yaml import _parse_yaml_document


Expand Down Expand Up @@ -347,13 +348,10 @@ 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)
objects = chart_resources.apply(
lambda text: pulumi.runtime.invoke('kubernetes:yaml:decode', {
'text': text, 'defaultNamespace': config.namespace}).value['result'])

# Parse the manifest and create the specified resources.
resources = objects.apply(
lambda objects: _parse_yaml_document(objects, opts, config.transformations))
resources = chart_resources.apply(
lambda yaml_str: _parse_yaml_document(yaml.safe_load_all(yaml_str), opts, config.transformations))

pulumi.Output.all(file, chart_dir, resources).apply(_cleanup_temp_dir)
return resources
Expand Down Expand Up @@ -474,7 +472,7 @@ def get_resource(self, group_version_kind, name, namespace=None) -> pulumi.Outpu

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

resource_id = id.apply(lambda x: f'{group_version_kind}:{x}')
Expand Down
9 changes: 5 additions & 4 deletions pkg/gen/python-templates/yaml.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ from typing import Callable, Dict, List, Optional

import pulumi.runtime
import requests
import yaml
import pulumi_kubernetes
from pulumi_kubernetes.apiextensions import CustomResource

from . import tables
Expand All @@ -23,6 +25,7 @@ class ConfigFile(pulumi.ComponentResource):
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 @@ -60,8 +63,7 @@ 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.
__ret__ = pulumi.runtime.invoke('kubernetes:yaml:decode', {'text': text}).value['result']
self.resources = _parse_yaml_document(__ret__, opts, transformations, resource_prefix)
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:
Expand All @@ -82,13 +84,12 @@ class ConfigFile(pulumi.ComponentResource):

# `id` will either be `${name}` or `${namespace}/${name}`.
id = pulumi.Output.from_input(name)
if namespace is not None:
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)
response.raise_for_status()
Expand Down
79 changes: 0 additions & 79 deletions pkg/kinds/kinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,24 @@ type Kind string

const (
APIService Kind = "APIService"
Binding Kind = "Binding"
CertificateSigningRequest Kind = "CertificateSigningRequest"
ClusterRole Kind = "ClusterRole"
ClusterRoleBinding Kind = "ClusterRoleBinding"
ComponentStatus Kind = "ComponentStatus"
ControllerRevision Kind = "ControllerRevision"
CustomResourceDefinition Kind = "CustomResourceDefinition"
ConfigMap Kind = "ConfigMap"
CronJob Kind = "CronJob"
CSIDriver Kind = "CSIDriver"
CSINode Kind = "CSINode"
DaemonSet Kind = "DaemonSet"
Deployment Kind = "Deployment"
Endpoints Kind = "Endpoints"
Event Kind = "Event"
HorizontalPodAutoscaler Kind = "HorizontalPodAutoscaler"
Ingress Kind = "Ingress"
Job Kind = "Job"
Lease Kind = "Lease"
LimitRange Kind = "LimitRange"
LocalSubjectAccessReview Kind = "LocalSubjectAccessReview"
MutatingWebhookConfiguration Kind = "MutatingWebhookConfiguration"
Namespace Kind = "Namespace"
NetworkPolicy Kind = "NetworkPolicy"
Node Kind = "Node"
PersistentVolume Kind = "PersistentVolume"
PersistentVolumeClaim Kind = "PersistentVolumeClaim"
Pod Kind = "Pod"
Expand All @@ -55,82 +48,10 @@ const (
ResourceQuota Kind = "ResourceQuota"
Role Kind = "Role"
RoleBinding Kind = "RoleBinding"
RuntimeClass Kind = "RuntimeClass"
Secret Kind = "Secret"
SelfSubjectAccessReview Kind = "SelfSubjectAccessReview"
SelfSubjectRulesReview Kind = "SelfSubjectRulesReview"
Service Kind = "Service"
ServiceAccount Kind = "ServiceAccount"
StatefulSet Kind = "StatefulSet"
SubjectAccessReview Kind = "SubjectAccessReview"
StorageClass Kind = "StorageClass"
TokenReview Kind = "TokenReview"
ValidatingWebhookConfiguration Kind = "ValidatingWebhookConfiguration"
VolumeAttachment Kind = "VolumeAttachment"
)

// Namespaced returns whether known resource Kinds are namespaced. If the Kind is unknown (such as CRD Kinds), the
// known return value will be false, and the namespaced value is unknown. In this case, this information can be
// queried separately from the k8s API server.
func (k Kind) Namespaced() (known bool, namespaced bool) {
switch k {
// Note: Use `kubectl api-resources --namespaced=true -o name` to retrieve a list of namespace-scoped resources.
case Binding,
ConfigMap,
ControllerRevision,
CronJob,
DaemonSet,
Deployment,
Endpoints,
Event,
HorizontalPodAutoscaler,
Ingress,
Job,
Lease,
LimitRange,
LocalSubjectAccessReview,
NetworkPolicy,
PersistentVolumeClaim,
Pod,
PodDisruptionBudget,
PodTemplate,
ReplicaSet,
ReplicationController,
ResourceQuota,
Role,
RoleBinding,
Secret,
Service,
ServiceAccount,
StatefulSet:
known, namespaced = true, true
// Note: Use `kubectl api-resources --namespaced=false -o name` to retrieve a list of cluster-scoped resources.
case APIService,
CertificateSigningRequest,
ClusterRole,
ClusterRoleBinding,
ComponentStatus,
CSIDriver,
CSINode,
CustomResourceDefinition,
MutatingWebhookConfiguration,
Namespace,
Node,
PersistentVolume,
PodSecurityPolicy,
PriorityClass,
RuntimeClass,
SelfSubjectAccessReview,
SelfSubjectRulesReview,
StorageClass,
SubjectAccessReview,
TokenReview,
ValidatingWebhookConfiguration,
VolumeAttachment:
known, namespaced = true, false
default:
known = false
}

return
}
Loading