-
Notifications
You must be signed in to change notification settings - Fork 88
/
version_store.go
627 lines (533 loc) · 20.5 KB
/
version_store.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
package s3pg
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/aws/aws-sdk-go/aws"
awssession "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/mholt/archiver"
"github.com/pkg/errors"
apptypes "github.com/replicatedhq/kots/kotsadm/pkg/app/types"
kotsconfig "github.com/replicatedhq/kots/kotsadm/pkg/config"
gitopstypes "github.com/replicatedhq/kots/kotsadm/pkg/gitops/types"
"github.com/replicatedhq/kots/kotsadm/pkg/persistence"
rendertypes "github.com/replicatedhq/kots/kotsadm/pkg/render/types"
kotss3 "github.com/replicatedhq/kots/kotsadm/pkg/s3"
"github.com/replicatedhq/kots/kotsadm/pkg/secrets"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
versiontypes "github.com/replicatedhq/kots/pkg/api/version/types"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/kustomize"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"k8s.io/client-go/kubernetes/scheme"
)
func (s S3PGStore) IsRollbackSupportedForVersion(appID string, sequence int64) (bool, error) {
db := persistence.MustGetPGSession()
query := `select kots_app_spec from app_version where app_id = $1 and sequence = $2`
row := db.QueryRow(query, appID, sequence)
var kotsAppSpecStr sql.NullString
if err := row.Scan(&kotsAppSpecStr); err != nil {
if err == sql.ErrNoRows {
return false, nil
}
return false, errors.Wrap(err, "failed to scan")
}
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(kotsAppSpecStr.String), nil, nil)
if err != nil {
return false, errors.Wrap(err, "failed to decode kots app spec yaml")
}
kotsAppSpec := obj.(*kotsv1beta1.Application)
return kotsAppSpec.Spec.AllowRollback, nil
}
func (s S3PGStore) IsIdentityServiceSupportedForVersion(appID string, sequence int64) (bool, error) {
db := persistence.MustGetPGSession()
query := `select identity_spec from app_version where app_id = $1 and sequence = $2`
row := db.QueryRow(query, appID, sequence)
var identitySpecStr sql.NullString
if err := row.Scan(&identitySpecStr); err != nil {
if err == sql.ErrNoRows {
return false, nil
}
return false, errors.Wrap(err, "failed to scan")
}
return identitySpecStr.String != "", nil
}
func (s S3PGStore) IsSnapshotsSupportedForVersion(a *apptypes.App, sequence int64, renderer rendertypes.Renderer) (bool, error) {
db := persistence.MustGetPGSession()
query := `select backup_spec from app_version where app_id = $1 and sequence = $2`
row := db.QueryRow(query, a.ID, sequence)
var backupSpecStr sql.NullString
if err := row.Scan(&backupSpecStr); err != nil {
if err == sql.ErrNoRows {
return false, nil
}
return false, errors.Wrap(err, "failed to scan")
}
if backupSpecStr.String == "" {
return false, nil
}
archiveDir, err := ioutil.TempDir("", "kotsadm")
if err != nil {
return false, errors.Wrap(err, "failed to create temp dir")
}
defer os.RemoveAll(archiveDir)
err = s.GetAppVersionArchive(a.ID, sequence, archiveDir)
if err != nil {
return false, errors.Wrap(err, "failed to get app version archive")
}
kotsKinds, err := kotsutil.LoadKotsKindsFromPath(archiveDir)
if err != nil {
return false, errors.Wrap(err, "failed to load kots kinds from path")
}
registrySettings, err := s.GetRegistryDetailsForApp(a.ID)
if err != nil {
return false, errors.Wrap(err, "failed to get registry settings for app")
}
// as far as I can tell, this is the only place within kotsadm/pkg/store that uses templating
rendered, err := renderer.RenderFile(kotsKinds, registrySettings, a.Slug, sequence, a.IsAirgap, []byte(backupSpecStr.String))
if err != nil {
return false, errors.Wrap(err, "failed to render backup spec")
}
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode(rendered, nil, nil)
if err != nil {
return false, errors.Wrap(err, "failed to decode rendered backup spec yaml")
}
backupSpec := obj.(*velerov1.Backup)
annotations := backupSpec.ObjectMeta.Annotations
if annotations == nil {
// Backup exists and there are no annotation overrides so snapshots are enabled
return true, nil
}
if exclude, ok := annotations["kots.io/exclude"]; ok && exclude == "true" {
return false, nil
}
if when, ok := annotations["kots.io/when"]; ok && when == "false" {
return false, nil
}
return true, nil
}
// CreateAppVersion takes an unarchived app, makes an archive and then uploads it
// to s3 with the appID and sequence specified
func (s S3PGStore) CreateAppVersionArchive(appID string, sequence int64, archivePath string) error {
paths := []string{
filepath.Join(archivePath, "upstream"),
filepath.Join(archivePath, "base"),
filepath.Join(archivePath, "overlays"),
}
skippedFilesPath := filepath.Join(archivePath, "skippedFiles")
if _, err := os.Stat(skippedFilesPath); err == nil {
paths = append(paths, skippedFilesPath)
}
tmpDir, err := ioutil.TempDir("", "kotsadm")
if err != nil {
return errors.Wrap(err, "failed to create temp file")
}
defer os.RemoveAll(tmpDir)
fileToUpload := filepath.Join(tmpDir, "archive.tar.gz")
tarGz := archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: false,
},
}
if err := tarGz.Archive(paths, fileToUpload); err != nil {
return errors.Wrap(err, "failed to create archive")
}
storageBaseURI := os.Getenv("STORAGE_BASEURI")
if storageBaseURI == "" {
// KOTS 1.15 and earlier only supported s3 and there was no configuration
storageBaseURI = fmt.Sprintf("s3://%s/%s", os.Getenv("S3_ENDPOINT"), os.Getenv("S3_BUCKET_NAME"))
}
bucket := aws.String(os.Getenv("S3_BUCKET_NAME"))
key := aws.String(fmt.Sprintf("%s/%d.tar.gz", appID, sequence))
newSession := awssession.New(kotss3.GetConfig())
s3Client := s3.New(newSession)
f, err := os.Open(fileToUpload)
if err != nil {
return errors.Wrap(err, "failed to open archive file")
}
_, err = s3Client.PutObject(&s3.PutObjectInput{
Body: f,
Bucket: bucket,
Key: key,
})
if err != nil {
return errors.Wrap(err, "failed to upload to s3")
}
return nil
}
// GetAppVersionArchive will fetch the archive and return a string that contains a
// directory name where it's extracted into
func (s S3PGStore) GetAppVersionArchive(appID string, sequence int64, dstPath string) error {
// too noisy
// logger.Debug("getting app version archive",
// zap.String("appID", appID),
// zap.Int64("sequence", sequence))
storageBaseURI := os.Getenv("STORAGE_BASEURI")
if storageBaseURI == "" {
// KOTS 1.15 and earlier only supported s3 and there was no configuration
storageBaseURI = fmt.Sprintf("s3://%s/%s", os.Getenv("S3_ENDPOINT"), os.Getenv("S3_BUCKET_NAME"))
}
// Get the archive from object store
newSession := awssession.New(kotss3.GetConfig())
bucket := aws.String(os.Getenv("S3_BUCKET_NAME"))
key := aws.String(fmt.Sprintf("%s/%d.tar.gz", appID, sequence))
tmpFile, err := ioutil.TempFile("", "kotsadm")
if err != nil {
return errors.Wrap(err, "failed to create temp file")
}
defer tmpFile.Close()
defer os.RemoveAll(tmpFile.Name())
downloader := s3manager.NewDownloader(newSession)
_, err = downloader.Download(tmpFile,
&s3.GetObjectInput{
Bucket: bucket,
Key: key,
})
if err != nil {
return errors.Wrapf(err, "failed to download app version archive %q from bucket %q", *key, *bucket)
}
tarGz := archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: false,
},
}
if err := tarGz.Unarchive(tmpFile.Name(), dstPath); err != nil {
return errors.Wrap(err, "failed to unarchive")
}
return nil
}
func (s S3PGStore) CreateAppVersion(appID string, currentSequence *int64, filesInDir string, source string, skipPreflights bool, gitops gitopstypes.DownstreamGitOps) (int64, error) {
db := persistence.MustGetPGSession()
tx, err := db.Begin()
if err != nil {
return 0, errors.Wrap(err, "failed to begin")
}
defer tx.Rollback()
newSequence, err := s.createAppVersion(tx, appID, currentSequence, filesInDir, source, skipPreflights, gitops)
if err != nil {
return 0, err
}
if err := tx.Commit(); err != nil {
return 0, errors.Wrap(err, "failed to commit")
}
return newSequence, nil
}
func (s S3PGStore) createAppVersion(tx *sql.Tx, appID string, currentSequence *int64, filesInDir string, source string, skipPreflights bool, gitops gitopstypes.DownstreamGitOps) (int64, error) {
kotsKinds, err := kotsutil.LoadKotsKindsFromPath(filesInDir)
if err != nil {
return int64(0), errors.Wrap(err, "failed to read kots kinds")
}
appName := kotsKinds.KotsApplication.Spec.Title
if appName == "" {
a, err := s.GetApp(appID)
if err != nil {
return int64(0), errors.Wrap(err, "failed to get app")
}
appName = a.Name
}
appIcon := kotsKinds.KotsApplication.Spec.Icon
if err := secrets.ReplaceSecretsInPath(filesInDir); err != nil {
return int64(0), errors.Wrap(err, "failed to replace secrets")
}
newSequence, err := s.createAppVersionRecord(tx, appID, currentSequence, appName, appIcon, kotsKinds)
if err != nil {
return int64(0), errors.Wrap(err, "failed to create app version")
}
if err := s.CreateAppVersionArchive(appID, int64(newSequence), filesInDir); err != nil {
return int64(0), errors.Wrap(err, "failed to create app version archive")
}
previousArchiveDir := ""
if currentSequence != nil {
previousDir, err := ioutil.TempDir("", "kotsadm")
if err != nil {
return int64(0), errors.Wrap(err, "failed to create temp dir")
}
defer os.RemoveAll(previousDir)
// Get the previous archive, we need this to calculate the diff
err = s.GetAppVersionArchive(appID, *currentSequence, previousDir)
if err != nil {
return int64(0), errors.Wrap(err, "failed to get previous archive")
}
previousArchiveDir = previousDir
}
registryInfo, err := s.GetRegistryDetailsForApp(appID)
if err != nil {
return int64(0), errors.Wrap(err, "failed to get app registry info")
}
downstreams, err := s.ListDownstreamsForApp(appID)
if err != nil {
return int64(0), errors.Wrap(err, "failed to list downstreams")
}
for _, d := range downstreams {
// there's a small chance this is not optimal, but no current code path
// will support multiple downstreams, so this is cleaner here for now
licenseSpec, err := kotsKinds.Marshal("kots.io", "v1beta1", "License")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal license spec")
}
configSpec, err := kotsKinds.Marshal("kots.io", "v1beta1", "Config")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal config spec")
}
configValuesSpec, err := kotsKinds.Marshal("kots.io", "v1beta1", "ConfigValues")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal configvalues spec")
}
identityConfigSpec, err := kotsKinds.Marshal("kots.io", "v1beta1", "IdentityConfig")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal identityconfig spec")
}
configOpts := kotsconfig.ConfigOptions{
ConfigSpec: configSpec,
ConfigValuesSpec: configValuesSpec,
LicenseSpec: licenseSpec,
IdentityConfigSpec: identityConfigSpec,
}
if registryInfo != nil {
configOpts.RegistryHost = registryInfo.Hostname
configOpts.RegistryNamespace = registryInfo.Namespace
configOpts.RegistryUser = registryInfo.Username
configOpts.RegistryPassword = registryInfo.Password
}
downstreamStatus := "pending"
if currentSequence == nil && kotsKinds.Config != nil {
downstreamStatus = "pending_config"
} else if kotsKinds.Preflight != nil && !skipPreflights {
downstreamStatus = "pending_preflight"
}
diffSummary, diffSummaryError := "", ""
if currentSequence != nil {
// diff this release from the last release
diff, err := kustomize.DiffAppVersionsForDownstream(d.Name, filesInDir, previousArchiveDir, kotsKinds.KustomizeVersion())
if err != nil {
diffSummaryError = errors.Wrap(err, "failed to diff").Error()
} else {
b, err := json.Marshal(diff)
if err != nil {
diffSummaryError = errors.Wrap(err, "failed to marshal diff").Error()
}
diffSummary = string(b)
}
}
commitURL, err := gitops.CreateGitOpsDownstreamCommit(appID, d.ClusterID, int(newSequence), filesInDir, d.Name)
if err != nil {
return int64(0), errors.Wrap(err, "failed to create gitops commit")
}
err = s.addAppVersionToDownstream(tx, appID, d.ClusterID, newSequence,
kotsKinds.Installation.Spec.VersionLabel, downstreamStatus, source,
diffSummary, diffSummaryError, commitURL, commitURL != "")
if err != nil {
return int64(0), errors.Wrap(err, "failed to create downstream version")
}
}
return newSequence, nil
}
func (s S3PGStore) createAppVersionRecord(tx *sql.Tx, appID string, currentSequence *int64, appName string, appIcon string, kotsKinds *kotsutil.KotsKinds) (int64, error) {
// we marshal these here because it's a decision of the store to cache them in the app version table
// not all stores will do this
supportBundleSpec, err := kotsKinds.Marshal("troubleshoot.replicated.com", "v1beta1", "Collector")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal support bundle spec")
}
analyzersSpec, err := kotsKinds.Marshal("troubleshoot.replicated.com", "v1beta1", "Analyzer")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal analyzer spec")
}
preflightSpec, err := kotsKinds.Marshal("troubleshoot.replicated.com", "v1beta1", "Preflight")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal preflight spec")
}
appSpec, err := kotsKinds.Marshal("app.k8s.io", "v1beta1", "Application")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal app spec")
}
kotsAppSpec, err := kotsKinds.Marshal("kots.io", "v1beta1", "Application")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal kots app spec")
}
kotsInstallationSpec, err := kotsKinds.Marshal("kots.io", "v1beta1", "Installation")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal kots installation spec")
}
backupSpec, err := kotsKinds.Marshal("velero.io", "v1", "Backup")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal backup spec")
}
identitySpec, err := kotsKinds.Marshal("kots.io", "v1beta1", "Identity")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal identity spec")
}
licenseSpec, err := kotsKinds.Marshal("kots.io", "v1beta1", "License")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal license spec")
}
configSpec, err := kotsKinds.Marshal("kots.io", "v1beta1", "Config")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal config spec")
}
configValuesSpec, err := kotsKinds.Marshal("kots.io", "v1beta1", "ConfigValues")
if err != nil {
return int64(0), errors.Wrap(err, "failed to marshal configvalues spec")
}
newSequence := int64(0)
if currentSequence != nil {
row := tx.QueryRow(`select max(sequence) from app_version where app_id = $1`, appID)
if err := row.Scan(&newSequence); err != nil {
return 0, errors.Wrap(err, "failed to find current max sequence in row")
}
newSequence++
}
var releasedAt *time.Time
if kotsKinds.Installation.Spec.ReleasedAt != nil {
releasedAt = &kotsKinds.Installation.Spec.ReleasedAt.Time
}
query := `insert into app_version (app_id, sequence, created_at, version_label, release_notes, update_cursor, channel_id, channel_name, upstream_released_at, encryption_key,
supportbundle_spec, analyzer_spec, preflight_spec, app_spec, kots_app_spec, kots_installation_spec, kots_license, config_spec, config_values, backup_spec, identity_spec)
values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)
ON CONFLICT(app_id, sequence) DO UPDATE SET
created_at = EXCLUDED.created_at,
version_label = EXCLUDED.version_label,
release_notes = EXCLUDED.release_notes,
update_cursor = EXCLUDED.update_cursor,
channel_id = EXCLUDED.channel_id,
channel_name = EXCLUDED.channel_name,
upstream_released_at = EXCLUDED.upstream_released_at,
encryption_key = EXCLUDED.encryption_key,
supportbundle_spec = EXCLUDED.supportbundle_spec,
analyzer_spec = EXCLUDED.analyzer_spec,
preflight_spec = EXCLUDED.preflight_spec,
app_spec = EXCLUDED.app_spec,
kots_app_spec = EXCLUDED.kots_app_spec,
kots_installation_spec = EXCLUDED.kots_installation_spec,
kots_license = EXCLUDED.kots_license,
config_spec = EXCLUDED.config_spec,
config_values = EXCLUDED.config_values,
backup_spec = EXCLUDED.backup_spec,
identity_spec = EXCLUDED.identity_spec`
_, err = tx.Exec(query, appID, newSequence, time.Now(),
kotsKinds.Installation.Spec.VersionLabel,
kotsKinds.Installation.Spec.ReleaseNotes,
kotsKinds.Installation.Spec.UpdateCursor,
kotsKinds.Installation.Spec.ChannelID,
kotsKinds.Installation.Spec.ChannelName,
releasedAt,
kotsKinds.Installation.Spec.EncryptionKey,
supportBundleSpec,
analyzersSpec,
preflightSpec,
appSpec,
kotsAppSpec,
kotsInstallationSpec,
licenseSpec,
configSpec,
configValuesSpec,
backupSpec,
identitySpec)
if err != nil {
return int64(0), errors.Wrap(err, "failed to insert app version")
}
query = "update app set current_sequence = $1, name = $2, icon_uri = $3 where id = $4"
_, err = tx.Exec(query, int64(newSequence), appName, appIcon, appID)
if err != nil {
return int64(0), errors.Wrap(err, "failed to update app")
}
return int64(newSequence), nil
}
func (s S3PGStore) addAppVersionToDownstream(tx *sql.Tx, appID string, clusterID string, sequence int64, versionLabel string, status string, source string, diffSummary string, diffSummaryError string, commitURL string, gitDeployable bool) error {
query := `insert into app_downstream_version (app_id, cluster_id, sequence, parent_sequence, created_at, version_label, status, source, diff_summary, diff_summary_error, git_commit_url, git_deployable) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`
_, err := tx.Exec(
query,
appID,
clusterID,
sequence,
sequence,
time.Now(),
versionLabel,
status,
source,
diffSummary,
diffSummaryError,
commitURL,
gitDeployable)
if err != nil {
return errors.Wrap(err, "failed to execute query")
}
return nil
}
func (s S3PGStore) GetAppVersion(appID string, sequence int64) (*versiontypes.AppVersion, error) {
db := persistence.MustGetPGSession()
query := `select sequence, created_at, status, applied_at, kots_installation_spec, kots_app_spec from app_version where app_id = $1 and sequence = $2`
row := db.QueryRow(query, appID, sequence)
var status sql.NullString
var deployedAt sql.NullTime
var installationSpec sql.NullString
var kotsAppSpec sql.NullString
v := versiontypes.AppVersion{}
if err := row.Scan(&v.Sequence, &v.CreatedOn, &status, &deployedAt, &installationSpec, &kotsAppSpec); err != nil {
if err == sql.ErrNoRows {
return nil, ErrNotFound
}
return nil, errors.Wrap(err, "failed to scan")
}
kotsKinds := kotsutil.KotsKinds{}
// why is this a nullstring but we don't check if it's null?
installation, err := kotsutil.LoadInstallationFromContents([]byte(installationSpec.String))
if err != nil {
return nil, errors.Wrap(err, "failed to read installation spec")
}
kotsKinds.Installation = *installation
if kotsAppSpec.Valid {
kotsApp, err := kotsutil.LoadKotsAppFromContents([]byte(kotsAppSpec.String))
if err != nil {
return nil, errors.Wrap(err, "failed to read kotsapp spec")
}
if kotsApp != nil {
kotsKinds.KotsApplication = *kotsApp
}
}
if deployedAt.Valid {
v.DeployedAt = &deployedAt.Time
}
v.KOTSKinds = &kotsKinds
v.Status = status.String
return &v, nil
}
func (s S3PGStore) GetAppVersionsAfter(appID string, sequence int64) ([]*versiontypes.AppVersion, error) {
db := persistence.MustGetPGSession()
query := `select sequence, created_at, status, applied_at, kots_installation_spec from app_version where app_id = $1 and sequence > $2`
rows, err := db.Query(query, appID, sequence)
if err != nil {
return nil, errors.Wrap(err, "failed to query")
}
defer rows.Close()
var status sql.NullString
var deployedAt sql.NullTime
var installationSpec sql.NullString
versions := []*versiontypes.AppVersion{}
for rows.Next() {
v := versiontypes.AppVersion{}
if err := rows.Scan(&v.Sequence, &v.CreatedOn, &status, &deployedAt, &installationSpec); err != nil {
return nil, errors.Wrap(err, "failed to scan")
}
kotsKinds := kotsutil.KotsKinds{}
installation, err := kotsutil.LoadInstallationFromContents([]byte(installationSpec.String))
if err != nil {
return nil, errors.Wrap(err, "failed to read installation spec")
}
kotsKinds.Installation = *installation
if deployedAt.Valid {
v.DeployedAt = &deployedAt.Time
}
v.Status = status.String
versions = append(versions, &v)
}
return versions, nil
}