-
Notifications
You must be signed in to change notification settings - Fork 83
/
utils.go
229 lines (199 loc) · 7 KB
/
utils.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
// 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 (
"context"
"errors"
"io/fs"
"os"
"path/filepath"
directcsi "github.com/minio/direct-csi/pkg/apis/direct.csi.min.io/v1beta2"
"github.com/minio/direct-csi/pkg/sys"
"github.com/minio/direct-csi/pkg/utils"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
)
func (d *Discovery) verifyDriveMount(existingDrive *directcsi.DirectCSIDrive) error {
driveMounter := &sys.DefaultDriveMounter{}
switch existingDrive.Status.DriveStatus {
case directcsi.DriveStatusInUse, directcsi.DriveStatusReady:
directCSIDevice := sys.GetDirectCSIPath(existingDrive.Status.FilesystemUUID)
// verify and fix the (major, minor) if it has changed
if err := sys.MakeBlockFile(directCSIDevice,
existingDrive.Status.MajorNumber,
existingDrive.Status.MinorNumber); err != nil {
return err
}
mountTarget := filepath.Join(sys.MountRoot, existingDrive.Status.FilesystemUUID)
// Check if the drive is mounted
isMounted := false
for _, mount := range d.mounts {
if mount.MountSource == directCSIDevice {
isMounted = true
break
}
}
// Mount if umounted
if !isMounted {
if err := driveMounter.MountDrive(directCSIDevice, mountTarget, []string{}); err != nil {
return err
}
existingDrive.Status.Mountpoint = mountTarget
}
}
return nil
}
func syncDriveStatesOnDiscovery(existingObj *directcsi.DirectCSIDrive, localDrive *directcsi.DirectCSIDrive) {
existingObjVersion := utils.GetLabelV(existingObj, utils.VersionLabel)
// overwrite existing object labels
existingObj.SetLabels(localDrive.GetLabels())
utils.UpdateLabels(existingObj,
utils.AccessTierLabel, string(existingObj.Status.AccessTier), // set access-tier labels
utils.VersionLabel, existingObjVersion, // set obj version labels
)
// Sync the possible states
existingObj.Status.RootPartition = localDrive.Status.RootPartition
existingObj.Status.PartitionNum = localDrive.Status.PartitionNum
existingObj.Status.Filesystem = localDrive.Status.Filesystem
existingObj.Status.Mountpoint = localDrive.Status.Mountpoint
existingObj.Status.MountOptions = localDrive.Status.MountOptions
existingObj.Status.ModelNumber = localDrive.Status.ModelNumber
existingObj.Status.PhysicalBlockSize = localDrive.Status.PhysicalBlockSize
existingObj.Status.LogicalBlockSize = localDrive.Status.LogicalBlockSize
existingObj.Status.Path = localDrive.Status.Path
existingObj.Status.FilesystemUUID = localDrive.Status.FilesystemUUID
existingObj.Status.SerialNumber = localDrive.Status.SerialNumber
existingObj.Status.PartitionUUID = localDrive.Status.PartitionUUID
existingObj.Status.MajorNumber = localDrive.Status.MajorNumber
existingObj.Status.MinorNumber = localDrive.Status.MinorNumber
existingObj.Status.TotalCapacity = localDrive.Status.TotalCapacity
if localDrive.Status.DriveStatus == directcsi.DriveStatusUnavailable {
existingObj.Status.DriveStatus = directcsi.DriveStatusUnavailable
existingObj.Status.Conditions = localDrive.Status.Conditions
existingObj.Spec.DirectCSIOwned = false
existingObj.Spec.RequestedFormat = nil
}
// Capacity sync
allocatedCapacity := localDrive.Status.AllocatedCapacity
if existingObj.Status.DriveStatus == directcsi.DriveStatusInUse {
// size reserved for allocated volumes
allocatedCapacity = existingObj.Status.AllocatedCapacity
}
existingObj.Status.FreeCapacity = localDrive.Status.TotalCapacity - allocatedCapacity
existingObj.Status.AllocatedCapacity = allocatedCapacity
}
func (d *Discovery) syncDrive(ctx context.Context, localDrive *directcsi.DirectCSIDrive) error {
directCSIClient := d.directcsiClient.DirectV1beta2()
driveClient := directCSIClient.DirectCSIDrives()
driveSync := func() error {
existingDrive, err := driveClient.Get(ctx, localDrive.ObjectMeta.Name, metav1.GetOptions{
TypeMeta: utils.DirectCSIDriveTypeMeta(),
})
if err != nil {
return err
}
// Sync remote drive states
syncDriveStatesOnDiscovery(existingDrive, localDrive)
// Verify mounts
message := ""
if err := d.verifyDriveMount(existingDrive); err != nil {
message = err.Error()
klog.V(3).Infof("mounting failed with: %v", err)
}
utils.UpdateCondition(existingDrive.Status.Conditions,
string(directcsi.DirectCSIDriveConditionInitialized),
utils.BoolToCondition(message == ""),
string(directcsi.DirectCSIDriveReasonInitialized),
message,
)
updateOpts := metav1.UpdateOptions{
TypeMeta: utils.DirectCSIDriveTypeMeta(),
}
_, err = driveClient.Update(ctx, existingDrive, updateOpts)
return err
}
if err := retry.RetryOnConflict(retry.DefaultRetry, driveSync); err != nil {
if !kerrors.IsNotFound(err) {
return err
}
}
return nil
}
func (d *Discovery) deleteUnmatchedRemoteDrives(ctx context.Context) error {
// For unmatched drives, check the OS if the underlying drive is still active
// of if it is lost.
// If lost, then delete the unmapped remote drives
// else, leave it running
directCSIClient := d.directcsiClient.DirectV1beta2()
driveClient := directCSIClient.DirectCSIDrives()
for _, remoteDrive := range d.remoteDrives {
if remoteDrive.matched {
continue
}
if ok, err := d.verifyDriveLost(remoteDrive.DirectCSIDrive); !ok {
klog.Errorf("drive %s still active, will not delete %v", remoteDrive.Name, err)
continue
}
if err := driveClient.Delete(ctx, remoteDrive.Name, metav1.DeleteOptions{}); err != nil {
return err
}
}
return nil
}
func (d *Discovery) verifyDriveLost(drive directcsi.DirectCSIDrive) (bool, error) {
path := sys.GetDirectCSIPath(drive.Status.FilesystemUUID)
stat, err := os.Stat(path)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return true, nil
}
return false, err
}
if stat.Mode() != fs.ModeDevice {
return true, nil
}
mountPoint := drive.Status.Mountpoint
mounts, errM := sys.ProbeMountInfo()
if errM != nil {
return false, errM
}
found := false
for _, m := range mounts {
if m.Mountpoint == mountPoint {
found = true
break
}
}
if !found {
return true, nil
}
fp := filepath.Join(mountPoint, ".drive_verifier")
f, err := os.OpenFile(fp,
os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return true, nil
}
defer func() {
f.Close()
os.RemoveAll(fp)
}()
if _, err := f.Write([]byte{0x00}); err != nil {
return true, nil
}
return false, nil
}