-
Notifications
You must be signed in to change notification settings - Fork 88
/
upstream.go
284 lines (252 loc) · 9.36 KB
/
upstream.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
package upstream
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"time"
"github.com/pkg/errors"
identity "github.com/replicatedhq/kots/pkg/kotsadmidentity"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/preflight"
"github.com/replicatedhq/kots/pkg/pull"
"github.com/replicatedhq/kots/pkg/render"
"github.com/replicatedhq/kots/pkg/reporting"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/upstream"
"github.com/replicatedhq/kots/pkg/upstream/types"
"github.com/replicatedhq/kots/pkg/util"
"github.com/replicatedhq/kots/pkg/version"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
)
func DownloadUpdate(appID string, update types.Update, skipPreflights bool, skipCompatibilityCheck bool) (finalSequence *int64, finalError error) {
taskID := "update-download"
var finishedCh chan struct{}
if update.AppSequence != nil {
taskID = fmt.Sprintf("update-download.%d", *update.AppSequence)
// The entire "update-download" task state is managed ouside of this function.
// Version specific tasks are managed in this sope only.
finishedCh = make(chan struct{}, 1)
go func() {
for {
select {
case <-time.After(time.Second):
if err := store.GetStore().UpdateTaskStatusTimestamp(taskID); err != nil {
logger.Error(errors.Wrapf(err, "failed to update %s task status timestamp", taskID))
}
case <-finishedCh:
return
}
}
}()
}
if err := store.GetStore().SetTaskStatus(taskID, "Fetching update...", "running"); err != nil {
finalError = errors.Wrap(err, "failed to set task status")
return
}
defer func() {
if finishedCh != nil {
close(finishedCh)
}
if finalError == nil {
if update.AppSequence != nil {
// this could be an older version that is being downloaded at a later point
// update the diff summary of the next version in the list (if exists)
err := store.GetStore().UpdateNextAppVersionDiffSummary(appID, *update.AppSequence)
if err != nil {
logger.Error(errors.Wrapf(err, "failed to update next app version diff summary for base sequence %d", *update.AppSequence))
}
}
err := store.GetStore().ClearTaskStatus(taskID)
if err != nil {
logger.Error(errors.Wrapf(err, "failed to clear %s task status", taskID))
}
return
}
errMsg := finalError.Error()
if cause, ok := errors.Cause(finalError).(util.ActionableError); ok {
errMsg = cause.Error()
}
var kotsApplication *kotsv1beta1.Application
var license *kotsv1beta1.License
if cause, ok := errors.Cause(finalError).(upstream.IncompatibleAppError); ok {
errMsg = cause.Error()
kotsApplication = cause.KotsApplication
license = cause.License
finalError = util.ActionableError{
NoRetry: true,
Message: cause.Error(),
}
}
if update.AppSequence != nil || finalSequence != nil {
// a version already exists or has been created
err := store.GetStore().SetTaskStatus(taskID, errMsg, "failed")
if err != nil {
logger.Error(errors.Wrapf(err, "failed to set %s task status", taskID))
}
return
}
// no version has been created for the update yet, create the version as pending download
newSequence, err := store.GetStore().CreatePendingDownloadAppVersion(appID, update, kotsApplication, license)
if err != nil {
logger.Error(errors.Wrapf(err, "failed to create pending download app version for update %s", update.VersionLabel))
if err := store.GetStore().SetTaskStatus(taskID, errMsg, "failed"); err != nil {
logger.Error(errors.Wrapf(err, "failed to set %s task status", taskID))
}
return
}
finalSequence = &newSequence
// a pending download version has been created, bind the download error to it
// clear the global task status at the end to avoid a race condition with the UI
sequenceTaskID := fmt.Sprintf("update-download.%d", *finalSequence)
if err := store.GetStore().SetTaskStatus(sequenceTaskID, errMsg, "failed"); err != nil {
logger.Error(errors.Wrapf(err, "failed to set %s task status", sequenceTaskID))
}
if err := store.GetStore().ClearTaskStatus(taskID); err != nil {
logger.Error(errors.Wrapf(err, "failed to clear %s task status", taskID))
}
}()
archiveDir, baseSequence, err := store.GetStore().GetAppVersionBaseArchive(appID, update.VersionLabel)
if err != nil {
finalError = errors.Wrapf(err, "failed to get base archive dir for version %s", update.VersionLabel)
return
}
defer os.RemoveAll(archiveDir)
beforeKotsKinds, err := kotsutil.LoadKotsKindsFromPath(filepath.Join(archiveDir, "upstream"))
if err != nil {
finalError = errors.Wrap(err, "failed to read kots kinds before update")
return
}
beforeInstallation := beforeKotsKinds.Installation.Spec
pipeReader, pipeWriter := io.Pipe()
defer func() {
pipeWriter.CloseWithError(finalError)
}()
go func() {
scanner := bufio.NewScanner(pipeReader)
for scanner.Scan() {
if err := store.GetStore().SetTaskStatus(taskID, scanner.Text(), "running"); err != nil {
logger.Error(err)
}
}
pipeReader.CloseWithError(scanner.Err())
}()
a, err := store.GetStore().GetApp(appID)
if err != nil {
finalError = errors.Wrap(err, "failed to get app")
return
}
downstreams, err := store.GetStore().ListDownstreamsForApp(a.ID)
if err != nil {
finalError = errors.Wrap(err, "failed to list downstreams for app")
return
}
downstreamNames := []string{}
for _, d := range downstreams {
downstreamNames = append(downstreamNames, d.Name)
}
appNamespace := util.AppNamespace()
appSequence, err := store.GetStore().GetNextAppSequence(a.ID)
if err != nil {
finalError = errors.Wrap(err, "failed to get new app sequence")
return
}
if update.AppSequence != nil {
appSequence = *update.AppSequence
}
latestLicense, err := store.GetStore().GetLatestLicenseForApp(a.ID)
if err != nil {
finalError = errors.Wrap(err, "failed to get latest license")
return
}
identityConfigFile := filepath.Join(archiveDir, "upstream", "userdata", "identityconfig.yaml")
if _, err := os.Stat(identityConfigFile); os.IsNotExist(err) {
file, err := identity.InitAppIdentityConfig(a.Slug)
if err != nil {
finalError = errors.Wrap(err, "failed to init identity config")
return
}
identityConfigFile = file
defer os.Remove(identityConfigFile)
} else if err != nil {
finalError = errors.Wrap(err, "failed to get stat identity config file")
return
}
registrySettings, err := store.GetStore().GetRegistryDetailsForApp(appID)
if err != nil {
finalError = errors.Wrap(err, "failed to get registry settings")
return
}
if err := pull.CleanBaseArchive(archiveDir); err != nil {
finalError = errors.Wrap(err, "failed to clean base archive")
return
}
pullOptions := pull.PullOptions{
LicenseObj: latestLicense,
Namespace: appNamespace,
ConfigFile: filepath.Join(archiveDir, "upstream", "userdata", "config.yaml"),
IdentityConfigFile: identityConfigFile,
InstallationFile: filepath.Join(archiveDir, "upstream", "userdata", "installation.yaml"),
UpdateCursor: update.Cursor,
RootDir: archiveDir,
Downstreams: downstreamNames,
ExcludeKotsKinds: true,
ExcludeAdminConsole: true,
CreateAppDir: false,
ReportWriter: pipeWriter,
AppSlug: a.Slug,
AppSequence: appSequence,
IsGitOps: a.IsGitOps,
ReportingInfo: reporting.GetReportingInfo(a.ID),
RewriteImages: registrySettings.IsValid(),
RewriteImageOptions: registrySettings,
SkipCompatibilityCheck: skipCompatibilityCheck,
}
_, err = pull.Pull(fmt.Sprintf("replicated://%s", beforeKotsKinds.License.Spec.AppSlug), pullOptions)
if err != nil {
if errors.Cause(err) != pull.ErrConfigNeeded {
finalError = errors.Wrap(err, "failed to pull")
return
}
}
if update.AppSequence == nil {
afterKotsKinds, err := kotsutil.LoadKotsKindsFromPath(filepath.Join(archiveDir, "upstream"))
if err != nil {
finalError = errors.Wrap(err, "failed to read kots kinds after update")
return
}
if afterKotsKinds.Installation.Spec.UpdateCursor == beforeInstallation.UpdateCursor && afterKotsKinds.Installation.Spec.ChannelID == beforeInstallation.ChannelID {
return
}
newSequence, err := store.GetStore().CreateAppVersion(a.ID, &baseSequence, archiveDir, "Upstream Update", skipPreflights, &version.DownstreamGitOps{}, render.Renderer{})
if err != nil {
finalError = errors.Wrap(err, "failed to create version")
return
}
finalSequence = &newSequence
} else {
err := store.GetStore().UpdateAppVersion(a.ID, *update.AppSequence, &baseSequence, archiveDir, "Upstream Update", skipPreflights, &version.DownstreamGitOps{}, render.Renderer{})
if err != nil {
finalError = errors.Wrap(err, "failed to create version")
return
}
finalSequence = update.AppSequence
}
hasStrictPreflights, err := store.GetStore().HasStrictPreflights(a.ID, *finalSequence)
if err != nil {
finalError = errors.Wrap(err, "failed to check if app preflight has strict analyzers")
return
}
if hasStrictPreflights && skipPreflights {
logger.Warnf("preflights will not be skipped, strict preflights are set to %t", hasStrictPreflights)
}
if !skipPreflights || hasStrictPreflights {
if err := preflight.Run(appID, a.Slug, *finalSequence, a.IsAirgap, archiveDir); err != nil {
finalError = errors.Wrap(err, "failed to run preflights")
return
}
}
return
}