-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathtarget_hcl2.go
174 lines (152 loc) · 5 KB
/
target_hcl2.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
/*
Copyright 2020 The Kubernetes Authors.
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.
*/
package terraform
import (
"fmt"
"sort"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/featureflag"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/terraformWriter"
)
func (t *TerraformTarget) finishHCL2(taskMap map[string]fi.Task) error {
f := hclwrite.NewEmptyFile()
rootBody := f.Body()
outputs, err := t.GetOutputs()
if err != nil {
return err
}
writeLocalsOutputs(rootBody, outputs)
providerName := string(t.Cloud.ProviderID())
if t.Cloud.ProviderID() == kops.CloudProviderGCE {
providerName = "google"
}
providerBlock := rootBody.AppendNewBlock("provider", []string{providerName})
providerBody := providerBlock.Body()
providerBody.SetAttributeValue("region", cty.StringVal(t.Cloud.Region()))
for k, v := range tfGetProviderExtraConfig(t.clusterSpecTarget) {
providerBody.SetAttributeValue(k, cty.StringVal(v))
}
rootBody.AppendNewline()
resourcesByType, err := t.GetResourcesByType()
if err != nil {
return err
}
resourceTypes := make([]string, 0, len(resourcesByType))
for resourceType := range resourcesByType {
resourceTypes = append(resourceTypes, resourceType)
}
sort.Strings(resourceTypes)
for _, resourceType := range resourceTypes {
resources := resourcesByType[resourceType]
resourceNames := make([]string, 0, len(resources))
for resourceName := range resources {
resourceNames = append(resourceNames, resourceName)
}
sort.Strings(resourceNames)
for _, resourceName := range resourceNames {
item := resources[resourceName]
resBlock := rootBody.AppendNewBlock("resource", []string{resourceType, resourceName})
resBody := resBlock.Body()
resType, err := gocty.ImpliedType(item)
if err != nil {
return err
}
resVal, err := gocty.ToCtyValue(item, resType)
if err != nil {
return err
}
if resVal.IsNull() {
continue
}
resVal.ForEachElement(func(key cty.Value, value cty.Value) bool {
writeValue(resBody, key.AsString(), value)
return false
})
rootBody.AppendNewline()
}
}
terraformBlock := rootBody.AppendNewBlock("terraform", []string{})
terraformBody := terraformBlock.Body()
terraformBody.SetAttributeValue("required_version", cty.StringVal(">= 0.12.26"))
requiredProvidersBlock := terraformBody.AppendNewBlock("required_providers", []string{})
requiredProvidersBody := requiredProvidersBlock.Body()
if t.Cloud.ProviderID() == kops.CloudProviderGCE {
writeMap(requiredProvidersBody, "google", map[string]cty.Value{
"source": cty.StringVal("hashicorp/google"),
"version": cty.StringVal(">= 2.19.0"),
})
} else if t.Cloud.ProviderID() == kops.CloudProviderAWS {
writeMap(requiredProvidersBody, "aws", map[string]cty.Value{
"source": cty.StringVal("hashicorp/aws"),
"version": cty.StringVal(">= 3.34.0"),
})
if featureflag.Spotinst.Enabled() {
writeMap(requiredProvidersBody, "spotinst", map[string]cty.Value{
"source": cty.StringVal("spotinst/spotinst"),
"version": cty.StringVal(">= 1.33.0"),
})
}
}
bytes := hclwrite.Format(f.Bytes())
t.Files["kubernetes.tf"] = bytes
return nil
}
// writeLocalsOutputs creates the locals block and output blocks for all output variables
// Example:
// locals {
// key1 = "value1"
// key2 = "value2"
// }
// output "key1" {
// value = "value1"
// }
// output "key2" {
// value = "value2"
// }
func writeLocalsOutputs(body *hclwrite.Body, outputs map[string]terraformWriter.OutputValue) error {
if len(outputs) == 0 {
return nil
}
localsBlock := body.AppendNewBlock("locals", []string{})
body.AppendNewline()
// each output is added to a single locals block and its own output block
localsBody := localsBlock.Body()
existingOutputVars := make(map[string]bool)
outputNames := make([]string, 0, len(outputs))
for k := range outputs {
outputNames = append(outputNames, k)
}
sort.Strings(outputNames)
for _, tfName := range outputNames {
v := outputs[tfName]
outputBlock := body.AppendNewBlock("output", []string{tfName})
outputBody := outputBlock.Body()
if v.Value != nil {
writeLiteral(outputBody, "value", v.Value)
writeLiteral(localsBody, tfName, v.Value)
} else {
writeLiteralList(outputBody, "value", v.ValueArray)
writeLiteralList(localsBody, tfName, v.ValueArray)
}
if existingOutputVars[tfName] {
return fmt.Errorf("duplicate variable found: %s", tfName)
}
existingOutputVars[tfName] = true
body.AppendNewline()
}
return nil
}