-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
target.go
115 lines (93 loc) · 3.17 KB
/
target.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
/*
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"
"os"
"path"
"k8s.io/klog/v2"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/terraformWriter"
"k8s.io/kops/util/pkg/vfs"
)
type TerraformTarget struct {
terraformWriter.TerraformWriter
Cloud fi.Cloud
Project string
ClusterName string
outDir string
// extra config to add to the provider block
clusterSpecTarget *kops.TargetSpec
filesProvider *vfs.TerraformProvider
}
func NewTerraformTarget(cloud fi.Cloud, project string, filesProvider *vfs.TerraformProvider, outDir string, clusterSpecTarget *kops.TargetSpec) *TerraformTarget {
target := TerraformTarget{
Cloud: cloud,
Project: project,
outDir: outDir,
clusterSpecTarget: clusterSpecTarget,
filesProvider: filesProvider,
}
target.InitTerraformWriter()
return &target
}
var _ fi.Target = &TerraformTarget{}
func (t *TerraformTarget) AddFileResource(resourceType string, resourceName string, key string, r fi.Resource, base64 bool) (*terraformWriter.Literal, error) {
d, err := fi.ResourceAsBytes(r)
if err != nil {
id := resourceType + "_" + resourceName + "_" + key
return nil, fmt.Errorf("error rending resource %s %v", id, err)
}
return t.AddFileBytes(resourceType, resourceName, key, d, base64)
}
func (t *TerraformTarget) ProcessDeletions() bool {
// Terraform tracks & performs deletions itself
return false
}
// tfGetProviderExtraConfig is a helper function to get extra config with safety checks on the pointers.
func tfGetProviderExtraConfig(c *kops.TargetSpec) map[string]string {
if c != nil &&
c.Terraform != nil &&
c.Terraform.FilesProviderExtraConfig != nil {
return *c.Terraform.ProviderExtraConfig
}
return nil
}
// tfGetFilesProviderExtraConfig is a helper function to get extra config with safety checks on the pointers.
func tfGetFilesProviderExtraConfig(c *kops.TargetSpec) map[string]string {
if c != nil &&
c.Terraform != nil &&
c.Terraform.FilesProviderExtraConfig != nil {
return *c.Terraform.FilesProviderExtraConfig
}
return nil
}
func (t *TerraformTarget) Finish(taskMap map[string]fi.Task) error {
if err := t.finishHCL2(); err != nil {
return err
}
for relativePath, contents := range t.Files {
p := path.Join(t.outDir, relativePath)
err := os.MkdirAll(path.Dir(p), os.FileMode(0o755))
if err != nil {
return fmt.Errorf("error creating output directory %q: %v", path.Dir(p), err)
}
err = os.WriteFile(p, contents, os.FileMode(0o644))
if err != nil {
return fmt.Errorf("error writing terraform data to output file %q: %v", p, err)
}
}
klog.Infof("Terraform output is in %s", t.outDir)
return nil
}