-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtfk8s.go
283 lines (241 loc) · 6.98 KB
/
tfk8s.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
package main
import (
"bytes"
"fmt"
"io"
"os"
"regexp"
"runtime/debug"
"strings"
flag "github.com/spf13/pflag"
cty "github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
yaml "sigs.k8s.io/yaml"
"github.com/jrhouston/tfk8s/contrib/hashicorp/terraform"
)
// toolVersion is the version that gets printed when you run --version
var toolVersion string
// resourceType is the type of Terraform resource
var resourceType = "kubernetes_manifest"
// ignoreMetadata is the list of metadata fields to strip
// when --strip is supplied
var ignoreMetadata = []string{
"creationTimestamp",
"resourceVersion",
"selfLink",
"uid",
"managedFields",
"finalizers",
"generation",
}
// ignoreAnnotations is the list of annotations to strip
// when --strip is supplied
var ignoreAnnotations = []string{
"kubectl.kubernetes.io/last-applied-configuration",
}
// stripServerSideFields removes fields that have been added on the
// server side after the resource was created such as the status field
func stripServerSideFields(doc cty.Value) cty.Value {
m := doc.AsValueMap()
// strip server-side metadata
metadata := m["metadata"].AsValueMap()
for _, f := range ignoreMetadata {
delete(metadata, f)
}
if v, ok := metadata["annotations"]; ok {
annotations := v.AsValueMap()
for _, a := range ignoreAnnotations {
delete(annotations, a)
}
if len(annotations) == 0 {
delete(metadata, "annotations")
} else {
metadata["annotations"] = cty.ObjectVal(annotations)
}
}
if ns, ok := metadata["namespace"]; ok && ns.AsString() == "default" {
delete(metadata, "namespace")
}
m["metadata"] = cty.ObjectVal(metadata)
// strip finalizer from spec
if v, ok := m["spec"]; ok {
mm := v.AsValueMap()
delete(mm, "finalizers")
m["spec"] = cty.ObjectVal(mm)
}
// strip status field
delete(m, "status")
return cty.ObjectVal(m)
}
// snakify converts "a-String LIKE this" to "a_string_like_this"
func snakify(s string) string {
re := regexp.MustCompile(`\W`)
return strings.ToLower(re.ReplaceAllString(s, "_"))
}
// escape incidences of ${} with $${} to prevent Terraform trying to interpolate them
func escapeShellVars(s string) string {
r := regexp.MustCompile(`(\${.*?)`)
return r.ReplaceAllString(s, `$$$1`)
}
// yamlToHCL converts a single YAML document Terraform HCL
func yamlToHCL(
doc cty.Value, providerAlias string,
stripServerSide bool, mapOnly bool, stripKeyQuotes bool) (string, error) {
m := doc.AsValueMap()
docs := []cty.Value{doc}
if strings.HasSuffix(m["kind"].AsString(), "List") {
docs = m["items"].AsValueSlice()
}
hcl := ""
for i, doc := range docs {
mm := doc.AsValueMap()
kind := mm["kind"].AsString()
metadata := mm["metadata"].AsValueMap()
var namespace string
if v, ok := metadata["namespace"]; ok {
namespace = v.AsString()
}
var name string
if n, ok := metadata["name"]; ok {
name = n.AsString()
} else if n, ok := metadata["generateName"]; ok {
name = n.AsString()
if name[len(name)-1] == '-' {
name = name[:len(name)-1]
}
}
resourceName := kind
if namespace != "" && namespace != "default" {
resourceName = resourceName + "_" + namespace
}
resourceName = resourceName + "_" + name
resourceName = snakify(resourceName)
if stripServerSide {
doc = stripServerSideFields(doc)
}
s := terraform.FormatValue(doc, 0, stripKeyQuotes)
s = escapeShellVars(s)
if mapOnly {
hcl += fmt.Sprintf("%v\n", s)
} else {
hcl += fmt.Sprintf("resource %q %q {\n", resourceType, resourceName)
if providerAlias != "" {
hcl += fmt.Sprintf(" provider = %v\n\n", providerAlias)
}
hcl += fmt.Sprintf(" manifest = %v\n", strings.ReplaceAll(s, "\n", "\n "))
hcl += "}\n"
}
if i != len(docs)-1 {
hcl += "\n"
}
}
return hcl, nil
}
var yamlSeparator = "\n---"
// YAMLToTerraformResources takes a file containing one or more Kubernetes configs
// and converts it to resources that can be used by the Terraform Kubernetes Provider
//
// FIXME this function has too many arguments now, use functional options instead
func YAMLToTerraformResources(
r io.Reader, providerAlias string, stripServerSide bool,
mapOnly bool, stripKeyQuotes bool) (string, error) {
hcl := ""
buf := bytes.Buffer{}
_, err := buf.ReadFrom(r)
if err != nil {
return "", err
}
count := 0
manifest := buf.String()
docs := strings.Split(manifest, yamlSeparator)
for _, doc := range docs {
if strings.TrimSpace(doc) == "" {
// some manifests have empty documents
continue
}
var b []byte
b, err = yaml.YAMLToJSON([]byte(doc))
if err != nil {
return "", err
}
t, err := ctyjson.ImpliedType(b)
if err != nil {
return "", err
}
doc, err := ctyjson.Unmarshal(b, t)
if err != nil {
return "", err
}
if doc.IsNull() {
// skip empty YAML docs
continue
}
if !doc.Type().IsObjectType() {
return "", fmt.Errorf("the manifest must be a YAML document")
}
formatted, err := yamlToHCL(doc, providerAlias, stripServerSide, mapOnly, stripKeyQuotes)
if err != nil {
return "", fmt.Errorf("error converting YAML to HCL: %s", err)
}
if count > 0 {
hcl += "\n"
}
hcl += formatted
count++
}
return hcl, nil
}
func capturePanic() {
if r := recover(); r != nil {
fmt.Printf(
"panic: %s\n\n%s\n\n"+
"⚠️ Oh no! Looks like your manifest caused tfk8s to crash.\n\n"+
"Please open a GitHub issue and include your manifest YAML with the stack trace above,\n"+
"or ping me on slack and I'll try and fix it!\n\n"+
"GitHub: https://github.com/jrhouston/tfk8s/issues\n"+
"Slack: #terraform-providers on https://kubernetes.slack.com\n\n"+
"- Thanks, @jrhouston\n\n",
r, debug.Stack())
}
}
func main() {
defer capturePanic()
infile := flag.StringP("file", "f", "-", "Input file containing Kubernetes YAML manifests")
outfile := flag.StringP("output", "o", "-", "Output file to write Terraform config")
providerAlias := flag.StringP("provider", "p", "", "Provider alias to populate the `provider` attribute")
stripServerSide := flag.BoolP("strip", "s", false, "Strip out server side fields - use if you are piping from kubectl get")
version := flag.BoolP("version", "V", false, "Show tool version")
mapOnly := flag.BoolP("map-only", "M", false, "Output only an HCL map structure")
stripKeyQuotes := flag.BoolP("strip-key-quotes", "Q", false, "Strip out quotes from HCL map keys unless they are required.")
flag.Parse()
if *version {
fmt.Println(toolVersion)
os.Exit(0)
}
var file *os.File
if *infile == "-" {
file = os.Stdin
} else {
var err error
file, err = os.Open(*infile)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\r\n", err.Error())
os.Exit(1)
}
}
hcl, err := YAMLToTerraformResources(
file, *providerAlias, *stripServerSide, *mapOnly, *stripKeyQuotes)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\r\n", err.Error())
os.Exit(1)
}
if *outfile == "-" {
fmt.Print(hcl)
} else {
err := os.WriteFile(*outfile, []byte(hcl), 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\r\n", err.Error())
os.Exit(1)
}
}
}