/
util.go
1105 lines (945 loc) · 40 KB
/
util.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package util
import (
"context"
"fmt"
"math"
"reflect"
"strconv"
"strings"
"sync"
"golang.org/x/mod/semver"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/labels"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
emeta "github.com/longhorn/longhorn-engine/pkg/meta"
"github.com/longhorn/longhorn-manager/meta"
"github.com/longhorn/longhorn-manager/types"
longhorn "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta2"
lhclientset "github.com/longhorn/longhorn-manager/k8s/pkg/client/clientset/versioned"
)
const (
// LonghornV1ToV2MinorVersionNum v1 minimal minor version when the upgrade path is from v1.x to v2.0
// TODO: decide the v1 minimum version that could be upgraded to v2.0
LonghornV1ToV2MinorVersionNum = 30
)
type ProgressMonitor struct {
description string
targetValue int
currentValue int
currentProgressInPercentage float64
mutex *sync.RWMutex
}
func NewProgressMonitor(description string, currentValue, targetValue int) *ProgressMonitor {
pm := &ProgressMonitor{
description: description,
targetValue: targetValue,
currentValue: currentValue,
currentProgressInPercentage: math.Floor(float64(currentValue*100) / float64(targetValue)),
mutex: &sync.RWMutex{},
}
pm.logCurrentProgress()
return pm
}
func (pm *ProgressMonitor) logCurrentProgress() {
logrus.Infof("%v: current progress %v%% (%v/%v)", pm.description, pm.currentProgressInPercentage, pm.currentValue, pm.targetValue)
}
func (pm *ProgressMonitor) Inc() int {
pm.mutex.Lock()
defer pm.mutex.Unlock()
oldProgressInPercentage := pm.currentProgressInPercentage
pm.currentValue++
pm.currentProgressInPercentage = math.Floor(float64(pm.currentValue*100) / float64(pm.targetValue))
if pm.currentProgressInPercentage != oldProgressInPercentage {
pm.logCurrentProgress()
}
return pm.currentValue
}
func (pm *ProgressMonitor) SetCurrentValue(newValue int) {
pm.mutex.Lock()
defer pm.mutex.Unlock()
oldProgressInPercentage := pm.currentProgressInPercentage
pm.currentValue = newValue
pm.currentProgressInPercentage = math.Floor(float64(pm.currentValue*100) / float64(pm.targetValue))
if pm.currentProgressInPercentage != oldProgressInPercentage {
pm.logCurrentProgress()
}
}
func (pm *ProgressMonitor) GetCurrentProgress() (int, int, float64) {
pm.mutex.RLock()
defer pm.mutex.RUnlock()
return pm.currentValue, pm.targetValue, pm.currentProgressInPercentage
}
func ListShareManagerPods(namespace string, kubeClient *clientset.Clientset) ([]corev1.Pod, error) {
smPodsList, err := kubeClient.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: labels.Set(types.GetShareManagerComponentLabel()).String(),
})
if err != nil {
return nil, err
}
return smPodsList.Items, nil
}
func ListIMPods(namespace string, kubeClient *clientset.Clientset) ([]corev1.Pod, error) {
imPodsList, err := kubeClient.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", types.GetLonghornLabelComponentKey(), types.LonghornLabelInstanceManager),
})
if err != nil {
return nil, err
}
return imPodsList.Items, nil
}
func ListManagerPods(namespace string, kubeClient *clientset.Clientset) ([]corev1.Pod, error) {
managerPodsList, err := kubeClient.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: labels.Set(types.GetManagerLabels()).String(),
})
if err != nil {
return nil, err
}
return managerPodsList.Items, nil
}
func MergeStringMaps(baseMap, overwriteMap map[string]string) map[string]string {
result := map[string]string{}
for k, v := range baseMap {
result[k] = v
}
for k, v := range overwriteMap {
result[k] = v
}
return result
}
func GetCurrentLonghornVersion(namespace string, lhClient lhclientset.Interface) (string, error) {
currentLHVersionSetting, err := lhClient.LonghornV1beta2().Settings(namespace).Get(context.TODO(), string(types.SettingNameCurrentLonghornVersion), metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return "", nil
}
return "", err
}
return currentLHVersionSetting.Value, nil
}
func GetCurrentReferredLonghornEngineImageVersions(namespace string, lhClient lhclientset.Interface) (map[string]emeta.VersionOutput, error) {
engineImages, err := lhClient.LonghornV1beta2().EngineImages(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil, nil
}
return nil, err
}
engineImageVersionMap := map[string]emeta.VersionOutput{}
for _, engineImage := range engineImages.Items {
if engineImage.Status.RefCount == 0 {
continue
}
engineImageVersionMap[engineImage.Name] = emeta.VersionOutput{
ControllerAPIVersion: engineImage.Status.ControllerAPIVersion,
CLIAPIVersion: engineImage.Status.CLIAPIVersion,
}
}
return engineImageVersionMap, nil
}
func CreateOrUpdateLonghornVersionSetting(namespace string, lhClient *lhclientset.Clientset) error {
s, err := lhClient.LonghornV1beta2().Settings(namespace).Get(context.TODO(), string(types.SettingNameCurrentLonghornVersion), metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
return err
}
s = &longhorn.Setting{
ObjectMeta: metav1.ObjectMeta{
Name: string(types.SettingNameCurrentLonghornVersion),
},
Value: meta.Version,
}
_, err := lhClient.LonghornV1beta2().Settings(namespace).Create(context.TODO(), s, metav1.CreateOptions{})
return err
}
if s.Value != meta.Version {
s.Value = meta.Version
if s.Annotations == nil {
s.Annotations = make(map[string]string)
}
s.Annotations[types.GetLonghornLabelKey(types.UpdateSettingFromLonghorn)] = ""
s, err = lhClient.LonghornV1beta2().Settings(namespace).Update(context.TODO(), s, metav1.UpdateOptions{})
if err != nil {
return err
}
delete(s.Annotations, types.GetLonghornLabelKey(types.UpdateSettingFromLonghorn))
_, err = lhClient.LonghornV1beta2().Settings(namespace).Update(context.TODO(), s, metav1.UpdateOptions{})
return err
}
return nil
}
func CheckUpgradePathSupported(namespace string, lhClient lhclientset.Interface) error {
if err := checkLHUpgradePathSupported(namespace, lhClient); err != nil {
return err
}
return checkEngineUpgradePathSupported(namespace, lhClient, emeta.GetVersion())
}
// checkLHUpgradePathSupported returns if the upgrade path from lhCurrentVersion to meta.Version is supported.
//
// For example: upgrade path is from x.y.z to a.b.c,
// 0 <= a-x <= 1 is supported, and y should be after a specific version if a-x == 1
// 0 <= b-y <= 1 is supported when a-x == 0
// all downgrade is not supported
func checkLHUpgradePathSupported(namespace string, lhClient lhclientset.Interface) error {
lhCurrentVersion, err := GetCurrentLonghornVersion(namespace, lhClient)
if err != nil {
return err
}
if lhCurrentVersion == "" {
return nil
}
logrus.Infof("Checking if the upgrade path from %v to %v is supported", lhCurrentVersion, meta.Version)
if !semver.IsValid(meta.Version) {
return fmt.Errorf("failed to upgrade since upgrading version %v is not valid", meta.Version)
}
lhNewMajorVersion := semver.Major(meta.Version)
lhCurrentMajorVersion := semver.Major(lhCurrentVersion)
lhNewMajorVersionNum, lhNewMinorVersionNum, err := getMajorMinorInt(meta.Version)
if err != nil {
return errors.Wrapf(err, "failed to parse upgrading %v major/minor version", meta.Version)
}
lhCurrentMajorVersionNum, lhCurrentMinorVersionNum, err := getMajorMinorInt(lhCurrentVersion)
if err != nil {
return errors.Wrapf(err, "failed to parse current %v major/minor version", meta.Version)
}
if semver.Compare(lhCurrentMajorVersion, lhNewMajorVersion) > 0 {
return fmt.Errorf("failed to upgrade since downgrading from %v to %v for major version is not supported", lhCurrentVersion, meta.Version)
}
if semver.Compare(lhCurrentMajorVersion, lhNewMajorVersion) < 0 {
if (lhNewMajorVersionNum - lhCurrentMajorVersionNum) > 1 {
return fmt.Errorf("failed to upgrade since upgrading from %v to %v for major version is not supported", lhCurrentVersion, meta.Version)
}
if lhCurrentMinorVersionNum < LonghornV1ToV2MinorVersionNum {
return fmt.Errorf("failed to upgrade since upgrading major version with minor version under %v is not supported", LonghornV1ToV2MinorVersionNum)
}
return nil
}
if (lhNewMinorVersionNum - lhCurrentMinorVersionNum) > 1 {
return fmt.Errorf("failed to upgrade since upgrading from %v to %v for minor version is not supported", lhCurrentVersion, meta.Version)
}
if (lhNewMinorVersionNum - lhCurrentMinorVersionNum) == 1 {
return nil
}
if semver.Compare(lhCurrentVersion, meta.Version) > 0 {
return fmt.Errorf("failed to upgrade since downgrading from %v to %v is not supported", lhCurrentVersion, meta.Version)
}
return nil
}
// checkEngineUpgradePathSupported returns error if the upgrade path from
// current EngineImage version to emeta.ControllerAPIVersion is not supported.
func checkEngineUpgradePathSupported(namespace string, lhClient lhclientset.Interface, newEngineVersion emeta.VersionOutput) (err error) {
defer func() {
err = errors.Wrapf(err, "failed checking Engine upgrade path")
}()
engineImageVersions, err := GetCurrentReferredLonghornEngineImageVersions(namespace, lhClient)
if err != nil {
return err
}
if len(engineImageVersions) == 0 {
return nil
}
logrus.WithFields(logrus.Fields{
"newEngineControllerAPIVersion": newEngineVersion.ControllerAPIVersion,
"newEngineControllerAPIMinVersion": newEngineVersion.ControllerAPIMinVersion,
"newEngineClientAPIVersion": newEngineVersion.CLIAPIVersion,
"newEngineClientAPIMinVersion": newEngineVersion.CLIAPIMinVersion,
}).Infof("Checking if the engine upgrade path from %+v is supported", engineImageVersions)
checkSupportVersion := func(currentVersion, newVersion, newMinVersion int) error {
if currentVersion > newVersion {
return fmt.Errorf("downgrading from %v to %v is not supported", currentVersion, newVersion)
}
if currentVersion < newMinVersion {
return fmt.Errorf("found version %v is below required minimal version %v", currentVersion, newMinVersion)
}
return nil
}
for name, engineImageVersion := range engineImageVersions {
err = checkSupportVersion(engineImageVersion.ControllerAPIVersion, newEngineVersion.ControllerAPIVersion, newEngineVersion.ControllerAPIMinVersion)
if err != nil {
return errors.Wrapf(err, "incompatible Engine %v controller API version", name)
}
err = checkSupportVersion(engineImageVersion.CLIAPIVersion, newEngineVersion.CLIAPIVersion, newEngineVersion.CLIAPIMinVersion)
if err != nil {
return errors.Wrapf(err, "incompatible Engine %v client API version", name)
}
}
return nil
}
func DeleteRemovedSettings(namespace string, lhClient *lhclientset.Clientset) error {
isKnownSetting := func(knownSettingNames []types.SettingName, name types.SettingName) bool {
for _, knownSettingName := range knownSettingNames {
if name == knownSettingName {
return true
}
}
return false
}
existingSettingList, err := lhClient.LonghornV1beta2().Settings(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
for _, existingSetting := range existingSettingList.Items {
if !isKnownSetting(types.SettingNameList, types.SettingName(existingSetting.Name)) {
logrus.Infof("Deleting removed setting: %v", existingSetting.Name)
if err = lhClient.LonghornV1beta2().Settings(namespace).Delete(context.TODO(), existingSetting.Name, metav1.DeleteOptions{}); err != nil {
return err
}
}
}
return nil
}
func getMajorMinorInt(v string) (int, int, error) {
majorNum, err := getMajorInt(v)
if err != nil {
return -1, -1, err
}
minorNum, err := getMinorInt(v)
if err != nil {
return -1, -1, err
}
return majorNum, minorNum, nil
}
func getMajorInt(v string) (int, error) {
versionStr := removeFirstChar(v)
return strconv.Atoi(strings.Split(versionStr, ".")[0])
}
func getMinorInt(v string) (int, error) {
versionStr := removeFirstChar(v)
return strconv.Atoi(strings.Split(versionStr, ".")[1])
}
func removeFirstChar(v string) string {
if v[0] == 'v' {
return v[1:]
}
return v
}
// ListAndUpdateSettingsInProvidedCache list all settings and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateSettingsInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.Setting, error) {
if v, ok := resourceMaps[types.LonghornKindSetting]; ok {
return v.(map[string]*longhorn.Setting), nil
}
settings := map[string]*longhorn.Setting{}
settingList, err := lhClient.LonghornV1beta2().Settings(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, setting := range settingList.Items {
settingCopy := longhorn.Setting{}
if err := copier.Copy(&settingCopy, setting); err != nil {
return nil, err
}
settings[setting.Name] = &settingCopy
}
resourceMaps[types.LonghornKindSetting] = settings
return settings, nil
}
// ListAndUpdateNodesInProvidedCache list all nodes and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateNodesInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.Node, error) {
if v, ok := resourceMaps[types.LonghornKindNode]; ok {
return v.(map[string]*longhorn.Node), nil
}
nodes := map[string]*longhorn.Node{}
nodeList, err := lhClient.LonghornV1beta2().Nodes(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, node := range nodeList.Items {
nodes[node.Name] = &nodeList.Items[i]
}
resourceMaps[types.LonghornKindNode] = nodes
return nodes, nil
}
// ListAndUpdateOrphansInProvidedCache list all orphans and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateOrphansInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.Orphan, error) {
if v, ok := resourceMaps[types.LonghornKindOrphan]; ok {
return v.(map[string]*longhorn.Orphan), nil
}
orphans := map[string]*longhorn.Orphan{}
orphanList, err := lhClient.LonghornV1beta2().Orphans(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, orphan := range orphanList.Items {
orphans[orphan.Name] = &orphanList.Items[i]
}
resourceMaps[types.LonghornKindOrphan] = orphans
return orphans, nil
}
// ListAndUpdateInstanceManagersInProvidedCache list all instanceManagers and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateInstanceManagersInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.InstanceManager, error) {
if v, ok := resourceMaps[types.LonghornKindInstanceManager]; ok {
return v.(map[string]*longhorn.InstanceManager), nil
}
ims := map[string]*longhorn.InstanceManager{}
imList, err := lhClient.LonghornV1beta2().InstanceManagers(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, im := range imList.Items {
ims[im.Name] = &imList.Items[i]
}
resourceMaps[types.LonghornKindInstanceManager] = ims
return ims, nil
}
// ListAndUpdateVolumesInProvidedCache list all volumes and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateVolumesInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.Volume, error) {
if v, ok := resourceMaps[types.LonghornKindVolume]; ok {
return v.(map[string]*longhorn.Volume), nil
}
volumes := map[string]*longhorn.Volume{}
volumeList, err := lhClient.LonghornV1beta2().Volumes(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, volume := range volumeList.Items {
volumes[volume.Name] = &volumeList.Items[i]
}
resourceMaps[types.LonghornKindVolume] = volumes
return volumes, nil
}
// ListAndUpdateReplicasInProvidedCache list all replicas and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateReplicasInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.Replica, error) {
if v, ok := resourceMaps[types.LonghornKindReplica]; ok {
return v.(map[string]*longhorn.Replica), nil
}
replicas := map[string]*longhorn.Replica{}
replicaList, err := lhClient.LonghornV1beta2().Replicas(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, replica := range replicaList.Items {
replicas[replica.Name] = &replicaList.Items[i]
}
resourceMaps[types.LonghornKindReplica] = replicas
return replicas, nil
}
// ListAndUpdateEnginesInProvidedCache list all engines and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateEnginesInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.Engine, error) {
if v, ok := resourceMaps[types.LonghornKindEngine]; ok {
return v.(map[string]*longhorn.Engine), nil
}
engines := map[string]*longhorn.Engine{}
engineList, err := lhClient.LonghornV1beta2().Engines(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, engine := range engineList.Items {
engines[engine.Name] = &engineList.Items[i]
}
resourceMaps[types.LonghornKindEngine] = engines
return engines, nil
}
// ListAndUpdateBackupsInProvidedCache list all backups and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateBackupsInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.Backup, error) {
if v, ok := resourceMaps[types.LonghornKindBackup]; ok {
return v.(map[string]*longhorn.Backup), nil
}
backups := map[string]*longhorn.Backup{}
backupList, err := lhClient.LonghornV1beta2().Backups(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, backup := range backupList.Items {
backups[backup.Name] = &backupList.Items[i]
}
resourceMaps[types.LonghornKindBackup] = backups
return backups, nil
}
// ListAndUpdateSnapshotsInProvidedCache list all snapshots and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateSnapshotsInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.Snapshot, error) {
if v, ok := resourceMaps[types.LonghornKindSnapshot]; ok {
return v.(map[string]*longhorn.Snapshot), nil
}
snapshots := map[string]*longhorn.Snapshot{}
snapshotList, err := lhClient.LonghornV1beta2().Snapshots(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, snapshot := range snapshotList.Items {
snapshots[snapshot.Name] = &snapshotList.Items[i]
}
resourceMaps[types.LonghornKindSnapshot] = snapshots
return snapshots, nil
}
// ListAndUpdateEngineImagesInProvidedCache list all engineImages and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateEngineImagesInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.EngineImage, error) {
if v, ok := resourceMaps[types.LonghornKindEngineImage]; ok {
return v.(map[string]*longhorn.EngineImage), nil
}
eis := map[string]*longhorn.EngineImage{}
eiList, err := lhClient.LonghornV1beta2().EngineImages(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, ei := range eiList.Items {
eis[ei.Name] = &eiList.Items[i]
}
resourceMaps[types.LonghornKindEngineImage] = eis
return eis, nil
}
// ListAndUpdateShareManagersInProvidedCache list all shareManagers and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateShareManagersInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.ShareManager, error) {
if v, ok := resourceMaps[types.LonghornKindShareManager]; ok {
return v.(map[string]*longhorn.ShareManager), nil
}
sms := map[string]*longhorn.ShareManager{}
smList, err := lhClient.LonghornV1beta2().ShareManagers(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, sm := range smList.Items {
sms[sm.Name] = &smList.Items[i]
}
resourceMaps[types.LonghornKindShareManager] = sms
return sms, nil
}
// ListAndUpdateBackingImagesInProvidedCache list all backingImages and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateBackingImagesInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.BackingImage, error) {
if v, ok := resourceMaps[types.LonghornKindBackingImage]; ok {
return v.(map[string]*longhorn.BackingImage), nil
}
bis := map[string]*longhorn.BackingImage{}
biList, err := lhClient.LonghornV1beta2().BackingImages(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, bi := range biList.Items {
bis[bi.Name] = &biList.Items[i]
}
resourceMaps[types.LonghornKindBackingImage] = bis
return bis, nil
}
// ListAndUpdateBackingImageDataSourcesInProvidedCache list all backingImageDataSources and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateBackingImageDataSourcesInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.BackingImageDataSource, error) {
if v, ok := resourceMaps[types.LonghornKindBackingImageDataSource]; ok {
return v.(map[string]*longhorn.BackingImageDataSource), nil
}
bidss := map[string]*longhorn.BackingImageDataSource{}
bidsList, err := lhClient.LonghornV1beta2().BackingImageDataSources(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, bids := range bidsList.Items {
bidss[bids.Name] = &bidsList.Items[i]
}
resourceMaps[types.LonghornKindBackingImageDataSource] = bidss
return bidss, nil
}
// ListAndUpdateRecurringJobsInProvidedCache list all recurringJobs and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateRecurringJobsInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.RecurringJob, error) {
if v, ok := resourceMaps[types.LonghornKindRecurringJob]; ok {
return v.(map[string]*longhorn.RecurringJob), nil
}
recurringJobs := map[string]*longhorn.RecurringJob{}
recurringJobList, err := lhClient.LonghornV1beta2().RecurringJobs(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, recurringJob := range recurringJobList.Items {
recurringJobs[recurringJob.Name] = &recurringJobList.Items[i]
}
resourceMaps[types.LonghornKindRecurringJob] = recurringJobs
return recurringJobs, nil
}
// ListAndUpdateVolumeAttachmentsInProvidedCache list all volumeAttachments and save them into the provided cached `resourceMap`. This method is not thread-safe.
func ListAndUpdateVolumeAttachmentsInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) (map[string]*longhorn.VolumeAttachment, error) {
if v, ok := resourceMaps[types.LonghornKindVolumeAttachment]; ok {
return v.(map[string]*longhorn.VolumeAttachment), nil
}
volumeAttachments := map[string]*longhorn.VolumeAttachment{}
volumeAttachmentList, err := lhClient.LonghornV1beta2().VolumeAttachments(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i, volumeAttachment := range volumeAttachmentList.Items {
volumeAttachments[volumeAttachment.Name] = &volumeAttachmentList.Items[i]
}
resourceMaps[types.LonghornKindVolumeAttachment] = volumeAttachments
return volumeAttachments, nil
}
// CreateAndUpdateRecurringJobInProvidedCache creates a recurringJob and saves it into the provided cached `resourceMap`. This method is not thread-safe.
func CreateAndUpdateRecurringJobInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}, job *longhorn.RecurringJob) (*longhorn.RecurringJob, error) {
obj, err := lhClient.LonghornV1beta2().RecurringJobs(namespace).Create(context.TODO(), job, metav1.CreateOptions{})
if err != nil {
return obj, err
}
var recurringJobs map[string]*longhorn.RecurringJob
if v, ok := resourceMaps[types.LonghornKindRecurringJob]; ok {
recurringJobs = v.(map[string]*longhorn.RecurringJob)
} else {
recurringJobs = map[string]*longhorn.RecurringJob{}
}
recurringJobs[job.Name] = obj
resourceMaps[types.LonghornKindRecurringJob] = recurringJobs
return obj, nil
}
// CreateAndUpdateBackingImageInProvidedCache creates a backingImage and saves it into the provided cached `resourceMap`. This method is not thread-safe.
func CreateAndUpdateBackingImageInProvidedCache(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}, bid *longhorn.BackingImageDataSource) (*longhorn.BackingImageDataSource, error) {
obj, err := lhClient.LonghornV1beta2().BackingImageDataSources(namespace).Create(context.TODO(), bid, metav1.CreateOptions{})
if err != nil {
return obj, err
}
var bids map[string]*longhorn.BackingImageDataSource
if v, ok := resourceMaps[types.LonghornKindBackingImageDataSource]; ok {
bids = v.(map[string]*longhorn.BackingImageDataSource)
} else {
bids = map[string]*longhorn.BackingImageDataSource{}
}
bids[bid.Name] = obj
resourceMaps[types.LonghornKindBackingImageDataSource] = bids
return obj, nil
}
// UpdateResources persists all the resources' spec changes in provided cached `resourceMap`. This method is not thread-safe.
func UpdateResources(namespace string, lhClient *lhclientset.Clientset, resourceMaps map[string]interface{}) error {
var err error
for resourceKind, resourceMap := range resourceMaps {
switch resourceKind {
case types.LonghornKindNode:
err = updateNodes(namespace, lhClient, resourceMap.(map[string]*longhorn.Node))
case types.LonghornKindVolume:
err = updateVolumes(namespace, lhClient, resourceMap.(map[string]*longhorn.Volume))
case types.LonghornKindEngine:
err = updateEngines(namespace, lhClient, resourceMap.(map[string]*longhorn.Engine))
case types.LonghornKindReplica:
err = updateReplicas(namespace, lhClient, resourceMap.(map[string]*longhorn.Replica))
case types.LonghornKindBackup:
err = updateBackups(namespace, lhClient, resourceMap.(map[string]*longhorn.Backup))
case types.LonghornKindEngineImage:
err = updateEngineImages(namespace, lhClient, resourceMap.(map[string]*longhorn.EngineImage))
case types.LonghornKindInstanceManager:
err = updateInstanceManagers(namespace, lhClient, resourceMap.(map[string]*longhorn.InstanceManager))
case types.LonghornKindShareManager:
err = updateShareManagers(namespace, lhClient, resourceMap.(map[string]*longhorn.ShareManager))
case types.LonghornKindBackingImage:
err = updateBackingImages(namespace, lhClient, resourceMap.(map[string]*longhorn.BackingImage))
case types.LonghornKindRecurringJob:
err = updateRecurringJobs(namespace, lhClient, resourceMap.(map[string]*longhorn.RecurringJob))
case types.LonghornKindSetting:
err = updateSettings(namespace, lhClient, resourceMap.(map[string]*longhorn.Setting))
case types.LonghornKindVolumeAttachment:
err = updateVolumeAttachments(namespace, lhClient, resourceMap.(map[string]*longhorn.VolumeAttachment))
case types.LonghornKindSnapshot:
err = updateSnapshots(namespace, lhClient, resourceMap.(map[string]*longhorn.Snapshot))
case types.LonghornKindOrphan:
err = updateOrphans(namespace, lhClient, resourceMap.(map[string]*longhorn.Orphan))
default:
return fmt.Errorf("resource kind %v is not able to updated", resourceKind)
}
if err != nil {
return err
}
}
return nil
}
func updateNodes(namespace string, lhClient *lhclientset.Clientset, nodes map[string]*longhorn.Node) error {
existingNodeList, err := lhClient.LonghornV1beta2().Nodes(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
for _, existingNode := range existingNodeList.Items {
node, ok := nodes[existingNode.Name]
if !ok {
continue
}
if !reflect.DeepEqual(existingNode.Spec, node.Spec) ||
!reflect.DeepEqual(existingNode.ObjectMeta, node.ObjectMeta) {
if _, err = lhClient.LonghornV1beta2().Nodes(namespace).Update(context.TODO(), node, metav1.UpdateOptions{}); err != nil {
return err
}
}
}
return nil
}
func updateVolumes(namespace string, lhClient *lhclientset.Clientset, volumes map[string]*longhorn.Volume) error {
existingVolumeList, err := lhClient.LonghornV1beta2().Volumes(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
for _, existingVolume := range existingVolumeList.Items {
volume, ok := volumes[existingVolume.Name]
if !ok {
continue
}
if !reflect.DeepEqual(existingVolume.Spec, volume.Spec) ||
!reflect.DeepEqual(existingVolume.ObjectMeta, volume.ObjectMeta) {
if _, err = lhClient.LonghornV1beta2().Volumes(namespace).Update(context.TODO(), volume, metav1.UpdateOptions{}); err != nil {
return err
}
}
}
return nil
}
func updateReplicas(namespace string, lhClient *lhclientset.Clientset, replicas map[string]*longhorn.Replica) error {
existingReplicaList, err := lhClient.LonghornV1beta2().Replicas(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
for _, existingReplica := range existingReplicaList.Items {
replica, ok := replicas[existingReplica.Name]
if !ok {
continue
}
if !reflect.DeepEqual(existingReplica.Spec, replica.Spec) ||
!reflect.DeepEqual(existingReplica.ObjectMeta, replica.ObjectMeta) {
if _, err = lhClient.LonghornV1beta2().Replicas(namespace).Update(context.TODO(), replica, metav1.UpdateOptions{}); err != nil {
return err
}
}
}
return nil
}
func updateEngines(namespace string, lhClient *lhclientset.Clientset, engines map[string]*longhorn.Engine) error {
existingEngineList, err := lhClient.LonghornV1beta2().Engines(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
for _, existingEngine := range existingEngineList.Items {
engine, ok := engines[existingEngine.Name]
if !ok {
continue
}
if !reflect.DeepEqual(existingEngine.Spec, engine.Spec) ||
!reflect.DeepEqual(existingEngine.ObjectMeta, engine.ObjectMeta) {
if _, err = lhClient.LonghornV1beta2().Engines(namespace).Update(context.TODO(), engine, metav1.UpdateOptions{}); err != nil {
return err
}
}
}
return nil
}
func updateBackups(namespace string, lhClient *lhclientset.Clientset, backups map[string]*longhorn.Backup) error {
existingBackupList, err := lhClient.LonghornV1beta2().Backups(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
for _, existingBackup := range existingBackupList.Items {
backup, ok := backups[existingBackup.Name]
if !ok {
continue
}
if !reflect.DeepEqual(existingBackup.Spec, backup.Spec) ||
!reflect.DeepEqual(existingBackup.ObjectMeta, backup.ObjectMeta) {
if _, err = lhClient.LonghornV1beta2().Backups(namespace).Update(context.TODO(), backup, metav1.UpdateOptions{}); err != nil {
return err
}
}
}
return nil
}
func updateEngineImages(namespace string, lhClient *lhclientset.Clientset, engineImages map[string]*longhorn.EngineImage) error {
existingEngineImageList, err := lhClient.LonghornV1beta2().EngineImages(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
for _, existingEngineImage := range existingEngineImageList.Items {
engineImage, ok := engineImages[existingEngineImage.Name]
if !ok {
continue
}
if !reflect.DeepEqual(existingEngineImage.Spec, engineImage.Spec) ||
!reflect.DeepEqual(existingEngineImage.ObjectMeta, engineImage.ObjectMeta) {
if _, err = lhClient.LonghornV1beta2().EngineImages(namespace).Update(context.TODO(), engineImage, metav1.UpdateOptions{}); err != nil {
return err
}
}
}
return nil
}
func updateInstanceManagers(namespace string, lhClient *lhclientset.Clientset, instanceManagers map[string]*longhorn.InstanceManager) error {
existingInstanceManagerList, err := lhClient.LonghornV1beta2().InstanceManagers(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
for _, existingInstanceManager := range existingInstanceManagerList.Items {
instanceManager, ok := instanceManagers[existingInstanceManager.Name]
if !ok {
continue
}
if !reflect.DeepEqual(existingInstanceManager.Spec, instanceManager.Spec) ||
!reflect.DeepEqual(existingInstanceManager.ObjectMeta, instanceManager.ObjectMeta) {
if _, err = lhClient.LonghornV1beta2().InstanceManagers(namespace).Update(context.TODO(), instanceManager, metav1.UpdateOptions{}); err != nil {
return err
}
}
}
return nil
}
func updateShareManagers(namespace string, lhClient *lhclientset.Clientset, shareManagers map[string]*longhorn.ShareManager) error {
existingShareManagerList, err := lhClient.LonghornV1beta2().ShareManagers(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
for _, existingShareManager := range existingShareManagerList.Items {
shareManager, ok := shareManagers[existingShareManager.Name]
if !ok {
continue
}
if !reflect.DeepEqual(existingShareManager.Spec, shareManager.Spec) ||
!reflect.DeepEqual(existingShareManager.ObjectMeta, shareManager.ObjectMeta) {
if _, err = lhClient.LonghornV1beta2().ShareManagers(namespace).Update(context.TODO(), shareManager, metav1.UpdateOptions{}); err != nil {
return err
}
}
}
return nil
}
func updateBackingImages(namespace string, lhClient *lhclientset.Clientset, backingImages map[string]*longhorn.BackingImage) error {
existingBackingImagesList, err := lhClient.LonghornV1beta2().BackingImages(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
for _, existingBackingImage := range existingBackingImagesList.Items {
backingImage, ok := backingImages[existingBackingImage.Name]
if !ok {
continue
}
if !reflect.DeepEqual(existingBackingImage.Spec, backingImage.Spec) ||
!reflect.DeepEqual(existingBackingImage.ObjectMeta, backingImage.ObjectMeta) {
if _, err = lhClient.LonghornV1beta2().BackingImages(namespace).Update(context.TODO(), backingImage, metav1.UpdateOptions{}); err != nil {
return err
}
}
}
return nil
}
func updateRecurringJobs(namespace string, lhClient *lhclientset.Clientset, recurringJobs map[string]*longhorn.RecurringJob) error {
existingRecurringJobList, err := lhClient.LonghornV1beta2().RecurringJobs(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
for _, existingRecurringJob := range existingRecurringJobList.Items {
recurringJob, ok := recurringJobs[existingRecurringJob.Name]
if !ok {
continue
}
if !reflect.DeepEqual(existingRecurringJob.Spec, recurringJob.Spec) ||
!reflect.DeepEqual(existingRecurringJob.ObjectMeta, recurringJob.ObjectMeta) {
if _, err = lhClient.LonghornV1beta2().RecurringJobs(namespace).Update(context.TODO(), recurringJob, metav1.UpdateOptions{}); err != nil {
return err
}
}
}
return nil
}
func updateSettings(namespace string, lhClient *lhclientset.Clientset, settings map[string]*longhorn.Setting) error {
existingSettingList, err := lhClient.LonghornV1beta2().Settings(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
for _, existingSetting := range existingSettingList.Items {
setting, ok := settings[existingSetting.Name]
if !ok {
continue
}
if !reflect.DeepEqual(existingSetting.Value, setting.Value) {
if setting.Annotations == nil {
setting.Annotations = make(map[string]string)
}
setting.Annotations[types.GetLonghornLabelKey(types.UpdateSettingFromLonghorn)] = ""
setting, err = lhClient.LonghornV1beta2().Settings(namespace).Update(context.TODO(), setting, metav1.UpdateOptions{})
if err != nil {
return err
}
delete(setting.Annotations, types.GetLonghornLabelKey(types.UpdateSettingFromLonghorn))
if _, err = lhClient.LonghornV1beta2().Settings(namespace).Update(context.TODO(), setting, metav1.UpdateOptions{}); err != nil {
return err
}