Skip to content
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

Create unversioned.ListOptions object. #17047

Merged
merged 2 commits into from
Nov 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/api/unversioned/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand All @@ -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())
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/api/unversioned/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -276,6 +297,7 @@ const (
CauseTypeUnexpectedServerResponse CauseType = "UnexpectedServerResponse"
)

func (*ListOptions) IsAnAPIObject() {}
func (*Status) IsAnAPIObject() {}
func (*APIVersions) IsAnAPIObject() {}
func (*APIGroupList) IsAnAPIObject() {}
Expand Down
13 changes: 13 additions & 0 deletions pkg/api/unversioned/types_swagger_doc_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
}
Expand Down
33 changes: 33 additions & 0 deletions pkg/api/v1/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually more worried about accidentally changing the JSON (and/or url.Values?). If you have time, it would be great to verify that the wire format doesn't change. Feel free to self-lgtm.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - I verified it. I think the best verification is to use the old format in client and a new format in the server - and I'm doing exactly this in #17369

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)
}
}
}