github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/meta/table/table.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package table 18 19 import ( 20 "time" 21 22 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/api/meta" 23 metav1 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/apis/meta/v1" 24 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime" 25 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/util/duration" 26 ) 27 28 // MetaToTableRow converts a list or object into one or more table rows. The provided rowFn is invoked for 29 // each accessed item, with name and age being passed to each. 30 func MetaToTableRow(obj runtime.Object, rowFn func(obj runtime.Object, m metav1.Object, name, age string) ([]interface{}, error)) ([]metav1.TableRow, error) { 31 if meta.IsListType(obj) { 32 rows := make([]metav1.TableRow, 0, 16) 33 err := meta.EachListItem(obj, func(obj runtime.Object) error { 34 nestedRows, err := MetaToTableRow(obj, rowFn) 35 if err != nil { 36 return err 37 } 38 rows = append(rows, nestedRows...) 39 return nil 40 }) 41 if err != nil { 42 return nil, err 43 } 44 return rows, nil 45 } 46 47 rows := make([]metav1.TableRow, 0, 1) 48 m, err := meta.Accessor(obj) 49 if err != nil { 50 return nil, err 51 } 52 row := metav1.TableRow{ 53 Object: runtime.RawExtension{Object: obj}, 54 } 55 row.Cells, err = rowFn(obj, m, m.GetName(), ConvertToHumanReadableDateType(m.GetCreationTimestamp())) 56 if err != nil { 57 return nil, err 58 } 59 rows = append(rows, row) 60 return rows, nil 61 } 62 63 // ConvertToHumanReadableDateType returns the elapsed time since timestamp in 64 // human-readable approximation. 65 func ConvertToHumanReadableDateType(timestamp metav1.Time) string { 66 if timestamp.IsZero() { 67 return "<unknown>" 68 } 69 return duration.HumanDuration(time.Since(timestamp.Time)) 70 }