-
Notifications
You must be signed in to change notification settings - Fork 107
/
hcl_writer.go
633 lines (518 loc) · 17.1 KB
/
hcl_writer.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
package main
import (
"fmt"
"reflect"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/sl1pm4t/k2tf/pkg/k8sutils"
"github.com/sl1pm4t/k2tf/pkg/tfkschema"
"k8s.io/apimachinery/pkg/util/intstr"
"github.com/rs/zerolog"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/mitchellh/reflectwalk"
"github.com/rs/zerolog/log"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
)
// WriteObject converts a Kubernetes runtime.Object to HCL
func WriteObject(obj runtime.Object, dst *hclwrite.Body) (int, error) {
w, err := NewObjectWalker(obj, dst)
if err != nil {
return 0, err
}
reflectwalk.Walk(obj, w)
return w.warnCount, nil
}
// ObjectWalker implements reflectwalk.Walker interfaces
// It's used to "walk" the Kubernetes API Objects structure and generate
// an HCL document based on the values defined.
type ObjectWalker struct {
// The Kubernetes API Object to be walked
RuntimeObject runtime.Object
// The HCL body where HCL blocks will be appended
dst *hclwrite.Body
// Terraform resource type (e.g. kubernetes_pod)
resourceType string
// Terraform resource name (adapted from ObjectMeta name attribute)
resourceName string
// top level HCL
isTopLevel bool
// sub block tracking
currentBlock *hclBlock
// stack of Struct fields
fields []*reflect.StructField
// slices of structs
slices []*reflect.StructField
// sliceField tracks the reflect.StructField for the current slice
sliceField *reflect.StructField
// the stack of the Slice element types that are popped and pushed as we walk through object graph
sliceElemTypes []reflect.Type
// Flag to indicate if our reflectwalk functions can skip further processing of slice elements.
// Slices of primitive values get rendered all at once when we enter the Slice so they don't need
// further processing for each element.
ignoreSliceElems bool
warnCount int
}
// NewObjectWalker returns a new ObjectWalker object
// dst is the hclwrite.Body where HCL blocks will be appended.
func NewObjectWalker(obj runtime.Object, dst *hclwrite.Body) (*ObjectWalker, error) {
if obj == nil {
return nil, fmt.Errorf("obj cannot be nil")
}
w := &ObjectWalker{
RuntimeObject: obj,
isTopLevel: true,
dst: dst,
}
return w, nil
}
func (w *ObjectWalker) setCurrentSlice(f *reflect.StructField) {
if f != nil {
w.debugf("setting setCurrentSlice to %s", f.Name)
w.sliceField = f
}
}
func (w *ObjectWalker) currentSlice() *reflect.StructField {
if len(w.slices) > 0 {
return w.slices[len(w.slices)-1]
}
return nil
}
func (w *ObjectWalker) fieldPop() *reflect.StructField {
result := w.fields[len(w.fields)-1]
w.fields = w.fields[:len(w.fields)-1]
w.debugf("fieldPop %s", result.Name)
return result
}
func (w *ObjectWalker) fieldPush(v *reflect.StructField) {
w.fields = append(w.fields, v)
w.debugf("fieldPush %s", v.Name)
}
func (w *ObjectWalker) field() *reflect.StructField {
if len(w.fields) > 0 {
f := w.fields[len(w.fields)-1]
return f
}
return nil
}
func (w *ObjectWalker) slicePop() *reflect.StructField {
result := w.slices[len(w.slices)-1]
w.slices = w.slices[:len(w.slices)-1]
w.debugf("slicePop %s", result.Name)
w.setCurrentSlice(result)
return result
}
func (w *ObjectWalker) slicePush(v *reflect.StructField) {
w.slices = append(w.slices, v)
w.debugf("slicePush %s", v.Name)
w.setCurrentSlice(v)
}
func (w *ObjectWalker) sliceType() reflect.Type {
var result reflect.Type
currSlice := w.currentSlice()
if currSlice != nil {
result = currSlice.Type
w.debugf("sliceType %s", result.Name())
} else {
result = reflect.TypeOf(nil)
w.debug("sliceType nil")
}
return result
}
func (w *ObjectWalker) sliceElemTypePush(ty reflect.Type) {
w.sliceElemTypes = append(w.sliceElemTypes, ty)
w.debugf("sliceElemTypePush %s", ty.Name())
}
func (w *ObjectWalker) sliceElemTypePop() reflect.Type {
result := w.sliceElemTypes[len(w.sliceElemTypes)-1]
w.sliceElemTypes = w.sliceElemTypes[:len(w.sliceElemTypes)-1]
w.debugf("sliceElemTypePop %s", result.Name())
return result
}
func (w *ObjectWalker) sliceElemType() reflect.Type {
var result reflect.Type
if len(w.sliceElemTypes) > 0 {
result = w.sliceElemTypes[len(w.sliceElemTypes)-1]
} else {
result = reflect.TypeOf(struct{}{})
}
w.debugf("sliceElemType %s", result.Name())
return result
}
// openBlock opens a new HCL resource block or sub-block
// It creates a hclBlock object so we can track hierarchy of blocks
// within the resource tree
func (w *ObjectWalker) openBlock(name, fieldName string, hcl *hclwrite.Block) *hclBlock {
w.debugf("opening hclBlock for field: %s", name)
b := &hclBlock{
name: name,
fieldName: fieldName,
parent: w.currentBlock,
hcl: hcl,
}
w.currentBlock = b
return b
}
// closeBlock writes the generated HCL to the hclwriter
func (w *ObjectWalker) closeBlock() *hclBlock {
w.debugf("closing hclBlock: %s", w.currentBlock.name)
parent := w.currentBlock.parent
current := w.currentBlock
// TODO: move append logic to hcl_block to be consistent
if parent == nil {
// we are closing the top level block, write directly to HCL File
w.dst.AppendBlock(current.hcl)
} else {
if current.hasValue || tfkschema.IncludedOnZero(w.currentBlock.fieldName) || current.isRequired() {
if !includeUnsupported && current.unsupported {
// don't append this block or child blocks
w.warn().
Str("field", current.FullFieldName()).
Msgf("excluding attribute [%s] not found in Terraform schema", current.FullSchemaName())
} else {
// communicate back up the tree that we found a non-zero value
parent.hasValue = true
if current.isMap {
if len(current.hclMap) > 0 {
parent.SetAttributeValue(current.name, cty.MapVal(current.hclMap))
}
} else if !current.inlined {
parent.AppendBlock(current.hcl)
}
}
}
w.currentBlock = parent
}
return w.currentBlock
}
// Enter is called by reflectwalk.Walk each time we enter a level
func (w *ObjectWalker) Enter(l reflectwalk.Location) error {
w.debug(fmt.Sprint("entering ", l))
return nil
}
// Exit is called by reflectwalk each time it exits from a reflectwalk.Location
func (w *ObjectWalker) Exit(l reflectwalk.Location) error {
switch l {
case reflectwalk.Slice:
if !w.ignoreSliceElems {
w.sliceElemTypePop()
}
w.ignoreSliceElems = false
w.slicePop()
case reflectwalk.Struct:
fallthrough
case reflectwalk.Map:
w.closeBlock()
case reflectwalk.StructField:
w.fieldPop()
}
w.debugf("exiting %s", l)
return nil
}
// Struct is called every time reflectwalk enters a Struct
func (w *ObjectWalker) Struct(v reflect.Value) error {
if !v.CanInterface() {
w.debugf("skipping Struct [field: %s, type: %s] - CanInterface() = false", w.field().Name, v.Type())
return nil
}
ty := reflect.TypeOf(v.Interface())
if w.isTopLevel {
// Create the top level HCL block
// e.g.
// resource "kubernetes_pod" "name" { }
topLevelBlock := hclwrite.NewBlock("resource", []string{w.ResourceType(), w.ResourceName()})
w.openBlock(w.ResourceType(), k8sutils.TypeMeta(w.RuntimeObject).Kind, topLevelBlock)
w.isTopLevel = false
} else {
// this struct will be a sub-block
// create a new HCL block and add to parent
field := w.field()
if w.sliceElemType() == ty || w.sliceType() == ty {
// When iterating over a slice of complex types, each HCL block name is based on the
// StructField metadata of the containing Slice instead of the StructField of each Slice element.
// Update field, so when we create the HCL block below it uses the Slice StructField
field = w.currentSlice()
}
// generate a block name
blockName := tfkschema.ToTerraformSubBlockName(field, w.currentBlock.FullSchemaName())
w.debugf("creating block [%s] for field [%s]", blockName, field.Name)
b := w.openBlock(blockName, field.Name, hclwrite.NewBlock(blockName, nil))
// Skip some Kubernetes complex types that should be treated as primitives.
// Do this after opening the block above because reflectwalk will
// still call Exit for this struct and we need the calls to closeBlock() to marry up
// TODO: figure out a uniform way to handle these cases
switch v.Interface().(type) {
case resource.Quantity:
return reflectwalk.SkipEntry
case intstr.IntOrString:
ios := v.Interface().(intstr.IntOrString)
if ios.IntVal > 0 || ios.StrVal != "" {
b.hasValue = false
b.parent.SetAttributeValue(blockName, w.convertCtyValue(v.Interface()))
b.parent.hasValue = true
}
return reflectwalk.SkipEntry
}
// flag inlined
b.inlined = IsInlineStruct(field)
// check if block is supported by Terraform
b.unsupported = !tfkschema.IsAttributeSupported(b.FullSchemaName())
}
return nil
}
// StructField is called by reflectwalk whenever it enters a field of a struct.
// We ignore Invalid fields, or some API fields we don't need to convert to HCL.
// The rest are added to the StuctField stack so we have access to the
// StructField data in other funcs.
func (w *ObjectWalker) StructField(field reflect.StructField, v reflect.Value) error {
if !v.IsValid() {
w.debug(fmt.Sprint("skipping invalid ", field.Name))
return reflectwalk.SkipEntry
} else if ignoredField(field.Name) {
w.debug(fmt.Sprint("ignoring ", field.Name))
return reflectwalk.SkipEntry
} else {
w.fieldPush(&field)
}
return nil
}
// Primitive is called whenever reflectwalk visits a Primitive value.
// If it's not a zero value, add an Attribute to the current HCL Block.
func (w *ObjectWalker) Primitive(v reflect.Value) error {
if !w.ignoreSliceElems && v.CanAddr() && v.CanInterface() {
w.debug(fmt.Sprintf("Primitive: %s = %v (%T)", w.field().Name, v.Interface(), v.Interface()))
if !IsZero(v) || tfkschema.IncludedOnZero(w.field().Name) {
w.currentBlock.hasValue = true
w.currentBlock.SetAttributeValue(
tfkschema.ToTerraformAttributeName(w.field(), w.currentBlock.FullSchemaName()),
w.convertCtyValue(v.Interface()),
)
}
}
return nil
}
// Map is called everytime reflectwalk enters a Map
// Golang maps are usually output as HCL maps, but sometimes as sub-blocks.
func (w *ObjectWalker) Map(m reflect.Value) error {
blockName := tfkschema.ToTerraformSubBlockName(w.field(), w.currentBlock.FullSchemaName())
// https://github.com/sl1pm4t/k2tf/issues/109
// terraform provider has no field for stringData
// so: handle this case as a special condition
if (w.field() != nil && w.field().Name == "StringData") &&
(w.currentBlock != nil && w.currentBlock.FullSchemaName() == "kubernetes_secret" ||
w.currentBlock != nil && w.currentBlock.FullSchemaName() == "kubernetes_secret_v1") {
blockName = "data"
}
hcl := hclwrite.NewBlock(blockName, nil)
b := w.openBlock(blockName, w.field().Name, hcl)
// If this field is also typed as Map in the Terraform schema, flag the block appropriately.
// This will impact whether the block is rendered as a map or HCL sub-block.
schemaElem := tfkschema.ResourceField(w.currentBlock.FullSchemaName())
if schemaElem != nil && schemaElem.Type == schema.TypeMap {
b.isMap = true
b.hclMap = map[string]cty.Value{}
}
return nil
}
// MapElem is called every time reflectwalk enters a Map element
//
// normalize the element key, and write element value to the HCL block as an attribute value
func (w *ObjectWalker) MapElem(m, k, v reflect.Value) error {
w.debug(fmt.Sprintf("MapElem: %s = %v (%T)", k, v.Interface(), v.Interface()))
if !IsZero(v) {
w.currentBlock.hasValue = true
w.currentBlock.SetAttributeValue(
k.String(),
w.convertCtyValue(v.Interface()),
)
}
return nil
}
/*
Slice implements reflectwalk.SliceWalker interface, and is called each time reflectwalk enters a Slice
Golang slices need to be converted to HCL in one of two ways:
*1 - a simple list of primitive values:
list_name = ["foo", "bar", "baz"]
*2 - a list of complex objects that will be rendered as repeating HCL blocks
container {
name = "blah"
image = "nginx"
}
container {
name = "foo"
image = "sidecar"
}
For the second case, each time we process a SliceElem we need to use the StructField data of the Slice itself, and not the slice elem.
*/
func (w *ObjectWalker) Slice(v reflect.Value) error {
w.slicePush(w.field())
if !v.IsValid() {
w.debug("skipping invalid slice ")
w.ignoreSliceElems = true
} else if IsZero(v) {
w.debug("skipping empty slice ")
w.ignoreSliceElems = true
} else {
// determine type of slice elements
numEntries := v.Len()
var vt reflect.Type
if numEntries > 0 {
w.currentBlock.hasValue = true
vt = v.Index(0).Type()
}
switch {
case vt.Kind() == reflect.Struct:
fallthrough
case vt.Kind() == reflect.Ptr:
// Slice of complex types
w.sliceElemTypePush(vt)
default:
// Slice of primitives
valTy, err := gocty.ImpliedType(v.Interface())
if err != nil {
log.Panic().Interface("cannot encode %T as HCL expression", v.Interface()).Err(err)
}
val, err := gocty.ToCtyValue(v.Interface(), valTy)
if err != nil {
// This should never happen, since we should always be able
// to decode into the implied type.
log.Panic().Interface("failed to encode", v.Interface()).Interface("as %#v", valTy).Err(err)
}
// primitive type
w.currentBlock.hasValue = true
w.currentBlock.hcl.Body().SetAttributeValue(
tfkschema.ToTerraformAttributeName(w.field(), w.currentBlock.FullSchemaName()),
val,
)
// hint to other funcs that we don't need to walk through all Slice Elements because the
// primitive values have already been rendered
w.ignoreSliceElems = true
}
}
return nil
}
// SliceElem implements reflectwalk.SliceWalker interface
func (w *ObjectWalker) SliceElem(i int, v reflect.Value) error {
w.debugf("Elem %d: %T", i, v.Interface())
return nil
}
// convertCtyValue takes an interface and converts to HCL types
func (w *ObjectWalker) convertCtyValue(val interface{}) cty.Value {
w.debugf("processing %s (%T)", w.field().Name, val)
switch val.(type) {
case string:
return cty.StringVal(val.(string))
case bool:
return cty.BoolVal(val.(bool))
case int:
return cty.NumberIntVal(int64(val.(int)))
case int32:
// On volume source blocks, the mode and default_mode attributes are now mandatorily a string representation of an octal value with a leading zero
if w.currentSlice() != nil && w.currentSlice().Name == "Volumes" && (w.field().Name == "DefaultMode" || w.field().Name == "Mode") {
str := "0" + strconv.FormatInt(int64(val.(int32)), 8)
w.debugf("converting %s from decimal int '%d' to octal string '%s'", w.field().Name, val.(int32), str)
return cty.StringVal(str)
}
return cty.NumberIntVal(int64(val.(int32)))
case *int32:
val = *val.(*int32)
return cty.NumberIntVal(int64(val.(int32)))
case int64:
return cty.NumberIntVal(int64(val.(int64)))
case float64:
return cty.NumberFloatVal(float64(val.(float64)))
case map[string]interface{}:
ctyMap := map[string]cty.Value{}
for k, v := range val.(map[string]interface{}) {
ctyMap[k] = w.convertCtyValue(v)
}
return cty.ObjectVal(ctyMap)
case resource.Quantity:
qty := val.(resource.Quantity)
qtyPtr := &qty
return cty.StringVal(qtyPtr.String())
case intstr.IntOrString:
ios := val.(intstr.IntOrString)
iosPtr := &ios
return cty.StringVal(iosPtr.String())
default:
if s, ok := val.(fmt.Stringer); ok {
return cty.StringVal(s.String())
}
log.Debug().Msg(fmt.Sprintf("unhandled variable type: %T", val))
// last resort
return cty.StringVal(fmt.Sprintf("%s", val))
}
}
var ignoredFields = []string{
"CreationTimestamp",
"DeletionTimestamp",
"Generation",
"OwnerReferences",
"ResourceVersion",
"SelfLink",
"TypeMeta",
"Status",
"UID",
}
var ignoredFieldMap map[string]bool
func init() {
ignoredFieldMap = make(map[string]bool, len(ignoredFields))
for _, v := range ignoredFields {
ignoredFieldMap[v] = true
}
}
func ignoredField(name string) bool {
_, ok := ignoredFieldMap[name]
return ok
}
func (w *ObjectWalker) log(s string, e *zerolog.Event) {
e.
Str("type", w.ResourceType()).
Str("name", w.ResourceName())
if w.field() != nil {
e.Str("field", w.field().Name)
}
if w.currentSlice() != nil {
e.Str("slice", w.currentSlice().Name)
}
e.Msg(s)
}
func (w *ObjectWalker) info(s string) {
w.log(s, log.Info())
}
func (w *ObjectWalker) infof(format string, a ...interface{}) {
w.info(fmt.Sprintf(format, a...))
}
func (w *ObjectWalker) debug(s string) {
w.log(s, log.Debug())
}
func (w *ObjectWalker) debugf(format string, a ...interface{}) {
w.debug(fmt.Sprintf(format, a...))
}
func (w *ObjectWalker) warn() *zerolog.Event {
w.warnCount++
return w.decorateEvent(log.Warn())
}
func (w *ObjectWalker) decorateEvent(e *zerolog.Event) *zerolog.Event {
e.
Str("name", w.ResourceName()).
Str("type", w.ResourceType())
return e
}
// ResourceName returns the Terraform Resource name for the Kubernetes Object
func (w *ObjectWalker) ResourceName() string {
if w.resourceName == "" {
w.resourceName = tfkschema.ToTerraformResourceName(w.RuntimeObject)
}
return w.resourceName
}
// ResourceType returns the Terraform Resource type for the Kubernetes Object
func (w *ObjectWalker) ResourceType() string {
if w.resourceType == "" {
w.resourceType = tfkschema.ToTerraformResourceType(w.RuntimeObject)
}
return w.resourceType
}