/
list_drives.go
236 lines (209 loc) · 5.44 KB
/
list_drives.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
// This file is part of MinIO DirectPV
// Copyright (c) 2021, 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"
"github.com/jedib0t/go-pretty/v6/table"
directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/drive"
"github.com/minio/directpv/pkg/types"
"github.com/minio/directpv/pkg/utils"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var listDrivesCmd = &cobra.Command{
Use: "drives [DRIVE ...]",
Aliases: []string{"drive", "dr"},
Short: "List drives",
SilenceUsage: true,
SilenceErrors: true,
Example: strings.ReplaceAll(
`1. List all ready drives
$ kubectl {PLUGIN_NAME} list drives
2. List all drives from a node
$ kubectl {PLUGIN_NAME} list drives --nodes=node1
3. List a drive from all nodes
$ kubectl {PLUGIN_NAME} list drives --drives=nvme1n1
4. List specific drives from specific nodes
$ kubectl {PLUGIN_NAME} list drives --nodes=node{1...4} --drives=sd{a...f}
5. List drives are in 'error' status
$ kubectl {PLUGIN_NAME} list drives --status=error
6. List all drives from all nodes with all information.
$ kubectl {PLUGIN_NAME} list drives --output wide
7. List drives with labels.
$ kubectl {PLUGIN_NAME} list drives --show-labels
8. List drives filtered by labels
$ kubectl {PLUGIN_NAME} list drives --labels tier=hot`,
`{PLUGIN_NAME}`,
consts.AppName,
),
Run: func(c *cobra.Command, args []string) {
driveIDArgs = args
if err := validateListDrivesArgs(); err != nil {
utils.Eprintf(quietFlag, true, "%v\n", err)
os.Exit(-1)
}
listDrivesMain(c.Context())
},
}
func init() {
listDrivesCmd.Flags().SortFlags = false
listDrivesCmd.InheritedFlags().SortFlags = false
listDrivesCmd.LocalFlags().SortFlags = false
listDrivesCmd.LocalNonPersistentFlags().SortFlags = false
listDrivesCmd.NonInheritedFlags().SortFlags = false
listDrivesCmd.PersistentFlags().SortFlags = false
addDriveStatusFlag(listDrivesCmd, "Filter output by drive status")
addShowLabelsFlag(listDrivesCmd)
addLabelsFlag(listDrivesCmd, "Filter output by drive labels")
addAllFlag(listDrivesCmd, "If present, list all drives")
}
func validateListDrivesArgs() error {
if err := validateDriveStatusArgs(); err != nil {
return err
}
if err := validateDriveIDArgs(); err != nil {
return err
}
switch {
case allFlag:
case len(nodesArgs) != 0:
case len(drivesArgs) != 0:
case len(driveStatusArgs) != 0:
case len(driveIDArgs) != 0:
case len(labelArgs) != 0:
default:
driveStatusSelectors = append(driveStatusSelectors, directpvtypes.DriveStatusReady)
}
if allFlag {
nodesArgs = nil
drivesArgs = nil
driveStatusSelectors = nil
driveIDSelectors = nil
labelSelectors = nil
}
return nil
}
func listDrivesMain(ctx context.Context) {
drives, err := drive.NewLister().
NodeSelector(toLabelValues(nodesArgs)).
DriveNameSelector(toLabelValues(drivesArgs)).
StatusSelector(driveStatusSelectors).
DriveIDSelector(driveIDSelectors).
LabelSelector(labelSelectors).
Get(ctx)
if err != nil {
utils.Eprintf(quietFlag, true, "%v\n", err)
os.Exit(1)
}
if dryRunPrinter != nil {
driveList := types.DriveList{
TypeMeta: metav1.TypeMeta{
Kind: "List",
APIVersion: "v1",
},
Items: drives,
}
dryRunPrinter(driveList)
return
}
headers := table.Row{
"NODE",
"NAME",
"MAKE",
"SIZE",
"FREE",
"VOLUMES",
"STATUS",
}
if wideOutput {
headers = append(headers, "DRIVE ID")
}
if showLabels {
headers = append(headers, "LABELS")
}
writer := newTableWriter(
headers,
[]table.SortBy{
{
Name: "NODE",
Mode: table.Asc,
},
{
Name: "STATUS",
Mode: table.Asc,
},
{
Name: "SIZE",
Mode: table.Asc,
},
{
Name: "FREE",
Mode: table.Asc,
},
{
Name: "NAME",
Mode: table.Asc,
},
},
noHeaders,
)
for _, drive := range drives {
volumeCount := drive.GetVolumeCount()
volumes := "-"
if volumeCount > 0 {
volumes = fmt.Sprintf("%v", volumeCount)
}
status := drive.Status.Status
if drive.IsUnschedulable() {
status += ",SchedulingDisabled"
}
driveMake := drive.Status.Make
if driveMake == "" {
driveMake = "-"
}
row := []interface{}{
drive.GetNodeID(),
drive.GetDriveName(),
driveMake,
printableBytes(drive.Status.TotalCapacity),
printableBytes(drive.Status.FreeCapacity),
volumes,
status,
}
if wideOutput {
row = append(row, drive.GetDriveID())
}
if showLabels {
row = append(row, labelsToString(drive.GetLabels()))
}
writer.AppendRow(row)
}
if writer.Length() != 0 {
writer.Render()
return
}
if allFlag {
utils.Eprintf(quietFlag, false, "No resources found\n")
} else {
utils.Eprintf(quietFlag, false, "No matching resources found\n")
}
os.Exit(1)
}