k3s/vendor/k8s.io/kubectl/pkg/cmd/get/table_printer.go

95 lines
2.8 KiB
Go
Raw Normal View History

2019-08-30 18:33:25 +00:00
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package get
import (
"fmt"
"io"
2019-09-27 21:51:53 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2019-08-30 18:33:25 +00:00
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
2020-03-26 21:07:15 +00:00
"k8s.io/apimachinery/pkg/runtime/schema"
2019-08-30 18:33:25 +00:00
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/klog"
)
// TablePrinter decodes table objects into typed objects before delegating to another printer.
// Non-table types are simply passed through
type TablePrinter struct {
Delegate printers.ResourcePrinter
}
func (t *TablePrinter) PrintObj(obj runtime.Object, writer io.Writer) error {
table, err := decodeIntoTable(obj)
if err == nil {
return t.Delegate.PrintObj(table, writer)
}
// if we are unable to decode server response into a v1beta1.Table,
// fallback to client-side printing with whatever info the server returned.
klog.V(2).Infof("Unable to decode server response into a Table. Falling back to hardcoded types: %v", err)
return t.Delegate.PrintObj(obj, writer)
}
2020-03-26 21:07:15 +00:00
var recognizedTableVersions = map[schema.GroupVersionKind]bool{
metav1beta1.SchemeGroupVersion.WithKind("Table"): true,
metav1.SchemeGroupVersion.WithKind("Table"): true,
}
// assert the types are identical, since we're decoding both types into a metav1.Table
var _ metav1.Table = metav1beta1.Table{}
var _ metav1beta1.Table = metav1.Table{}
2019-08-30 18:33:25 +00:00
func decodeIntoTable(obj runtime.Object) (runtime.Object, error) {
2019-09-27 21:51:53 +00:00
event, isEvent := obj.(*metav1.WatchEvent)
if isEvent {
obj = event.Object.Object
}
2020-03-26 21:07:15 +00:00
if !recognizedTableVersions[obj.GetObjectKind().GroupVersionKind()] {
return nil, fmt.Errorf("attempt to decode non-Table object")
2019-08-30 18:33:25 +00:00
}
unstr, ok := obj.(*unstructured.Unstructured)
if !ok {
return nil, fmt.Errorf("attempt to decode non-Unstructured object")
}
2020-03-26 21:07:15 +00:00
table := &metav1.Table{}
2019-08-30 18:33:25 +00:00
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstr.Object, table); err != nil {
return nil, err
}
for i := range table.Rows {
row := &table.Rows[i]
if row.Object.Raw == nil || row.Object.Object != nil {
continue
}
converted, err := runtime.Decode(unstructured.UnstructuredJSONScheme, row.Object.Raw)
if err != nil {
return nil, err
}
row.Object.Object = converted
}
2019-09-27 21:51:53 +00:00
if isEvent {
event.Object.Object = table
return event, nil
}
2019-08-30 18:33:25 +00:00
return table, nil
}