/
configFile.go
180 lines (166 loc) · 6.02 KB
/
configFile.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
// Copyright 2016-2020, 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"
)
// ConfigFile creates a set of Kubernetes resources from a Kubernetes YAML file.
//
// ## 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.NewConfigFile(ctx, "example",
// &yaml.ConfigFileArgs{
// File: "foo.yaml",
// },
// )
// 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.NewConfigFile(ctx, "example",
// &yaml.ConfigFileArgs{
// File: "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 ConfigFile struct {
pulumi.ResourceState
Resources map[string]pulumi.Resource
}
// The set of arguments for constructing a ConfigFile resource.
type ConfigFileArgs struct {
// File is a path or URL that uniquely identifies a file.
File string
// 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
}
// NewConfigFile registers a new resource with the given unique name, arguments, and options.
func NewConfigFile(ctx *pulumi.Context,
name string, args *ConfigFileArgs, opts ...pulumi.ResourceOption) (*ConfigFile, error) {
// Register the resulting resource state.
configFile := &ConfigFile{
Resources: map[string]pulumi.Resource{},
}
err := ctx.RegisterComponentResource("kubernetes:yaml:ConfigFile", name, configFile, opts...)
if err != nil {
return nil, err
}
// Now provision all child resources by parsing the YAML file.
if args != nil {
// Honor the resource name prefix if specified.
if args.ResourcePrefix != "" {
name = args.ResourcePrefix + "-" + name
}
// Parse and decode the YAML files.
rs, err := parseDecodeYamlFiles(ctx, &ConfigGroupArgs{
Files: []string{args.File},
Transformations: args.Transformations,
ResourcePrefix: args.ResourcePrefix,
}, true, pulumi.Parent(configFile))
if err != nil {
return nil, err
}
configFile.Resources = rs
}
// Finally, register all of the resources found.
err = ctx.RegisterResourceOutputs(configFile, pulumi.Map{})
if err != nil {
return nil, errors.Wrapf(err, "registering child resources")
}
return configFile, 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 *ConfigFile) 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]
}