-
Notifications
You must be signed in to change notification settings - Fork 90
/
write.go
198 lines (164 loc) · 5.68 KB
/
write.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
package upstream
import (
"bytes"
"io/ioutil"
"os"
"path"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/crypto"
"github.com/replicatedhq/kots/pkg/upstream/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes/scheme"
)
func WriteUpstream(u *types.Upstream, options types.WriteOptions) error {
renderDir := options.RootDir
if options.CreateAppDir {
renderDir = path.Join(renderDir, u.Name)
}
renderDir = path.Join(renderDir, "upstream")
if options.IncludeAdminConsole {
adminConsoleFiles, err := generateAdminConsoleFiles(renderDir, options.SharedPassword)
if err != nil {
return errors.Wrap(err, "failed to generate admin console")
}
u.Files = append(u.Files, adminConsoleFiles...)
}
var previousValuesContent []byte
var previousInstallationContent []byte
_, err := os.Stat(renderDir)
if err == nil {
// if there's already a config values yaml, we need to save
_, err := os.Stat(path.Join(renderDir, "userdata", "config.yaml"))
if err == nil {
c, err := ioutil.ReadFile(path.Join(renderDir, "userdata", "config.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read existing config values")
}
previousValuesContent = c
}
_, err = os.Stat(path.Join(renderDir, "userdata", "installation.yaml"))
if err == nil {
c, err := ioutil.ReadFile(path.Join(renderDir, "userdata", "installation.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read existing installation")
}
previousInstallationContent = c
}
if err := os.RemoveAll(renderDir); err != nil {
return errors.Wrap(err, "failed to remove previous content in upstream")
}
}
for _, file := range u.Files {
fileRenderPath := path.Join(renderDir, file.Path)
d, _ := path.Split(fileRenderPath)
if _, err := os.Stat(d); os.IsNotExist(err) {
if err := os.MkdirAll(d, 0744); err != nil {
return errors.Wrap(err, "failed to mkdir")
}
}
if err := ioutil.WriteFile(fileRenderPath, file.Content, 0644); err != nil {
return errors.Wrap(err, "failed to write upstream file")
}
}
if previousValuesContent != nil {
for i, f := range u.Files {
if f.Path == path.Join("userdata", "config.yaml") {
mergedValues, err := mergeValues(previousValuesContent, f.Content)
if err != nil {
return errors.Wrap(err, "failed to merge config values")
}
err = ioutil.WriteFile(path.Join(renderDir, "userdata", "config.yaml"), mergedValues, 0644)
if err != nil {
return errors.Wrap(err, "failed to replace configg values with previous config values")
}
updatedValues := types.UpstreamFile{
Path: f.Path,
Content: mergedValues,
}
u.Files[i] = updatedValues
}
}
}
// Write the installation status (update cursor, etc)
// but preserving the encryption key, if there already is one
encryptionKey, err := getEncryptionKey(previousInstallationContent)
if err != nil {
return errors.Wrap(err, "failed to get encryption key")
}
installation := kotsv1beta1.Installation{
TypeMeta: metav1.TypeMeta{
APIVersion: "kots.io/v1beta1",
Kind: "Installation",
},
ObjectMeta: metav1.ObjectMeta{
Name: u.Name,
},
Spec: kotsv1beta1.InstallationSpec{
UpdateCursor: u.UpdateCursor,
VersionLabel: u.VersionLabel,
ReleaseNotes: u.ReleaseNotes,
EncryptionKey: encryptionKey,
},
}
if _, err := os.Stat(path.Join(renderDir, "userdata")); os.IsNotExist(err) {
if err := os.MkdirAll(path.Join(renderDir, "userdata"), 0755); err != nil {
return errors.Wrap(err, "failed to create userdata dir")
}
}
err = ioutil.WriteFile(path.Join(renderDir, "userdata", "installation.yaml"), mustMarshalInstallation(&installation), 0644)
if err != nil {
return errors.Wrap(err, "failed to write installation")
}
return nil
}
func getEncryptionKey(previousInstallationContent []byte) (string, error) {
if previousInstallationContent == nil {
cipher, err := crypto.NewAESCipher()
if err != nil {
return "", errors.Wrap(err, "failed to create new AES cipher")
}
return cipher.ToString(), nil
}
decode := scheme.Codecs.UniversalDeserializer().Decode
prevObj, _, err := decode(previousInstallationContent, nil, nil)
if err != nil {
return "", errors.Wrap(err, "failed to decode previous installation")
}
installation := prevObj.(*kotsv1beta1.Installation)
return installation.Spec.EncryptionKey, nil
}
func mergeValues(previousValues []byte, applicationDeliveredValues []byte) ([]byte, error) {
decode := scheme.Codecs.UniversalDeserializer().Decode
prevObj, _, err := decode(previousValues, nil, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to decode previous values")
}
prevValues := prevObj.(*kotsv1beta1.ConfigValues)
applicationValuesObj, _, err := decode(applicationDeliveredValues, nil, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to decode application delivered values")
}
applicationValues := applicationValuesObj.(*kotsv1beta1.ConfigValues)
for name, value := range applicationValues.Spec.Values {
_, ok := prevValues.Spec.Values[name]
if !ok {
prevValues.Spec.Values[name] = value
}
}
s := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var b bytes.Buffer
if err := s.Encode(prevValues, &b); err != nil {
return nil, errors.Wrap(err, "failed to encode merged values")
}
return b.Bytes(), nil
}
func mustMarshalInstallation(installation *kotsv1beta1.Installation) []byte {
s := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var b bytes.Buffer
if err := s.Encode(installation, &b); err != nil {
panic(err)
}
return b.Bytes()
}