/
configGroup.go
296 lines (282 loc) · 9 KB
/
configGroup.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
// 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 yaml
import (
"fmt"
"github.com/pkg/errors"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
// ConfigGroup creates a set of Kubernetes resources from Kubernetes YAML. The YAML text may be supplied using
// any of the following formats:
//
// 1. Using a filename or a list of filenames:
// 2. Using a file pattern or a list of file patterns:
// 3. Using a literal string containing YAML, or a list of such strings:
// 4. Any combination of files, patterns, or YAML strings:
//
// ## Example Usage
// ### Local File
//
// ```go
// package main
//
// import (
// "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
// "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
// )
//
// func main() {
// pulumi.Run(func(ctx *pulumi.Context) error {
// _, err := yaml.NewConfigGroup(ctx, "example",
// &yaml.ConfigGroupArgs{
// Files: []string{"foo.yaml"},
// },
// )
// if err != nil {
// return err
// }
//
// return nil
// })
// }
// ```
// ### Multiple Local Files
//
// ```go
// package main
//
// import (
// "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
// "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
// )
//
// func main() {
// pulumi.Run(func(ctx *pulumi.Context) error {
// _, err := yaml.NewConfigGroup(ctx, "example",
// &yaml.ConfigGroupArgs{
// Files: []string{"foo.yaml", "bar.yaml"},
// },
// )
// if err != nil {
// return err
// }
//
// return nil
// })
// }
// ```
// ### Local File Pattern
//
// ```go
// package main
//
// import (
// "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
// "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
// )
//
// func main() {
// pulumi.Run(func(ctx *pulumi.Context) error {
// _, err := yaml.NewConfigGroup(ctx, "example",
// &yaml.ConfigGroupArgs{
// Files: []string{"yaml/*.yaml"},
// },
// )
// if err != nil {
// return err
// }
//
// return nil
// })
// }
// ```
// ### Multiple Local File Patterns
//
// ```go
// package main
//
// import (
// "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
// "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
// )
//
// func main() {
// pulumi.Run(func(ctx *pulumi.Context) error {
// _, err := yaml.NewConfigGroup(ctx, "example",
// &yaml.ConfigGroupArgs{
// Files: []string{"yaml/*.yaml", "bar/*.yaml"},
// },
// )
// if err != nil {
// return err
// }
//
// return nil
// })
// }
// ```
// ### Literal YAML String
//
// ```go
// package main
//
// import (
// "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
// "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
// )
//
// func main() {
// pulumi.Run(func(ctx *pulumi.Context) error {
// _, err := yaml.NewConfigGroup(ctx, "example",
// &yaml.ConfigGroupArgs{
// YAML: []string{
// `
// apiVersion: v1
// kind: Namespace
// metadata:
// name: foo
// `,
// },
// })
// if err != nil {
// return err
// }
//
// return nil
// })
// }
// ```
// ### YAML with Transformations
//
// ```go
// package main
//
// import (
// "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
// "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
// )
//
// func main() {
// pulumi.Run(func(ctx *pulumi.Context) error {
// _, err := yaml.NewConfigGroup(ctx, "example",
// &yaml.ConfigGroupArgs{
// Files: []string{"foo.yaml"},
// 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"] = "core/v1"
// state["kind"] = "List"
// }
// },
// },
// },
// )
// if err != nil {
// return err
// }
//
// return nil
// })
// }
// ```
type ConfigGroup struct {
pulumi.ResourceState
Resources map[string]pulumi.Resource
}
// The set of arguments for constructing a ConfigGroup resource.
type ConfigGroupArgs struct {
// Files is a set of paths, globs, or URLs that uniquely identify files.
Files []string
// YAML is list of strings containing Kubernetes resource definitions in YAML.
YAML []string
// Objs is a collection of object maps representing Kubernetes resources.
Objs []map[string]interface{}
// Transformations is an optional list of transformations to apply to Kubernetes resource definitions
// before registering with the engine.
Transformations []Transformation
// ResourcePrefix is an optional prefix for the auto-generated resource names. For example, a resource named `bar`
// created with resource prefix of `"foo"` would produce a resource named `"foo-bar"`.
ResourcePrefix string
// Skip await logic for all resources in this YAML. Resources will be marked ready as soon as they are created.
// Warning: This option should not be used if you have resources depending on Outputs from the YAML.
SkipAwait bool
}
// NewConfigGroup registers a new resource with the given unique name, arguments, and options.
func NewConfigGroup(ctx *pulumi.Context,
name string, args *ConfigGroupArgs, opts ...pulumi.ResourceOption) (*ConfigGroup, error) {
// Register the resulting resource state.
configGroup := &ConfigGroup{
Resources: map[string]pulumi.Resource{},
}
err := ctx.RegisterComponentResource("kubernetes:yaml:ConfigGroup", name, configGroup, opts...)
if err != nil {
return nil, err
}
// Now provision all child resources by parsing the YAML files.
if args != nil {
// Honor the resource name prefix if specified.
if args.ResourcePrefix != "" {
name = args.ResourcePrefix + "-" + name
}
// Parse and decode the YAML files.
parseOpts := append(opts, pulumi.Parent(configGroup))
rs, err := parseDecodeYamlFiles(ctx, args, true, parseOpts...)
if err != nil {
return nil, err
}
configGroup.Resources = rs
}
// Finally, register all of the resources found.
err = ctx.RegisterResourceOutputs(configGroup, pulumi.Map{})
if err != nil {
return nil, errors.Wrapf(err, "registering child resources")
}
return configGroup, 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 (cf *ConfigGroup) GetResource(gvk, name, namespace string) pulumi.Resource {
id := name
if len(namespace) > 0 && namespace != "default" {
id = fmt.Sprintf("%s/%s", namespace, name)
}
key := fmt.Sprintf("%s::%s", gvk, id)
return cf.Resources[key]
}