forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rolling.go
285 lines (258 loc) · 10.1 KB
/
rolling.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
package rolling
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"time"
kapi "k8s.io/kubernetes/pkg/api"
kerrors "k8s.io/kubernetes/pkg/api/errors"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/wait"
"github.com/openshift/origin/pkg/client"
deployapi "github.com/openshift/origin/pkg/deploy/api"
strat "github.com/openshift/origin/pkg/deploy/strategy"
stratsupport "github.com/openshift/origin/pkg/deploy/strategy/support"
deployutil "github.com/openshift/origin/pkg/deploy/util"
)
// TODO: This should perhaps be made public upstream. See:
// https://github.com/kubernetes/kubernetes/issues/7851
const sourceIdAnnotation = "kubectl.kubernetes.io/update-source-id"
const DefaultApiRetryPeriod = 1 * time.Second
const DefaultApiRetryTimeout = 10 * time.Second
// RollingDeploymentStrategy is a Strategy which implements rolling
// deployments using the upstream Kubernetes RollingUpdater.
//
// Currently, there are some caveats:
//
// 1. When there is no existing prior deployment, deployment delegates to
// another strategy.
// 2. The interface to the RollingUpdater is not very clean.
//
// These caveats can be resolved with future upstream refactorings to
// RollingUpdater[1][2].
//
// [1] https://github.com/kubernetes/kubernetes/pull/7183
// [2] https://github.com/kubernetes/kubernetes/issues/7851
type RollingDeploymentStrategy struct {
// out and errOut control where output is sent during the strategy
out, errOut io.Writer
// until is a condition that, if reached, will cause the strategy to exit early
until string
// initialStrategy is used when there are no prior deployments.
initialStrategy acceptingDeploymentStrategy
// client is used to deal with ReplicationControllers.
client kclient.Interface
// client is used to perform tag actions
tags client.ImageStreamTagsNamespacer
// rollingUpdate knows how to perform a rolling update.
rollingUpdate func(config *kubectl.RollingUpdaterConfig) error
// decoder is used to access the encoded config on a deployment.
decoder runtime.Decoder
// hookExecutor can execute a lifecycle hook.
hookExecutor hookExecutor
// getUpdateAcceptor returns an UpdateAcceptor to verify the first replica
// of the deployment.
getUpdateAcceptor func(timeout time.Duration) strat.UpdateAcceptor
// apiRetryPeriod is how long to wait before retrying a failed API call.
apiRetryPeriod time.Duration
// apiRetryTimeout is how long to retry API calls before giving up.
apiRetryTimeout time.Duration
}
// acceptingDeploymentStrategy is a DeploymentStrategy which accepts an
// injected UpdateAcceptor as part of the deploy function. This is a hack to
// support using the Recreate strategy for initial deployments and should be
// removed when https://github.com/kubernetes/kubernetes/pull/7183 is
// fixed.
type acceptingDeploymentStrategy interface {
DeployWithAcceptor(from *kapi.ReplicationController, to *kapi.ReplicationController, desiredReplicas int, updateAcceptor strat.UpdateAcceptor) error
}
// AcceptorInterval is how often the UpdateAcceptor should check for
// readiness.
const AcceptorInterval = 1 * time.Second
// NewRollingDeploymentStrategy makes a new RollingDeploymentStrategy.
func NewRollingDeploymentStrategy(namespace string, client kclient.Interface, tags client.ImageStreamTagsNamespacer, decoder runtime.Decoder, initialStrategy acceptingDeploymentStrategy, out, errOut io.Writer, until string) *RollingDeploymentStrategy {
if out == nil {
out = ioutil.Discard
}
if errOut == nil {
errOut = ioutil.Discard
}
return &RollingDeploymentStrategy{
out: out,
errOut: errOut,
until: until,
decoder: decoder,
initialStrategy: initialStrategy,
client: client,
tags: tags,
apiRetryPeriod: DefaultApiRetryPeriod,
apiRetryTimeout: DefaultApiRetryTimeout,
rollingUpdate: func(config *kubectl.RollingUpdaterConfig) error {
updater := kubectl.NewRollingUpdater(namespace, client)
return updater.Update(config)
},
hookExecutor: stratsupport.NewHookExecutor(client, tags, os.Stdout, decoder),
getUpdateAcceptor: func(timeout time.Duration) strat.UpdateAcceptor {
return stratsupport.NewAcceptNewlyObservedReadyPods(out, client, timeout, AcceptorInterval)
},
}
}
func (s *RollingDeploymentStrategy) Deploy(from *kapi.ReplicationController, to *kapi.ReplicationController, desiredReplicas int) error {
config, err := deployutil.DecodeDeploymentConfig(to, s.decoder)
if err != nil {
return fmt.Errorf("couldn't decode DeploymentConfig from deployment %s: %v", deployutil.LabelForDeployment(to), err)
}
params := config.Spec.Strategy.RollingParams
updateAcceptor := s.getUpdateAcceptor(time.Duration(*params.TimeoutSeconds) * time.Second)
// If there's no prior deployment, delegate to another strategy since the
// rolling updater only supports transitioning between two deployments.
//
// Hook support is duplicated here for now. When the rolling updater can
// handle initial deployments, all of this code can go away.
if from == nil {
// Execute any pre-hook.
if params.Pre != nil {
if err := s.hookExecutor.Execute(params.Pre, to, deployapi.PreHookPodSuffix, "pre"); err != nil {
return fmt.Errorf("Pre hook failed: %s", err)
}
}
// Execute the delegate strategy.
err := s.initialStrategy.DeployWithAcceptor(from, to, desiredReplicas, updateAcceptor)
if err != nil {
return err
}
// Execute any post-hook. Errors are logged and ignored.
if params.Post != nil {
if err := s.hookExecutor.Execute(params.Post, to, deployapi.PostHookPodSuffix, "post"); err != nil {
return fmt.Errorf("post hook failed: %s", err)
}
}
// All done.
return nil
}
// Prepare for a rolling update.
// Execute any pre-hook.
if params.Pre != nil {
if err := s.hookExecutor.Execute(params.Pre, to, deployapi.PreHookPodSuffix, "pre"); err != nil {
return fmt.Errorf("pre hook failed: %s", err)
}
}
if s.until == "pre" {
return strat.NewConditionReachedErr("pre hook succeeded")
}
if s.until == "0%" {
return strat.NewConditionReachedErr("Reached 0% (before rollout)")
}
// HACK: Assign the source ID annotation that the rolling updater expects,
// unless it already exists on the deployment.
//
// Related upstream issue:
// https://github.com/kubernetes/kubernetes/pull/7183
err = wait.Poll(s.apiRetryPeriod, s.apiRetryTimeout, func() (done bool, err error) {
existing, err := s.client.ReplicationControllers(to.Namespace).Get(to.Name)
if err != nil {
msg := fmt.Sprintf("couldn't look up deployment %s: %s", to.Name, err)
if kerrors.IsNotFound(err) {
return false, fmt.Errorf("%s", msg)
}
// Try again.
fmt.Fprintln(s.errOut, "error:", msg)
return false, nil
}
if _, hasSourceId := existing.Annotations[sourceIdAnnotation]; !hasSourceId {
existing.Annotations[sourceIdAnnotation] = fmt.Sprintf("%s:%s", from.Name, from.ObjectMeta.UID)
if _, err := s.client.ReplicationControllers(existing.Namespace).Update(existing); err != nil {
msg := fmt.Sprintf("couldn't assign source annotation to deployment %s: %v", existing.Name, err)
if kerrors.IsNotFound(err) {
return false, fmt.Errorf("%s", msg)
}
// Try again.
fmt.Fprintln(s.errOut, "error:", msg)
return false, nil
}
}
return true, nil
})
if err != nil {
return err
}
to, err = s.client.ReplicationControllers(to.Namespace).Get(to.Name)
if err != nil {
return err
}
// HACK: There's a validation in the rolling updater which assumes that when
// an existing RC is supplied, it will have >0 replicas- a validation which
// is then disregarded as the desired count is obtained from the annotation
// on the RC. For now, fake it out by just setting replicas to 1.
//
// Related upstream issue:
// https://github.com/kubernetes/kubernetes/pull/7183
to.Spec.Replicas = 1
// Perform a rolling update.
rollingConfig := &kubectl.RollingUpdaterConfig{
Out: &rollingUpdaterWriter{w: s.out},
OldRc: from,
NewRc: to,
UpdatePeriod: time.Duration(*params.UpdatePeriodSeconds) * time.Second,
Interval: time.Duration(*params.IntervalSeconds) * time.Second,
Timeout: time.Duration(*params.TimeoutSeconds) * time.Second,
CleanupPolicy: kubectl.PreserveRollingUpdateCleanupPolicy,
MaxSurge: params.MaxSurge,
MaxUnavailable: params.MaxUnavailable,
OnProgress: func(oldRc, newRc *kapi.ReplicationController, percentage int) error {
if expect, ok := strat.Percentage(s.until); ok && percentage >= expect {
return strat.NewConditionReachedErr(fmt.Sprintf("Reached %s (currently %d%%)", s.until, percentage))
}
return nil
},
}
if err := s.rollingUpdate(rollingConfig); err != nil {
return err
}
// Execute any post-hook.
if params.Post != nil {
if err := s.hookExecutor.Execute(params.Post, to, deployapi.PostHookPodSuffix, "post"); err != nil {
return fmt.Errorf("post hook failed: %s", err)
}
}
return nil
}
// rollingUpdaterWriter is an io.Writer that delegates to glog.
type rollingUpdaterWriter struct {
w io.Writer
called bool
}
func (w *rollingUpdaterWriter) Write(p []byte) (n int, err error) {
n = len(p)
if bytes.HasPrefix(p, []byte("Continuing update with ")) {
return n, nil
}
if bytes.HasSuffix(p, []byte("\n")) {
p = p[:len(p)-1]
}
for _, line := range bytes.Split(p, []byte("\n")) {
if w.called {
fmt.Fprintln(w.w, " ", string(line))
} else {
w.called = true
fmt.Fprintln(w.w, "-->", string(line))
}
}
return n, nil
}
// hookExecutor knows how to execute a deployment lifecycle hook.
type hookExecutor interface {
Execute(hook *deployapi.LifecycleHook, deployment *kapi.ReplicationController, suffix, label string) error
}
// hookExecutorImpl is a pluggable hookExecutor.
type hookExecutorImpl struct {
executeFunc func(hook *deployapi.LifecycleHook, deployment *kapi.ReplicationController, suffix, label string) error
}
// Execute executes the provided lifecycle hook
func (i *hookExecutorImpl) Execute(hook *deployapi.LifecycleHook, deployment *kapi.ReplicationController, suffix, label string) error {
return i.executeFunc(hook, deployment, suffix, label)
}