-
Notifications
You must be signed in to change notification settings - Fork 90
/
templates.go
179 lines (153 loc) · 4.75 KB
/
templates.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
package main
import "C"
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/mholt/archiver"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
kotsconfig "github.com/replicatedhq/kots/pkg/config"
"github.com/replicatedhq/kots/pkg/crypto"
"github.com/replicatedhq/kots/pkg/template"
"k8s.io/client-go/kubernetes/scheme"
)
//export RenderFile
func RenderFile(socket string, filePath string, archivePath string, registryJson string) {
go func() {
var ffiResult *FFIResult
statusClient, err := connectToStatusServer(socket)
if err != nil {
fmt.Printf("failed to connect to status server: %s\n", err)
return
}
defer func() {
statusClient.end(ffiResult)
}()
registryInfo := struct {
Host string `json:"registryHostname"`
Username string `json:"registryUsername"`
Password string `json:"registryPassword"`
Namespace string `json:"namespace"`
}{}
if err := json.Unmarshal([]byte(registryJson), ®istryInfo); err != nil {
fmt.Printf("failed to unmarshal registry info: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
tmpRoot, err := ioutil.TempDir("", "kots")
if err != nil {
fmt.Printf("failed to create temp root path: %s\n", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
defer os.RemoveAll(tmpRoot)
tarGz := archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: false,
},
}
if err := tarGz.Unarchive(archivePath, tmpRoot); err != nil {
fmt.Printf("failed to unarchive %s\n", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
// look for config
config, values, license, installation, err := findConfig(tmpRoot)
if err != nil {
ffiResult = NewFFIResult(-1).WithError(err)
return
}
var cipher *crypto.AESCipher
if installation != nil {
c, err := crypto.AESCipherFromString(installation.Spec.EncryptionKey)
if err != nil {
ffiResult = NewFFIResult(-1).WithError(err)
return
}
cipher = c
}
templateContextValues := make(map[string]template.ItemValue)
if values != nil {
for k, v := range values.Spec.Values {
templateContextValues[k] = template.ItemValue{
Value: v.Value,
Default: v.Default,
}
}
}
configGroups := []kotsv1beta1.ConfigGroup{}
if config != nil {
configGroups = config.Spec.Groups
}
localRegistry := template.LocalRegistry{
Host: registryInfo.Host,
Namespace: registryInfo.Namespace,
Username: registryInfo.Username,
Password: registryInfo.Password,
}
builder, configVals, err := template.NewBuilder(configGroups, templateContextValues, localRegistry, cipher, license)
if err != nil {
fmt.Printf("failed to create config context %s\n", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
if config != nil {
kotsconfig.ApplyValuesToConfig(config, configVals)
}
inputContent, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Printf("failed to read file %s\n", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
rendered, err := builder.RenderTemplate(filePath, string(inputContent))
if err != nil {
fmt.Printf("failed to render template %s\n", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
ioutil.WriteFile(filePath, []byte(rendered), 0644)
ffiResult = NewFFIResult(0)
}()
}
func findConfig(archivePath string) (*kotsv1beta1.Config, *kotsv1beta1.ConfigValues, *kotsv1beta1.License, *kotsv1beta1.Installation, error) {
var config *kotsv1beta1.Config
var values *kotsv1beta1.ConfigValues
var license *kotsv1beta1.License
var installation *kotsv1beta1.Installation
err := filepath.Walk(archivePath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, gvk, err := decode(content, nil, nil)
if err != nil {
return nil
}
if gvk.Group == "kots.io" && gvk.Version == "v1beta1" && gvk.Kind == "Config" {
config = obj.(*kotsv1beta1.Config)
} else if gvk.Group == "kots.io" && gvk.Version == "v1beta1" && gvk.Kind == "ConfigValues" {
values = obj.(*kotsv1beta1.ConfigValues)
} else if gvk.Group == "kots.io" && gvk.Version == "v1beta1" && gvk.Kind == "License" {
license = obj.(*kotsv1beta1.License)
} else if gvk.Group == "kots.io" && gvk.Version == "v1beta1" && gvk.Kind == "Installation" {
installation = obj.(*kotsv1beta1.Installation)
}
return nil
})
if err != nil {
return nil, nil, nil, nil, errors.Wrap(err, "failed to walk archive dir")
}
return config, values, license, installation, nil
}