/
directory.go
193 lines (179 loc) · 6.58 KB
/
directory.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
// 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 kustomize
import (
"github.com/pkg/errors"
"github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/yaml"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
// Directory is a component representing a collection of resources described by a kustomize directory (kustomization).
//
// ## Example Usage
// ### Local Kustomize Directory
//
// ```go
// package main
//
// import (
// "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/kustomize"
// "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
// )
//
// func main() {
// pulumi.Run(func(ctx *pulumi.Context) error {
// _, err := kustomize.NewDirectory(ctx, "helloWorldLocal",
// kustomize.DirectoryArgs{
// Directory: pulumi.String("./helloWorld"),
// },
// )
// if err != nil {
// return err
// }
//
// return nil
// })
// }
// ```
// ### Kustomize Directory from a Git Repo
//
// ```go
// package main
//
// import (
// "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/kustomize"
// "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
// )
//
// func main() {
// pulumi.Run(func(ctx *pulumi.Context) error {
// _, err := kustomize.NewDirectory(ctx, "helloWorldRemote",
// kustomize.DirectoryArgs{
// Directory: pulumi.String("https://github.com/kubernetes-sigs/kustomize/tree/v3.3.1/examples/helloWorld"),
// },
// )
// if err != nil {
// return err
// }
//
// return nil
// })
// }
// ```
// ### Kustomize Directory with Transformations
//
// ```go
// package main
//
// import (
// "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/kustomize"
// "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 := kustomize.NewDirectory(ctx, "helloWorldRemote",
// kustomize.DirectoryArgs{
// Directory: pulumi.String("https://github.com/kubernetes-sigs/kustomize/tree/v3.3.1/examples/helloWorld"),
// 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 Directory struct {
pulumi.ResourceState
Resources pulumi.Output
}
// NewDirectory registers a new resource with the given unique name, arguments, and options.
func NewDirectory(ctx *pulumi.Context,
name string, args DirectoryArgs, opts ...pulumi.ResourceOption) (*Directory, error) {
// Register the resulting resource state.
chart := &Directory{}
err := ctx.RegisterComponentResource("kubernetes:kustomize:Directory", name, chart, opts...)
if err != nil {
return nil, err
}
// Honor the resource name prefix if specified.
if args.ResourcePrefix != "" {
name = args.ResourcePrefix + "-" + name
}
resources := args.ToDirectoryArgsOutput().ApplyT(func(args directoryArgs) (map[string]pulumi.Resource, error) {
return parseDirectory(ctx, name, args, pulumi.Parent(chart))
})
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")
}
return chart, nil
}
func parseDirectory(ctx *pulumi.Context, name string, args directoryArgs, opts ...pulumi.ResourceOption,
) (map[string]pulumi.Resource, error) {
invokeArgs := struct {
Directory string `pulumi:"directory"`
}{Directory: args.Directory}
var ret struct {
Result []map[string]interface{} `pulumi:"result"`
}
if err := ctx.Invoke("kubernetes:kustomize:directory", &invokeArgs, &ret); err != nil {
return nil, errors.Wrap(err, "kustomize invoke failed")
}
resources, err := yaml.ParseYamlObjects(ctx, ret.Result, args.Transformations, args.ResourcePrefix, opts...)
if err != nil {
return nil, err
}
return resources, nil
}