-
Notifications
You must be signed in to change notification settings - Fork 104
/
cluster_change.go
290 lines (230 loc) · 8.05 KB
/
cluster_change.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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package clusterapply
import (
"fmt"
"strings"
uierrs "github.com/cppforlife/go-cli-ui/errors"
ctlconf "github.com/k14s/kapp/pkg/kapp/config"
ctldiff "github.com/k14s/kapp/pkg/kapp/diff"
ctlres "github.com/k14s/kapp/pkg/kapp/resources"
ctlresm "github.com/k14s/kapp/pkg/kapp/resourcesmisc"
)
const (
disableWaitAnnKey = "kapp.k14s.io/disable-wait" // valid values: ''
)
type ClusterChangeApplyOp string
const (
ClusterChangeApplyOpAdd ClusterChangeApplyOp = "add"
ClusterChangeApplyOpDelete ClusterChangeApplyOp = "delete"
ClusterChangeApplyOpUpdate ClusterChangeApplyOp = "update"
ClusterChangeApplyOpNoop ClusterChangeApplyOp = "noop"
ClusterChangeApplyOpExists ClusterChangeApplyOp = "exists"
)
type ClusterChangeWaitOp string
const (
ClusterChangeWaitOpOK ClusterChangeWaitOp = "ok"
ClusterChangeWaitOpDelete ClusterChangeWaitOp = "delete"
ClusterChangeWaitOpNoop ClusterChangeWaitOp = "noop"
)
type ClusterChangeApplyStrategyOp string
const (
noopStrategyOp ClusterChangeApplyStrategyOp = ""
UnknownStrategyOp ClusterChangeApplyStrategyOp = "unknown"
)
type ApplyStrategy interface {
Op() ClusterChangeApplyStrategyOp
Apply() error
}
type ClusterChangeOpts struct {
ApplyIgnored bool
Wait bool
WaitIgnored bool
AddOrUpdateChangeOpts
}
type ClusterChange struct {
change ctldiff.Change
opts ClusterChangeOpts
identifiedResources ctlres.IdentifiedResources
changeFactory ctldiff.ChangeFactory
changeSetFactory ctldiff.ChangeSetFactory
convergedResFactory ConvergedResourceFactory
ui UI
markedNeedsWaiting bool
diffMaskRules []ctlconf.DiffMaskRule
}
var _ ChangeView = &ClusterChange{}
func NewClusterChange(change ctldiff.Change, opts ClusterChangeOpts,
identifiedResources ctlres.IdentifiedResources,
changeFactory ctldiff.ChangeFactory,
changeSetFactory ctldiff.ChangeSetFactory,
convergedResFactory ConvergedResourceFactory, ui UI,
diffMaskRules []ctlconf.DiffMaskRule) *ClusterChange {
return &ClusterChange{change, opts, identifiedResources,
changeFactory, changeSetFactory, convergedResFactory, ui, false, diffMaskRules}
}
func (c *ClusterChange) ApplyOp() ClusterChangeApplyOp {
if !c.opts.ApplyIgnored {
if c.change.IsIgnored() {
return ClusterChangeApplyOpNoop
}
}
switch c.change.Op() {
case ctldiff.ChangeOpAdd:
return ClusterChangeApplyOpAdd
case ctldiff.ChangeOpDelete:
return ClusterChangeApplyOpDelete
case ctldiff.ChangeOpUpdate:
return ClusterChangeApplyOpUpdate
case ctldiff.ChangeOpKeep:
return ClusterChangeApplyOpNoop
case ctldiff.ChangeOpExists:
return ClusterChangeApplyOpExists
case ctldiff.ChangeOpNoop:
return ClusterChangeApplyOpNoop
default:
panic("Unknown change apply op")
}
}
func (c *ClusterChange) WaitOp() ClusterChangeWaitOp {
if !c.opts.Wait {
return ClusterChangeWaitOpNoop
}
if !c.opts.WaitIgnored {
if c.change.IsIgnored() {
return ClusterChangeWaitOpNoop
}
}
if _, found := c.Resource().Annotations()[disableWaitAnnKey]; found {
return ClusterChangeWaitOpNoop
}
switch c.change.Op() {
case ctldiff.ChangeOpAdd, ctldiff.ChangeOpUpdate:
return ClusterChangeWaitOpOK
case ctldiff.ChangeOpDelete:
return ClusterChangeWaitOpDelete
case ctldiff.ChangeOpKeep:
// Return if this change was explicitly marked for waiting
// as it may be a link in a dependency such as this:
// A -> B -> C where,
// A is apply add
// B is apply noop and wait noop
// C is apply update and wait noop
// Without marking B explicitly as needed to wait,
// change in C may not be waited by A thru B.
if c.markedNeedsWaiting {
return ClusterChangeWaitOpOK
}
// TODO associated resources
// If existing resource is not in a "done successful" state,
// indicate that this will be something we need to wait for
existingResState, _, existingErr := c.convergedResFactory.New(c.change.ExistingResource(), nil).IsDoneApplying()
if existingErr != nil || !(existingResState.Done && existingResState.Successful) {
return ClusterChangeWaitOpOK
}
return ClusterChangeWaitOpNoop
case ctldiff.ChangeOpExists:
return ClusterChangeWaitOpOK
case ctldiff.ChangeOpNoop:
return ClusterChangeWaitOpNoop
default:
panic("Unknown change wait op")
}
}
func (c *ClusterChange) MarkNeedsWaiting() { c.markedNeedsWaiting = true }
func (c *ClusterChange) ApplyStrategyOp() (ClusterChangeApplyStrategyOp, error) {
strategy, err := c.applyStrategy()
if err != nil {
return UnknownStrategyOp, err
}
return strategy.Op(), nil
}
func (c *ClusterChange) Apply() (bool, []string, error) {
descMsgs := []string{c.ApplyDescription()}
var retryable bool
strategy, err := c.applyStrategy()
if err != nil {
return false, descMsgs, err
}
err = strategy.Apply()
if err != nil {
switch err.(type) {
case ExistsChangeError:
retryable = true
default:
retryable = ctlres.IsResourceChangeBlockedErr(err)
}
}
if retryable {
descMsgs = append(descMsgs, uiWaitMsgPrefix+"Retryable error: "+err.Error())
}
return retryable, descMsgs, c.applyErr(err)
}
func (c *ClusterChange) applyStrategy() (ApplyStrategy, error) {
op := c.ApplyOp()
switch op {
case ClusterChangeApplyOpAdd, ClusterChangeApplyOpUpdate:
return AddOrUpdateChange{
c.change, c.identifiedResources, c.changeFactory,
c.changeSetFactory, c.opts.AddOrUpdateChangeOpts, c.diffMaskRules}.ApplyStrategy()
case ClusterChangeApplyOpDelete:
return DeleteChange{c.change, c.identifiedResources}.ApplyStrategy()
case ClusterChangeApplyOpNoop:
return NoopStrategy{}, nil
case ClusterChangeApplyOpExists:
return ExistsChange{c.change, c.identifiedResources}.ApplyStrategy()
default:
return nil, fmt.Errorf("Unknown change apply operation: %s", op)
}
}
func (c *ClusterChange) IsDoneApplying() (ctlresm.DoneApplyState, []string, error) {
state, descMsgs, err := c.isDoneApplying()
primaryDescMsg := fmt.Sprintf("%s: %s", NewDoneApplyStateUI(state, err).State, c.WaitDescription())
return state, append([]string{primaryDescMsg}, descMsgs...), err
}
func (c *ClusterChange) isDoneApplying() (ctlresm.DoneApplyState, []string, error) {
op := c.WaitOp()
switch op {
case ClusterChangeWaitOpOK:
return ReconcilingChange{c.change, c.identifiedResources, c.convergedResFactory}.IsDoneApplying()
case ClusterChangeWaitOpDelete:
return DeleteChange{c.change, c.identifiedResources}.IsDoneApplying()
case ClusterChangeWaitOpNoop:
return ctlresm.DoneApplyState{Done: true, Successful: true}, nil, nil
default:
return ctlresm.DoneApplyState{}, nil, fmt.Errorf("Unknown change wait operation: %s", op)
}
}
func (c *ClusterChange) ApplyDescription() string {
return fmt.Sprintf("%s %s", applyOpCodeUI[c.ApplyOp()], c.change.NewOrExistingResource().Description())
}
func (c *ClusterChange) WaitDescription() string {
return fmt.Sprintf("%s %s", waitOpCodeUI[c.WaitOp()], c.change.NewOrExistingResource().Description())
}
func (c *ClusterChange) Resource() ctlres.Resource { return c.change.NewOrExistingResource() }
func (c *ClusterChange) ExistingResource() ctlres.Resource { return c.change.ExistingResource() }
func (c *ClusterChange) ConfigurableTextDiff() *ctldiff.ConfigurableTextDiff {
return c.change.ConfigurableTextDiff()
}
func (c *ClusterChange) applyErr(err error) error {
if err == nil {
return nil
}
hintMsg := ""
hintableErrs := map[string]string{
// TODO detect based on CRD content?
"the server does not allow this method on the requested resource": "resource is possibly not namespaced but must be",
"the server could not find the requested resource": "resource is possibly namespaced but cannot be",
}
for errText, hintText := range hintableErrs {
if strings.Contains(err.Error(), errText) {
hintMsg = fmt.Sprintf(" (hint: %s)", hintText)
break
}
}
return fmt.Errorf("Applying %s: %s%s", c.ApplyDescription(),
uierrs.NewSemiStructuredError(err), hintMsg)
}
type NoopStrategy struct{}
func (s NoopStrategy) Op() ClusterChangeApplyStrategyOp { return noopStrategyOp }
func (s NoopStrategy) Apply() error { return nil }