/
chart.go
368 lines (340 loc) · 11.1 KB
/
chart.go
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
// Copyright 2016-2021, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// *** WARNING: this file was generated by pulumigen. ***
// *** Do not edit by hand unless you're certain you know what you are doing! ***
package helm
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/yaml"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
// 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
//
// ```go
// package main
//
// import (
//
// "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/helm/v3"
// "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
//
// )
//
// func main() {
// pulumi.Run(func(ctx *pulumi.Context) error {
// _, err := helm.NewChart(ctx, "nginx-ingress", helm.ChartArgs{
// Path: pulumi.String("./nginx-ingress"),
// })
// if err != nil {
// return err
// }
//
// return nil
// })
// }
//
// ```
// ### Remote Chart
//
// ```go
// package main
//
// import (
//
// "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/helm/v3"
// "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
//
// )
//
// func main() {
// pulumi.Run(func(ctx *pulumi.Context) error {
// _, err := helm.NewChart(ctx, "nginx-ingress", helm.ChartArgs{
// Chart: pulumi.String("nginx-ingress"),
// Version: pulumi.String("1.24.4"),
// FetchArgs: helm.FetchArgs{
// Repo: pulumi.String("https://charts.helm.sh/stable"),
// },
// })
// if err != nil {
// return err
// }
//
// return nil
// })
// }
//
// ```
// ### Set Chart values
//
// ```go
// package main
//
// import (
//
// "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/helm/v3"
// "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
//
// )
//
// func main() {
// pulumi.Run(func(ctx *pulumi.Context) error {
// _, err := helm.NewChart(ctx, "nginx-ingress", helm.ChartArgs{
// Chart: pulumi.String("nginx-ingress"),
// Version: pulumi.String("1.24.4"),
// FetchArgs: helm.FetchArgs{
// Repo: pulumi.String("https://charts.helm.sh/stable"),
// },
// Values: pulumi.Map{
// "controller": pulumi.Map{
// "metrics": pulumi.Map{
// "enabled": pulumi.Bool(true),
// },
// },
// },
// })
// if err != nil {
// return err
// }
//
// return nil
// })
// }
//
// ```
// ### Deploy Chart into Namespace
//
// ```go
// package main
//
// import (
//
// "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/helm/v3"
// "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
//
// )
//
// func main() {
// pulumi.Run(func(ctx *pulumi.Context) error {
// _, err := helm.NewChart(ctx, "nginx-ingress", helm.ChartArgs{
// Chart: pulumi.String("nginx-ingress"),
// Version: pulumi.String("1.24.4"),
// Namespace: pulumi.String("test-namespace"),
// FetchArgs: helm.FetchArgs{
// Repo: pulumi.String("https://charts.helm.sh/stable"),
// },
// })
// if err != nil {
// return err
// }
//
// return nil
// })
// }
//
// ```
// ### Chart with Transformations
//
// ```go
// package main
//
// import (
//
// "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/helm/v3"
// "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/yaml"
// "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
//
// )
//
// func main() {
// pulumi.Run(func(ctx *pulumi.Context) error {
// _, err := helm.NewChart(ctx, "nginx-ingress", helm.ChartArgs{
// Chart: pulumi.String("nginx-ingress"),
// Version: pulumi.String("1.24.4"),
// FetchArgs: helm.FetchArgs{
// Repo: pulumi.String("https://charts.helm.sh/stable"),
// },
// Transformations: []yaml.Transformation{
// // Make every service private to the cluster, i.e., turn all services into ClusterIP
// // instead of LoadBalancer.
// func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
// if state["kind"] == "Service" {
// spec := state["spec"].(map[string]interface{})
// spec["type"] = "ClusterIP"
// }
// },
//
// // Set a resource alias for a previous name.
// func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
// if state["kind"] == "Deployment" {
// aliases := pulumi.Aliases([]pulumi.Alias{
// {
// Name: pulumi.String("oldName"),
// },
// })
// opts = append(opts, aliases)
// }
// },
//
// // Omit a resource from the Chart by transforming the specified resource definition
// // to an empty List.
// func(state map[string]interface{}, opts ...pulumi.ResourceOption) {
// name := state["metadata"].(map[string]interface{})["name"]
// if state["kind"] == "Pod" && name == "test" {
// state["apiVersion"] = "v1"
// state["kind"] = "List"
// }
// },
// },
// })
// if err != nil {
// return err
// }
//
// return nil
// })
// }
//
// ```
type Chart struct {
pulumi.ResourceState
Ready pulumi.ResourceArrayOutput
Resources pulumi.Output
}
// NewChart registers a new resource with the given unique name, arguments, and options.
func NewChart(ctx *pulumi.Context,
name string, args ChartArgs, opts ...pulumi.ResourceOption) (*Chart, error) {
// Register the resulting resource state.
chart := &Chart{}
aliases := pulumi.Aliases([]pulumi.Alias{
{
Type: pulumi.String("kubernetes:helm.sh/v2:Chart"),
},
})
opts = append(opts, aliases)
err := ctx.RegisterComponentResource("kubernetes:helm.sh/v3:Chart", name, chart, opts...)
if err != nil {
return nil, err
}
releaseName := name
if args.ResourcePrefix != "" {
releaseName = args.ResourcePrefix + "-" + name
}
parseOpts, err := yaml.GetChildOptions(chart, opts)
if err != nil {
return nil, err
}
resources := args.ToChartArgsOutput().ApplyT(func(args chartArgs) (map[string]pulumi.Resource, error) {
return parseChart(ctx, releaseName, args, parseOpts...)
})
chart.Resources = resources
// Finally, register all of the resources found.
// Note: Go requires that we "pull" on our futures in order to get them scheduled for execution. Here, we use
// the engine's RegisterResourceOutputs to wait for the resolution of all resources that this Helm chart created.
err = ctx.RegisterResourceOutputs(chart, pulumi.Map{"resources": resources})
if err != nil {
return nil, errors.Wrap(err, "registering child resources")
}
chart.Ready = resources.ApplyT(func(x interface{}) []pulumi.Resource {
resources := x.(map[string]pulumi.Resource)
var outputs []pulumi.Resource
for _, r := range resources {
outputs = append(outputs, r)
}
return outputs
}).(pulumi.ResourceArrayOutput)
return chart, nil
}
func parseChart(ctx *pulumi.Context, releaseName string, args chartArgs, opts ...pulumi.ResourceOption,
) (map[string]pulumi.Resource, error) {
type jsonOptsArgs struct {
chartArgs
ReleaseName string `json:"release_name,omitempty"`
}
jsonOpts := jsonOptsArgs{
chartArgs: args,
ReleaseName: releaseName,
}
b, err := json.Marshal(jsonOpts)
if err != nil {
return nil, err
}
invokeOpts, err := yaml.GetInvokeOptions(opts)
if err != nil {
return nil, err
}
objs, err := helmTemplate(ctx, string(b), invokeOpts...)
if err != nil {
return nil, err
}
transformations := args.Transformations
if args.SkipAwait {
transformations = yaml.AddSkipAwaitTransformation(transformations)
}
resources, err := yaml.ParseYamlObjects(ctx, objs, transformations, args.ResourcePrefix, opts...)
if err != nil {
return nil, err
}
return resources, nil
}
// helmTemplate invokes the function to fetch and template a Helm Chart and decompose it into object structures.
func helmTemplate(
ctx *pulumi.Context, jsonOpts string, opts ...pulumi.InvokeOption,
) ([]map[string]interface{}, error) {
args := struct {
JsonOpts string `pulumi:"jsonOpts"`
}{JsonOpts: jsonOpts}
var ret struct {
Result []map[string]interface{} `pulumi:"result"`
}
if err := ctx.Invoke("kubernetes:helm:template", &args, &ret, opts...); err != nil {
return nil, errors.Wrap(err, "failed to invoke helm template")
}
return ret.Result, nil
}
// GetResource returns a resource defined by a built-in Kubernetes group/version/kind, name and namespace.
// For example, GetResource("v1/Pod", "foo", "") would return a Pod called "foo" from the "default" namespace.
func (c *Chart) GetResource(gvk, name, namespace string) pulumi.AnyOutput {
id := name
if len(namespace) > 0 && namespace != "default" {
id = fmt.Sprintf("%s/%s", namespace, name)
}
key := fmt.Sprintf("%s::%s", gvk, id)
return c.Resources.ApplyT(func(x interface{}) interface{} {
resources := x.(map[string]pulumi.Resource)
if r, ok := resources[key]; ok {
return r
}
// Some resources in the default namespace do not adhere to the name only pattern, so we need to fallback
// to searching for the resource by namespace and name. This occurs when the Helm template manifest explicitly
// sets the namespace field on the resource.
if namespace == "default" || namespace == "" {
return resources[fmt.Sprintf("%s::default/%s", gvk, name)]
}
return nil
}).(pulumi.AnyOutput)
}