Add persistentVolumeClaim table printer

This commit is contained in:
Haoran Wang 2017-07-27 16:34:23 +08:00
parent 11b531b739
commit c1ff87996a

View File

@ -61,7 +61,6 @@ const loadBalancerWidth = 16
// NOTE: When adding a new resource type here, please update the list
// pkg/kubectl/cmd/get.go to reflect the new resource type.
var (
persistentVolumeClaimColumns = []string{"NAME", "STATUS", "VOLUME", "CAPACITY", "ACCESSMODES", "STORAGECLASS", "AGE"}
componentStatusColumns = []string{"NAME", "STATUS", "MESSAGE", "ERROR"}
thirdPartyResourceColumns = []string{"NAME", "DESCRIPTION", "VERSION(S)"}
roleBindingColumns = []string{"NAME", "AGE"}
@ -295,8 +294,16 @@ func AddHandlers(h printers.PrintHandler) {
h.TableHandler(persistentVolumeColumnDefinitions, printPersistentVolume)
h.TableHandler(persistentVolumeColumnDefinitions, printPersistentVolumeList)
h.Handler(persistentVolumeClaimColumns, nil, printPersistentVolumeClaim)
h.Handler(persistentVolumeClaimColumns, nil, printPersistentVolumeClaimList)
persistentVolumeClaimColumnDefinitions := []metav1alpha1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
{Name: "Status", Type: "string", Description: apiv1.PersistentVolumeClaimStatus{}.SwaggerDoc()["phase"]},
{Name: "Capacity", Type: "string", Description: apiv1.PersistentVolumeClaimStatus{}.SwaggerDoc()["capacity"]},
{Name: "AccessModes", Type: "string", Description: apiv1.PersistentVolumeClaimStatus{}.SwaggerDoc()["accessModes"]},
{Name: "StorageClass", Type: "string", Description: "StorageClass of the pvc"},
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
}
h.TableHandler(persistentVolumeClaimColumnDefinitions, printPersistentVolumeClaim)
h.TableHandler(persistentVolumeClaimColumnDefinitions, printPersistentVolumeClaimList)
h.Handler(componentStatusColumns, nil, printComponentStatus)
h.Handler(componentStatusColumns, nil, printComponentStatusList)
@ -1139,44 +1146,35 @@ func printPersistentVolumeList(list *api.PersistentVolumeList, options printers.
return rows, nil
}
func printPersistentVolumeClaim(pvc *api.PersistentVolumeClaim, w io.Writer, options printers.PrintOptions) error {
name := printers.FormatResourceName(options.Kind, pvc.Name, options.WithKind)
namespace := pvc.Namespace
if options.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
return err
}
func printPersistentVolumeClaim(obj *api.PersistentVolumeClaim, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {
row := metav1alpha1.TableRow{
Object: runtime.RawExtension{Object: obj},
}
phase := pvc.Status.Phase
storage := pvc.Spec.Resources.Requests[api.ResourceStorage]
phase := obj.Status.Phase
storage := obj.Spec.Resources.Requests[api.ResourceStorage]
capacity := ""
accessModes := ""
if pvc.Spec.VolumeName != "" {
accessModes = helper.GetAccessModesAsString(pvc.Status.AccessModes)
storage = pvc.Status.Capacity[api.ResourceStorage]
if obj.Spec.VolumeName != "" {
accessModes = helper.GetAccessModesAsString(obj.Status.AccessModes)
storage = obj.Status.Capacity[api.ResourceStorage]
capacity = storage.String()
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s", name, phase, pvc.Spec.VolumeName, capacity, accessModes, helper.GetPersistentVolumeClaimClass(pvc), translateTimestamp(pvc.CreationTimestamp)); err != nil {
return err
}
if _, err := fmt.Fprint(w, printers.AppendLabels(pvc.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, pvc.Labels))
return err
row.Cells = append(row.Cells, obj.Name, phase, obj.Spec.VolumeName, capacity, accessModes, helper.GetPersistentVolumeClaimClass(obj), translateTimestamp(obj.CreationTimestamp))
return []metav1alpha1.TableRow{row}, nil
}
func printPersistentVolumeClaimList(list *api.PersistentVolumeClaimList, w io.Writer, options printers.PrintOptions) error {
for _, psd := range list.Items {
if err := printPersistentVolumeClaim(&psd, w, options); err != nil {
return err
func printPersistentVolumeClaimList(list *api.PersistentVolumeClaimList, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {
rows := make([]metav1alpha1.TableRow, 0, len(list.Items))
for i := range list.Items {
r, err := printPersistentVolumeClaim(&list.Items[i], options)
if err != nil {
return nil, err
}
rows = append(rows, r...)
}
return nil
return rows, nil
}
func printEvent(obj *api.Event, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {