| 1 |
/* |
| 2 |
Copyright 2014 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 rest |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"net/http" |
| 23 |
"time" |
| 24 |
|
| 25 |
"k8s.io/apimachinery/pkg/api/meta" |
| 26 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 |
"k8s.io/apimachinery/pkg/runtime" |
| 28 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 29 |
genericapirequest "k8s.io/apiserver/pkg/endpoints/request" |
| 30 |
) |
| 31 |
|
| 32 |
type defaultTableConvertor struct { |
| 33 |
defaultQualifiedResource schema.GroupResource |
| 34 |
} |
| 35 |
|
| 36 |
// NewDefaultTableConvertor creates a default convertor; the provided resource is used for error messages |
| 37 |
// if no resource info can be determined from the context passed to ConvertToTable. |
| 38 |
func NewDefaultTableConvertor(defaultQualifiedResource schema.GroupResource) TableConvertor { |
| 39 |
return defaultTableConvertor{defaultQualifiedResource: defaultQualifiedResource} |
| 40 |
} |
| 41 |
|
| 42 |
var swaggerMetadataDescriptions = metav1.ObjectMeta{}.SwaggerDoc() |
| 43 |
|
| 44 |
func (c defaultTableConvertor) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) { |
| 45 |
var table metav1.Table |
| 46 |
fn := func(obj runtime.Object) error { |
| 47 |
m, err := meta.Accessor(obj) |
| 48 |
if err != nil { |
| 49 |
resource := c.defaultQualifiedResource |
| 50 |
if info, ok := genericapirequest.RequestInfoFrom(ctx); ok { |
| 51 |
resource = schema.GroupResource{Group: info.APIGroup, Resource: info.Resource} |
| 52 |
} |
| 53 |
return errNotAcceptable{resource: resource} |
| 54 |
} |
| 55 |
table.Rows = append(table.Rows, metav1.TableRow{ |
| 56 |
Cells: []interface{}{m.GetName(), m.GetCreationTimestamp().Time.UTC().Format(time.RFC3339)}, |
| 57 |
Object: runtime.RawExtension{Object: obj}, |
| 58 |
}) |
| 59 |
return nil |
| 60 |
} |
| 61 |
switch { |
| 62 |
case meta.IsListType(object): |
| 63 |
if err := meta.EachListItem(object, fn); err != nil { |
| 64 |
return nil, err |
| 65 |
} |
| 66 |
default: |
| 67 |
if err := fn(object); err != nil { |
| 68 |
return nil, err |
| 69 |
} |
| 70 |
} |
| 71 |
if m, err := meta.ListAccessor(object); err == nil { |
| 72 |
table.ResourceVersion = m.GetResourceVersion() |
| 73 |
table.Continue = m.GetContinue() |
| 74 |
table.RemainingItemCount = m.GetRemainingItemCount() |
| 75 |
} else { |
| 76 |
if m, err := meta.CommonAccessor(object); err == nil { |
| 77 |
table.ResourceVersion = m.GetResourceVersion() |
| 78 |
} |
| 79 |
} |
| 80 |
if opt, ok := tableOptions.(*metav1.TableOptions); !ok || !opt.NoHeaders { |
| 81 |
table.ColumnDefinitions = []metav1.TableColumnDefinition{ |
| 82 |
{Name: "Name", Type: "string", Format: "name", Description: swaggerMetadataDescriptions["name"]}, |
| 83 |
{Name: "Created At", Type: "date", Description: swaggerMetadataDescriptions["creationTimestamp"]}, |
| 84 |
} |
| 85 |
} |
| 86 |
return &table, nil |
| 87 |
} |
| 88 |
|
| 89 |
// errNotAcceptable indicates the resource doesn't support Table conversion |
| 90 |
type errNotAcceptable struct { |
| 91 |
resource schema.GroupResource |
| 92 |
} |
| 93 |
|
| 94 |
func (e errNotAcceptable) Error() string { |
| 95 |
return fmt.Sprintf("the resource %s does not support being converted to a Table", e.resource) |
| 96 |
} |
| 97 |
|
| 98 |
func (e errNotAcceptable) Status() metav1.Status { |
| 99 |
return metav1.Status{ |
| 100 |
Status: metav1.StatusFailure, |
| 101 |
Code: http.StatusNotAcceptable, |
| 102 |
Reason: metav1.StatusReason("NotAcceptable"), |
| 103 |
Message: e.Error(), |
| 104 |
} |
| 105 |
} |