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 3 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
113 changes: 62 additions & 51 deletions pkg/gen/typegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,68 @@ 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
Member

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
})
aliasesForGVK := func(gvk schema.GroupVersionKind) []string {
var results []string

for _, alias := range aliases[gvk.Kind] {
results = append(results, alias.(string))
}
return results
}

//
// Assemble a `KindConfig` for each Kubernetes kind.
//
Expand Down Expand Up @@ -1230,54 +1292,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,13 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

super(MutatingWebhookConfiguration.__pulumiType, name, props, opts);
const _opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:admissionregistration.k8s.io/v1:MutatingWebhookConfiguration", name: name },
lblackstone marked this conversation as resolved.
Show resolved Hide resolved
{ parent: opts.parent, type: "kubernetes:admissionregistration.k8s.io/v1beta1:MutatingWebhookConfiguration", name: name },
lblackstone marked this conversation as resolved.
Show resolved Hide resolved
],
});

super(MutatingWebhookConfiguration.__pulumiType, name, props, _opts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

super(ValidatingWebhookConfiguration.__pulumiType, name, props, opts);
const _opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:admissionregistration.k8s.io/v1:ValidatingWebhookConfiguration", name: name },
{ parent: opts.parent, type: "kubernetes:admissionregistration.k8s.io/v1beta1:ValidatingWebhookConfiguration", name: name },
],
});

super(ValidatingWebhookConfiguration.__pulumiType, name, props, _opts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

super(MutatingWebhookConfiguration.__pulumiType, name, props, opts);
const _opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:admissionregistration.k8s.io/v1:MutatingWebhookConfiguration", name: name },
lblackstone marked this conversation as resolved.
Show resolved Hide resolved
{ parent: opts.parent, type: "kubernetes:admissionregistration.k8s.io/v1beta1:MutatingWebhookConfiguration", name: name },
],
});

super(MutatingWebhookConfiguration.__pulumiType, name, props, _opts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

super(ValidatingWebhookConfiguration.__pulumiType, name, props, opts);
const _opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:admissionregistration.k8s.io/v1:ValidatingWebhookConfiguration", name: name },
{ parent: opts.parent, type: "kubernetes:admissionregistration.k8s.io/v1beta1:ValidatingWebhookConfiguration", name: name },
],
});

super(ValidatingWebhookConfiguration.__pulumiType, name, props, _opts);
}
}
9 changes: 8 additions & 1 deletion sdk/nodejs/apiextensions/v1/CustomResourceDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

super(CustomResourceDefinition.__pulumiType, name, props, opts);
const _opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:apiextensions.k8s.io/v1:CustomResourceDefinition", name: name },
{ parent: opts.parent, type: "kubernetes:apiextensions.k8s.io/v1beta1:CustomResourceDefinition", name: name },
],
});

super(CustomResourceDefinition.__pulumiType, name, props, _opts);
}
}
9 changes: 8 additions & 1 deletion sdk/nodejs/apiextensions/v1beta1/CustomResourceDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

super(CustomResourceDefinition.__pulumiType, name, props, opts);
const _opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:apiextensions.k8s.io/v1:CustomResourceDefinition", name: name },
{ parent: opts.parent, type: "kubernetes:apiextensions.k8s.io/v1beta1:CustomResourceDefinition", name: name },
],
});

super(CustomResourceDefinition.__pulumiType, name, props, _opts);
}
}
9 changes: 8 additions & 1 deletion sdk/nodejs/apiregistration/v1/APIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

super(APIService.__pulumiType, name, props, opts);
const _opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:apiregistration.k8s.io/v1:APIService", name: name },
{ parent: opts.parent, type: "kubernetes:apiregistration.k8s.io/v1beta1:APIService", name: name },
],
});

super(APIService.__pulumiType, name, props, _opts);
}
}
9 changes: 8 additions & 1 deletion sdk/nodejs/apiregistration/v1beta1/APIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

super(APIService.__pulumiType, name, props, opts);
const _opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:apiregistration.k8s.io/v1:APIService", name: name },
{ parent: opts.parent, type: "kubernetes:apiregistration.k8s.io/v1beta1:APIService", name: name },
],
});

super(APIService.__pulumiType, name, props, _opts);
}
}
10 changes: 9 additions & 1 deletion sdk/nodejs/apps/v1/ControllerRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

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

super(ControllerRevision.__pulumiType, name, props, _opts);
}
}
1 change: 0 additions & 1 deletion sdk/nodejs/apps/v1/StatefulSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ import { getVersion } from "../../version";
{ 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
],
});

Expand Down
10 changes: 9 additions & 1 deletion sdk/nodejs/apps/v1beta1/ControllerRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

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

super(ControllerRevision.__pulumiType, name, props, _opts);
}
}
1 change: 0 additions & 1 deletion sdk/nodejs/apps/v1beta1/StatefulSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ import { getVersion } from "../../version";
{ 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 },
],
});

Expand Down
10 changes: 9 additions & 1 deletion sdk/nodejs/apps/v1beta2/ControllerRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

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

super(ControllerRevision.__pulumiType, name, props, _opts);
}
}
1 change: 0 additions & 1 deletion sdk/nodejs/apps/v1beta2/StatefulSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ import { getVersion } from "../../version";
{ 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 },
],
});

Expand Down
9 changes: 8 additions & 1 deletion sdk/nodejs/authentication/v1/TokenReview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

super(TokenReview.__pulumiType, name, props, opts);
const _opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:authentication.k8s.io/v1:TokenReview", name: name },
{ parent: opts.parent, type: "kubernetes:authentication.k8s.io/v1beta1:TokenReview", name: name },
],
});

super(TokenReview.__pulumiType, name, props, _opts);
}
}
9 changes: 8 additions & 1 deletion sdk/nodejs/authentication/v1beta1/TokenReview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

super(TokenReview.__pulumiType, name, props, opts);
const _opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:authentication.k8s.io/v1:TokenReview", name: name },
{ parent: opts.parent, type: "kubernetes:authentication.k8s.io/v1beta1:TokenReview", name: name },
],
});

super(TokenReview.__pulumiType, name, props, _opts);
}
}
9 changes: 8 additions & 1 deletion sdk/nodejs/authorization/v1/LocalSubjectAccessReview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

super(LocalSubjectAccessReview.__pulumiType, name, props, opts);
const _opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:authorization.k8s.io/v1:LocalSubjectAccessReview", name: name },
{ parent: opts.parent, type: "kubernetes:authorization.k8s.io/v1beta1:LocalSubjectAccessReview", name: name },
],
});

super(LocalSubjectAccessReview.__pulumiType, name, props, _opts);
}
}
9 changes: 8 additions & 1 deletion sdk/nodejs/authorization/v1/SelfSubjectAccessReview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ import { getVersion } from "../../version";
opts.version = getVersion();
}

super(SelfSubjectAccessReview.__pulumiType, name, props, opts);
const _opts = pulumi.mergeOptions(opts, {
aliases: [
{ parent: opts.parent, type: "kubernetes:authorization.k8s.io/v1:SelfSubjectAccessReview", name: name },
{ parent: opts.parent, type: "kubernetes:authorization.k8s.io/v1beta1:SelfSubjectAccessReview", name: name },
],
});

super(SelfSubjectAccessReview.__pulumiType, name, props, _opts);
}
}
Loading