-
Notifications
You must be signed in to change notification settings - Fork 274
Fix error when output is set to name #775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||||
|
|
||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| } | ||||
|
|
||||
| func toUnstructured(obj runtime.Object) (*unstructured.Unstructured, error) { | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see if you can reuse https://github.com/knative/pkg/blob/master/apis/duck/unstructured.go#L32
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the information. |
||||
| 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 | ||||
| } | ||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,86 @@ | ||||||
| // Copyright © 2019 The Knative Authors | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| // | ||||||
| // 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, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does it require to set |
||||||
| }, | ||||||
| "spec": map[string]interface{}{ | ||||||
| "template": map[string]interface{}{ | ||||||
| "metadata": map[string]interface{}{"creationTimestamp": nil}, | ||||||
| "spec": map[string]interface{}{"containers": nil}, | ||||||
| }, | ||||||
| }, | ||||||
| "status": map[string]interface{}{}, | ||||||
| }, | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is the unit test for this
ToUnstructuredListexported function?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see
TestToUnstructuredListin pkg/util/unstructured_test.go