-
Notifications
You must be signed in to change notification settings - Fork 200
/
jiva_upgrade.go
624 lines (574 loc) · 17.7 KB
/
jiva_upgrade.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
Copyright 2019 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"text/template"
"time"
utask "github.com/openebs/maya/pkg/apis/openebs.io/upgrade/v1alpha1"
jivaClient "github.com/openebs/maya/pkg/client/jiva"
templates "github.com/openebs/maya/pkg/upgrade/templates/v1"
errors "github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
k8serror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
types "k8s.io/apimachinery/pkg/types"
"k8s.io/klog"
// auth plugins
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
type replicaPatchDetails struct {
UpgradeVersion, ImageTag, PVName, ReplicaContainerName, ReplicaImage string
}
type controllerPatchDetails struct {
UpgradeVersion, ImageTag, ControllerContainerName, ControllerImage, MExporterImage string
}
type replicaDetails struct {
patchDetails *replicaPatchDetails
version, name string
}
type controllerDetails struct {
patchDetails *controllerPatchDetails
version, name string
}
func getReplicaPatchDetails(d *appsv1.Deployment) (
*replicaPatchDetails,
error,
) {
patchDetails := &replicaPatchDetails{}
// verify delpoyment name
if d.Name == "" {
return nil, errors.New("missing deployment name")
}
name, err := getContainerName(d)
if err != nil {
return nil, err
}
patchDetails.ReplicaContainerName = name
image, err := getBaseImage(d, patchDetails.ReplicaContainerName)
if err != nil {
return nil, err
}
patchDetails.ReplicaImage = image
if imageTag != "" {
patchDetails.ImageTag = imageTag
} else {
patchDetails.ImageTag = upgradeVersion
}
return patchDetails, nil
}
func getControllerPatchDetails(d *appsv1.Deployment) (
*controllerPatchDetails,
error,
) {
patchDetails := &controllerPatchDetails{}
// verify delpoyment name
if d.Name == "" {
return nil, errors.New("missing deployment name")
}
name, err := getContainerName(d)
if err != nil {
return nil, err
}
patchDetails.ControllerContainerName = name
image, err := getBaseImage(d, patchDetails.ControllerContainerName)
if err != nil {
return nil, err
}
patchDetails.ControllerImage = image
image, err = getBaseImage(d, "maya-volume-exporter")
if err != nil {
return nil, err
}
patchDetails.MExporterImage = image
if imageTag != "" {
patchDetails.ImageTag = imageTag
} else {
patchDetails.ImageTag = upgradeVersion
}
return patchDetails, nil
}
func getReplica(replicaLabel, namespace string) (*replicaDetails, error) {
replicaObj := &replicaDetails{}
deployObj, err := getDeployment(replicaLabel, namespace)
if err != nil {
return nil, errors.Wrapf(err, "failed to get replica deployment")
}
if deployObj.Name == "" {
return nil, errors.Errorf("missing deployment name for replica")
}
replicaObj.name = deployObj.Name
version, err := getOpenEBSVersion(deployObj)
if err != nil {
return nil, err
}
if (version != currentVersion) && (version != upgradeVersion) {
return nil, errors.Errorf(
"replica version %s is neither %s nor %s\n",
version,
currentVersion,
upgradeVersion,
)
}
replicaObj.version = version
patchDetails, err := getReplicaPatchDetails(deployObj)
if err != nil {
return nil, err
}
replicaObj.patchDetails = patchDetails
replicaObj.patchDetails.UpgradeVersion = upgradeVersion
return replicaObj, nil
}
func getController(controllerLabel, namespace string) (*controllerDetails, error) {
controllerObj := &controllerDetails{}
deployObj, err := getDeployment(controllerLabel, namespace)
if err != nil {
return nil, errors.Wrapf(err, "failed to get controller deployment")
}
if deployObj.Name == "" {
return nil, errors.Errorf("missing deployment name for controller")
}
controllerObj.name = deployObj.Name
version, err := getOpenEBSVersion(deployObj)
if err != nil {
return nil, err
}
if (version != currentVersion) && (version != upgradeVersion) {
return nil, errors.Errorf(
"controller version %s is neither %s nor %s\n",
version,
currentVersion,
upgradeVersion,
)
}
controllerObj.version = version
patchDetails, err := getControllerPatchDetails(deployObj)
if err != nil {
return nil, err
}
controllerObj.patchDetails = patchDetails
controllerObj.patchDetails.UpgradeVersion = upgradeVersion
return controllerObj, nil
}
func patchReplica(replicaObj *replicaDetails, namespace string) error {
if replicaObj.version == currentVersion {
tmpl, err := template.New("replicaPatch").
Parse(templates.JivaReplicaPatch)
if err != nil {
return errors.Wrapf(err, "failed to create template for replica patch")
}
err = tmpl.Execute(&buffer, replicaObj.patchDetails)
if err != nil {
return errors.Wrapf(err, "failed to populate template for replica patch")
}
replicaPatch := buffer.String()
buffer.Reset()
err = patchDelpoyment(
replicaObj.name,
namespace,
types.StrategicMergePatchType,
[]byte(replicaPatch),
)
if err != nil {
return errors.Wrapf(err, "failed to patch replica deployment")
}
klog.Infof("%s patched", replicaObj.name)
} else {
klog.Infof("replica deployment already in %s version", upgradeVersion)
}
return nil
}
func patchController(controllerObj *controllerDetails, namespace string) error {
if controllerObj.version == currentVersion {
tmpl, err := template.New("controllerPatch").
Parse(templates.JivaTargetPatch)
if err != nil {
return errors.Wrapf(err, "failed to create template for controller patch")
}
err = tmpl.Execute(&buffer, controllerObj.patchDetails)
if err != nil {
return errors.Wrapf(err, "failed to populate template for controller patch")
}
controllerPatch := buffer.String()
buffer.Reset()
err = patchDelpoyment(
controllerObj.name,
namespace,
types.StrategicMergePatchType,
[]byte(controllerPatch),
)
if err != nil {
return errors.Wrapf(err, "failed to patch replica deployment")
}
klog.Infof("%s patched", controllerObj.name)
} else {
klog.Infof("controller deployment already in %s version\n", upgradeVersion)
}
return nil
}
func getPVCDeploymentsNamespace(
pvName,
pvLabel,
openebsNamespace string) (ns string, err error) {
pvObj, err := pvClient.Get(pvName, metav1.GetOptions{})
if err != nil {
return "", err
}
// verifying whether the pvc is deployed with DeployInOpenebsNamespace cas config
deployList, err := deployClient.WithNamespace(openebsNamespace).List(
&metav1.ListOptions{
LabelSelector: pvLabel,
})
if err != nil {
return "", err
}
// check whether pvc pods are openebs namespace or not
if len(deployList.Items) > 0 {
ns = openebsNamespace
return ns, nil
}
// if pvc pods are not in openebs namespace take the namespace of pvc
if pvObj.Spec.ClaimRef.Namespace == "" {
return "", errors.Errorf("namespace missing for pv %s", pvName)
}
ns = pvObj.Spec.ClaimRef.Namespace
// check for pv deployments in pv refclaim namespace
deployList, err = deployClient.WithNamespace(ns).List(
&metav1.ListOptions{
LabelSelector: pvLabel,
})
if err != nil {
return "", err
}
if len(deployList.Items) == 0 {
return "", errors.Errorf(
"failed to get deployments for pv %s in %s or %s namespace",
pvName,
openebsNamespace,
ns,
)
}
return ns, nil
}
func getReplicationFactor(ctrlLabel, namespace string) (int, error) {
ctrlList, err := podClient.WithNamespace(namespace).List(
metav1.ListOptions{
LabelSelector: ctrlLabel,
})
if err != nil {
return 0, err
}
if len(ctrlList.Items) == 0 {
return 0, errors.Errorf("no deployments found for %s in %s", ctrlLabel, namespace)
}
ctrlPod := ctrlList.Items[0]
// the only env in jiva target pod is "REPLICATION_FACTOR"
return strconv.Atoi(ctrlPod.Spec.Containers[0].Env[0].Value)
}
func getAPIURL(svcLabel, namespace string) (string, error) {
svcList, err := serviceClient.WithNamespace(namespace).List(
metav1.ListOptions{
LabelSelector: svcLabel,
})
if err != nil {
return "", err
}
if len(svcList.Items) == 0 {
return "", errors.Errorf("no service found for %s in %s", svcLabel, namespace)
}
targetIP := svcList.Items[0].Spec.ClusterIP
apiURL := "http://" + targetIP + ":9501/v1/replicas"
return apiURL, nil
}
func validateSync(pvLabel, namespace string) error {
klog.Infof("Verifying replica sync")
ctrlLabel := "openebs.io/controller=jiva-controller," + pvLabel
svcLabel := "openebs.io/controller-service=jiva-controller-svc," + pvLabel
quorum := false
syncedReplicas := 0
replicationFactor, err := getReplicationFactor(ctrlLabel, namespace)
if err != nil {
return err
}
apiURL, err := getAPIURL(svcLabel, namespace)
if err != nil {
return err
}
for syncedReplicas != replicationFactor {
syncedReplicas = 0
httpClient := &http.Client{Timeout: 2 * time.Second}
resp, err := httpClient.Get(apiURL)
if err != nil {
return err
}
defer resp.Body.Close()
replicas := jivaClient.ReplicaCollection{}
err = json.NewDecoder(resp.Body).Decode(&replicas)
if err != nil {
return err
}
for _, replica := range replicas.Data {
if replica.Mode == "RW" {
syncedReplicas = syncedReplicas + 1
}
}
if !quorum && syncedReplicas > (replicationFactor/2) {
klog.Infof("Synced replica quorum is reached")
quorum = true
}
}
klog.Infof("Replica syncing complete")
return nil
}
type jivaVolumeOptions struct {
utaskObj *utask.UpgradeTask
replicaObj *replicaDetails
controllerObj *controllerDetails
ns string
}
func (j *jivaVolumeOptions) preupgrade(pvName, openebsNamespace string) error {
var (
pvLabel = "openebs.io/persistent-volume=" + pvName
replicaLabel = "openebs.io/replica=jiva-replica," + pvLabel
controllerLabel = "openebs.io/controller=jiva-controller," + pvLabel
uerr, err error
)
statusObj := utask.UpgradeDetailedStatuses{Step: utask.PreUpgrade}
statusObj.Phase = utask.StepErrored
j.ns, err = getPVCDeploymentsNamespace(pvName, pvLabel, openebsNamespace)
if err != nil {
statusObj.Message = "failed to get namespace for pvc deployments"
statusObj.Reason = strings.Replace(err.Error(), ":", "", -1)
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
return errors.Wrapf(err, "failed to get namespace for pvc deployments")
}
// fetching replica deployment details
j.replicaObj, err = getReplica(replicaLabel, j.ns)
if err != nil {
statusObj.Message = "failed to get replica details"
statusObj.Reason = strings.Replace(err.Error(), ":", "", -1)
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
return err
}
j.replicaObj.patchDetails.PVName = pvName
// fetching controller deployment details
j.controllerObj, err = getController(controllerLabel, j.ns)
if err != nil {
statusObj.Message = "failed to get target details"
statusObj.Reason = strings.Replace(err.Error(), ":", "", -1)
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
return err
}
statusObj.Phase = utask.StepCompleted
statusObj.Message = "Pre-upgrade steps were successful"
statusObj.Reason = ""
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
return nil
}
func scaleDownTargetDeploy(name, namespace string) error {
klog.Infof("Scaling down target deploy %s in %s namespace", name, namespace)
deployObj, err := deployClient.WithNamespace(namespace).Get(name)
if err != nil {
return err
}
pvLabelKey := "openebs.io/persistent-volume"
pvName := deployObj.Labels[pvLabelKey]
controllerLabel := "openebs.io/controller=jiva-controller," +
pvLabelKey + "=" + pvName
var zero int32
deployObj.Spec.Replicas = &zero
_, err = deployClient.WithNamespace(namespace).Update(deployObj)
if err != nil {
return err
}
podList := &corev1.PodList{}
// Wait for up to 5 minutes for target pod to go away.
for i := 0; i < 60; i++ {
podList, err = podClient.WithNamespace(namespace).List(
metav1.ListOptions{
LabelSelector: controllerLabel,
})
if err != nil {
return err
}
if len(podList.Items) > 0 {
time.Sleep(time.Second * 5)
} else {
break
}
}
// If pod is not deleted within 5 minutes return error.
if len(podList.Items) > 0 {
return errors.Errorf("target pod still present")
}
return nil
}
func (j *jivaVolumeOptions) replicaUpgrade(openebsNamespace string) error {
var err, uerr error
statusObj := utask.UpgradeDetailedStatuses{Step: utask.ReplicaUpgrade}
statusObj.Phase = utask.StepWaiting
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
statusObj.Phase = utask.StepErrored
err = scaleDownTargetDeploy(j.controllerObj.name, j.ns)
if err != nil {
statusObj.Message = "failed to scale down target depoyment"
statusObj.Reason = strings.Replace(err.Error(), ":", "", -1)
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
return errors.Wrap(err, "failed to scale down target depoyment")
}
// replica patch
err = patchReplica(j.replicaObj, j.ns)
if err != nil {
statusObj.Message = "failed to patch replica depoyment"
statusObj.Reason = strings.Replace(err.Error(), ":", "", -1)
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
return err
}
statusObj.Phase = utask.StepCompleted
statusObj.Message = "Replica upgrade was successful"
statusObj.Reason = ""
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
return nil
}
func (j *jivaVolumeOptions) targetUpgrade(pvName, openebsNamespace string) error {
var err, uerr error
statusObj := utask.UpgradeDetailedStatuses{Step: utask.TargetUpgrade}
statusObj.Phase = utask.StepWaiting
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
statusObj.Phase = utask.StepErrored
// controller patch
err = patchController(j.controllerObj, j.ns)
if err != nil {
statusObj.Message = "failed to patch target depoyment"
statusObj.Reason = strings.Replace(err.Error(), ":", "", -1)
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
return err
}
pvLabel := "openebs.io/persistent-volume=" + pvName
serviceLabel := "openebs.io/controller-service=jiva-controller-svc," + pvLabel
err = patchService(serviceLabel, j.ns)
if err != nil {
statusObj.Message = "failed to patch target service"
statusObj.Reason = strings.Replace(err.Error(), ":", "", -1)
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
return err
}
statusObj.Phase = utask.StepCompleted
statusObj.Message = "Target upgrade was successful"
statusObj.Reason = ""
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
return nil
}
func (j *jivaVolumeOptions) verify(pvLabel, openebsNamespace string) error {
var err, uerr error
statusObj := utask.UpgradeDetailedStatuses{Step: utask.Verify}
statusObj.Phase = utask.StepWaiting
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
statusObj.Phase = utask.StepErrored
// Verify synced replicas
err = validateSync(pvLabel, j.ns)
if err != nil {
statusObj.Message = "failed to verify synced replicas. Please check it manually using the steps mentioned in https://docs.openebs.io/docs/next/mayactl.html"
statusObj.Reason = strings.Replace(err.Error(), ":", "", -1)
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
if k8serror.IsForbidden(err) {
klog.Warningf("failed to verify replica sync : %v\n Please check it manually using the steps mentioned in https://docs.openebs.io/docs/next/mayactl.html", err)
return nil
}
return err
}
statusObj.Phase = utask.StepCompleted
statusObj.Message = "Replica sync was successful"
statusObj.Reason = ""
j.utaskObj, uerr = updateUpgradeDetailedStatus(j.utaskObj, statusObj, openebsNamespace)
if uerr != nil && isENVPresent {
return uerr
}
return nil
}
func jivaUpgrade(pvName, openebsNamespace string, utaskObj *utask.UpgradeTask) (*utask.UpgradeTask, error) {
var (
pvLabel = "openebs.io/persistent-volume=" + pvName
err error
)
options := &jivaVolumeOptions{}
options.utaskObj = utaskObj
// PreUpgrade
err = options.preupgrade(pvName, openebsNamespace)
if err != nil {
return options.utaskObj, err
}
// ReplicaUpgrade
err = options.replicaUpgrade(openebsNamespace)
if err != nil {
return options.utaskObj, err
}
// TargetUpgrade
err = options.targetUpgrade(pvName, openebsNamespace)
if err != nil {
return options.utaskObj, err
}
// Verify
err = options.verify(pvLabel, openebsNamespace)
if err != nil {
return options.utaskObj, err
}
klog.Info("Upgrade Successful for", pvName)
return options.utaskObj, nil
}