-
Notifications
You must be signed in to change notification settings - Fork 113
/
Program.cs
276 lines (250 loc) · 9.98 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using Pulumi;
using Pulumi.Kubernetes;
using Pulumi.Kubernetes.Yaml;
using Pulumi.Kubernetes.Kustomize;
using Pulumi.Kubernetes.Core.V1;
using Pulumi.Kubernetes.Helm;
using Pulumi.Kubernetes.Helm.V3;
using Pulumi.Kubernetes.Types.Inputs.Core.V1;
using Pulumi.Kubernetes.Types.Inputs.Apps.V1;
using Pulumi.Kubernetes.Types.Inputs.Meta.V1;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
using Time = Pulumiverse.Time;
Provider? nulloptsProvider = null;
// apply_default_opts is a stack transformation that applies default opts to any resource whose name ends with "-nullopts".
// this is intended to be applied to component resources only.
ResourceTransformationResult? applyDefaultOpts(ResourceTransformationArgs args)
{
if (nulloptsProvider is null)
{
return null;
}
if (args.Resource.GetResourceName().EndsWith("-nullopts"))
{
if (args.Options is ComponentResourceOptions)
{
var options = ComponentResourceOptions.Merge(
(ComponentResourceOptions)args.Options,
new ComponentResourceOptions { Provider = nulloptsProvider });
return new ResourceTransformationResult(args.Args, options);
}
if (args.Options is CustomResourceOptions)
{
var options = CustomResourceOptions.Merge(
(CustomResourceOptions)args.Options,
new CustomResourceOptions { Provider = nulloptsProvider });
return new ResourceTransformationResult(args.Args, options);
}
}
return null;
}
return await Deployment.RunAsync(async () =>
{
var bootstrapProvider = new Provider("bootstrap", new ProviderArgs { });
// create a set of providers across namespaces, simply to facilitate the reuse of manifests in the below tests.
var nulloptsNs = new Namespace("nullopts", new NamespaceArgs { }, new CustomResourceOptions { Provider = bootstrapProvider });
var aNs = new Namespace("a", new NamespaceArgs { }, new CustomResourceOptions { Provider = bootstrapProvider });
var bNs = new Namespace("b", new NamespaceArgs { }, new CustomResourceOptions { Provider = bootstrapProvider });
nulloptsProvider = new Provider("nullopts", new ProviderArgs { Namespace = nulloptsNs.Metadata.Apply(m => m.Name) });
var aProvider = new Provider("a", new ProviderArgs { Namespace = aNs.Metadata.Apply(m => m.Name) });
var bProvider = new Provider("b", new ProviderArgs { Namespace = bNs.Metadata.Apply(m => m.Name) });
// a sleep resource to exercise the "depends_on" component-level option
var sleep = new Time.Sleep("sleep", new Time.SleepArgs { CreateDuration = "1s" });
// a parent for "a" resources to exercise aliasing: https://github.com/pulumi/pulumi-kubernetes/issues/1214
var aParent = new ComponentResource("pkg:index:MyComponent", "a", new ComponentResourceOptions
{
Providers = { aProvider },
});
// note: applyDefaultOpts is defined above because stack transformations must be registered eagerly.
// applyAlias is a Pulumi transformation that applies a unique alias to each resource.
ResourceTransformationResult? applyAlias(ResourceTransformationArgs args)
{
string name = args.Resource.GetResourceName();
if (args.Options is ComponentResourceOptions)
{
var options = ComponentResourceOptions.Merge(
(ComponentResourceOptions)args.Options,
new ComponentResourceOptions { Aliases = { new Alias { Name = $"{name}-aliased" } } });
return new ResourceTransformationResult(args.Args, options);
}
if (args.Options is CustomResourceOptions)
{
var options = CustomResourceOptions.Merge(
(CustomResourceOptions)args.Options,
new CustomResourceOptions { Aliases = { new Alias { Name = $"{name}-aliased" } } });
return new ResourceTransformationResult(args.Args, options);
}
return null;
}
// transform_k8s is a Kubernetes transformation that applies a unique alias and annotation to each resource.
ImmutableDictionary<string, object> transformK8s(ImmutableDictionary<string, object> obj, CustomResourceOptions opts)
{
var meta = (ImmutableDictionary<string, object>)obj["metadata"];
var name = (string)meta["name"];
opts.Aliases.Add(new Alias { Name = $"{name}-k8s-aliased" });
obj = obj.SetItem("metadata", meta.SetItem("annotations", new Dictionary<string, object> { { "transformed", "true" } }.ToImmutableDictionary()));
return obj;
return null;
}
// --- ConfigGroup ---
// options: Providers, Aliases, DependsOn, IgnoreChanges, Protect, ResourceTransformations, Version, PluginDownloadURL
// args: ResourcePrefix, SkipAwait, Transformations
new ConfigGroup("cg-options", new ConfigGroupArgs
{
ResourcePrefix = "cg-options",
SkipAwait = true,
Transformations = { transformK8s },
Files = new string[] { "testdata/options/configgroup/*.yaml" },
Yaml = { @"
apiVersion: v1
kind: ConfigMap
metadata:
name: cg-options-cm-1
",
}
}, new ComponentResourceOptions
{
Parent = aParent,
Providers = { aProvider },
Aliases = { new Alias { Name = "cg-options-old" } },
IgnoreChanges = { "ignored" },
Protect = true,
DependsOn = { sleep },
ResourceTransformations = { applyAlias },
Version = "1.2.3",
PluginDownloadURL = "https://a.pulumi.test",
});
// "provider" option
new ConfigGroup("cg-provider", new ConfigGroupArgs
{
ResourcePrefix = "cg-provider",
Yaml = { @"
apiVersion: v1
kind: ConfigMap
metadata:
name: cg-provider-cm-1
",
}
}, new ComponentResourceOptions { Provider = bProvider });
// null opts
new ConfigGroup("cg-nullopts", new ConfigGroupArgs
{
ResourcePrefix = "cg-nullopts",
Yaml = { @"
apiVersion: v1
kind: ConfigMap
metadata:
name: cg-nullopts-cm-1
",
}
});
// --- ConfigFile ---
// options: Providers, Aliases, DependsOn, IgnoreChanges, Protect, ResourceTransformations, Version, PluginDownloadURL
// args: ResourcePrefix, SkipAwait, Transformations
new ConfigFile("cf-options", new ConfigFileArgs
{
ResourcePrefix = "cf-options",
SkipAwait = true,
Transformations = { transformK8s },
File = "testdata/options/configfile/manifest.yaml",
}, new ComponentResourceOptions
{
Parent = aParent,
Providers = { aProvider },
Aliases = { new Alias { Name = "cf-options-old" } },
IgnoreChanges = { "ignored" },
Protect = true,
DependsOn = { sleep },
ResourceTransformations = { applyAlias },
Version = "1.2.3",
PluginDownloadURL = "https://a.pulumi.test",
});
// "provider" option
new ConfigFile("cf-provider", new ConfigFileArgs
{
ResourcePrefix = "cf-provider",
File = "testdata/options/configfile/manifest.yaml",
}, new ComponentResourceOptions { Providers = { bProvider } });
// null opts
new ConfigFile("cf-nullopts", new ConfigFileArgs
{
ResourcePrefix = "cf-nullopts",
File = "testdata/options/configfile/manifest.yaml",
});
// empty manifest
new ConfigFile("cf-empty", new ConfigFileArgs
{
ResourcePrefix = "cf-empty",
File = "testdata/options/configfile/empty.yaml",
}, new ComponentResourceOptions { Providers = { bProvider } });
// --- Directory ---
// options: Providers, Aliases, DependsOn, IgnoreChanges, Protect, ResourceTransformations, Version, PluginDownloadURL
// args: ResourcePrefix, Transformations
new Directory("kustomize-options", new DirectoryArgs
{
ResourcePrefix = "kustomize-options",
Transformations = { transformK8s },
Directory = "testdata/options/kustomize",
}, new ComponentResourceOptions
{
Parent = aParent,
Providers = { aProvider },
Aliases = { new Alias { Name = "kustomize-options-old" } },
IgnoreChanges = { "ignored" },
Protect = true,
DependsOn = { sleep },
ResourceTransformations = { applyAlias },
Version = "1.2.3",
PluginDownloadURL = "https://a.pulumi.test",
});
// "provider" option
new Directory("kustomize-provider", new DirectoryArgs
{
ResourcePrefix = "kustomize-provider",
Directory = "testdata/options/kustomize",
}, new ComponentResourceOptions { Provider = bProvider });
// null opts
new Directory("kustomize-nullopts", new DirectoryArgs
{
ResourcePrefix = "kustomize-nullopts",
Directory = "testdata/options/kustomize",
});
// --- Chart ---
// options: Providers, Aliases, DependsOn, IgnoreChanges, Protect, ResourceTransformations, Version, PluginDownloadURL
// args: ResourcePrefix, SkipAwait, Transformations
new Chart("chart-options", new LocalChartArgs
{
ResourcePrefix = "chart-options",
SkipAwait = true,
Transformations = { transformK8s },
Path = "testdata/options/chart",
}, new ComponentResourceOptions
{
Parent = aParent,
Providers = { aProvider },
Aliases = { new Alias { Name = "chart-options-old" } },
IgnoreChanges = { "ignored" },
Protect = true,
DependsOn = { sleep },
ResourceTransformations = { applyAlias },
Version = "1.2.3",
PluginDownloadURL = "https://a.pulumi.test",
});
// "provider" option
new Chart("chart-provider", new LocalChartArgs
{
ResourcePrefix = "chart-provider",
Path = "testdata/options/chart",
}, new ComponentResourceOptions { Provider = bProvider });
// null opts
new Chart("chart-nullopts", new LocalChartArgs
{
ResourcePrefix = "chart-nullopts",
Path = "testdata/options/chart",
});
return new Dictionary<string, object?>
{
};
}, new StackOptions { ResourceTransformations = { applyDefaultOpts } });