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

Auto-generate aliases for all resource kinds #991

Merged
merged 4 commits into from
Feb 18, 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)

### Improvements

- Auto-generate aliases for all resource kinds. (https://github.com/pulumi/pulumi-kubernetes/pull/991).

### Bug fixes

- Fix aliases for several resource kinds. (https://github.com/pulumi/pulumi-kubernetes/pull/990).
Expand Down
8 changes: 2 additions & 6 deletions pkg/gen/nodejs-templates/kind.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import { getVersion } from "../../version";
}

{{#MergeOptsRequired}}
const _opts = pulumi.mergeOptions(opts, {
opts = pulumi.mergeOptions(opts, {
{{#AdditionalSecretOutputsPresent}}
additionalSecretOutputs: [
{{#AdditionalSecretOutputs}}
Expand All @@ -85,16 +85,12 @@ import { getVersion } from "../../version";
{{#AliasesPresent}}
aliases: [
{{#Aliases}}
{ parent: opts.parent, type: "{{.}}", name: name },
{ type: "{{.}}" },
{{/Aliases}}
],
{{/AliasesPresent}}
});

super({{Kind}}.__pulumiType, name, props, _opts);
{{/MergeOptsRequired}}
{{^MergeOptsRequired}}
super({{Kind}}.__pulumiType, name, props, opts);
{{/MergeOptsRequired}}
}
}
2 changes: 1 addition & 1 deletion pkg/gen/python-templates/kind.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class {{Kind}}(pulumi.CustomResource):
parent = opts.parent if opts and opts.parent else None
aliases = [
{{#Aliases}}
pulumi.Alias(parent=parent, type_="{{.}}", name=resource_name),
pulumi.Alias(type_="{{.}}"),
{{/Aliases}}
]
{{/AliasesPresent}}
Expand Down
121 changes: 69 additions & 52 deletions pkg/gen/typegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,74 @@ func createGroups(definitionsJSON map[string]interface{}, opts groupOpts) []*Gro
}).
ToSlice(&definitions)

// Compute aliases for Kinds. Many k8s resources have multiple GVs, so create a map from Kind -> GV string.
// For Kinds with more than one GV, create aliases in the SDKs.
type SimpleKind struct {
kind string
apiVersion string
}
aliases := map[string][]interface{}{}
linq.From(definitions).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to say, whoever wrote linq for go is both a hero and also this is extremely cursed code thanks to the lack of generics

@ahmetb: thanks, I hate it! ;)

WhereT(func(d *definition) bool { return isTopLevel(d) && !strings.HasSuffix(d.gvk.Kind, "List") }).
OrderByT(func(d *definition) string { return d.gvk.String() }).
SelectManyT(func(d *definition) linq.Query {
// Make fully-qualified GroupVersion. The fully-qualified version is the "official" GV
// (e.g., `core/v1` instead of `v1` or `admissionregistration.k8s.io/v1alpha1` instead of
// `admissionregistration/v1alpha1`).
var fqGroupVersion string
if gvks, gvkExists := d.data["x-kubernetes-group-version-kind"].([]interface{}); gvkExists && len(gvks) > 0 {
gvk := gvks[0].(map[string]interface{})
group := gvk["group"].(string)
version := gvk["version"].(string)
if group == "" {
fqGroupVersion = fmt.Sprintf(`core/%s`, version)
} else {
fqGroupVersion = fmt.Sprintf(`%s/%s`, group, version)
}
} else {
gv := d.gvk.GroupVersion().String()
fqGroupVersion = gv
}

return linq.From([]SimpleKind{
{
kind: d.gvk.Kind,
apiVersion: fqGroupVersion,
},
})
}).
GroupByT(
func(kind SimpleKind) string {
return kind.kind
},
func(kind SimpleKind) string {
return fmt.Sprintf("kubernetes:%s:%s", kind.apiVersion, kind.kind)
}).
WhereT(func(group linq.Group) bool {
return len(group.Group) > 1
}).
ToMapBy(&aliases,
func(i interface{}) interface{} {
return i.(linq.Group).Key
},
func(i interface{}) interface{} {
return i.(linq.Group).Group
})
aliasesForKind := func(kind, fqGroupVersion string) []string {
var results []string

for _, alias := range aliases[kind] {
aliasString := alias.(string)
re := fmt.Sprintf(`:%s:`, fqGroupVersion)
match, err := regexp.MatchString(re, aliasString)
if err == nil && match {
continue
}
results = append(results, aliasString)
}
return results
}

//
// Assemble a `KindConfig` for each Kubernetes kind.
//
Expand Down Expand Up @@ -1125,7 +1193,7 @@ func createGroups(definitionsJSON map[string]interface{}, opts groupOpts) []*Gro
requiredInputProperties: requiredInputProperties,
optionalInputProperties: optionalInputProperties,
additionalSecretOutputs: additionalSecretOutputs(d.gvk),
aliases: aliasesForGVK(d.gvk),
aliases: aliasesForKind(d.gvk.Kind, fqGroupVersion),
gvk: &d.gvk,
apiVersion: fqGroupVersion,
rawAPIVersion: defaultGroupVersion,
Expand Down Expand Up @@ -1230,54 +1298,3 @@ func additionalSecretOutputs(gvk schema.GroupVersionKind) []string {
return []string{}
}
}

// aliasesForGVK returns a list of alias strings for a given GVK. These values are derived from the Kubernetes
// API docs: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/
func aliasesForGVK(gvk schema.GroupVersionKind) []string {
kind := kinds.Kind(gvk.Kind)

switch kind {
case kinds.ClusterRole, kinds.ClusterRoleBinding, kinds.Role, kinds.RoleBinding:
return []string{
fmt.Sprintf("kubernetes:rbac.authorization.k8s.io/v1:%s", gvk.Kind),
fmt.Sprintf("kubernetes:rbac.authorization.k8s.io/v1beta1:%s", gvk.Kind),
fmt.Sprintf("kubernetes:rbac.authorization.k8s.io/v1alpha1:%s", gvk.Kind),
}
case kinds.DaemonSet, kinds.ReplicaSet:
return []string{
fmt.Sprintf("kubernetes:apps/v1:%s", gvk.Kind),
fmt.Sprintf("kubernetes:apps/v1beta2:%s", gvk.Kind),
fmt.Sprintf("kubernetes:extensions/v1beta1:%s", gvk.Kind),
}
case kinds.Deployment, kinds.StatefulSet:
return []string{
fmt.Sprintf("kubernetes:apps/v1:%s", gvk.Kind),
fmt.Sprintf("kubernetes:apps/v1beta1:%s", gvk.Kind),
fmt.Sprintf("kubernetes:apps/v1beta2:%s", gvk.Kind),
fmt.Sprintf("kubernetes:extensions/v1beta1:%s", gvk.Kind),
}
case kinds.Ingress:
return []string{
fmt.Sprintf("kubernetes:networking.k8s.io/v1beta1:%s", gvk.Kind),
fmt.Sprintf("kubernetes:extensions/v1beta1:%s", gvk.Kind),
}
case kinds.NetworkPolicy:
return []string{
fmt.Sprintf("kubernetes:networking.k8s.io/v1:%s", gvk.Kind),
fmt.Sprintf("kubernetes:extensions/v1beta1:%s", gvk.Kind),
}
case kinds.PodSecurityPolicy:
return []string{
fmt.Sprintf("kubernetes:policy/v1beta1:%s", gvk.Kind),
fmt.Sprintf("kubernetes:extensions/v1beta1:%s", gvk.Kind),
}
case kinds.PriorityClass:
return []string{
fmt.Sprintf("kubernetes:scheduling.k8s.io/v1:%s", gvk.Kind),
fmt.Sprintf("kubernetes:scheduling.k8s.io/v1beta1:%s", gvk.Kind),
fmt.Sprintf("kubernetes:scheduling.k8s.io/v1alpha1:%s", gvk.Kind),
}
default:
return []string{}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

opts = pulumi.mergeOptions(opts, {
aliases: [
{ type: "kubernetes:admissionregistration.k8s.io/v1beta1:MutatingWebhookConfiguration" },
],
});
super(MutatingWebhookConfiguration.__pulumiType, name, props, opts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

opts = pulumi.mergeOptions(opts, {
aliases: [
{ type: "kubernetes:admissionregistration.k8s.io/v1beta1:ValidatingWebhookConfiguration" },
],
});
super(ValidatingWebhookConfiguration.__pulumiType, name, props, opts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

opts = pulumi.mergeOptions(opts, {
aliases: [
{ type: "kubernetes:admissionregistration.k8s.io/v1:MutatingWebhookConfiguration" },
],
});
super(MutatingWebhookConfiguration.__pulumiType, name, props, opts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

opts = pulumi.mergeOptions(opts, {
aliases: [
{ type: "kubernetes:admissionregistration.k8s.io/v1:ValidatingWebhookConfiguration" },
],
});
super(ValidatingWebhookConfiguration.__pulumiType, name, props, opts);
}
}
5 changes: 5 additions & 0 deletions sdk/nodejs/apiextensions/v1/CustomResourceDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

opts = pulumi.mergeOptions(opts, {
aliases: [
{ type: "kubernetes:apiextensions.k8s.io/v1beta1:CustomResourceDefinition" },
],
});
super(CustomResourceDefinition.__pulumiType, name, props, opts);
}
}
5 changes: 5 additions & 0 deletions sdk/nodejs/apiextensions/v1beta1/CustomResourceDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

opts = pulumi.mergeOptions(opts, {
aliases: [
{ type: "kubernetes:apiextensions.k8s.io/v1:CustomResourceDefinition" },
],
});
super(CustomResourceDefinition.__pulumiType, name, props, opts);
}
}
5 changes: 5 additions & 0 deletions sdk/nodejs/apiregistration/v1/APIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

opts = pulumi.mergeOptions(opts, {
aliases: [
{ type: "kubernetes:apiregistration.k8s.io/v1beta1:APIService" },
],
});
super(APIService.__pulumiType, name, props, opts);
}
}
5 changes: 5 additions & 0 deletions sdk/nodejs/apiregistration/v1beta1/APIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

opts = pulumi.mergeOptions(opts, {
aliases: [
{ type: "kubernetes:apiregistration.k8s.io/v1:APIService" },
],
});
super(APIService.__pulumiType, name, props, opts);
}
}
6 changes: 6 additions & 0 deletions sdk/nodejs/apps/v1/ControllerRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

opts = pulumi.mergeOptions(opts, {
aliases: [
{ type: "kubernetes:apps/v1beta1:ControllerRevision" },
{ type: "kubernetes:apps/v1beta2:ControllerRevision" },
],
});
super(ControllerRevision.__pulumiType, name, props, opts);
}
}
10 changes: 4 additions & 6 deletions sdk/nodejs/apps/v1/DaemonSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,12 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

const _opts = pulumi.mergeOptions(opts, {
opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:apps/v1:DaemonSet", name: name },
{ parent: opts.parent, type: "kubernetes:apps/v1beta2:DaemonSet", name: name },
{ parent: opts.parent, type: "kubernetes:extensions/v1beta1:DaemonSet", name: name },
{ type: "kubernetes:apps/v1beta2:DaemonSet" },
{ type: "kubernetes:extensions/v1beta1:DaemonSet" },
],
});

super(DaemonSet.__pulumiType, name, props, _opts);
super(DaemonSet.__pulumiType, name, props, opts);
}
}
12 changes: 5 additions & 7 deletions sdk/nodejs/apps/v1/Deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,13 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

const _opts = pulumi.mergeOptions(opts, {
opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:apps/v1:Deployment", name: name },
{ parent: opts.parent, type: "kubernetes:apps/v1beta1:Deployment", name: name },
{ parent: opts.parent, type: "kubernetes:apps/v1beta2:Deployment", name: name },
{ parent: opts.parent, type: "kubernetes:extensions/v1beta1:Deployment", name: name },
{ type: "kubernetes:apps/v1beta1:Deployment" },
{ type: "kubernetes:apps/v1beta2:Deployment" },
{ type: "kubernetes:extensions/v1beta1:Deployment" },
],
});

super(Deployment.__pulumiType, name, props, _opts);
super(Deployment.__pulumiType, name, props, opts);
}
}
10 changes: 4 additions & 6 deletions sdk/nodejs/apps/v1/ReplicaSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,12 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

const _opts = pulumi.mergeOptions(opts, {
opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:apps/v1:ReplicaSet", name: name },
{ parent: opts.parent, type: "kubernetes:apps/v1beta2:ReplicaSet", name: name },
{ parent: opts.parent, type: "kubernetes:extensions/v1beta1:ReplicaSet", name: name },
{ type: "kubernetes:apps/v1beta2:ReplicaSet" },
{ type: "kubernetes:extensions/v1beta1:ReplicaSet" },
],
});

super(ReplicaSet.__pulumiType, name, props, _opts);
super(ReplicaSet.__pulumiType, name, props, opts);
}
}
11 changes: 4 additions & 7 deletions sdk/nodejs/apps/v1/StatefulSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,12 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

const _opts = pulumi.mergeOptions(opts, {
opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:apps/v1:StatefulSet", name: name },
{ parent: opts.parent, type: "kubernetes:apps/v1beta1:StatefulSet", name: name },
{ parent: opts.parent, type: "kubernetes:apps/v1beta2:StatefulSet", name: name },
{ parent: opts.parent, type: "kubernetes:extensions/v1beta1:StatefulSet", name: name },
lblackstone marked this conversation as resolved.
Show resolved Hide resolved
{ type: "kubernetes:apps/v1beta1:StatefulSet" },
{ type: "kubernetes:apps/v1beta2:StatefulSet" },
],
});

super(StatefulSet.__pulumiType, name, props, _opts);
super(StatefulSet.__pulumiType, name, props, opts);
}
}
6 changes: 6 additions & 0 deletions sdk/nodejs/apps/v1beta1/ControllerRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

opts = pulumi.mergeOptions(opts, {
aliases: [
{ type: "kubernetes:apps/v1:ControllerRevision" },
{ type: "kubernetes:apps/v1beta2:ControllerRevision" },
],
});
super(ControllerRevision.__pulumiType, name, props, opts);
}
}
Loading