-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
target_0_11.go
157 lines (133 loc) · 4.39 KB
/
target_0_11.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
/*
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 (
"encoding/json"
"fmt"
"os"
"path"
hcl_parser "github.com/hashicorp/hcl/json/parser"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/featureflag"
"k8s.io/kops/upup/pkg/fi"
)
func (t *TerraformTarget) finish011(taskMap map[string]fi.Task) error {
resourcesByType := make(map[string]map[string]interface{})
for _, res := range t.resources {
resources := resourcesByType[res.ResourceType]
if resources == nil {
resources = make(map[string]interface{})
resourcesByType[res.ResourceType] = resources
}
tfName := tfSanitize(res.ResourceName)
if resources[tfName] != nil {
return fmt.Errorf("duplicate resource found: %s.%s", res.ResourceType, tfName)
}
resources[tfName] = res.Item
}
providersByName := make(map[string]map[string]interface{})
if t.Cloud.ProviderID() == kops.CloudProviderGCE {
providerGoogle := make(map[string]interface{})
providerGoogle["project"] = t.Project
providerGoogle["region"] = t.Region
for k, v := range tfGetProviderExtraConfig(t.clusterSpecTarget) {
providerGoogle[k] = v
}
providersByName["google"] = providerGoogle
providerGoogle["version"] = ">= 3.0.0"
} else if t.Cloud.ProviderID() == kops.CloudProviderAWS {
providerAWS := make(map[string]interface{})
providerAWS["region"] = t.Region
for k, v := range tfGetProviderExtraConfig(t.clusterSpecTarget) {
providerAWS[k] = v
}
providersByName["aws"] = providerAWS
}
outputVariables := make(map[string]interface{})
for _, v := range t.outputs {
tfName := tfSanitize(v.Key)
if outputVariables[tfName] != nil {
return fmt.Errorf("duplicate variable found: %s", tfName)
}
tfVar := make(map[string]interface{})
if v.Value != nil {
tfVar["value"] = v.Value
} else {
SortLiterals(v.ValueArray)
deduped, err := DedupLiterals(v.ValueArray)
if err != nil {
return err
}
tfVar["value"] = deduped
}
outputVariables[tfName] = tfVar
}
localVariables := make(map[string]interface{})
for _, v := range t.outputs {
tfName := tfSanitize(v.Key)
if localVariables[tfName] != nil {
return fmt.Errorf("duplicate variable found: %s", tfName)
}
if v.Value != nil {
localVariables[tfName] = v.Value
} else {
SortLiterals(v.ValueArray)
deduped, err := DedupLiterals(v.ValueArray)
if err != nil {
return err
}
localVariables[tfName] = deduped
}
}
// See https://github.com/kubernetes/kops/pull/2424 for why we require 0.9.3
terraformConfiguration := make(map[string]interface{})
if featureflag.TerraformJSON.Enabled() {
terraformConfiguration["required_version"] = ">= 0.12.0"
} else {
terraformConfiguration["required_version"] = ">= 0.9.3"
}
data := make(map[string]interface{})
data["terraform"] = terraformConfiguration
data["resource"] = resourcesByType
if len(providersByName) != 0 {
data["provider"] = providersByName
}
if len(outputVariables) != 0 {
data["output"] = outputVariables
}
if len(localVariables) != 0 {
data["locals"] = localVariables
}
jsonBytes, err := json.MarshalIndent(data, "", " ")
if err != nil {
return fmt.Errorf("error marshaling terraform data to json: %v", err)
}
if featureflag.TerraformJSON.Enabled() {
t.files["kubernetes.tf.json"] = jsonBytes
p := path.Join(t.outDir, "kubernetes.tf")
if _, err := os.Stat(p); err == nil {
return fmt.Errorf("Error generating kubernetes.tf.json: If you are upgrading from terraform 0.11 or earlier please read the release notes. Also, the kubernetes.tf file is already present. Please move the file away since it will be replaced by the kubernetes.tf.json file. ")
}
} else {
f, err := hcl_parser.Parse(jsonBytes)
if err != nil {
return fmt.Errorf("error parsing terraform json: %v", err)
}
b, err := hclPrint(f)
if err != nil {
return fmt.Errorf("error writing terraform data to output: %v", err)
}
t.files["kubernetes.tf"] = b
}
return nil
}