-
Notifications
You must be signed in to change notification settings - Fork 99
/
app_reconcile.go
265 lines (207 loc) · 6.56 KB
/
app_reconcile.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
// Copyright 2021 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package pkgrepository
import (
"fmt"
"time"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/exec"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/memdir"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
// Reconcile is not expected to be called concurrently
func (a *App) Reconcile(force bool) (reconcile.Result, error) {
defer a.flushUpdateStatus("packagerepository reconciled")
var err error
switch {
case a.app.Spec.Paused:
a.log.Info("PackageRepository is paused, not reconciling")
a.markObservedLatest()
a.app.Status.FriendlyDescription = "Paused"
err = a.updateStatus("PackageRepository paused")
case a.app.DeletionTimestamp != nil:
a.log.Info("Started delete")
defer func() { a.log.Info("Completed delete") }()
err = a.reconcileDelete()
case force || NewReconcileTimer(a.app).IsReadyAt(time.Now()):
a.log.Info("Started deploy")
defer func() { a.log.Info("Completed deploy") }()
err = a.reconcileDeploy()
default:
a.log.Info("Reconcile noop")
}
return reconcile.Result{RequeueAfter: NewReconcileTimer(a.app).DurationUntilReady(err)}, err
}
func (a *App) reconcileDelete() error {
a.markObservedLatest()
a.setDeleting()
vendir := a.fetchFactory.NewVendir(a.app.Namespace)
err := vendir.ClearCache(a.cacheID())
if err != nil {
return err
}
err = a.updateStatus("marking deleting")
if err != nil {
return err
}
a.resetLastDeployStartedAt()
result := a.delete()
a.setDeleteCompleted(result)
// Resource is gone so this will error, ignore it
_ = a.updateStatus("marking delete completed")
return nil
}
func (a *App) reconcileDeploy() error {
a.markObservedLatest()
a.setReconciling()
err := a.updateStatus("marking reconciling")
if err != nil {
return err
}
result := a.reconcileFetchTemplateDeploy()
a.setReconcileCompleted(result)
return a.updateStatus("marking reconcile completed")
}
func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
tmpDir := memdir.NewTmpDir("fetch-template-deploy")
err := tmpDir.Create()
if err != nil {
return exec.NewCmdRunResultWithErr(err)
}
defer tmpDir.Remove()
assetsPath := tmpDir.Path()
{
a.resetLastFetchStartedAt()
var fetchResult exec.CmdRunResult
assetsPath, fetchResult = a.fetch(assetsPath)
a.app.Status.Fetch = &v1alpha1.AppStatusFetch{
Stderr: fetchResult.Stderr,
Stdout: fetchResult.Stdout,
ExitCode: fetchResult.ExitCode,
Error: fetchResult.ErrorStr(),
StartedAt: a.app.Status.Fetch.StartedAt,
UpdatedAt: metav1.NewTime(time.Now().UTC()),
}
err := a.updateStatus("marking fetch completed")
if err != nil {
return exec.NewCmdRunResultWithErr(err)
}
if fetchResult.Error != nil {
return fetchResult
}
}
tplResult := a.template(assetsPath)
a.app.Status.Template = &v1alpha1.AppStatusTemplate{
Stderr: tplResult.Stderr,
ExitCode: tplResult.ExitCode,
Error: tplResult.ErrorStr(),
UpdatedAt: metav1.NewTime(time.Now().UTC()),
}
err = a.updateStatus("marking template completed")
if err != nil {
return exec.NewCmdRunResultWithErr(err)
}
if tplResult.Error != nil {
return tplResult
}
a.resetLastDeployStartedAt()
return a.updateLastDeploy(a.deploy(tplResult.Stdout))
}
func (a *App) updateLastDeploy(result exec.CmdRunResult) exec.CmdRunResult {
result = result.WithFriendlyYAMLStrings()
a.app.Status.Deploy = &v1alpha1.AppStatusDeploy{
Stdout: result.Stdout,
Stderr: result.Stderr,
Finished: result.Finished,
ExitCode: result.ExitCode,
Error: result.ErrorStr(),
StartedAt: a.app.Status.Deploy.StartedAt,
UpdatedAt: metav1.NewTime(time.Now().UTC()),
}
a.updateStatus("marking last deploy")
return result
}
func (a *App) resetLastFetchStartedAt() {
if a.app.Status.Fetch == nil {
a.app.Status.Fetch = &v1alpha1.AppStatusFetch{}
}
a.app.Status.Fetch.StartedAt = metav1.NewTime(time.Now().UTC())
}
func (a *App) resetLastDeployStartedAt() {
if a.app.Status.Deploy == nil {
a.app.Status.Deploy = &v1alpha1.AppStatusDeploy{}
}
a.app.Status.Deploy.StartedAt = metav1.NewTime(time.Now().UTC())
}
func (a *App) markObservedLatest() {
a.app.Status.ObservedGeneration = a.app.Generation
}
func (a *App) setReconciling() {
a.removeAllConditions()
a.app.Status.Conditions = append(a.app.Status.Conditions, v1alpha1.Condition{
Type: v1alpha1.Reconciling,
Status: corev1.ConditionTrue,
})
a.app.Status.FriendlyDescription = "Reconciling"
}
func (a *App) setReconcileCompleted(result exec.CmdRunResult) {
a.removeAllConditions()
if result.Error != nil {
a.app.Status.Conditions = append(a.app.Status.Conditions, v1alpha1.Condition{
Type: v1alpha1.ReconcileFailed,
Status: corev1.ConditionTrue,
Message: result.ErrorStr(),
})
a.app.Status.ConsecutiveReconcileFailures++
a.app.Status.ConsecutiveReconcileSuccesses = 0
a.app.Status.FriendlyDescription = fmt.Sprintf("Reconcile failed: %s", result.ErrorStr())
a.setUsefulErrorMessage(result)
} else {
a.app.Status.Conditions = append(a.app.Status.Conditions, v1alpha1.Condition{
Type: v1alpha1.ReconcileSucceeded,
Status: corev1.ConditionTrue,
Message: "",
})
a.app.Status.ConsecutiveReconcileSuccesses++
a.app.Status.ConsecutiveReconcileFailures = 0
a.app.Status.FriendlyDescription = "Reconcile succeeded"
a.app.Status.UsefulErrorMessage = ""
}
}
func (a *App) setDeleting() {
a.removeAllConditions()
a.app.Status.Conditions = append(a.app.Status.Conditions, v1alpha1.Condition{
Type: v1alpha1.Deleting,
Status: corev1.ConditionTrue,
})
a.app.Status.FriendlyDescription = "Deleting"
}
func (a *App) setDeleteCompleted(result exec.CmdRunResult) {
a.removeAllConditions()
if result.Error != nil {
a.app.Status.Conditions = append(a.app.Status.Conditions, v1alpha1.Condition{
Type: v1alpha1.DeleteFailed,
Status: corev1.ConditionTrue,
Message: result.ErrorStr(),
})
a.app.Status.ConsecutiveReconcileFailures++
a.app.Status.ConsecutiveReconcileSuccesses = 0
a.app.Status.FriendlyDescription = fmt.Sprintf("Delete failed: %s", result.ErrorStr())
a.setUsefulErrorMessage(result)
} else {
// assume resource will be deleted, hence nothing to update
}
}
func (a *App) removeAllConditions() {
a.app.Status.Conditions = nil
}
func (a *App) setUsefulErrorMessage(result exec.CmdRunResult) {
switch {
case result.Stderr != "":
a.app.Status.UsefulErrorMessage = result.Stderr
default:
a.app.Status.UsefulErrorMessage = result.ErrorStr()
}
}