Skip to content

Commit

Permalink
fix error when output is set to name
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisy Guo committed Apr 3, 2020
1 parent e524610 commit 314787e
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/kn/commands/service/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
58 changes: 58 additions & 0 deletions pkg/util/unstructured.go
Original file line number Diff line number Diff line change
@@ -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
}
79 changes: 79 additions & 0 deletions pkg/util/unstructured_test.go
Original file line number Diff line number Diff line change
@@ -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{}{},
},
}
}

0 comments on commit 314787e

Please sign in to comment.