forked from knative/client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix error when output is set to name (knative#775)
* fix error when output is set to name * add e2e test * change to flags/listprint.go Signed-off-by: Roland Huß <roland@ro14nd.de> # Conflicts: # test/e2e/basic_workflow_test.go
- Loading branch information
Showing
7 changed files
with
201 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// 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" | ||
) | ||
|
||
// ToUnstructuredList is to converts an object to unstructured.UnstructuredList. | ||
// If the object is not a list type, it will convert to a single item UnstructuredList. | ||
func ToUnstructuredList(obj runtime.Object) (*unstructured.UnstructuredList, error) { | ||
unstructuredList := &unstructured.UnstructuredList{} | ||
if meta.IsListType(obj) { | ||
unstructuredList.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind()) | ||
items, err := meta.ExtractList(obj) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, obji := range items { | ||
ud, err := toUnstructured(obji) | ||
if err != nil { | ||
return nil, err | ||
} | ||
unstructuredList.Items = append(unstructuredList.Items, *ud) | ||
} | ||
|
||
} else { | ||
ud, err := toUnstructured(obj) | ||
if err != nil { | ||
return nil, err | ||
} | ||
unstructuredList.Items = append(unstructuredList.Items, *ud) | ||
} | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// 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) | ||
|
||
service1 := createService("s3") | ||
expectedList = &unstructured.UnstructuredList{} | ||
expectedList.Items = []unstructured.Unstructured{createUnstructured("s3")} | ||
unstructedList, err = ToUnstructuredList(&service1) | ||
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{}{}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters