-
Notifications
You must be signed in to change notification settings - Fork 90
/
write.go
478 lines (399 loc) · 15.9 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package midstream
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/base"
"github.com/replicatedhq/kots/pkg/disasterrecovery"
"github.com/replicatedhq/kots/pkg/docker/registry"
dockerregistrytypes "github.com/replicatedhq/kots/pkg/docker/registry/types"
"github.com/replicatedhq/kots/pkg/image"
imagetypes "github.com/replicatedhq/kots/pkg/image/types"
"github.com/replicatedhq/kots/pkg/k8sdoc"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/template"
"github.com/replicatedhq/kots/pkg/util"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
yaml "gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
kustomizetypes "sigs.k8s.io/kustomize/api/types"
k8syaml "sigs.k8s.io/yaml"
)
const (
secretFilename = "secret.yaml"
patchesFilename = "pullsecrets.yaml"
disasterRecoveryLabelTransformerFileName = "backup-label-transformer.yaml"
)
type WriteOptions struct {
MidstreamDir string
Base *base.Base
BaseDir string
AppSlug string
IsGitOps bool
IsOpenShift bool
Builder template.Builder
HTTPProxyEnvValue string
HTTPSProxyEnvValue string
NoProxyEnvValue string
NewHelmCharts []*kotsv1beta1.HelmChart
ProcessImageOptions imagetypes.ProcessImageOptions
License *kotsv1beta1.License
KotsKinds *kotsutil.KotsKinds
IdentityConfig *kotsv1beta1.IdentityConfig
UpstreamDir string
Log *logger.CLILogger
}
func WriteMidstream(opts WriteOptions) (*Midstream, error) {
var images []kustomizetypes.Image
var objects []k8sdoc.K8sDoc
var pullSecretRegistries []string
var pullSecretUsername string
var pullSecretPassword string
clientset, err := k8sutil.GetClientset()
if err != nil {
return nil, errors.Wrap(err, "failed to get k8s clientset")
}
// do not fail on being unable to get dockerhub credentials, since they're just used to increase the rate limit
var dockerHubRegistryCreds registry.Credentials
dockerhubSecret, _ := registry.GetDockerHubPullSecret(clientset, util.PodNamespace, opts.ProcessImageOptions.Namespace, opts.ProcessImageOptions.AppSlug)
if dockerhubSecret != nil {
dockerHubRegistryCreds, _ = registry.GetCredentialsForRegistryFromConfigJSON(dockerhubSecret.Data[".dockerconfigjson"], registry.DockerHubRegistryName)
}
var destRegistry *dockerregistrytypes.RegistryOptions
if opts.ProcessImageOptions.RewriteImages {
destRegistry = &dockerregistrytypes.RegistryOptions{
Endpoint: opts.ProcessImageOptions.RegistrySettings.Hostname,
Namespace: opts.ProcessImageOptions.RegistrySettings.Namespace,
Username: opts.ProcessImageOptions.RegistrySettings.Username,
Password: opts.ProcessImageOptions.RegistrySettings.Password,
}
}
baseImages, objects, err := base.FindImages(opts.Base)
if err != nil {
return nil, errors.Wrap(err, "failed to find base images")
}
kotsKindsImages, err := kotsutil.GetImagesFromKotsKinds(opts.KotsKinds, destRegistry)
if err != nil {
return nil, errors.Wrap(err, "failed to get images from kots kinds")
}
allImages := append(baseImages, kotsKindsImages...)
if err := image.UpdateInstallationImages(image.UpdateInstallationImagesOptions{
Images: allImages,
KotsKinds: opts.KotsKinds,
IsAirgap: opts.ProcessImageOptions.IsAirgap,
UpstreamDir: opts.UpstreamDir,
DockerHubRegistryCreds: dockerHubRegistryCreds,
}); err != nil {
return nil, errors.Wrap(err, "failed to update installation images")
}
if opts.ProcessImageOptions.RewriteImages {
// A target registry is configured. Rewrite all images to point to the configured destination registry, and copy them (if necessary).
if opts.ProcessImageOptions.CopyImages {
opts.Log.ActionWithSpinner("Copying images")
io.WriteString(opts.ProcessImageOptions.ReportWriter, "Copying images\n")
if opts.ProcessImageOptions.IsAirgap {
err := image.CopyAirgapImages(opts.ProcessImageOptions, opts.Log)
if err != nil {
return nil, errors.Wrap(err, "failed to copy airgap images")
}
} else {
err := image.CopyOnlineImages(opts.ProcessImageOptions, allImages, opts.KotsKinds, opts.License, dockerHubRegistryCreds, opts.Log)
if err != nil {
return nil, errors.Wrap(err, "failed to copy online images")
}
}
}
// Rewrite images to point to the configured destination registry.
opts.Log.ActionWithSpinner("Rewriting images")
io.WriteString(opts.ProcessImageOptions.ReportWriter, "Rewriting images\n")
rewrittenImages, err := base.RewriteImages(allImages, *destRegistry)
if err != nil {
return nil, errors.Wrap(err, "failed to rewrite images")
}
images = rewrittenImages
// Use target registry credentials to create image pull secrets for all objects that have images.
pullSecretRegistries = []string{opts.ProcessImageOptions.RegistrySettings.Hostname}
pullSecretUsername = opts.ProcessImageOptions.RegistrySettings.Username
pullSecretPassword = opts.ProcessImageOptions.RegistrySettings.Password
if pullSecretUsername == "" {
pullSecretUsername, pullSecretPassword, err = registry.LoadAuthForRegistry(opts.ProcessImageOptions.RegistrySettings.Hostname)
if err != nil {
return nil, errors.Wrapf(err, "failed to load registry auth for %q", opts.ProcessImageOptions.RegistrySettings.Hostname)
}
}
} else if opts.License != nil {
// A target registry is NOT configured. Rewrite private images to be proxied through proxy.replicated.com
rewrittenImages, err := base.RewritePrivateImages(baseImages, opts.KotsKinds, opts.License)
if err != nil {
return nil, errors.Wrap(err, "failed to rewrite private images")
}
images = rewrittenImages
// Use license to create image pull secrets for all objects that have private images.
pullSecretRegistries = registry.GetRegistryProxyInfo(opts.License, &opts.KotsKinds.Installation, &opts.KotsKinds.KotsApplication).ToSlice()
pullSecretUsername = opts.License.Spec.LicenseID
pullSecretPassword = opts.License.Spec.LicenseID
}
// For the newer style charts, create a new secret per chart as helm adds chart specific
// details to annotations and labels to it.
namePrefix := opts.ProcessImageOptions.AppSlug
for _, v := range opts.NewHelmCharts {
if v.Spec.UseHelmInstall && filepath.Base(opts.Base.Path) != "." {
namePrefix = fmt.Sprintf("%s-%s", opts.ProcessImageOptions.AppSlug, filepath.Base(opts.Base.Path))
break
}
}
pullSecrets, err := registry.PullSecretForRegistries(
pullSecretRegistries,
pullSecretUsername,
pullSecretPassword,
opts.ProcessImageOptions.Namespace,
namePrefix,
)
if err != nil {
return nil, errors.Wrap(err, "create pull secret")
}
pullSecrets.DockerHubSecret = dockerhubSecret
m, err := CreateMidstream(opts.Base, images, objects, &pullSecrets, opts.KotsKinds.Identity, opts.IdentityConfig)
if err != nil {
return nil, errors.Wrap(err, "failed to create midstream")
}
if err := m.Write(opts); err != nil {
return nil, errors.Wrap(err, "failed to write common midstream")
}
return m, nil
}
func (m *Midstream) Write(options WriteOptions) error {
if err := os.MkdirAll(options.MidstreamDir, 0744); err != nil {
return errors.Wrap(err, "failed to mkdir")
}
existingKustomization, err := m.getExistingKustomization(options)
if err != nil {
return errors.Wrap(err, "get existing kustomization")
}
secretFilename, err := m.writePullSecret(options)
if err != nil {
return errors.Wrap(err, "failed to write secret")
}
if secretFilename != "" {
m.Kustomization.Resources = append(m.Kustomization.Resources, secretFilename)
}
identityBase, err := m.writeIdentityService(context.TODO(), options)
if err != nil {
return errors.Wrap(err, "failed to write identity service")
}
if identityBase != "" {
m.Kustomization.Resources = append(m.Kustomization.Resources, identityBase)
}
if err := m.writeObjectsWithPullSecret(options); err != nil {
return errors.Wrap(err, "failed to write patches")
}
// transformers
drLabelTransformerFilename, err := m.writeDisasterRecoveryLabelTransformer(options)
if err != nil {
return errors.Wrap(err, "failed to write disaster recovery label transformer")
}
m.Kustomization.Transformers = append(m.Kustomization.Transformers, drLabelTransformerFilename)
// annotations
if m.Kustomization.CommonAnnotations == nil {
m.Kustomization.CommonAnnotations = make(map[string]string)
}
m.Kustomization.CommonAnnotations["kots.io/app-slug"] = options.AppSlug
if existingKustomization != nil {
// Note that this function does nothing on the initial install
// if the user is not presented with the config screen.
m.mergeKustomization(options, *existingKustomization)
}
if err := m.writeKustomization(options); err != nil {
return errors.Wrap(err, "failed to write kustomization")
}
return nil
}
func (m *Midstream) getExistingKustomization(options WriteOptions) (*kustomizetypes.Kustomization, error) {
kustomizationFilename := filepath.Join(options.MidstreamDir, "kustomization.yaml")
_, err := os.Stat(kustomizationFilename)
if os.IsNotExist(err) {
return nil, nil
} else if err != nil {
return nil, errors.Wrap(err, "stat existing kustomization")
}
k, err := k8sutil.ReadKustomizationFromFile(kustomizationFilename)
if err != nil {
return nil, errors.Wrap(err, "load existing kustomization")
}
return k, nil
}
func (m *Midstream) mergeKustomization(options WriteOptions, existing kustomizetypes.Kustomization) {
existing.PatchesStrategicMerge = removeFromPatches(existing.PatchesStrategicMerge, patchesFilename)
m.Kustomization.PatchesStrategicMerge = uniquePatches(existing.PatchesStrategicMerge, m.Kustomization.PatchesStrategicMerge)
m.Kustomization.Resources = uniqueStrings(existing.Resources, m.Kustomization.Resources)
m.Kustomization.Transformers = uniqueStrings(existing.Transformers, m.Kustomization.Transformers)
// annotations
if existing.CommonAnnotations == nil {
existing.CommonAnnotations = make(map[string]string)
}
delete(existing.CommonAnnotations, "kots.io/app-sequence")
m.Kustomization.CommonAnnotations = mergeMaps(m.Kustomization.CommonAnnotations, existing.CommonAnnotations)
}
func removeFromPatches(patches []kustomizetypes.PatchStrategicMerge, filename string) []kustomizetypes.PatchStrategicMerge {
newPatches := []kustomizetypes.PatchStrategicMerge{}
for _, patch := range patches {
if string(patch) != filename {
newPatches = append(newPatches, patch)
}
}
return newPatches
}
func mergeMaps(new map[string]string, existing map[string]string) map[string]string {
merged := existing
if merged == nil {
merged = make(map[string]string)
}
for key, value := range new {
merged[key] = value
}
return merged
}
func (m *Midstream) writeKustomization(options WriteOptions) error {
relativeBaseDir, err := filepath.Rel(options.MidstreamDir, options.BaseDir)
if err != nil {
return errors.Wrap(err, "failed to determine relative path for base from midstream")
}
kustomizationFilename := filepath.Join(options.MidstreamDir, "kustomization.yaml")
m.Kustomization.Bases = []string{
relativeBaseDir,
}
if err := k8sutil.WriteKustomizationToFile(*m.Kustomization, kustomizationFilename); err != nil {
return errors.Wrap(err, "failed to write kustomization to file")
}
return nil
}
func (m *Midstream) writeDisasterRecoveryLabelTransformer(options WriteOptions) (string, error) {
additionalLabels := map[string]string{
"kots.io/app-slug": options.AppSlug,
}
drLabelTransformerYAML, err := disasterrecovery.GetLabelTransformerYAML(additionalLabels)
if err != nil {
return "", errors.Wrap(err, "failed to get disaster recovery label transformer yaml")
}
absFilename := filepath.Join(options.MidstreamDir, disasterRecoveryLabelTransformerFileName)
if err := ioutil.WriteFile(absFilename, drLabelTransformerYAML, 0644); err != nil {
return "", errors.Wrap(err, "failed to write disaster recovery label transformer yaml file")
}
return disasterRecoveryLabelTransformerFileName, nil
}
func (m *Midstream) writePullSecret(options WriteOptions) (string, error) {
var secretBytes []byte
if m.AppPullSecret != nil {
b, err := k8syaml.Marshal(m.AppPullSecret)
if err != nil {
return "", errors.Wrap(err, "failed to marshal app pull secret")
}
secretBytes = b
}
if m.AdminConsolePullSecret != nil {
if secretBytes != nil {
secretBytes = append(secretBytes, []byte("\n---\n")...)
}
b, err := k8syaml.Marshal(m.AdminConsolePullSecret)
if err != nil {
return "", errors.Wrap(err, "failed to marshal kots pull secret")
}
secretBytes = append(secretBytes, b...)
}
if m.DockerHubPullSecret != nil {
if secretBytes != nil {
secretBytes = append(secretBytes, []byte("\n---\n")...)
}
b, err := k8syaml.Marshal(m.DockerHubPullSecret)
if err != nil {
return "", errors.Wrap(err, "failed to marshal kots pull secret")
}
secretBytes = append(secretBytes, b...)
}
if secretBytes == nil {
return "", nil
}
absFilename := filepath.Join(options.MidstreamDir, secretFilename)
if err := ioutil.WriteFile(absFilename, secretBytes, 0644); err != nil {
return "", errors.Wrap(err, "failed to write pull secret file")
}
return secretFilename, nil
}
func (m *Midstream) writeObjectsWithPullSecret(options WriteOptions) error {
filename := filepath.Join(options.MidstreamDir, patchesFilename)
if len(m.DocForPatches) == 0 {
err := os.Remove(filename)
if err != nil && !os.IsNotExist(err) {
return errors.Wrap(err, "failed to delete pull secret patches")
}
return nil
}
f, err := os.Create(filename)
if err != nil {
return errors.Wrap(err, "failed to create resources file")
}
defer f.Close()
secrets := []*corev1.Secret{}
if m.AppPullSecret != nil {
secrets = append(secrets, m.AppPullSecret)
}
if m.DockerHubPullSecret != nil {
secrets = append(secrets, m.DockerHubPullSecret)
}
for _, o := range m.DocForPatches {
for _, secret := range secrets {
withPullSecret := o.PatchWithPullSecret(secret)
if withPullSecret == nil {
continue
}
b, err := yaml.Marshal(withPullSecret)
if err != nil {
return errors.Wrap(err, "failed to marshal object")
}
if _, err := f.Write([]byte("---\n")); err != nil {
return errors.Wrap(err, "failed to write doc separator")
}
if _, err := f.Write(b); err != nil {
return errors.Wrap(err, "failed to write object")
}
}
}
m.Kustomization.PatchesStrategicMerge = append(m.Kustomization.PatchesStrategicMerge, patchesFilename)
return nil
}
func EnsureDisasterRecoveryLabelTransformer(archiveDir string, additionalLabels map[string]string) error {
labelTransformerExists := false
dirPath := filepath.Join(archiveDir, "overlays", "midstream")
// TODO (ch35027): this will not work with multiple kustomization files
k, err := k8sutil.ReadKustomizationFromFile(filepath.Join(dirPath, "kustomization.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read kustomization file from midstream")
}
for _, transformer := range k.Transformers {
if transformer == disasterRecoveryLabelTransformerFileName {
labelTransformerExists = true
break
}
}
if !labelTransformerExists {
drLabelTransformerYAML, err := disasterrecovery.GetLabelTransformerYAML(additionalLabels)
if err != nil {
return errors.Wrap(err, "failed to get disaster recovery label transformer yaml")
}
absFilename := filepath.Join(dirPath, disasterRecoveryLabelTransformerFileName)
if err := ioutil.WriteFile(absFilename, drLabelTransformerYAML, 0644); err != nil {
return errors.Wrap(err, "failed to write disaster recovery label transformer yaml file")
}
k.Transformers = append(k.Transformers, disasterRecoveryLabelTransformerFileName)
if err := k8sutil.WriteKustomizationToFile(*k, filepath.Join(dirPath, "kustomization.yaml")); err != nil {
return errors.Wrap(err, "failed to write kustomization file to midstream")
}
}
return nil
}