diff --git a/pkg/api/unversioned/selector.go b/pkg/api/unversioned/selector.go index f7959afb723..8928b02e51f 100644 --- a/pkg/api/unversioned/selector.go +++ b/pkg/api/unversioned/selector.go @@ -30,6 +30,9 @@ type FieldSelector struct { } func (sh FieldSelector) MarshalJSON() ([]byte, error) { + if sh.Selector == nil { + return json.Marshal("") + } return json.Marshal(sh.Selector.String()) } @@ -53,6 +56,9 @@ type LabelSelector struct { } func (sh LabelSelector) MarshalJSON() ([]byte, error) { + if sh.Selector == nil { + return json.Marshal("") + } return json.Marshal(sh.Selector.String()) } diff --git a/pkg/api/unversioned/types.go b/pkg/api/unversioned/types.go index 7dfa7d527f3..32160a54d7b 100644 --- a/pkg/api/unversioned/types.go +++ b/pkg/api/unversioned/types.go @@ -54,6 +54,27 @@ type ListMeta struct { ResourceVersion string `json:"resourceVersion,omitempty"` } +// ListOptions is the query options to a standard REST list/watch calls. +type ListOptions struct { + TypeMeta `json:",inline"` + + // A selector to restrict the list of returned objects by their labels. + // Defaults to everything. + LabelSelector LabelSelector `json:"labelSelector,omitempty"` + // A selector to restrict the list of returned objects by their fields. + // Defaults to everything. + FieldSelector FieldSelector `json:"fieldSelector,omitempty"` + + // Watch for changes to the described resources and return them as a stream of + // add, update, and remove notifications. Specify resourceVersion. + Watch bool `json:"watch,omitempty"` + // When specified with a watch call, shows changes that occur after that particular version of a resource. + // Defaults to changes from the beginning of history. + ResourceVersion string `json:"resourceVersion,omitempty"` + // Timeout for the list/watch call. + TimeoutSeconds *int64 `json:"timeoutSeconds,omitempty"` +} + // Status is a return value for calls that don't return other objects. type Status struct { TypeMeta `json:",inline"` @@ -276,6 +297,7 @@ const ( CauseTypeUnexpectedServerResponse CauseType = "UnexpectedServerResponse" ) +func (*ListOptions) IsAnAPIObject() {} func (*Status) IsAnAPIObject() {} func (*APIVersions) IsAnAPIObject() {} func (*APIGroupList) IsAnAPIObject() {} diff --git a/pkg/api/unversioned/types_swagger_doc_generated.go b/pkg/api/unversioned/types_swagger_doc_generated.go index 25242a59449..a10658fb229 100644 --- a/pkg/api/unversioned/types_swagger_doc_generated.go +++ b/pkg/api/unversioned/types_swagger_doc_generated.go @@ -96,6 +96,19 @@ func (ListMeta) SwaggerDoc() map[string]string { return map_ListMeta } +var map_ListOptions = map[string]string{ + "": "ListOptions is the query options to a standard REST list/watch calls.", + "labelSelector": "A selector to restrict the list of returned objects by their labels. Defaults to everything.", + "fieldSelector": "A selector to restrict the list of returned objects by their fields. Defaults to everything.", + "watch": "Watch for changes to the described resources and return them as a stream of add, update, and remove notifications. Specify resourceVersion.", + "resourceVersion": "When specified with a watch call, shows changes that occur after that particular version of a resource. Defaults to changes from the beginning of history.", + "timeoutSeconds": "Timeout for the list/watch call.", +} + +func (ListOptions) SwaggerDoc() map[string]string { + return map_ListOptions +} + var map_Patch = map[string]string{ "": "Patch is provided to give a concrete name and type to the Kubernetes PATCH request body.", } diff --git a/pkg/api/v1/conversion_test.go b/pkg/api/v1/conversion_test.go index ad911599485..d6496cfb5e0 100644 --- a/pkg/api/v1/conversion_test.go +++ b/pkg/api/v1/conversion_test.go @@ -17,9 +17,12 @@ limitations under the License. package v1_test import ( + "encoding/json" + "reflect" "testing" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/unversioned" versioned "k8s.io/kubernetes/pkg/api/v1" ) @@ -68,3 +71,33 @@ func TestPodSpecConversion(t *testing.T) { } } } + +func TestListOptionsConversion(t *testing.T) { + testCases := []versioned.ListOptions{ + {}, + {ResourceVersion: "1"}, + {LabelSelector: "a=b,c=d", FieldSelector: "a=b,c!=d", ResourceVersion: "5"}, + } + + for _, test := range testCases { + marshalled, err := json.Marshal(test) + if err != nil { + t.Errorf("unexpected error: %#v", err) + } + newRep := unversioned.ListOptions{} + if err := json.Unmarshal(marshalled, &newRep); err != nil { + t.Errorf("unexpected error: %#v", err) + } + unversionedMarshalled, err := json.Marshal(newRep) + if err != nil { + t.Errorf("unexpected error: %#", err) + } + base := versioned.ListOptions{} + if err := json.Unmarshal(unversionedMarshalled, &base); err != nil { + t.Errorf("unexpected error: %#v", err) + } + if !reflect.DeepEqual(test, base) { + t.Errorf("expected: %#v, got: %#v", test, base) + } + } +}