Skip to content

Commit

Permalink
Auto-alias resource apiVersions
Browse files Browse the repository at this point in the history
Kubernetes apiVersions are mostly forward/backward
compatible, so for cases where we know it's safe, we
auto-alias the apiVersions so that the engine does not
force a replacement when a resource is updated to a
compatible apiVersion.
  • Loading branch information
lblackstone committed Sep 19, 2019
1 parent a28147b commit 74d9191
Show file tree
Hide file tree
Showing 375 changed files with 2,319 additions and 182 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Improvements

- Auto-alias resource apiVersions. (https://github.com/pulumi/pulumi-kubernetes/pull/798).
- Provide detailed error for removed apiVersions. (https://github.com/pulumi/pulumi-kubernetes/pull/809).

## 1.1.0 (September 18, 2019)
Expand Down
8 changes: 8 additions & 0 deletions pkg/gen/nodejs-templates/kind.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
{{#Aliases}}
{ parent: opts.parent, type: "{{.}}", name: name },
{{/Aliases}}
...((opts && opts.aliases) || []),
];

super({{Kind}}.__pulumiType, name, props, opts);
}
}
1 change: 1 addition & 0 deletions pkg/gen/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func NodeJSClient(swagger map[string]interface{}, templateDir string,
"Properties": kind.Properties(),
"RequiredInputProperties": kind.RequiredInputProperties(),
"OptionalInputProperties": kind.OptionalInputProperties(),
"Aliases": kind.Aliases(),
"URNAPIVersion": kind.URNAPIVersion(),
"Version": version.Version(),
"AwaitComment": kind.awaitComment,
Expand Down
10 changes: 9 additions & 1 deletion pkg/gen/python-templates/kind.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,15 @@ class {{Kind}}(pulumi.CustomResource):

__props__['status'] = None

opts = pulumi.ResourceOptions.merge(opts, pulumi.ResourceOptions(version=version.get_version()))
parent = opts.parent if opts and opts.parent else None
aliases = [
{{#Aliases}}
pulumi.Alias(parent=parent, type_="{{.}}", name=resource_name),
{{/Aliases}}
]

opts = pulumi.ResourceOptions.merge(opts, pulumi.ResourceOptions(
version=version.get_version(), aliases=aliases))

super({{Kind}}, self).__init__(
"kubernetes:{{URNAPIVersion}}:{{Kind}}",
Expand Down
55 changes: 55 additions & 0 deletions pkg/gen/typegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/ahmetb/go-linq"
"github.com/jinzhu/copier"
"github.com/mitchellh/go-wordwrap"
"github.com/pulumi/pulumi-kubernetes/pkg/kinds"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"

Expand Down Expand Up @@ -138,6 +139,7 @@ type KindConfig struct {
properties []*Property
requiredInputProperties []*Property
optionalInputProperties []*Property
aliases []string

gvk *schema.GroupVersionKind // Used for sorting.
apiVersion string
Expand Down Expand Up @@ -167,6 +169,9 @@ func (kc *KindConfig) RequiredInputProperties() []*Property { return kc.required
// Kubernetes API kind (i.e., things that we will want to provide, like `thing.metadata`, etc.).
func (kc *KindConfig) OptionalInputProperties() []*Property { return kc.optionalInputProperties }

// Aliases returns the list of aliases for a Kubernetes API kind.
func (kc *KindConfig) Aliases() []string { return kc.aliases }

// APIVersion returns the fully-qualified apiVersion (e.g., `storage.k8s.io/v1` for storage, etc.)
func (kc *KindConfig) APIVersion() string { return kc.apiVersion }

Expand Down Expand Up @@ -828,6 +833,7 @@ func createGroups(definitionsJSON map[string]interface{}, opts groupOpts) []*Gro
properties: properties,
requiredInputProperties: requiredInputProperties,
optionalInputProperties: optionalInputProperties,
aliases: aliasesForGVK(d.gvk),
gvk: &d.gvk,
apiVersion: fqGroupVersion,
rawAPIVersion: defaultGroupVersion,
Expand Down Expand Up @@ -900,3 +906,52 @@ func createGroups(definitionsJSON map[string]interface{}, opts groupOpts) []*Gro

return groups
}

func aliasesForGVK(gvk schema.GroupVersionKind) []string {
kind := kinds.Kind(gvk.Kind)

// It's unsafe to move between `extensions/v1beta1`, and the newer apiVersions due to differences in
// the behavior of the Deployment and ReplicaSet. Even if the apiVersion is changed,
// the resource will continue to use the old behavior, which will break the await logic. Without an
// alias set, the engine will recreate the resource with the newer apiVersion.
if gvk.GroupVersion().String() == "extensions/v1beta1" {
switch kind {
case kinds.DaemonSet, kinds.Deployment, kinds.ReplicaSet, kinds.StatefulSet:
return []string{}
}
}

switch kind {
case kinds.DaemonSet:
return []string{
"kubernetes:apps/v1:DaemonSet",
// For some reason, there is no `apps/v1beta1:DaemonSet`.
"kubernetes:apps/v1beta2:DaemonSet",
}
case kinds.Deployment:
return []string{
"kubernetes:apps/v1:Deployment",
"kubernetes:apps/v1beta1:Deployment",
"kubernetes:apps/v1beta2:Deployment",
}
case kinds.Ingress:
return []string{
"kubernetes:networking/v1beta1:Ingress",
"kubernetes:extensions/v1beta1:Ingress",
}
case kinds.ReplicaSet:
return []string{
"kubernetes:apps/v1:ReplicaSet",
// For some reason, there is no `apps/v1beta1:ReplicaSet`.
"kubernetes:apps/v1beta2:ReplicaSet",
}
case kinds.StatefulSet:
return []string{
"kubernetes:apps/v1:StatefulSet",
"kubernetes:apps/v1beta1:StatefulSet",
"kubernetes:apps/v1beta2:StatefulSet",
}
default:
return []string{}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
...((opts && opts.aliases) || []),
];

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

opts.aliases = [
...((opts && opts.aliases) || []),
];

super(MutatingWebhookConfigurationList.__pulumiType, name, props, opts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
...((opts && opts.aliases) || []),
];

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

opts.aliases = [
...((opts && opts.aliases) || []),
];

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

opts.aliases = [
...((opts && opts.aliases) || []),
];

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

opts.aliases = [
...((opts && opts.aliases) || []),
];

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

opts.aliases = [
...((opts && opts.aliases) || []),
];

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

opts.aliases = [
...((opts && opts.aliases) || []),
];

super(ValidatingWebhookConfigurationList.__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 @@ -95,6 +95,11 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
...((opts && opts.aliases) || []),
];

super(CustomResourceDefinition.__pulumiType, name, props, opts);
}
}
5 changes: 5 additions & 0 deletions sdk/nodejs/apiextensions/v1/CustomResourceDefinitionList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
...((opts && opts.aliases) || []),
];

super(CustomResourceDefinitionList.__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 @@ -96,6 +96,11 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
...((opts && opts.aliases) || []),
];

super(CustomResourceDefinition.__pulumiType, name, props, opts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
...((opts && opts.aliases) || []),
];

super(CustomResourceDefinitionList.__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 @@ -94,6 +94,11 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
...((opts && opts.aliases) || []),
];

super(APIService.__pulumiType, name, props, opts);
}
}
5 changes: 5 additions & 0 deletions sdk/nodejs/apiregistration/v1/APIServiceList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
...((opts && opts.aliases) || []),
];

super(APIServiceList.__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 @@ -94,6 +94,11 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
...((opts && opts.aliases) || []),
];

super(APIService.__pulumiType, name, props, opts);
}
}
5 changes: 5 additions & 0 deletions sdk/nodejs/apiregistration/v1beta1/APIServiceList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
...((opts && opts.aliases) || []),
];

super(APIServiceList.__pulumiType, name, props, opts);
}
}
5 changes: 5 additions & 0 deletions sdk/nodejs/apps/v1/ControllerRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
...((opts && opts.aliases) || []),
];

super(ControllerRevision.__pulumiType, name, props, opts);
}
}
5 changes: 5 additions & 0 deletions sdk/nodejs/apps/v1/ControllerRevisionList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
...((opts && opts.aliases) || []),
];

super(ControllerRevisionList.__pulumiType, name, props, opts);
}
}
7 changes: 7 additions & 0 deletions sdk/nodejs/apps/v1/DaemonSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
{ parent: opts.parent, type: "kubernetes:apps/v1:DaemonSet", name: name },
{ parent: opts.parent, type: "kubernetes:apps/v1beta2:DaemonSet", name: name },
...((opts && opts.aliases) || []),
];

super(DaemonSet.__pulumiType, name, props, opts);
}
}
5 changes: 5 additions & 0 deletions sdk/nodejs/apps/v1/DaemonSetList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

opts.aliases = [
...((opts && opts.aliases) || []),
];

super(DaemonSetList.__pulumiType, name, props, opts);
}
}
8 changes: 8 additions & 0 deletions sdk/nodejs/apps/v1/Deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ import { getVersion } from "../../version";
if (!opts.version) {
opts.version = getVersion();
}

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 },
...((opts && opts.aliases) || []),
];

super(Deployment.__pulumiType, name, props, opts);
}
}
Loading

0 comments on commit 74d9191

Please sign in to comment.