-
Notifications
You must be signed in to change notification settings - Fork 115
/
helm.py
561 lines (479 loc) · 23.8 KB
/
helm.py
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# *** WARNING: this file was generated by the Pulumi Kubernetes codegen tool. ***
# *** Do not edit by hand unless you're certain you know what you are doing! ***
import json
from typing import Any, Callable, Optional, Sequence, Tuple, Union
import pulumi.runtime
from pulumi_kubernetes.yaml import _parse_yaml_document
from ... import _utilities
class Chart(pulumi.ComponentResource):
resources: pulumi.Output[dict]
"""
Kubernetes resources contained in this Chart.
"""
def __init__(self,
release_name: str,
config: Union['ChartOpts', 'LocalChartOpts'],
opts: Optional[pulumi.ResourceOptions] = None):
"""
Chart is a component representing a collection of resources described by an arbitrary Helm
Chart. The Chart can be fetched from any source that is accessible to the `helm` command
line. Values in the `values.yml` file can be overridden using `ChartOpts.values` (equivalent
to `--set` or having multiple `values.yml` files). Objects can be transformed arbitrarily by
supplying callbacks to `ChartOpts.transformations`.
Chart does not use Tiller. The Chart specified is copied and expanded locally; the semantics
are equivalent to running `helm template` and then using Pulumi to manage the resulting YAML
manifests. Any values that would be retrieved in-cluster are assigned fake values, and
none of Tiller's server-side validity testing is executed.
## Example Usage
### Local Chart Directory
```python
from pulumi_kubernetes.helm.v3 import Chart, LocalChartOpts
nginx_ingress = Chart(
"nginx-ingress",
LocalChartOpts(
path="./nginx-ingress",
),
)
```
### Remote Chart
```python
from pulumi_kubernetes.helm.v3 import Chart, ChartOpts, FetchOpts
nginx_ingress = Chart(
"nginx-ingress",
ChartOpts(
chart="nginx-ingress",
version="1.24.4",
fetch_opts=FetchOpts(
repo="https://charts.helm.sh/stable",
),
),
)
```
### Set Chart Values
```python
from pulumi_kubernetes.helm.v3 import Chart, ChartOpts, FetchOpts
nginx_ingress = Chart(
"nginx-ingress",
ChartOpts(
chart="nginx-ingress",
version="1.24.4",
fetch_opts=FetchOpts(
repo="https://charts.helm.sh/stable",
),
values={
"controller": {
"metrics": {
"enabled": True,
},
},
},
),
)
```
### Deploy Chart into Namespace
```python
from pulumi_kubernetes.helm.v3 import Chart, ChartOpts, FetchOpts
nginx_ingress = Chart(
"nginx-ingress",
ChartOpts(
chart="nginx-ingress",
version="1.24.4",
namespace="test-namespace",
fetch_opts=FetchOpts(
repo="https://charts.helm.sh/stable",
),
),
)
```
### Chart with Transformations
```python
from pulumi_kubernetes.helm.v3 import Chart, ChartOpts, FetchOpts
# Make every service private to the cluster, i.e., turn all services into ClusterIP instead of LoadBalancer.
def make_service_private(obj, opts):
if obj["kind"] == "Service" and obj["apiVersion"] == "v1":
try:
t = obj["spec"]["type"]
if t == "LoadBalancer":
obj["spec"]["type"] = "ClusterIP"
except KeyError:
pass
# Set a resource alias for a previous name.
def alias(obj, opts):
if obj["kind"] == "Deployment":
opts.aliases = ["oldName"]
# Omit a resource from the Chart by transforming the specified resource definition to an empty List.
def omit_resource(obj, opts):
if obj["kind"] == "Pod" and obj["metadata"]["name"] == "test":
obj["apiVersion"] = "v1"
obj["kind"] = "List"
nginx_ingress = Chart(
"nginx-ingress",
ChartOpts(
chart="nginx-ingress",
version="1.24.4",
fetch_opts=FetchOpts(
repo="https://charts.helm.sh/stable",
),
transformations=[make_service_private, alias, omit_resource],
),
)
```
:param str release_name: Name of the Chart (e.g., nginx-ingress).
:param Union[ChartOpts, LocalChartOpts] config: Configuration options for the Chart.
:param Optional[pulumi.ResourceOptions] opts: A bag of options that control this
resource's behavior.
"""
if not release_name:
raise TypeError('Missing release name argument')
if not isinstance(release_name, str):
raise TypeError('Expected release name to be a string')
if config and not isinstance(config, ChartOpts) and not isinstance(config, LocalChartOpts):
raise TypeError('Expected config to be a ChartOpts or LocalChartOpts instance')
if opts is None:
opts = pulumi.ResourceOptions()
if not isinstance(opts, pulumi.ResourceOptions):
raise TypeError('Expected resource options to be a ResourceOptions instance')
__props__ = dict()
if config.resource_prefix:
release_name = f"{config.resource_prefix}-{release_name}"
alias_opts = pulumi.ResourceOptions(aliases=[pulumi.Alias(type_="kubernetes:helm.sh/v2:Chart")])
super(Chart, self).__init__(
"kubernetes:helm.sh/v3:Chart",
release_name,
__props__,
pulumi.ResourceOptions.merge(opts, alias_opts))
config.release_name = release_name
all_config = pulumi.Output.from_input((config, pulumi.ResourceOptions(parent=self)))
# 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 Helm chart created.
self.resources = all_config.apply(_parse_chart)
self.register_outputs({"resources": self.resources})
def get_resource(self,
group_version_kind: str,
name: str,
namespace: Optional[str] = None) -> pulumi.Output[pulumi.CustomResource]:
"""
get_resource returns a resource defined by a built-in Kubernetes group/version/kind and
name. For example: `get_resource("apps/v1/Deployment", "nginx")`
:param str group_version_kind: Group/Version/Kind of the resource, e.g., `apps/v1/Deployment`
:param str name: Name of the resource to retrieve
:param Optional[str] namespace: Optional namespace of the resource to retrieve
"""
# `id` will either be `${name}` or `${namespace}/${name}`.
id = pulumi.Output.from_input(name)
if namespace is not 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])
class FetchOpts:
"""
FetchOpts is a bag of configuration options to customize the fetching of the Helm chart.
"""
version: Optional[pulumi.Input[str]]
"""
Specific version of a chart. If unset, the latest version is fetched.
"""
ca_file: Optional[pulumi.Input[str]]
"""
Verify certificates of HTTPS-enabled servers using this CA bundle.
"""
cert_file: Optional[pulumi.Input[str]]
"""
Identify HTTPS client using this SSL certificate file.
"""
key_file: Optional[pulumi.Input[str]]
"""
Identify HTTPS client using this SSL key file.
"""
destination: Optional[pulumi.Input[str]]
"""
Location to write the chart. If this and [tardir] are specified, tardir is appended
to this (default ".").
"""
keyring: Optional[pulumi.Input[str]]
"""
Keyring containing public keys (default "/Users/alex/.gnupg/pubring.gpg").
"""
password: Optional[pulumi.Input[str]]
"""
Chart repository password.
"""
repo: Optional[pulumi.Input[str]]
"""
Chart repository url where to locate the requested chart.
"""
untar_dir: Optional[pulumi.Input[str]]
"""
If [untar] is specified, this flag specifies the name of the directory into which
the chart is expanded (default ".").
"""
username: Optional[pulumi.Input[str]]
"""
Chart repository username.
"""
home: Optional[pulumi.Input[str]]
"""
Location of your Helm config. Overrides $HELM_HOME (default "/Users/alex/.helm").
"""
devel: Optional[pulumi.Input[bool]]
"""
Use development versions, too. Equivalent to version '>0.0.0-0'. If [version] is set,
this is ignored.
"""
prov: Optional[pulumi.Input[bool]]
"""
Fetch the provenance file, but don't perform verification.
"""
untar: Optional[pulumi.Input[bool]]
"""
If set to false, will leave the chart as a tarball after downloading.
"""
verify: Optional[pulumi.Input[bool]]
"""
Verify the package against its signature.
"""
def __init__(self,
version: Optional[pulumi.Input[str]] = None,
ca_file: Optional[pulumi.Input[str]] = None,
cert_file: Optional[pulumi.Input[str]] = None,
key_file: Optional[pulumi.Input[str]] = None,
destination: Optional[pulumi.Input[str]] = None,
keyring: Optional[pulumi.Input[str]] = None,
password: Optional[pulumi.Input[str]] = None,
repo: Optional[pulumi.Input[str]] = None,
untar_dir: Optional[pulumi.Input[str]] = None,
username: Optional[pulumi.Input[str]] = None,
home: Optional[pulumi.Input[str]] = None,
devel: Optional[pulumi.Input[bool]] = None,
prov: Optional[pulumi.Input[bool]] = None,
untar: Optional[pulumi.Input[bool]] = None,
verify: Optional[pulumi.Input[bool]] = None):
"""
:param Optional[pulumi.Input[str]] version: Specific version of a chart. If unset,
the latest version is fetched.
:param Optional[pulumi.Input[str]] ca_file: Verify certificates of HTTPS-enabled
servers using this CA bundle.
:param Optional[pulumi.Input[str]] cert_file: Identify HTTPS client using this SSL
certificate file.
:param Optional[pulumi.Input[str]] key_file: Identify HTTPS client using this SSL
key file.
:param Optional[pulumi.Input[str]] destination: Location to write the chart.
If this and [tardir] are specified, tardir is appended to this (default ".").
:param Optional[pulumi.Input[str]] keyring: Keyring containing public keys
(default "/Users/<user>/.gnupg/pubring.gpg").
:param Optional[pulumi.Input[str]] password: Chart repository password.
:param Optional[pulumi.Input[str]] repo: Chart repository url where to locate
the requested chart.
:param Optional[pulumi.Input[str]] untar_dir: If [untar] is specified, this flag
specifies the name of the directory into which the chart is
expanded (default ".").
:param Optional[pulumi.Input[str]] username: Chart repository username.
:param Optional[pulumi.Input[str]] home: Location of your Helm config. Overrides
$HELM_HOME (default "/Users/<user>/.helm").
:param Optional[pulumi.Input[bool]] devel: Use development versions, too.
Equivalent to version '>0.0.0-0'. If [version] is set, this is ignored.
:param Optional[pulumi.Input[bool]] prov: Fetch the provenance file, but don't
perform verification.
:param Optional[pulumi.Input[bool]] untar: If set to false, will leave the
chart as a tarball after downloading.
:param Optional[pulumi.Input[bool]] verify: Verify the package against its signature.
"""
self.version = version
self.ca_file = ca_file
self.cert_file = cert_file
self.key_file = key_file
self.destination = destination
self.keyring = keyring
self.password = password
self.repo = repo
self.untar_dir = untar_dir
self.username = username
self.home = home
self.devel = devel
self.prov = prov
self.untar = untar
self.verify = verify
class BaseChartOpts:
"""
BaseChartOpts is a bag of common configuration options for a Helm chart.
"""
namespace: Optional[pulumi.Input[str]]
"""
Optional namespace to install chart resources into.
"""
values: Optional[pulumi.Inputs]
"""
Optional overrides for chart values.
"""
transformations: Optional[Sequence[Callable[[Any, pulumi.ResourceOptions], None]]]
"""
Optional list of transformations to apply to resources that will be created by this chart prior to
creation. Allows customization of the chart behaviour without directly modifying the chart itself.
"""
resource_prefix: Optional[str]
"""
Optional prefix for the auto-generated resource names.
Example: A resource created with resource_prefix="foo" would produce a resource named "foo-resourceName".
"""
api_versions: Optional[Sequence[pulumi.Input[str]]]
"""
Optional kubernetes api versions used for Capabilities.APIVersions.
"""
include_test_hook_resources: Optional[pulumi.Input[bool]]
"""
By default, Helm resources with the 'test', 'test-success', and 'test-failure' hooks are not installed. Set
this flag to true to include these resources.
"""
skip_crd_rendering: Optional[pulumi.Input[bool]]
"""
By default, CRDs are rendered along with Helm chart templates. Setting this to true will skip CRD rendering.
"""
def __init__(self,
namespace: Optional[pulumi.Input[str]] = None,
values: Optional[pulumi.Inputs] = None,
transformations: Optional[Sequence[Callable[[Any, pulumi.ResourceOptions], None]]] = None,
resource_prefix: Optional[str] = None,
api_versions: Optional[Sequence[pulumi.Input[str]]] = None,
include_test_hook_resources: Optional[pulumi.Input[bool]] = None,
skip_crd_rendering: Optional[pulumi.Input[bool]] = None):
"""
:param Optional[pulumi.Input[str]] namespace: Optional namespace to install chart resources into.
:param Optional[pulumi.Inputs] values: Optional overrides for chart values.
:param Optional[Sequence[Callable[[Any, pulumi.ResourceOptions], None]]] transformations: Optional list
of transformations to apply to resources that will be created by this chart prior to creation.
Allows customization of the chart behaviour without directly modifying the chart itself.
:param Optional[str] resource_prefix: An optional prefix for the auto-generated resource names.
Example: A resource created with resource_prefix="foo" would produce a resource named "foo-resourceName".
:param Optional[Sequence[pulumi.Input[str]]] api_versions: Optional kubernetes api versions used for
Capabilities.APIVersions.
:param Optional[pulumi.Input[bool]] include_test_hook_resources: By default, Helm resources with the 'test',
'test-success', and 'test-failure' hooks are not installed. Set this flag to true to include these
resources.
:param Optional[pulumi.Input[bool]] skip_crd_rendering: By default, CRDs are rendered along with Helm chart
templates. Setting this to true will skip CRD rendering.
"""
self.namespace = namespace
self.include_test_hook_resources = include_test_hook_resources
self.skip_crd_rendering = skip_crd_rendering
self.values = values
self.transformations = transformations
self.resource_prefix = resource_prefix
self.api_versions = api_versions
def to_json(self):
return pulumi.Output.from_input(self.__dict__).apply(
lambda x: json.dumps(x, default=lambda o: {k: v for (k, v) in o.__dict__.items() if v is not None}))
class ChartOpts(BaseChartOpts):
"""
ChartOpts is a bag of configuration options for a remote Helm chart.
"""
chart: pulumi.Input[str]
"""
The name of the chart to deploy. If `repo` is provided, this chart name will be prefixed by the repo name.
Example: repo: "stable", chart: "nginx-ingress" -> "stable/nginx-ingress"
Example: chart: "stable/nginx-ingress" -> "stable/nginx-ingress"
"""
repo: Optional[pulumi.Input[str]]
"""
The repository name of the chart to deploy.
Example: "stable"
"""
version: Optional[pulumi.Input[str]]
"""
The version of the chart to deploy. If not provided, the latest version will be deployed.
"""
fetch_opts: Optional[pulumi.Input[FetchOpts]]
"""
Additional options to customize the fetching of the Helm chart.
"""
def __init__(self,
chart: pulumi.Input[str],
namespace: Optional[pulumi.Input[str]] = None,
values: Optional[pulumi.Inputs] = None,
transformations: Optional[Sequence[Callable[[Any, pulumi.ResourceOptions], None]]] = None,
resource_prefix: Optional[str] = None,
repo: Optional[pulumi.Input[str]] = None,
version: Optional[pulumi.Input[str]] = None,
fetch_opts: Optional[pulumi.Input[FetchOpts]] = None,
api_versions: Optional[Sequence[pulumi.Input[str]]] = None,
include_test_hook_resources: Optional[pulumi.Input[bool]] = None,
skip_crd_install: Optional[pulumi.Input[bool]] = None):
"""
:param pulumi.Input[str] chart: The name of the chart to deploy. If `repo` is provided, this chart name
will be prefixed by the repo name.
Example: repo: "stable", chart: "nginx-ingress" -> "stable/nginx-ingress"
Example: chart: "stable/nginx-ingress" -> "stable/nginx-ingress"
:param Optional[pulumi.Input[str]] namespace: Optional namespace to install chart resources into.
:param Optional[pulumi.Inputs] values: Optional overrides for chart values.
:param Optional[Sequence[Callable[[Any, pulumi.ResourceOptions], None]]] transformations: Optional list of
transformations to apply to resources that will be created by this chart prior to creation.
Allows customization of the chart behaviour without directly modifying the chart itself.
:param Optional[str] resource_prefix: An optional prefix for the auto-generated resource names.
Example: A resource created with resource_prefix="foo" would produce a resource named "foo-resourceName".
:param Optional[pulumi.Input[str]] repo: The repository name of the chart to deploy.
Example: "stable"
:param Optional[pulumi.Input[str]] version: The version of the chart to deploy. If not provided,
the latest version will be deployed.
:param Optional[pulumi.Input[FetchOpts]] fetch_opts: Additional options to customize the
fetching of the Helm chart.
:param Optional[Sequence[pulumi.Input[str]]] api_versions: Optional kubernetes api versions used for
Capabilities.APIVersions.
:param Optional[pulumi.Input[bool]] include_test_hook_resources: By default, Helm resources with the 'test',
'test-success', and 'test-failure' hooks are not installed. Set this flag to true to include these
resources.
:param Optional[pulumi.Input[bool]] skip_crd_rendering: By default, CRDs are rendered along with Helm chart
templates. Setting this to true will skip CRD rendering.
"""
super(ChartOpts, self).__init__(namespace, values, transformations, resource_prefix, api_versions,
include_test_hook_resources, skip_crd_install)
self.chart = chart
self.repo = repo
self.version = version
self.fetch_opts = fetch_opts
class LocalChartOpts(BaseChartOpts):
"""
LocalChartOpts is a bag of configuration options for a local Helm chart.
"""
path: pulumi.Input[str]
"""
The path to the chart directory which contains the `Chart.yaml` file.
"""
def __init__(self,
path: pulumi.Input[str],
namespace: Optional[pulumi.Input[str]] = None,
values: Optional[pulumi.Inputs] = None,
transformations: Optional[Sequence[Callable[[Any, pulumi.ResourceOptions], None]]] = None,
resource_prefix: Optional[str] = None,
api_versions: Optional[Sequence[pulumi.Input[str]]] = None,
include_test_hook_resources: Optional[pulumi.Input[bool]] = None,
skip_crd_rendering: Optional[pulumi.Input[bool]] = None):
"""
:param pulumi.Input[str] path: The path to the chart directory which contains the
`Chart.yaml` file.
:param Optional[pulumi.Input[str]] namespace: Optional namespace to install chart resources into.
:param Optional[pulumi.Inputs] values: Optional overrides for chart values.
:param Optional[Sequence[Callable[[Any, pulumi.ResourceOptions], None]]] transformations: Optional list of
transformations to apply to resources that will be created by this chart prior to creation.
Allows customization of the chart behaviour without directly modifying the chart itself.
:param Optional[str] resource_prefix: An optional prefix for the auto-generated resource names.
Example: A resource created with resource_prefix="foo" would produce a resource named "foo-resourceName".
:param Optional[Sequence[pulumi.Input[str]]] api_versions: Optional kubernetes api versions used for
Capabilities.APIVersions.
:param Optional[pulumi.Input[bool]] include_test_hook_resources: By default, Helm resources with the 'test',
'test-success', and 'test-failure' hooks are not installed. Set this flag to true to include these
resources.
:param Optional[pulumi.Input[bool]] skip_crd_rendering: By default, CRDs are rendered along with Helm chart
templates. Setting this to true will skip CRD rendering.
"""
super(LocalChartOpts, self).__init__(namespace, values, transformations, resource_prefix, api_versions,
include_test_hook_resources, skip_crd_rendering)
self.path = path
def _parse_chart(all_config: Tuple[Union[ChartOpts, LocalChartOpts], pulumi.ResourceOptions]) -> pulumi.Output:
config, opts = all_config
json_opts = config.to_json()
# Rather than using the default provider for the following invoke call, use the version specified
# in package.json.
invoke_opts = pulumi.InvokeOptions(version=_utilities.get_version())
objects = json_opts.apply(lambda x: pulumi.runtime.invoke('kubernetes:helm:template',
{'jsonOpts': x}, invoke_opts).value['result'])
return objects.apply(lambda x: _parse_yaml_document(x, opts, config.transformations))