-
Notifications
You must be signed in to change notification settings - Fork 90
/
write.go
215 lines (178 loc) · 6.11 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package upstream
import (
"bytes"
"encoding/base64"
"os"
"path"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/archives"
"github.com/replicatedhq/kots/pkg/crypto"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/upstream/types"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
serializer "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)
if err != nil {
return errors.Wrap(err, "failed to generate admin console")
}
u.Files = append(u.Files, adminConsoleFiles...)
}
var previousInstallationContent []byte
_, err := os.Stat(renderDir)
if err == nil {
_, err = os.Stat(path.Join(renderDir, "userdata", "installation.yaml"))
if err == nil {
c, err := os.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")
}
}
var prevInstallation *kotsv1beta1.Installation
if previousInstallationContent != nil {
decode := scheme.Codecs.UniversalDeserializer().Decode
prevObj, _, err := decode(previousInstallationContent, nil, nil)
if err != nil {
return errors.Wrap(err, "failed to decode previous installation")
}
prevInstallation = prevObj.(*kotsv1beta1.Installation)
}
encryptionKey, err := getEncryptionKey(prevInstallation)
if err != nil {
return errors.Wrap(err, "failed to get encryption key")
}
_ = crypto.InitFromString(encryptionKey)
for i, 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 options.EncryptConfig {
configValues := contentToConfigValues(file.Content)
if configValues != nil {
content, err := encryptConfigValues(configValues)
if err != nil {
return errors.Wrap(err, "failed to encrypt config values")
}
file.Content = content
u.Files[i] = file
}
}
identityConfig := contentToIdentityConfig(file.Content)
if identityConfig != nil {
content, err := maybeEncryptIdentityConfig(identityConfig)
if err != nil {
return errors.Wrap(err, "failed to encrypt identity config")
}
file.Content = content
u.Files[i] = file
}
if archives.IsTGZ(file.Content) {
updatedContent, err := configureChart(file.Content, u, options)
if err != nil {
return errors.Wrap(err, "failed to configure replicated sdk")
}
file.Content = updatedContent
u.Files[i] = file
}
if err := os.WriteFile(fileRenderPath, file.Content, 0644); err != nil {
return errors.Wrap(err, "failed to write upstream file")
}
}
channelID, channelName := "", ""
if prevInstallation != nil && options.PreserveInstallation {
channelID = prevInstallation.Spec.ChannelID
channelName = prevInstallation.Spec.ChannelName
} else {
channelID = u.ChannelID
channelName = u.ChannelName
}
installation := kotsv1beta1.Installation{
TypeMeta: metav1.TypeMeta{
APIVersion: "kots.io/v1beta1",
Kind: "Installation",
},
ObjectMeta: metav1.ObjectMeta{
Name: u.Name,
},
Spec: kotsv1beta1.InstallationSpec{
UpdateCursor: u.UpdateCursor,
ChannelID: channelID,
ChannelName: channelName,
VersionLabel: u.VersionLabel,
IsRequired: u.IsRequired,
ReleaseNotes: u.ReleaseNotes,
ReplicatedRegistryDomain: u.ReplicatedRegistryDomain,
ReplicatedProxyDomain: u.ReplicatedProxyDomain,
ReplicatedChartNames: u.ReplicatedChartNames,
EncryptionKey: encryptionKey,
},
}
if u.ReleasedAt != nil {
releasedAt := metav1.NewTime(*u.ReleasedAt)
installation.Spec.ReleasedAt = &releasedAt
}
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")
}
}
installationBytes := kotsutil.MustMarshalInstallation(&installation)
u.Files = append(u.Files, types.UpstreamFile{
Path: "userdata/installation.yaml",
Content: installationBytes,
})
if err := os.WriteFile(path.Join(renderDir, "userdata", "installation.yaml"), installationBytes, 0644); err != nil {
return errors.Wrap(err, "failed to write installation")
}
return nil
}
func getEncryptionKey(prevInstallation *kotsv1beta1.Installation) (string, error) {
if prevInstallation == nil {
return "", nil
}
return prevInstallation.Spec.EncryptionKey, nil
}
func encryptConfigValues(configValues *kotsv1beta1.ConfigValues) ([]byte, error) {
for k, v := range configValues.Spec.Values {
if v.ValuePlaintext == "" {
continue
}
v.Value = base64.StdEncoding.EncodeToString(crypto.Encrypt([]byte(v.ValuePlaintext)))
v.ValuePlaintext = ""
configValues.Spec.Values[k] = v
}
s := serializer.NewYAMLSerializer(serializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var b bytes.Buffer
if err := s.Encode(configValues, &b); err != nil {
return nil, errors.Wrap(err, "failed to encode config values")
}
return b.Bytes(), nil
}
func maybeEncryptIdentityConfig(identityConfig *kotsv1beta1.IdentityConfig) ([]byte, error) {
identityConfig.Spec.ClientSecret.EncryptValue()
identityConfig.Spec.DexConnectors.EncryptValue()
s := serializer.NewYAMLSerializer(serializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var b bytes.Buffer
if err := s.Encode(identityConfig, &b); err != nil {
return nil, errors.Wrap(err, "failed to encode identity config")
}
return b.Bytes(), nil
}