-
Notifications
You must be signed in to change notification settings - Fork 90
/
replicated.go
863 lines (716 loc) · 24.4 KB
/
replicated.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
package upstream
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/crypto"
kotslicense "github.com/replicatedhq/kots/pkg/license"
"github.com/replicatedhq/kots/pkg/template"
"github.com/replicatedhq/kots/pkg/upstream/types"
"github.com/replicatedhq/kots/pkg/util"
"github.com/replicatedhq/kots/pkg/version"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
serializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes/scheme"
)
const DefaultMetadata = `apiVersion: kots.io/v1beta1
kind: Application
metadata:
name: "default-application"
spec:
title: "the application"
icon: https://cdn2.iconfinder.com/data/icons/mixd/512/16_kubernetes-512.png
releaseNotes: |
release notes`
type ReplicatedUpstream struct {
Channel *string
AppSlug string
VersionLabel *string
Sequence *int
}
type ReplicatedCursor struct {
ChannelID string
ChannelName string
Cursor string
}
type App struct {
Name string
}
type Release struct {
UpdateCursor ReplicatedCursor
VersionLabel string
ReleaseNotes string
ReleasedAt *time.Time
Manifests map[string][]byte
}
type ChannelRelease struct {
ChannelSequence int `json:"channelSequence"`
ReleaseSequence int `json:"releaseSequence"`
VersionLabel string `json:"versionLabel"`
}
func (this ReplicatedCursor) Equal(other ReplicatedCursor) bool {
if this.ChannelID != "" && other.ChannelID != "" {
return this.ChannelID == other.ChannelID && this.Cursor == other.Cursor
}
return this.ChannelName == other.ChannelName && this.Cursor == other.Cursor
}
func getUpdatesReplicated(u *url.URL, localPath string, currentCursor ReplicatedCursor, currentVersionLabel string, license *kotsv1beta1.License, reportingInfo *types.ReportingInfo) ([]Update, error) {
if localPath != "" {
parsedLocalRelease, err := readReplicatedAppFromLocalPath(localPath, currentCursor, currentVersionLabel)
if err != nil {
return nil, errors.Wrap(err, "failed to read replicated app from local path")
}
return []Update{{Cursor: parsedLocalRelease.UpdateCursor.Cursor, VersionLabel: currentVersionLabel}}, nil
}
// A license file is required to be set for this to succeed
if license == nil {
return nil, errors.New("No license was provided")
}
replicatedUpstream, err := parseReplicatedURL(u)
if err != nil {
return nil, errors.Wrap(err, "failed to parse replicated upstream")
}
remoteLicense, err := getSuccessfulHeadResponse(replicatedUpstream, license)
if err != nil {
return nil, errors.Wrap(err, "failed to get successful head response")
}
pendingReleases, err := listPendingChannelReleases(replicatedUpstream, remoteLicense, currentCursor, reportingInfo)
if err != nil {
return nil, errors.Wrap(err, "failed to list replicated app releases")
}
updates := []Update{}
for _, pendingRelease := range pendingReleases {
updates = append(updates, Update{
Cursor: strconv.Itoa(pendingRelease.ChannelSequence),
VersionLabel: pendingRelease.VersionLabel,
})
}
return updates, nil
}
func downloadReplicated(
u *url.URL,
localPath string,
rootDir string,
useAppDir bool,
license *kotsv1beta1.License,
existingConfigValues *kotsv1beta1.ConfigValues,
updateCursor ReplicatedCursor,
versionLabel string,
cipher *crypto.AESCipher,
appSequence int64,
isAirgap bool,
registry types.LocalRegistry,
reportingInfo *types.ReportingInfo,
) (*types.Upstream, error) {
var release *Release
if localPath != "" {
parsedLocalRelease, err := readReplicatedAppFromLocalPath(localPath, updateCursor, versionLabel)
if err != nil {
return nil, errors.Wrap(err, "failed to read replicated app from local path")
}
release = parsedLocalRelease
} else {
// A license file is required to be set for this to succeed
if license == nil {
return nil, errors.New("No license was provided")
}
replicatedUpstream, err := parseReplicatedURL(u)
if err != nil {
return nil, errors.Wrap(err, "failed to parse replicated upstream")
}
remoteLicense, err := getSuccessfulHeadResponse(replicatedUpstream, license)
if err != nil {
return nil, errors.Wrap(err, "failed to get successful head response")
}
downloadedRelease, err := downloadReplicatedApp(replicatedUpstream, remoteLicense, updateCursor, reportingInfo)
if err != nil {
return nil, errors.Wrap(err, "failed to download replicated app")
}
license, err = kotslicense.GetLatestLicense(license)
if err != nil {
return nil, errors.Wrap(err, "failed to get latest license")
}
release = downloadedRelease
}
// Find the config in the upstream and write out default values
application := findAppInRelease(release) // this function never returns nil
// NOTE: this currently comes from the application spec and not the channel release meta
if release.ReleaseNotes == "" {
release.ReleaseNotes = application.Spec.ReleaseNotes
}
// get channel name from license, if one was provided
channelID, channelName := "", ""
if license != nil {
channelID = license.Spec.ChannelID
channelName = license.Spec.ChannelName
}
if existingConfigValues == nil {
var prevConfigFile string
if useAppDir {
prevConfigFile = filepath.Join(rootDir, application.Name, "upstream", "userdata", "config.yaml")
} else {
prevConfigFile = filepath.Join(rootDir, "upstream", "userdata", "config.yaml")
}
var err error
existingConfigValues, err = findConfigValuesInFile(prevConfigFile)
if err != nil {
return nil, errors.Wrap(err, "failed to load existing config values")
}
}
config, _, _, _, err := findTemplateContextDataInRelease(release)
if err != nil {
return nil, errors.Wrap(err, "failed to find config in release")
}
if config != nil || existingConfigValues != nil {
versionInfo := template.VersionInfo{
Sequence: appSequence,
Cursor: updateCursor.Cursor,
ChannelName: channelName,
VersionLabel: release.VersionLabel,
ReleaseNotes: release.ReleaseNotes,
IsAirgap: isAirgap,
}
localRegistry := template.LocalRegistry{
Host: registry.Host,
Namespace: registry.Namespace,
Username: registry.Username,
Password: registry.Password,
}
// If config existed and was removed from the app,
// values will be carried over to the new version anyway.
configValues, err := createConfigValues(application.Name, config, existingConfigValues, cipher, license, &versionInfo, localRegistry)
if err != nil {
return nil, errors.Wrap(err, "failed to create empty config values")
}
release.Manifests["userdata/config.yaml"] = mustMarshalConfigValues(configValues)
}
// Add the license to the upstream, if one was provided
if license != nil {
release.Manifests["userdata/license.yaml"] = MustMarshalLicense(license)
}
files, err := releaseToFiles(release)
if err != nil {
return nil, errors.Wrap(err, "failed to get files from release")
}
upstream := &types.Upstream{
URI: u.RequestURI(),
Name: application.Name,
Files: files,
Type: "replicated",
UpdateCursor: release.UpdateCursor.Cursor,
ChannelID: channelID,
ChannelName: channelName,
VersionLabel: release.VersionLabel,
ReleaseNotes: release.ReleaseNotes,
ReleasedAt: release.ReleasedAt,
EncryptionKey: cipher.ToString(),
}
return upstream, nil
}
func (r *ReplicatedUpstream) getRequest(method string, license *kotsv1beta1.License, cursor ReplicatedCursor) (*http.Request, error) {
u, err := url.Parse(license.Spec.Endpoint)
if err != nil {
return nil, errors.Wrap(err, "failed to parse endpoint from license")
}
hostname := u.Hostname()
if u.Port() != "" {
hostname = fmt.Sprintf("%s:%s", u.Hostname(), u.Port())
}
urlPath := path.Join(hostname, "release", license.Spec.AppSlug)
if r.Channel != nil {
urlPath = path.Join(urlPath, *r.Channel)
}
urlValues := url.Values{}
urlValues.Set("channelSequence", cursor.Cursor)
urlValues.Add("licenseSequence", fmt.Sprintf("%d", license.Spec.LicenseSequence))
url := fmt.Sprintf("%s://%s?%s", u.Scheme, urlPath, urlValues.Encode())
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to call newrequest")
}
req.Header.Add("User-Agent", fmt.Sprintf("KOTS/%s", version.Version()))
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", license.Spec.LicenseID, license.Spec.LicenseID)))))
return req, nil
}
func parseReplicatedURL(u *url.URL) (*ReplicatedUpstream, error) {
replicatedUpstream := ReplicatedUpstream{}
if u.User != nil {
if u.User.Username() != "" {
replicatedUpstream.AppSlug = u.User.Username()
versionLabel := u.Hostname()
replicatedUpstream.VersionLabel = &versionLabel
}
}
if replicatedUpstream.AppSlug == "" {
replicatedUpstream.AppSlug = u.Hostname()
if u.Path != "" {
channel := strings.TrimPrefix(u.Path, "/")
replicatedUpstream.Channel = &channel
}
}
return &replicatedUpstream, nil
}
func getSuccessfulHeadResponse(replicatedUpstream *ReplicatedUpstream, license *kotsv1beta1.License) (*kotsv1beta1.License, error) {
headReq, err := replicatedUpstream.getRequest("HEAD", license, ReplicatedCursor{})
if err != nil {
return nil, errors.Wrap(err, "failed to create http request")
}
headResp, err := http.DefaultClient.Do(headReq)
if err != nil {
return nil, errors.Wrap(err, "failed to execute head request")
}
defer headResp.Body.Close()
if headResp.StatusCode == 401 {
return nil, errors.New("license was not accepted")
}
if headResp.StatusCode == 403 {
return nil, util.ActionableError{Message: "License is expired"}
}
if headResp.StatusCode >= 400 {
return nil, errors.Errorf("unexpected result from head request: %d", headResp.StatusCode)
}
return license, nil
}
func readReplicatedAppFromLocalPath(localPath string, localCursor ReplicatedCursor, versionLabel string) (*Release, error) {
release := Release{
Manifests: make(map[string][]byte),
UpdateCursor: localCursor,
VersionLabel: versionLabel,
}
err := filepath.Walk(localPath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
contents, err := ioutil.ReadFile(path)
if err != nil {
return err
}
// remove localpath prefix
appPath := strings.TrimPrefix(path, localPath)
appPath = strings.TrimLeft(appPath, string(os.PathSeparator))
release.Manifests[appPath] = contents
return nil
})
if err != nil {
return nil, errors.Wrap(err, "failed to walk local path")
}
return &release, nil
}
func downloadReplicatedApp(replicatedUpstream *ReplicatedUpstream, license *kotsv1beta1.License, cursor ReplicatedCursor, reportingInfo *types.ReportingInfo) (*Release, error) {
getReq, err := replicatedUpstream.getRequest("GET", license, cursor)
if err != nil {
return nil, errors.Wrap(err, "failed to create http request")
}
injectReportingInfo(getReq, reportingInfo)
getResp, err := http.DefaultClient.Do(getReq)
if err != nil {
return nil, errors.Wrap(err, "failed to execute get request")
}
defer getResp.Body.Close()
if getResp.StatusCode >= 300 {
body, _ := ioutil.ReadAll(getResp.Body)
if len(body) > 0 {
return nil, util.ActionableError{Message: string(body)}
}
return nil, errors.Errorf("unexpected result from get request: %d", getResp.StatusCode)
}
updateSequence := getResp.Header.Get("X-Replicated-ChannelSequence")
updateChannelID := getResp.Header.Get("X-Replicated-ChannelID")
updateChannelName := getResp.Header.Get("X-Replicated-ChannelName")
versionLabel := getResp.Header.Get("X-Replicated-VersionLabel")
releasedAtStr := getResp.Header.Get("X-Replicated-ReleasedAt")
var releasedAt *time.Time
r, err := time.Parse(time.RFC3339, releasedAtStr)
if err == nil {
releasedAt = &r
}
gzf, err := gzip.NewReader(getResp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to create new gzip reader")
}
release := Release{
Manifests: make(map[string][]byte),
UpdateCursor: ReplicatedCursor{
ChannelID: updateChannelID,
ChannelName: updateChannelName,
Cursor: updateSequence,
},
VersionLabel: versionLabel,
ReleasedAt: releasedAt,
// NOTE: release notes come from Application spec
}
tarReader := tar.NewReader(gzf)
i := 0
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, errors.Wrap(err, "failed to get next file from reader")
}
name := header.Name
switch header.Typeflag {
case tar.TypeDir:
continue
case tar.TypeReg:
content, err := ioutil.ReadAll(tarReader)
if err != nil {
return nil, errors.Wrap(err, "failed to read file from tar")
}
release.Manifests[name] = content
}
i++
}
return &release, nil
}
func listPendingChannelReleases(replicatedUpstream *ReplicatedUpstream, license *kotsv1beta1.License, currentCursor ReplicatedCursor, reportingInfo *types.ReportingInfo) ([]ChannelRelease, error) {
u, err := url.Parse(license.Spec.Endpoint)
if err != nil {
return nil, errors.Wrap(err, "failed to parse endpoint from license")
}
hostname := u.Hostname()
if u.Port() != "" {
hostname = fmt.Sprintf("%s:%s", u.Hostname(), u.Port())
}
sequence := currentCursor.Cursor
if license.Spec.ChannelID != "" && currentCursor.ChannelID != "" && license.Spec.ChannelID != currentCursor.ChannelID {
sequence = ""
} else if license.Spec.ChannelName != currentCursor.ChannelName {
sequence = ""
}
urlValues := url.Values{}
urlValues.Set("channelSequence", sequence)
urlValues.Add("licenseSequence", fmt.Sprintf("%d", license.Spec.LicenseSequence))
url := fmt.Sprintf("%s://%s/release/%s/pending?%s", u.Scheme, hostname, license.Spec.AppSlug, urlValues.Encode())
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to call newrequest")
}
injectReportingInfo(req, reportingInfo)
req.Header.Add("User-Agent", fmt.Sprintf("KOTS/%s", version.Version()))
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", license.Spec.LicenseID, license.Spec.LicenseID)))))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to execute get request")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read response body")
}
if resp.StatusCode >= 400 {
if len(body) > 0 {
return nil, util.ActionableError{Message: string(body)}
}
return nil, errors.Errorf("unexpected result from get request: %d", resp.StatusCode)
}
var channelReleases struct {
ChannelReleases []ChannelRelease `json:"channelReleases"`
}
if err := json.Unmarshal(body, &channelReleases); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal response")
}
return channelReleases.ChannelReleases, nil
}
func injectReportingInfo(req *http.Request, reportingInfo *types.ReportingInfo) {
if reportingInfo == nil {
return
}
req.Header.Set("X-Replicated-K8sVersion", reportingInfo.K8sVersion)
req.Header.Set("X-Replicated-IsKurl", fmt.Sprintf("%t", reportingInfo.IsKurl))
req.Header.Set("X-Replicated-AppStatus", reportingInfo.AppStatus)
req.Header.Set("X-Replicated-ClusterID", reportingInfo.ClusterID)
req.Header.Set("X-Replicated-InstanceID", reportingInfo.InstanceID)
if reportingInfo.DownstreamCursor != "" {
req.Header.Set("X-Replicated-DownstreamChannelSequence", reportingInfo.DownstreamCursor)
}
if reportingInfo.DownstreamChannelID != "" {
req.Header.Set("X-Replicated-DownstreamChannelID", reportingInfo.DownstreamChannelID)
} else if reportingInfo.DownstreamChannelName != "" {
req.Header.Set("X-Replicated-DownstreamChannelName", reportingInfo.DownstreamChannelName)
}
}
func MustMarshalLicense(license *kotsv1beta1.License) []byte {
s := serializer.NewYAMLSerializer(serializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var b bytes.Buffer
if err := s.Encode(license, &b); err != nil {
panic(err)
}
return b.Bytes()
}
func mustMarshalConfigValues(configValues *kotsv1beta1.ConfigValues) []byte {
s := serializer.NewYAMLSerializer(serializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var b bytes.Buffer
if err := s.Encode(configValues, &b); err != nil {
panic(err)
}
return b.Bytes()
}
func createConfigValues(applicationName string, config *kotsv1beta1.Config, existingConfigValues *kotsv1beta1.ConfigValues, cipher *crypto.AESCipher, license *kotsv1beta1.License, versionInfo *template.VersionInfo, localRegistry template.LocalRegistry) (*kotsv1beta1.ConfigValues, error) {
templateContextValues := make(map[string]template.ItemValue)
var newValues kotsv1beta1.ConfigValuesSpec
if existingConfigValues != nil {
for k, v := range existingConfigValues.Spec.Values {
value := v.Value
if value == "" {
value = v.ValuePlaintext
}
templateContextValues[k] = template.ItemValue{
Value: value,
Default: v.Default,
}
}
newValues = kotsv1beta1.ConfigValuesSpec{
Values: existingConfigValues.Spec.Values,
}
} else {
newValues = kotsv1beta1.ConfigValuesSpec{
Values: map[string]kotsv1beta1.ConfigValue{},
}
}
if config == nil {
return &kotsv1beta1.ConfigValues{
TypeMeta: metav1.TypeMeta{
APIVersion: "kots.io/v1beta1",
Kind: "ConfigValues",
},
ObjectMeta: metav1.ObjectMeta{
Name: applicationName,
},
Spec: newValues,
}, nil
}
builder, _, err := template.NewBuilder(config.Spec.Groups, templateContextValues, localRegistry, cipher, license, versionInfo)
if err != nil {
return nil, errors.Wrap(err, "failed to create config context")
}
for _, group := range config.Spec.Groups {
for _, item := range group.Items {
var foundValue, foundValuePlaintext string
prevValue, ok := newValues.Values[item.Name]
if ok {
foundValue = prevValue.Value
foundValuePlaintext = prevValue.ValuePlaintext
}
renderedValue, err := builder.RenderTemplate(item.Name, item.Value.String())
if err != nil {
return nil, errors.Wrap(err, "failed to render config item value")
}
renderedDefault, err := builder.RenderTemplate(item.Name, item.Default.String())
if err != nil {
return nil, errors.Wrap(err, "failed to render config item default")
}
if foundValue != "" || foundValuePlaintext != "" {
newValues.Values[item.Name] = kotsv1beta1.ConfigValue{
Value: foundValue,
ValuePlaintext: foundValuePlaintext,
Default: renderedDefault,
}
} else {
newValues.Values[item.Name] = kotsv1beta1.ConfigValue{
Value: renderedValue,
Default: renderedDefault,
}
}
}
}
configValues := kotsv1beta1.ConfigValues{
TypeMeta: metav1.TypeMeta{
APIVersion: "kots.io/v1beta1",
Kind: "ConfigValues",
},
ObjectMeta: metav1.ObjectMeta{
Name: applicationName,
},
Spec: newValues,
}
return &configValues, nil
}
func findConfigValuesInFile(filename string) (*kotsv1beta1.ConfigValues, error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, errors.Wrap(err, "failed to open file")
}
return contentToConfigValues(content), nil
}
func contentToConfigValues(content []byte) *kotsv1beta1.ConfigValues {
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 == "ConfigValues" {
return obj.(*kotsv1beta1.ConfigValues)
}
return nil
}
func findTemplateContextDataInRelease(release *Release) (*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
for _, content := range release.Manifests {
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, gvk, err := decode(content, nil, nil)
if err != nil {
continue
}
if gvk.Group == "kots.io" {
if gvk.Version == "v1beta1" {
if gvk.Kind == "Config" {
config = obj.(*kotsv1beta1.Config)
} else if gvk.Kind == "ConfigValues" {
values = obj.(*kotsv1beta1.ConfigValues)
} else if gvk.Kind == "License" {
license = obj.(*kotsv1beta1.License)
} else if gvk.Kind == "Installation" {
installation = obj.(*kotsv1beta1.Installation)
}
}
}
}
return config, values, license, installation, nil
}
func findAppInRelease(release *Release) *kotsv1beta1.Application {
for _, content := range release.Manifests {
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, gvk, err := decode(content, nil, nil)
if err != nil {
continue
}
if gvk.Group == "kots.io" {
if gvk.Version == "v1beta1" {
if gvk.Kind == "Application" {
return obj.(*kotsv1beta1.Application)
}
}
}
}
// Using Ship apps for now, so let's create an app manifest on the fly
app := &kotsv1beta1.Application{
TypeMeta: metav1.TypeMeta{
APIVersion: "kots.io/v1beta1",
Kind: "Application",
},
ObjectMeta: metav1.ObjectMeta{
Name: "replicated-kots-app",
},
Spec: kotsv1beta1.ApplicationSpec{
Title: "Replicated Kots App",
Icon: "",
},
}
return app
}
func releaseToFiles(release *Release) ([]types.UpstreamFile, error) {
upstreamFiles := []types.UpstreamFile{}
for filename, content := range release.Manifests {
upstreamFile := types.UpstreamFile{
Path: filename,
Content: content,
}
upstreamFiles = append(upstreamFiles, upstreamFile)
}
// Stash the user data for this search (we will readd at the end)
userdataFiles := []types.UpstreamFile{}
withoutUserdataFiles := []types.UpstreamFile{}
for _, file := range upstreamFiles {
d, _ := path.Split(file.Path)
dirs := strings.Split(d, string(os.PathSeparator))
if dirs[0] == "userdata" {
userdataFiles = append(userdataFiles, file)
} else {
withoutUserdataFiles = append(withoutUserdataFiles, file)
}
}
// remove any common prefix from all files
if len(withoutUserdataFiles) > 0 {
firstFileDir, _ := path.Split(withoutUserdataFiles[0].Path)
commonPrefix := strings.Split(firstFileDir, string(os.PathSeparator))
for _, file := range withoutUserdataFiles {
d, _ := path.Split(file.Path)
dirs := strings.Split(d, string(os.PathSeparator))
commonPrefix = util.CommonSlicePrefix(commonPrefix, dirs)
}
cleanedUpstreamFiles := []types.UpstreamFile{}
for _, file := range withoutUserdataFiles {
d, f := path.Split(file.Path)
d2 := strings.Split(d, string(os.PathSeparator))
cleanedUpstreamFile := file
d2 = d2[len(commonPrefix):]
cleanedUpstreamFile.Path = path.Join(path.Join(d2...), f)
cleanedUpstreamFiles = append(cleanedUpstreamFiles, cleanedUpstreamFile)
}
upstreamFiles = cleanedUpstreamFiles
}
upstreamFiles = append(upstreamFiles, userdataFiles...)
return upstreamFiles, nil
}
// GetApplicationMetadata will return any available application yaml from
// the upstream. If there is no application.yaml, it will return
// a placeholder one
func GetApplicationMetadata(upstream *url.URL) ([]byte, error) {
metadata, err := getApplicationMetadataFromHost("replicated.app", upstream)
if err != nil {
return nil, errors.Wrap(err, "failed to get metadata from replicated.app")
}
if metadata == nil {
metadata = []byte(DefaultMetadata)
}
return metadata, nil
}
func getApplicationMetadataFromHost(host string, upstream *url.URL) ([]byte, error) {
r, err := parseReplicatedURL(upstream)
if err != nil {
return nil, errors.Wrap(err, "failed to parse replicated upstream")
}
url := fmt.Sprintf("https://%s/metadata/%s", host, r.AppSlug)
if r.Channel != nil {
url = fmt.Sprintf("%s/%s", url, *r.Channel)
}
getReq, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to call newrequest")
}
getReq.Header.Add("User-Agent", fmt.Sprintf("KOTS/%s", version.Version()))
getResp, err := http.DefaultClient.Do(getReq)
if err != nil {
return nil, errors.Wrap(err, "failed to execute get request")
}
defer getResp.Body.Close()
if getResp.StatusCode == 404 {
// no metadata is not an error
return nil, nil
}
if getResp.StatusCode >= 400 {
return nil, errors.Errorf("unexpected result from get request: %d", getResp.StatusCode)
}
respBody, err := ioutil.ReadAll(getResp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read response body")
}
return respBody, nil
}