-
Notifications
You must be signed in to change notification settings - Fork 83
/
drive_matcher.go
114 lines (102 loc) · 3.71 KB
/
drive_matcher.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
// This file is part of MinIO Direct CSI
// Copyright (c) 2021 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package discovery
import (
"errors"
directcsi "github.com/minio/direct-csi/pkg/apis/direct.csi.min.io/v1beta2"
)
var (
ErrNoMatchFound = errors.New("No matching drive found")
)
func (d *Discovery) Identify(localDriveState directcsi.DirectCSIDriveStatus) (*remoteDrive, error) {
if len(d.remoteDrives) > 0 {
return d.identifyDriveByAttributes(localDriveState)
}
return nil, ErrNoMatchFound
}
func (d *Discovery) identifyDriveByAttributes(localDriveState directcsi.DirectCSIDriveStatus) (*remoteDrive, error) {
if selectedDrive, err := d.selectByFSUUID(localDriveState.FilesystemUUID); err == nil {
return selectedDrive, nil
}
if selectedDrive, err := d.selectByPartitionUUID(localDriveState.PartitionUUID); err == nil {
return selectedDrive, nil
}
if selectedDrive, err := d.selectBySerialNumber(localDriveState.SerialNumber,
localDriveState.PartitionNum); err == nil {
return selectedDrive, nil
}
return nil, ErrNoMatchFound
}
func (d *Discovery) selectByFSUUID(fsUUID string) (*remoteDrive, error) {
if fsUUID == "" {
// No FSUUID available to match
return nil, ErrNoMatchFound
}
for i, remoteDrive := range d.remoteDrives {
if !remoteDrive.matched &&
remoteDrive.Status.FilesystemUUID == fsUUID {
d.remoteDrives[i].matched = true
return d.remoteDrives[i], nil
}
}
return nil, ErrNoMatchFound
}
func (d *Discovery) selectByPartitionUUID(partUUID string) (*remoteDrive, error) {
if partUUID == "" {
// No partUUID available to match
return nil, ErrNoMatchFound
}
for i, remoteDrive := range d.remoteDrives {
if !remoteDrive.matched && remoteDrive.Status.PartitionUUID == partUUID {
d.remoteDrives[i].matched = true
return d.remoteDrives[i], nil
}
}
return nil, ErrNoMatchFound
}
func (d *Discovery) selectBySerialNumber(serialNumber string, partitionNum int) (*remoteDrive, error) {
if serialNumber == "" {
// No serialNumber available to match
return nil, ErrNoMatchFound
}
for i, remoteDrive := range d.remoteDrives {
if !remoteDrive.matched && remoteDrive.Status.SerialNumber == serialNumber && remoteDrive.Status.PartitionNum == partitionNum {
d.remoteDrives[i].matched = true
return d.remoteDrives[i], nil
}
}
return nil, ErrNoMatchFound
}
func (d *Discovery) identifyDriveByLegacyName(localDriveState directcsi.DirectCSIDriveStatus) (*remoteDrive, bool, error) {
v1beta1DriveName := makeV1beta1DriveName(d.NodeID, localDriveState.Path)
isNotUpgraded := func(driveObj directcsi.DirectCSIDrive) bool {
return driveObj.Status.SerialNumber == "" &&
driveObj.Status.FilesystemUUID == "" &&
driveObj.Status.PartitionUUID == "" &&
driveObj.Status.MajorNumber == uint32(0) &&
driveObj.Status.MinorNumber == uint32(0)
}
for i, remoteDrive := range d.remoteDrives {
if !remoteDrive.matched && remoteDrive.Name == v1beta1DriveName {
notUpgraded := isNotUpgraded(remoteDrive.DirectCSIDrive)
if notUpgraded {
d.remoteDrives[i].matched = true
}
return d.remoteDrives[i], notUpgraded, nil
}
}
return nil, false, ErrNoMatchFound
}