-
Notifications
You must be signed in to change notification settings - Fork 90
/
rewrite.go
334 lines (295 loc) · 11.2 KB
/
rewrite.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package rewrite
import (
"fmt"
"io"
"io/ioutil"
"path/filepath"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/base"
"github.com/replicatedhq/kots/pkg/docker/registry"
"github.com/replicatedhq/kots/pkg/downstream"
"github.com/replicatedhq/kots/pkg/k8sdoc"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/midstream"
"github.com/replicatedhq/kots/pkg/upstream"
upstreamtypes "github.com/replicatedhq/kots/pkg/upstream/types"
corev1 "k8s.io/api/core/v1"
kustomizetypes "sigs.k8s.io/kustomize/api/types"
)
type RewriteOptions struct {
RootDir string
UpstreamURI string
UpstreamPath string
Downstreams []string
K8sNamespace string
Silent bool
CreateAppDir bool
ExcludeKotsKinds bool
Installation *kotsv1beta1.Installation
License *kotsv1beta1.License
ConfigValues *kotsv1beta1.ConfigValues
ReportWriter io.Writer
CopyImages bool
IsAirgap bool
RegistryEndpoint string
RegistryUsername string
RegistryPassword string
RegistryNamespace string
AppSlug string
IsGitOps bool
AppSequence int64
ReportingInfo *upstreamtypes.ReportingInfo
}
func Rewrite(rewriteOptions RewriteOptions) error {
log := logger.NewLogger()
if rewriteOptions.Silent {
log.Silence()
}
log.Initialize()
if rewriteOptions.ReportWriter == nil {
rewriteOptions.ReportWriter = ioutil.Discard
}
fetchOptions := &upstreamtypes.FetchOptions{
RootDir: rewriteOptions.RootDir,
LocalPath: rewriteOptions.UpstreamPath,
CurrentCursor: rewriteOptions.Installation.Spec.UpdateCursor,
CurrentVersionLabel: rewriteOptions.Installation.Spec.VersionLabel,
EncryptionKey: rewriteOptions.Installation.Spec.EncryptionKey,
License: rewriteOptions.License,
LocalRegistry: upstreamtypes.LocalRegistry{
Host: rewriteOptions.RegistryEndpoint,
Namespace: rewriteOptions.RegistryNamespace,
Username: rewriteOptions.RegistryUsername,
Password: rewriteOptions.RegistryPassword,
},
ReportingInfo: rewriteOptions.ReportingInfo,
}
log.ActionWithSpinner("Pulling upstream")
io.WriteString(rewriteOptions.ReportWriter, "Pulling upstream\n")
u, err := upstream.FetchUpstream(rewriteOptions.UpstreamURI, fetchOptions)
if err != nil {
log.FinishSpinnerWithError()
return errors.Wrap(err, "failed to load upstream")
}
includeAdminConsole := false
writeUpstreamOptions := upstreamtypes.WriteOptions{
RootDir: rewriteOptions.RootDir,
CreateAppDir: rewriteOptions.CreateAppDir,
IncludeAdminConsole: includeAdminConsole,
PreserveInstallation: true,
}
if err := upstream.WriteUpstream(u, writeUpstreamOptions); err != nil {
log.FinishSpinnerWithError()
return errors.Wrap(err, "failed to write upstream")
}
log.FinishSpinner()
replicatedRegistryInfo := registry.ProxyEndpointFromLicense(rewriteOptions.License)
renderOptions := base.RenderOptions{
SplitMultiDocYAML: true,
Namespace: rewriteOptions.K8sNamespace,
LocalRegistryHost: rewriteOptions.RegistryEndpoint,
LocalRegistryNamespace: rewriteOptions.RegistryNamespace,
LocalRegistryUsername: rewriteOptions.RegistryUsername,
LocalRegistryPassword: rewriteOptions.RegistryPassword,
ExcludeKotsKinds: rewriteOptions.ExcludeKotsKinds,
Log: log,
Sequence: rewriteOptions.AppSequence,
IsAirgap: rewriteOptions.IsAirgap,
}
log.ActionWithSpinner("Creating base")
io.WriteString(rewriteOptions.ReportWriter, "Creating base\n")
b, err := base.RenderUpstream(u, &renderOptions)
if err != nil {
return errors.Wrap(err, "failed to render upstream")
}
if ff := b.ListErrorFiles(); len(ff) > 0 {
files := make([]kotsv1beta1.InstallationYAMLError, 0, len(ff))
for _, f := range ff {
file := kotsv1beta1.InstallationYAMLError{
Path: f.Path,
}
if f.Error != nil {
file.Error = f.Error.Error()
}
files = append(files, file)
}
newInstallation, err := upstream.LoadInstallation(u.GetUpstreamDir(writeUpstreamOptions))
if err != nil {
return errors.Wrap(err, "failed to load installation")
}
newInstallation.Spec.YAMLErrors = files
err = upstream.SaveInstallation(newInstallation, u.GetUpstreamDir(writeUpstreamOptions))
if err != nil {
return errors.Wrap(err, "failed to save installation")
}
}
log.FinishSpinner()
writeBaseOptions := base.WriteOptions{
BaseDir: u.GetBaseDir(writeUpstreamOptions),
SkippedDir: u.GetSkippedDir(writeUpstreamOptions),
Overwrite: true,
ExcludeKotsKinds: rewriteOptions.ExcludeKotsKinds,
}
if err := b.WriteBase(writeBaseOptions); err != nil {
return errors.Wrap(err, "failed to write base")
}
var pullSecret *corev1.Secret
var images []kustomizetypes.Image
var objects []k8sdoc.K8sDoc
if rewriteOptions.CopyImages || rewriteOptions.RegistryEndpoint != "" {
// When CopyImages is set, we copy images, rewrite all images, and use registry
// settings to create secrets for all objects that have images.
// When only registry endpoint is set, we don't need to copy images, but still
// need to rewrite them and create secrets.
newInstallation, err := upstream.LoadInstallation(u.GetUpstreamDir(writeUpstreamOptions))
if err != nil {
return errors.Wrap(err, "failed to load installation")
}
application, err := upstream.LoadApplication(u.GetUpstreamDir(writeUpstreamOptions))
if err != nil {
return errors.Wrap(err, "failed to load application")
}
writeUpstreamImageOptions := base.WriteUpstreamImageOptions{
BaseDir: writeBaseOptions.BaseDir,
ReportWriter: rewriteOptions.ReportWriter,
Log: log,
SourceRegistry: registry.RegistryOptions{
Endpoint: replicatedRegistryInfo.Registry,
ProxyEndpoint: replicatedRegistryInfo.Proxy,
},
DestRegistry: registry.RegistryOptions{
Endpoint: rewriteOptions.RegistryEndpoint,
Namespace: rewriteOptions.RegistryNamespace,
Username: rewriteOptions.RegistryUsername,
Password: rewriteOptions.RegistryPassword,
},
Installation: newInstallation,
Application: application,
DryRun: !rewriteOptions.CopyImages,
IsAirgap: rewriteOptions.IsAirgap,
}
if fetchOptions.License != nil {
writeUpstreamImageOptions.AppSlug = fetchOptions.License.Spec.AppSlug
writeUpstreamImageOptions.SourceRegistry.Username = fetchOptions.License.Spec.LicenseID
writeUpstreamImageOptions.SourceRegistry.Password = fetchOptions.License.Spec.LicenseID
}
copyResult, err := base.CopyUpstreamImages(writeUpstreamImageOptions)
if err != nil {
return errors.Wrap(err, "failed to write upstream images")
}
newInstallation.Spec.KnownImages = copyResult.CheckedImages
err = upstream.SaveInstallation(newInstallation, u.GetUpstreamDir(writeUpstreamOptions))
if err != nil {
return errors.Wrap(err, "failed to save installation")
}
findObjectsOptions := base.FindObjectsWithImagesOptions{
BaseDir: writeBaseOptions.BaseDir,
}
affectedObjects, err := base.FindObjectsWithImages(findObjectsOptions)
if err != nil {
return errors.Wrap(err, "failed to find objects with images")
}
registryUser := rewriteOptions.RegistryUsername
registryPass := rewriteOptions.RegistryPassword
if registryUser == "" {
// this will only work when envoked from CLI where `docker login` command has been executed
registryUser, registryPass, err = registry.LoadAuthForRegistry(rewriteOptions.RegistryEndpoint)
if err != nil {
return errors.Wrapf(err, "failed to load registry auth for %q", rewriteOptions.RegistryEndpoint)
}
}
pullSecret, err = registry.PullSecretForRegistries(
[]string{rewriteOptions.RegistryEndpoint},
registryUser,
registryPass,
rewriteOptions.K8sNamespace,
)
if err != nil {
return errors.Wrap(err, "failed to create private registry pull secret")
}
images = copyResult.Images
objects = affectedObjects
} else {
application, err := upstream.LoadApplication(u.GetUpstreamDir(writeUpstreamOptions))
if err != nil {
return errors.Wrap(err, "failed to load application")
}
allPrivate := false
if application != nil {
allPrivate = application.Spec.ProxyPublicImages
}
// When CopyImages is not set, we only rewrite private images and use license to create secrets
// for all objects that have private images
findPrivateImagesOptions := base.FindPrivateImagesOptions{
BaseDir: writeBaseOptions.BaseDir,
AppSlug: fetchOptions.License.Spec.AppSlug,
ReplicatedRegistry: registry.RegistryOptions{
Endpoint: replicatedRegistryInfo.Registry,
ProxyEndpoint: replicatedRegistryInfo.Proxy,
},
Installation: rewriteOptions.Installation,
AllImagesPrivate: allPrivate,
}
findResult, err := base.FindPrivateImages(findPrivateImagesOptions)
if err != nil {
return errors.Wrap(err, "failed to find private images")
}
newInstallation, err := upstream.LoadInstallation(u.GetUpstreamDir(writeUpstreamOptions))
if err != nil {
return errors.Wrap(err, "failed to load installation")
}
newInstallation.Spec.KnownImages = findResult.CheckedImages
err = upstream.SaveInstallation(newInstallation, u.GetUpstreamDir(writeUpstreamOptions))
if err != nil {
return errors.Wrap(err, "failed to save installation")
}
if len(findResult.Docs) > 0 {
replicatedRegistryInfo := registry.ProxyEndpointFromLicense(rewriteOptions.License)
pullSecret, err = registry.PullSecretForRegistries(
replicatedRegistryInfo.ToSlice(),
rewriteOptions.License.Spec.LicenseID,
rewriteOptions.License.Spec.LicenseID,
rewriteOptions.K8sNamespace,
)
if err != nil {
return errors.Wrap(err, "failed to create Replicated registry pull secret")
}
}
images = findResult.Images
objects = findResult.Docs
}
log.ActionWithSpinner("Creating midstream")
io.WriteString(rewriteOptions.ReportWriter, "Creating midstream\n")
m, err := midstream.CreateMidstream(b, images, objects, pullSecret)
if err != nil {
return errors.Wrap(err, "failed to create midstream")
}
log.FinishSpinner()
writeMidstreamOptions := midstream.WriteOptions{
MidstreamDir: filepath.Join(b.GetOverlaysDir(writeBaseOptions), "midstream"),
BaseDir: u.GetBaseDir(writeUpstreamOptions),
AppSlug: rewriteOptions.AppSlug,
IsGitOps: rewriteOptions.IsGitOps,
}
if err := m.WriteMidstream(writeMidstreamOptions); err != nil {
return errors.Wrap(err, "failed to write midstream")
}
for _, downstreamName := range rewriteOptions.Downstreams {
log.ActionWithSpinner("Creating downstream %q", downstreamName)
io.WriteString(rewriteOptions.ReportWriter, fmt.Sprintf("Creating downstream %q\n", downstreamName))
d, err := downstream.CreateDownstream(m, downstreamName)
if err != nil {
return errors.Wrap(err, "failed to create downstream")
}
writeDownstreamOptions := downstream.WriteOptions{
DownstreamDir: filepath.Join(b.GetOverlaysDir(writeBaseOptions), "downstreams", downstreamName),
MidstreamDir: writeMidstreamOptions.MidstreamDir,
}
if err := d.WriteDownstream(writeDownstreamOptions); err != nil {
return errors.Wrap(err, "failed to write downstream")
}
log.FinishSpinner()
}
return nil
}