From 4eb5f64edd329d76d17aaa93f4ffbbab7b3fc54b Mon Sep 17 00:00:00 2001 From: Daisy Guo Date: Fri, 3 Apr 2020 00:09:06 +0800 Subject: [PATCH] fix error when output is set to name --- pkg/kn/commands/service/list.go | 13 ++++-- pkg/util/unstructured.go | 58 ++++++++++++++++++++++++ pkg/util/unstructured_test.go | 79 +++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 pkg/util/unstructured.go create mode 100644 pkg/util/unstructured_test.go diff --git a/pkg/kn/commands/service/list.go b/pkg/kn/commands/service/list.go index 50cc13e1e1..974db35340 100644 --- a/pkg/kn/commands/service/list.go +++ b/pkg/kn/commands/service/list.go @@ -24,6 +24,7 @@ import ( "knative.dev/client/pkg/kn/commands" "knative.dev/client/pkg/kn/commands/flags" clientservingv1 "knative.dev/client/pkg/serving/v1" + "knative.dev/client/pkg/util" ) // NewServiceListCommand represents 'kn service list' command @@ -81,11 +82,15 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command { return a.ObjectMeta.Name < b.ObjectMeta.Name }) - err = printer.PrintObj(serviceList, cmd.OutOrStdout()) - if err != nil { - return err + if serviceListFlags.GenericPrintFlags.OutputFlagSpecified() { + unstructedList, err := util.ToUnstructuredList(serviceList) + if err != nil { + return err + } + return printer.PrintObj(unstructedList, cmd.OutOrStdout()) } - return nil + + return printer.PrintObj(serviceList, cmd.OutOrStdout()) }, } commands.AddNamespaceFlags(serviceListCommand.Flags(), true) diff --git a/pkg/util/unstructured.go b/pkg/util/unstructured.go new file mode 100644 index 0000000000..d69b4c82e9 --- /dev/null +++ b/pkg/util/unstructured.go @@ -0,0 +1,58 @@ +// Copyright © 2020 The Knative 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 util + +import ( + "encoding/json" + + "k8s.io/apimachinery/pkg/api/meta" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" +) + +func ToUnstructuredList(obj runtime.Object) (*unstructured.UnstructuredList, error) { + unstructuredList := &unstructured.UnstructuredList{} + unstructuredList.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind()) + if meta.IsListType(obj) { + items, err := meta.ExtractList(obj) + if err != nil { + return nil, err + } + for _, obj := range items { + ud, err := toUnstructured(obj) + if err != nil { + return nil, err + } + unstructuredList.Items = append(unstructuredList.Items, *ud) + } + + } else { + + } + return unstructuredList, nil + +} + +func toUnstructured(obj runtime.Object) (*unstructured.Unstructured, error) { + b, err := json.Marshal(obj) + if err != nil { + return nil, err + } + ud := &unstructured.Unstructured{} + if err := json.Unmarshal(b, ud); err != nil { + return nil, err + } + return ud, nil +} diff --git a/pkg/util/unstructured_test.go b/pkg/util/unstructured_test.go new file mode 100644 index 0000000000..b4cf4ddaad --- /dev/null +++ b/pkg/util/unstructured_test.go @@ -0,0 +1,79 @@ +// Copyright © 2019 The Knative 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 util + +import ( + "testing" + + "gotest.tools/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + servingv1 "knative.dev/serving/pkg/apis/serving/v1" +) + +func TestToUnstructuredList(t *testing.T) { + serviceList := servingv1.ServiceList{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "v1", + Kind: "List", + }, + Items: []servingv1.Service{createService("s1"), createService("s2")}, + } + expectedList := &unstructured.UnstructuredList{ + Object: map[string]interface{}{ + "apiVersion": string("v1"), + "kind": string("List"), + }, + } + expectedList.Items = []unstructured.Unstructured{createUnstructured("s1"), createUnstructured("s2")} + unstructedList, err := ToUnstructuredList(&serviceList) + assert.NilError(t, err) + assert.DeepEqual(t, unstructedList, expectedList) +} + +func createService(name string) servingv1.Service { + service := servingv1.Service{ + TypeMeta: metav1.TypeMeta{ + Kind: "Service", + APIVersion: "serving.knative.dev/v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: "default", + }, + } + return service +} + +func createUnstructured(name string) unstructured.Unstructured { + return unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "serving.knative.dev/v1", + "kind": "Service", + "metadata": map[string]interface{}{ + "namespace": "default", + "name": name, + "creationTimestamp": nil, + }, + "spec": map[string]interface{}{ + "template": map[string]interface{}{ + "metadata": map[string]interface{}{"creationTimestamp": nil}, + "spec": map[string]interface{}{"containers": nil}, + }, + }, + "status": map[string]interface{}{}, + }, + } +}