forked from vmware-archive/octant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistentvolumeclaim.go
352 lines (284 loc) · 11.1 KB
/
persistentvolumeclaim.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
/*
Copyright (c) 2019 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package printer
import (
"context"
"fmt"
"strings"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"github.com/vmware-tanzu/octant/pkg/store"
"github.com/vmware-tanzu/octant/pkg/view/component"
)
// PersistentVolumeClaimListHandler is a printFunc that prints persistentvolumeclaims
func PersistentVolumeClaimListHandler(ctx context.Context, list *corev1.PersistentVolumeClaimList, options Options) (component.Component, error) {
if list == nil {
return nil, errors.New("nil list")
}
cols := component.NewTableCols("Name", "Status", "Volume", "Capacity", "Access Modes", "Storage Class", "Age")
tbl := component.NewTable("Persistent Volume Claims",
"We couldn't find any persistent volume claims!", cols)
for _, persistentVolumeClaim := range list.Items {
row := component.TableRow{}
accessModes := ""
storage := persistentVolumeClaim.Spec.Resources.Requests[corev1.ResourceStorage]
capacity := ""
// Check if pvc is bound
if persistentVolumeClaim.Spec.VolumeName != "" {
accessModes = getAccessModesAsString(persistentVolumeClaim.Spec.AccessModes)
storage = persistentVolumeClaim.Status.Capacity[corev1.ResourceStorage]
capacity = storage.String()
}
nameLink, err := options.Link.ForObject(&persistentVolumeClaim, persistentVolumeClaim.Name)
if err != nil {
return nil, err
}
pv, err := getBoundPersistentVolume(ctx, &persistentVolumeClaim, options)
if err != nil {
return nil, err
}
if pv != nil {
volumeLink, err := options.Link.ForObject(pv, persistentVolumeClaim.Spec.VolumeName)
if err != nil {
return nil, err
}
row["Volume"] = volumeLink
} else {
row["Volume"] = component.NewText(persistentVolumeClaim.Spec.VolumeName)
}
row["Name"] = nameLink
row["Status"] = component.NewText(string(persistentVolumeClaim.Status.Phase))
row["Capacity"] = component.NewText(capacity)
row["Access Modes"] = component.NewText(accessModes)
row["Storage Class"] = component.NewText(printPersistentVolumeClaimClass(&persistentVolumeClaim))
ts := persistentVolumeClaim.CreationTimestamp.Time
row["Age"] = component.NewTimestamp(ts)
tbl.Add(row)
}
return tbl, nil
}
// PersistentVolumeClaimHandler is a printFunc that prints a PersistentVolumeClaim
func PersistentVolumeClaimHandler(ctx context.Context, persistentVolumeClaim *corev1.PersistentVolumeClaim, options Options) (component.Component, error) {
o := NewObject(persistentVolumeClaim)
o.EnableEvents()
ph, err := newPersistentVolumeClaimHandler(persistentVolumeClaim, o)
if err != nil {
return nil, err
}
if err := ph.Config(options); err != nil {
return nil, errors.Wrap(err, "print persistentvolumeclaim configuration")
}
if err := ph.Status(ctx, options); err != nil {
return nil, errors.Wrap(err, "print persistentvolumeclaim status")
}
if err := ph.MountedPodList(ctx, options); err != nil {
return nil, errors.Wrap(err, "print persistentvolumeclaim mounted pod list")
}
return o.ToComponent(ctx, options)
}
// PersistentVolumeClaimConfiguration generates a persistenvolumeclaim configuration
type PersistentVolumeClaimConfiguration struct {
persistentVolumeClaim *corev1.PersistentVolumeClaim
}
// NewPersistentVolumeClaimConfiguration creates an instance of PersistentVolumeClaimConfiguration
func NewPersistentVolumeClaimConfiguration(pvc *corev1.PersistentVolumeClaim) *PersistentVolumeClaimConfiguration {
return &PersistentVolumeClaimConfiguration{
persistentVolumeClaim: pvc,
}
}
// Create creates a persistentvolumeclaim configuration summary
func (p *PersistentVolumeClaimConfiguration) Create(options Options) (*component.Summary, error) {
if p == nil || p.persistentVolumeClaim == nil {
return nil, errors.New("persistentvolumeclaim is nil")
}
persistentVolumeClaim := p.persistentVolumeClaim
var sections component.SummarySections
if volumeMode := persistentVolumeClaim.Spec.VolumeMode; volumeMode != nil {
sections.AddText("Volume Mode", string(*volumeMode))
}
if accessMode := persistentVolumeClaim.Spec.AccessModes; accessMode != nil {
sections.AddText("Access Modes", getAccessModesAsString(accessMode))
}
if finalizers := persistentVolumeClaim.ObjectMeta.Finalizers; finalizers != nil {
sections.AddText("Finalizers", fmt.Sprint(finalizers))
}
if storageClassName := persistentVolumeClaim.Spec.StorageClassName; storageClassName != nil {
sections.AddText("Storage Class Name", string(*storageClassName))
}
if labels := persistentVolumeClaim.Labels; labels != nil {
sections.Add("Labels", component.NewLabels(labels))
}
if selector := persistentVolumeClaim.Spec.Selector; selector != nil {
sections.Add("Selectors", printSelector(selector))
}
summary := component.NewSummary("Configuration", sections...)
return summary, nil
}
func createPersistentVolumeClaimStatusView(ctx context.Context, persistentVolumeClaim *corev1.PersistentVolumeClaim, options Options) (*component.Summary, error) {
if persistentVolumeClaim == nil {
return nil, errors.New("persistentvolumeclaim is nil")
}
sections := component.SummarySections{}
if persistentVolumeClaim.Status.Phase != "" {
sections.AddText("Claim Status", string(persistentVolumeClaim.Status.Phase))
}
if requestedStorage, ok := persistentVolumeClaim.Spec.Resources.Requests[corev1.ResourceStorage]; ok {
sections.AddText("Storage Requested", requestedStorage.String())
}
if persistentVolumeClaim.Spec.VolumeName != "" {
if boundVolume := persistentVolumeClaim.Spec.VolumeName; boundVolume != "" {
pv, err := getBoundPersistentVolume(ctx, persistentVolumeClaim, options)
if err != nil {
return nil, err
}
volumeLink, err := options.Link.ForObject(pv, persistentVolumeClaim.Spec.VolumeName)
if err != nil {
return nil, err
}
sections = append(sections, component.SummarySection{
Header: "Bound Volume",
Content: volumeLink,
})
}
if availableStorage, ok := persistentVolumeClaim.Status.Capacity[corev1.ResourceStorage]; ok {
sections.AddText("Total Volume Capacity", availableStorage.String())
}
}
summary := component.NewSummary("Status", sections...)
return summary, nil
}
func printPersistentVolumeClaimClass(persistentVolumeClaim *corev1.PersistentVolumeClaim) string {
if class, found := persistentVolumeClaim.Annotations[corev1.BetaStorageClassAnnotation]; found {
return class
}
if persistentVolumeClaim.Spec.StorageClassName != nil {
return *persistentVolumeClaim.Spec.StorageClassName
}
return ""
}
func getAccessModesAsString(modes []corev1.PersistentVolumeAccessMode) string {
modes = removeDuplicateAccessModes(modes)
modesStr := []string{}
if containsAccessMode(modes, corev1.ReadWriteOnce) {
modesStr = append(modesStr, "RWO")
}
if containsAccessMode(modes, corev1.ReadOnlyMany) {
modesStr = append(modesStr, "ROX")
}
if containsAccessMode(modes, corev1.ReadWriteMany) {
modesStr = append(modesStr, "RWX")
}
return strings.Join(modesStr, ",")
}
func containsAccessMode(modes []corev1.PersistentVolumeAccessMode, mode corev1.PersistentVolumeAccessMode) bool {
for _, m := range modes {
if m == mode {
return true
}
}
return false
}
// removeDuplicateAccessModes returns an array of access modes without any duplicates
func removeDuplicateAccessModes(modes []corev1.PersistentVolumeAccessMode) []corev1.PersistentVolumeAccessMode {
accessModes := []corev1.PersistentVolumeAccessMode{}
for _, m := range modes {
if !containsAccessMode(accessModes, m) {
accessModes = append(accessModes, m)
}
}
return accessModes
}
func getBoundPersistentVolume(ctx context.Context, pvc *corev1.PersistentVolumeClaim, options Options) (runtime.Object, error) {
objectStore := options.DashConfig.ObjectStore()
persistentVolume := &corev1.PersistentVolume{}
if pvc.Spec.VolumeName == "" {
return nil, nil
}
key := store.Key{
APIVersion: "v1",
Kind: "PersistentVolume",
Name: pvc.Spec.VolumeName,
}
pv, err := objectStore.Get(ctx, key)
if err != nil {
return nil, errors.Wrapf(err, "get volume for key %+v", key)
}
if pv != nil {
isErr := runtime.DefaultUnstructuredConverter.FromUnstructured(pv.Object, persistentVolume)
if isErr != nil {
return nil, isErr
}
} else {
return nil, errors.New("volume not found")
}
return persistentVolume, nil
}
type persistentVolumeClaimObject interface {
Config(options Options) error
Status(ctx context.Context, options Options) error
MountedPodList(ctx context.Context, options Options) error
}
type persistentVolumeClaimHandler struct {
persistentVolumeClaim *corev1.PersistentVolumeClaim
configFunc func(*corev1.PersistentVolumeClaim, Options) (*component.Summary, error)
statusFunc func(context.Context, *corev1.PersistentVolumeClaim, Options) (*component.Summary, error)
mountedPodListFunc func(context.Context, string, string, Options) (component.Component, error)
object *Object
}
var _ persistentVolumeClaimObject = (*persistentVolumeClaimHandler)(nil)
func newPersistentVolumeClaimHandler(pvc *corev1.PersistentVolumeClaim, object *Object) (*persistentVolumeClaimHandler, error) {
if pvc == nil {
return nil, errors.New("can't print a nil persistentvolumeclaim")
}
if object == nil {
return nil, errors.New("can't print persistentvolume claim using a nil object printer")
}
ph := &persistentVolumeClaimHandler{
persistentVolumeClaim: pvc,
configFunc: defaultPersistentVolumeClaimConfig,
statusFunc: defaultPersistentVolumClaimStatus,
mountedPodListFunc: defaultPersistentVolumeMountedPodList,
object: object,
}
return ph, nil
}
func (p *persistentVolumeClaimHandler) Config(options Options) error {
out, err := p.configFunc(p.persistentVolumeClaim, options)
if err != nil {
return err
}
p.object.RegisterConfig(out)
return nil
}
func defaultPersistentVolumeClaimConfig(pvc *corev1.PersistentVolumeClaim, options Options) (*component.Summary, error) {
return NewPersistentVolumeClaimConfiguration(pvc).Create(options)
}
func (p *persistentVolumeClaimHandler) Status(ctx context.Context, options Options) error {
out, err := p.statusFunc(ctx, p.persistentVolumeClaim, options)
if err != nil {
return err
}
p.object.RegisterSummary(out)
return nil
}
func defaultPersistentVolumClaimStatus(ctx context.Context, pvc *corev1.PersistentVolumeClaim, options Options) (*component.Summary, error) {
return createPersistentVolumeClaimStatusView(ctx, pvc, options)
}
func (p *persistentVolumeClaimHandler) MountedPodList(ctx context.Context, options Options) error {
if p.persistentVolumeClaim == nil {
return errors.New("can't display mounted pod list for nil persistentvolumeclaim")
}
p.object.RegisterItems(ItemDescriptor{
Width: component.WidthFull,
Func: func() (component.Component, error) {
return p.mountedPodListFunc(ctx, p.persistentVolumeClaim.Namespace, p.persistentVolumeClaim.Name, options)
},
})
return nil
}
func defaultPersistentVolumeMountedPodList(ctx context.Context, namespace string, name string, options Options) (component.Component, error) {
return createMountedPodListView(ctx, namespace, name, options)
}