|
| 1 | +package printers |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + metatable "k8s.io/apimachinery/pkg/api/meta/table" |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + "k8s.io/apimachinery/pkg/runtime" |
| 9 | + "k8s.io/kube-aggregator/pkg/apis/apiregistration" |
| 10 | + "k8s.io/kubernetes/pkg/printers" |
| 11 | +) |
| 12 | + |
| 13 | +func AddAPIServiceHandler(h printers.PrintHandler) { |
| 14 | + columnDefinitions := []metav1.TableColumnDefinition{ |
| 15 | + {Name: "Name", Type: "string", Format: "name", Description: swaggerMetadataDescriptions["name"]}, |
| 16 | + {Name: "Service", Type: "string", Description: "The reference to the service that hosts this API endpoint."}, |
| 17 | + {Name: "Available", Type: "string", Description: "Whether this service is available."}, |
| 18 | + {Name: "Age", Type: "string", Description: swaggerMetadataDescriptions["creationTimestamp"]}, |
| 19 | + } |
| 20 | + |
| 21 | + _ = h.TableHandler(columnDefinitions, func(list *apiregistration.APIServiceList, options printers.GenerateOptions) ([]metav1.TableRow, error) { |
| 22 | + return metatable.MetaToTableRow(list, apiServiceHandler) |
| 23 | + }) |
| 24 | + _ = h.TableHandler(columnDefinitions, func(apiservice *apiregistration.APIService, options printers.GenerateOptions) ([]metav1.TableRow, error) { |
| 25 | + return metatable.MetaToTableRow(apiservice, apiServiceHandler) |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +// k8s.io/kube-aggregator/pkg/registry/apiservice/etcd.(*REST).ConvertToTable |
| 30 | +func apiServiceHandler(obj runtime.Object, m metav1.Object, name, age string) ([]interface{}, error) { |
| 31 | + svc := obj.(*apiregistration.APIService) |
| 32 | + service := "Local" |
| 33 | + if svc.Spec.Service != nil { |
| 34 | + service = fmt.Sprintf("%s/%s", svc.Spec.Service.Namespace, svc.Spec.Service.Name) |
| 35 | + } |
| 36 | + status := string(apiregistration.ConditionUnknown) |
| 37 | + if condition := getCondition(svc.Status.Conditions, "Available"); condition != nil { |
| 38 | + switch { |
| 39 | + case condition.Status == apiregistration.ConditionTrue: |
| 40 | + status = string(condition.Status) |
| 41 | + case len(condition.Reason) > 0: |
| 42 | + status = fmt.Sprintf("%s (%s)", condition.Status, condition.Reason) |
| 43 | + default: |
| 44 | + status = string(condition.Status) |
| 45 | + } |
| 46 | + } |
| 47 | + return []interface{}{name, service, status, age}, nil |
| 48 | +} |
| 49 | + |
| 50 | +func getCondition(conditions []apiregistration.APIServiceCondition, conditionType apiregistration.APIServiceConditionType) *apiregistration.APIServiceCondition { |
| 51 | + for i, condition := range conditions { |
| 52 | + if condition.Type == conditionType { |
| 53 | + return &conditions[i] |
| 54 | + } |
| 55 | + } |
| 56 | + return nil |
| 57 | +} |
0 commit comments