-
Notifications
You must be signed in to change notification settings - Fork 99
/
app.go
256 lines (222 loc) · 7.54 KB
/
app.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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package app
import (
"sync"
"github.com/go-logr/logr"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/deploy"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/fetch"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/metrics"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/reftracker"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/template"
"sigs.k8s.io/yaml"
)
type Hooks struct {
BlockDeletion func() error
UnblockDeletion func() error
UpdateStatus func(string) error
WatchChanges func(func(v1alpha1.App), chan struct{}) error
}
type App struct {
app v1alpha1.App
appPrev v1alpha1.App
hooks Hooks
fetchFactory fetch.Factory
templateFactory template.Factory
deployFactory deploy.Factory
log logr.Logger
appMetrics *metrics.AppMetrics
pendingStatusUpdate bool
flushAllStatusUpdates bool
}
func NewApp(app v1alpha1.App, hooks Hooks,
fetchFactory fetch.Factory, templateFactory template.Factory,
deployFactory deploy.Factory, log logr.Logger, appMetrics *metrics.AppMetrics) *App {
return &App{app: app, appPrev: *(app.DeepCopy()), hooks: hooks,
fetchFactory: fetchFactory, templateFactory: templateFactory,
deployFactory: deployFactory, log: log, appMetrics: appMetrics}
}
func (a *App) Name() string { return a.app.Name }
func (a *App) Namespace() string { return a.app.Namespace }
func (a *App) Status() v1alpha1.AppStatus { return a.app.Status }
func (a *App) StatusAsYAMLBytes() ([]byte, error) {
return yaml.Marshal(a.Status())
}
func (a *App) blockDeletion() error { return a.hooks.BlockDeletion() }
func (a *App) unblockDeletion() error { return a.hooks.UnblockDeletion() }
func (a *App) updateStatus(desc string) error {
a.pendingStatusUpdate = true
if !a.flushAllStatusUpdates {
// If there is no direct changes to the CRD, throttle status update
if a.app.Generation == a.appPrev.Status.ObservedGeneration {
return nil
}
}
a.pendingStatusUpdate = false
return a.hooks.UpdateStatus(desc)
}
func (a *App) startFlushingAllStatusUpdates() {
a.flushAllStatusUpdates = true
a.flushUpdateStatus("flush all")
}
func (a *App) flushUpdateStatus(desc string) error {
// Last possibility to save any pending status changes
if a.pendingStatusUpdate {
a.pendingStatusUpdate = false
return a.hooks.UpdateStatus("flushing: " + desc)
}
return nil
}
func (a *App) newCancelCh(conditions ...cancelCondition) (chan struct{}, func()) {
var cancelOnce sync.Once
cancelCh := make(chan struct{})
// Ends watching for app changes
cancelFunc := func() {
cancelOnce.Do(func() { close(cancelCh) })
}
cancelFuncOnApp := func(app v1alpha1.App) {
for _, condition := range conditions {
if condition(app) {
cancelFunc()
return
}
}
}
go func() {
if a.hooks.WatchChanges == nil {
// do nothing when cannot watch for changes
return
}
err := a.hooks.WatchChanges(cancelFuncOnApp, cancelCh)
if err != nil {
a.log.Error(err, "Watching changes") // TODO remove
}
}()
return cancelCh, cancelFunc
}
// Get all SecretRefs from App spec
func (a *App) SecretRefs() map[reftracker.RefKey]struct{} {
secrets := map[reftracker.RefKey]struct{}{}
// Fetch SecretRefs
for _, fetch := range a.app.Spec.Fetch {
switch {
case fetch.Inline != nil && fetch.Inline.PathsFrom != nil:
for _, pathsFrom := range fetch.Inline.PathsFrom {
if pathsFrom.SecretRef != nil {
refKey := reftracker.NewSecretKey(pathsFrom.SecretRef.Name, a.app.Namespace)
secrets[refKey] = struct{}{}
}
}
case fetch.Image != nil && fetch.Image.SecretRef != nil:
refKey := reftracker.NewSecretKey(fetch.Image.SecretRef.Name, a.app.Namespace)
secrets[refKey] = struct{}{}
case fetch.ImgpkgBundle != nil && fetch.ImgpkgBundle.SecretRef != nil:
refKey := reftracker.NewSecretKey(fetch.ImgpkgBundle.SecretRef.Name, a.app.Namespace)
secrets[refKey] = struct{}{}
case fetch.HTTP != nil && fetch.HTTP.SecretRef != nil:
refKey := reftracker.NewSecretKey(fetch.HTTP.SecretRef.Name, a.app.Namespace)
secrets[refKey] = struct{}{}
case fetch.Git != nil && fetch.Git.SecretRef != nil:
refKey := reftracker.NewSecretKey(fetch.Git.SecretRef.Name, a.app.Namespace)
secrets[refKey] = struct{}{}
case fetch.HelmChart != nil && fetch.HelmChart.Repository != nil:
if fetch.HelmChart.Repository.SecretRef != nil {
refKey := reftracker.NewSecretKey(fetch.HelmChart.Repository.SecretRef.Name, a.app.Namespace)
secrets[refKey] = struct{}{}
}
default:
}
}
// Templating SecretRefs
for _, tpl := range a.app.Spec.Template {
switch {
case tpl.Ytt != nil:
if tpl.Ytt.Inline != nil {
for _, pathsFrom := range tpl.Ytt.Inline.PathsFrom {
if pathsFrom.SecretRef != nil {
refKey := reftracker.NewSecretKey(pathsFrom.SecretRef.Name, a.app.Namespace)
secrets[refKey] = struct{}{}
}
}
}
for _, valuesFrom := range tpl.Ytt.ValuesFrom {
if valuesFrom.SecretRef != nil {
refKey := reftracker.NewSecretKey(valuesFrom.SecretRef.Name, a.app.Namespace)
secrets[refKey] = struct{}{}
}
}
case tpl.HelmTemplate != nil && tpl.HelmTemplate.ValuesFrom != nil:
for _, valsFrom := range tpl.HelmTemplate.ValuesFrom {
if valsFrom.SecretRef != nil {
refKey := reftracker.NewSecretKey(valsFrom.SecretRef.Name, a.app.Namespace)
secrets[refKey] = struct{}{}
}
}
default:
}
}
return secrets
}
// Get all ConfigMapRefs from App spec
func (a *App) ConfigMapRefs() map[reftracker.RefKey]struct{} {
configMaps := map[reftracker.RefKey]struct{}{}
// Fetch ConfigMapRefs
for _, fetch := range a.app.Spec.Fetch {
switch {
case fetch.Inline != nil && fetch.Inline.PathsFrom != nil:
for _, pathsFrom := range fetch.Inline.PathsFrom {
if pathsFrom.ConfigMapRef != nil {
refKey := reftracker.NewConfigMapKey(pathsFrom.ConfigMapRef.Name, a.app.Namespace)
configMaps[refKey] = struct{}{}
}
}
default:
}
}
// Templating ConfigMapRefs
for _, tpl := range a.app.Spec.Template {
switch {
case tpl.Ytt != nil:
if tpl.Ytt.Inline != nil {
for _, pathsFrom := range tpl.Ytt.Inline.PathsFrom {
if pathsFrom.ConfigMapRef != nil {
refKey := reftracker.NewConfigMapKey(pathsFrom.ConfigMapRef.Name, a.app.Namespace)
configMaps[refKey] = struct{}{}
}
}
}
for _, valuesFrom := range tpl.Ytt.ValuesFrom {
if valuesFrom.ConfigMapRef != nil {
refKey := reftracker.NewConfigMapKey(valuesFrom.ConfigMapRef.Name, a.app.Namespace)
configMaps[refKey] = struct{}{}
}
}
case tpl.HelmTemplate != nil && tpl.HelmTemplate.ValuesFrom != nil:
for _, valsFrom := range tpl.HelmTemplate.ValuesFrom {
if valsFrom.ConfigMapRef != nil {
refKey := reftracker.NewConfigMapKey(valsFrom.ConfigMapRef.Name, a.app.Namespace)
configMaps[refKey] = struct{}{}
}
}
default:
}
}
return configMaps
}
// HasImageOrImgpkgBundle is used to determine if the
// App's spec contains a fetch stage for an image or
// imgpkgbundle. It is mainly used to determine whether
// to retry a fetch attempt when placeholder secrets are
// involved with authenticating to private registries. Placeholder
// secrets are not always populated quick enough for Apps to use
// the secret, and private auth is only supported for images/bundles,
// so this helps to narrow down when to retry a fetch attempt.
func (a App) HasImageOrImgpkgBundle() bool {
for _, fetch := range a.app.Spec.Fetch {
if fetch.ImgpkgBundle != nil || fetch.Image != nil {
return true
}
}
return false
}