/
move.go
183 lines (160 loc) · 5.1 KB
/
move.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
// This file is part of MinIO DirectPV
// Copyright (c) 2022 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 main
import (
"context"
"fmt"
"os"
"strings"
directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/client"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/types"
"github.com/minio/directpv/pkg/volume"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var moveCmd = &cobra.Command{
Use: "move SRC-DRIVE DEST-DRIVE",
Aliases: []string{"mv"},
Short: "Move volumes excluding data from source drive to destination drive on a same node.",
Example: strings.ReplaceAll(
`# Move volumes on drive af3b8b4c-73b4-4a74-84b7-1ec30492a6f0 to drive 834e8f4c-14f4-49b9-9b77-e8ac854108d5
$ kubectl {PLUGIN_NAME} drives move af3b8b4c-73b4-4a74-84b7-1ec30492a6f0 834e8f4c-14f4-49b9-9b77-e8ac854108d5`,
`{PLUGIN_NAME}`,
consts.AppName,
),
Run: func(c *cobra.Command, args []string) {
if len(args) != 2 {
eprintf(quietFlag, true, "only one source and one destination drive must be provided\n")
os.Exit(-1)
}
src := strings.TrimSpace(args[0])
if src == "" {
eprintf(quietFlag, true, "empty source drive\n")
os.Exit(-1)
}
dest := strings.TrimSpace(args[1])
if dest == "" {
eprintf(quietFlag, true, "empty destination drive\n")
os.Exit(-1)
}
moveMain(c.Context(), src, dest)
},
}
func moveMain(ctx context.Context, src, dest string) {
if src == dest {
eprintf(quietFlag, true, "source and destination drives are same\n")
os.Exit(1)
}
srcDrive, err := client.DriveClient().Get(ctx, src, metav1.GetOptions{})
if err != nil {
eprintf(quietFlag, true, "unable to get source drive; %v\n", err)
os.Exit(1)
}
if !srcDrive.IsUnschedulable() {
eprintf(quietFlag, true, "source drive is not cordoned\n")
os.Exit(1)
}
var requiredCapacity int64
var volumes []types.Volume
for result := range volume.NewLister().VolumeNameSelector(srcDrive.GetVolumes()).List(ctx) {
if result.Err != nil {
eprintf(quietFlag, true, "%v\n", result.Err)
os.Exit(1)
}
if result.Volume.IsPublished() {
eprintf(quietFlag, true, "cannot move published volume %v\n", result.Volume.Name)
os.Exit(1)
}
requiredCapacity += result.Volume.Status.TotalCapacity
volumes = append(volumes, result.Volume)
}
if len(volumes) == 0 {
eprintf(quietFlag, false, "No volumes found in source drive %v\n", src)
return
}
destDrive, err := client.DriveClient().Get(ctx, dest, metav1.GetOptions{})
if err != nil {
eprintf(quietFlag, true, "unable to get destination drive; %v\n", err)
os.Exit(1)
}
if destDrive.GetNodeID() != srcDrive.GetNodeID() {
eprintf(
quietFlag,
true,
"source and destination drives must be in same node; source node %v; desination node %v\n",
srcDrive.GetNodeID(),
destDrive.GetNodeID(),
)
os.Exit(1)
}
if !destDrive.IsUnschedulable() {
eprintf(quietFlag, true, "destination drive is not cordoned\n")
os.Exit(1)
}
if destDrive.Status.Status != directpvtypes.DriveStatusReady {
eprintf(quietFlag, true, "destination drive is not ready state\n")
os.Exit(1)
}
if srcDrive.GetAccessTier() != destDrive.GetAccessTier() {
eprintf(
quietFlag,
true,
"source drive access-tier %v and destination drive access-tier %v differ\n",
srcDrive.GetAccessTier(),
destDrive.GetAccessTier(),
)
os.Exit(1)
}
if destDrive.Status.FreeCapacity < requiredCapacity {
eprintf(
quietFlag,
true,
"insufficient free capacity on destination drive; required=%v free=%v\n",
printableBytes(requiredCapacity),
printableBytes(destDrive.Status.FreeCapacity),
)
os.Exit(1)
}
for _, volume := range volumes {
if destDrive.AddVolumeFinalizer(volume.Name) {
destDrive.Status.FreeCapacity -= requiredCapacity
destDrive.Status.AllocatedCapacity += requiredCapacity
}
}
destDrive.Status.Status = directpvtypes.DriveStatusMoving
_, err = client.DriveClient().Update(
ctx, destDrive, metav1.UpdateOptions{TypeMeta: types.NewDriveTypeMeta()},
)
if err != nil {
eprintf(quietFlag, true, "unable to move volumes to destination drive; %v\n", err)
os.Exit(1)
}
for _, volume := range volumes {
if !quietFlag {
fmt.Println("Moving volume", volume.Name)
}
}
srcDrive.ResetFinalizers()
_, err = client.DriveClient().Update(
ctx, srcDrive, metav1.UpdateOptions{TypeMeta: types.NewDriveTypeMeta()},
)
if err != nil {
eprintf(quietFlag, true, "unable to remove volume references in source drive; %v\n", err)
os.Exit(1)
}
}