Skip to content

Commit

Permalink
update client.Request for GroupVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed Dec 3, 2015
1 parent 972abe6 commit 0fd17b7
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 61 deletions.
13 changes: 7 additions & 6 deletions cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
apierrors "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/client/record"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/controller"
Expand Down Expand Up @@ -423,7 +424,7 @@ containers:
}

func runReplicationControllerTest(c *client.Client) {
clientAPIVersion := c.APIVersion()
clientAPIVersion := c.APIVersion().String()
data, err := ioutil.ReadFile("cmd/integration/" + clientAPIVersion + "-controller.json")
if err != nil {
glog.Fatalf("Unexpected error: %v", err)
Expand Down Expand Up @@ -462,7 +463,7 @@ func runReplicationControllerTest(c *client.Client) {

func runAPIVersionsTest(c *client.Client) {
v, err := c.ServerAPIVersions()
clientVersion := c.APIVersion()
clientVersion := c.APIVersion().String()
if err != nil {
glog.Fatalf("Failed to get api versions: %v", err)
}
Expand Down Expand Up @@ -541,7 +542,7 @@ func runSelfLinkTestOnNamespace(c *client.Client, namespace string) {
func runAtomicPutTest(c *client.Client) {
svcBody := api.Service{
TypeMeta: unversioned.TypeMeta{
APIVersion: c.APIVersion(),
APIVersion: c.APIVersion().String(),
},
ObjectMeta: api.ObjectMeta{
Name: "atomicservice",
Expand Down Expand Up @@ -623,7 +624,7 @@ func runPatchTest(c *client.Client) {
resource := "services"
svcBody := api.Service{
TypeMeta: unversioned.TypeMeta{
APIVersion: c.APIVersion(),
APIVersion: c.APIVersion().String(),
},
ObjectMeta: api.ObjectMeta{
Name: name,
Expand All @@ -647,12 +648,12 @@ func runPatchTest(c *client.Client) {
glog.Fatalf("Failed creating patchservice: %v", err)
}

patchBodies := map[string]map[api.PatchType]struct {
patchBodies := map[unversioned.GroupVersion]map[api.PatchType]struct {
AddLabelBody []byte
RemoveLabelBody []byte
RemoveAllLabelsBody []byte
}{
"v1": {
v1.SchemeGroupVersion: {
api.JSONPatchType: {
[]byte(`[{"op":"add","path":"/metadata/labels","value":{"foo":"bar","baz":"qux"}}]`),
[]byte(`[{"op":"remove","path":"/metadata/labels/foo"}]`),
Expand Down
20 changes: 20 additions & 0 deletions pkg/api/unversioned/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
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 unversioned

// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = GroupVersion{Group: "", Version: ""}
2 changes: 1 addition & 1 deletion pkg/client/unversioned/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *deployments) Delete(name string, options *api.DeleteOptions) error {
if options == nil {
return c.client.Delete().Namespace(c.ns).Resource("deployments").Name(name).Do().Error()
}
body, err := api.Scheme.EncodeToVersion(options, c.client.APIVersion())
body, err := api.Scheme.EncodeToVersion(options, c.client.APIVersion().String())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/unversioned/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (e *events) Delete(name string) error {
// Returns the appropriate field selector based on the API version being used to communicate with the server.
// The returned field selector can be used with List and Watch to filter desired events.
func (e *events) GetFieldSelector(involvedObjectName, involvedObjectNamespace, involvedObjectKind, involvedObjectUID *string) fields.Selector {
apiVersion := e.client.APIVersion()
apiVersion := e.client.APIVersion().String()
field := fields.Set{}
if involvedObjectName != nil {
field[getInvolvedObjectNameFieldLabel(apiVersion)] = *involvedObjectName
Expand Down
10 changes: 5 additions & 5 deletions pkg/client/unversioned/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,23 @@ type RESTClient struct {
}

func (c *RESTClient) Get() *unversioned.Request {
return unversioned.NewRequest(c, "GET", &url.URL{Host: "localhost"}, testapi.Default.Version(), c.Codec)
return unversioned.NewRequest(c, "GET", &url.URL{Host: "localhost"}, *testapi.Default.GroupVersion(), c.Codec)
}

func (c *RESTClient) Put() *unversioned.Request {
return unversioned.NewRequest(c, "PUT", &url.URL{Host: "localhost"}, testapi.Default.Version(), c.Codec)
return unversioned.NewRequest(c, "PUT", &url.URL{Host: "localhost"}, *testapi.Default.GroupVersion(), c.Codec)
}

func (c *RESTClient) Patch(_ api.PatchType) *unversioned.Request {
return unversioned.NewRequest(c, "PATCH", &url.URL{Host: "localhost"}, testapi.Default.Version(), c.Codec)
return unversioned.NewRequest(c, "PATCH", &url.URL{Host: "localhost"}, *testapi.Default.GroupVersion(), c.Codec)
}

func (c *RESTClient) Post() *unversioned.Request {
return unversioned.NewRequest(c, "POST", &url.URL{Host: "localhost"}, testapi.Default.Version(), c.Codec)
return unversioned.NewRequest(c, "POST", &url.URL{Host: "localhost"}, *testapi.Default.GroupVersion(), c.Codec)
}

func (c *RESTClient) Delete() *unversioned.Request {
return unversioned.NewRequest(c, "DELETE", &url.URL{Host: "localhost"}, testapi.Default.Version(), c.Codec)
return unversioned.NewRequest(c, "DELETE", &url.URL{Host: "localhost"}, *testapi.Default.GroupVersion(), c.Codec)
}

func (c *RESTClient) Do(req *http.Request) (*http.Response, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/unversioned/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func RESTClientFor(config *Config) (*RESTClient, error) {
return nil, err
}

client := NewRESTClient(baseURL, config.GroupVersion.String(), config.Codec, config.QPS, config.Burst)
client := NewRESTClient(baseURL, *config.GroupVersion, config.Codec, config.QPS, config.Burst)

transport, err := TransportFor(config)
if err != nil {
Expand All @@ -422,7 +422,7 @@ func UnversionedRESTClientFor(config *Config) (*RESTClient, error) {
return nil, err
}

client := NewRESTClient(baseURL, "", config.Codec, config.QPS, config.Burst)
client := NewRESTClient(baseURL, unversioned.SchemeGroupVersion, config.Codec, config.QPS, config.Burst)

transport, err := TransportFor(config)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/unversioned/horizontalpodautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (c *horizontalPodAutoscalers) Delete(name string, options *api.DeleteOption
if options == nil {
return c.client.Delete().Namespace(c.ns).Resource("horizontalPodAutoscalers").Name(name).Do().Error()
}
body, err := api.Scheme.EncodeToVersion(options, c.client.APIVersion())
body, err := api.Scheme.EncodeToVersion(options, c.client.APIVersion().String())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/unversioned/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (c *ingress) Delete(name string, options *api.DeleteOptions) (err error) {
return c.r.Delete().Namespace(c.ns).Resource("ingresses").Name(name).Do().Error()
}

body, err := api.Scheme.EncodeToVersion(options, c.r.APIVersion())
body, err := api.Scheme.EncodeToVersion(options, c.r.APIVersion().String())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/unversioned/pod_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *podTemplates) Delete(name string, options *api.DeleteOptions) error {
if options == nil {
return c.r.Delete().Namespace(c.ns).Resource("podTemplates").Name(name).Do().Error()
}
body, err := api.Scheme.EncodeToVersion(options, c.r.APIVersion())
body, err := api.Scheme.EncodeToVersion(options, c.r.APIVersion().String())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/unversioned/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *pods) Delete(name string, options *api.DeleteOptions) error {
if options == nil {
return c.r.Delete().Namespace(c.ns).Resource("pods").Name(name).Do().Error()
}
body, err := api.Scheme.EncodeToVersion(options, c.r.APIVersion())
body, err := api.Scheme.EncodeToVersion(options, c.r.APIVersion().String())
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/client/unversioned/remotecommand/remotecommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"testing"

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/util/httpstream"
"k8s.io/kubernetes/pkg/util/httpstream/spdy"
Expand Down Expand Up @@ -189,7 +190,7 @@ func TestRequestExecuteRemoteCommand(t *testing.T) {
server := httptest.NewServer(fakeExecServer(t, i, testCase.Stdin, testCase.Stdout, testCase.Stderr, testCase.Error, testCase.Tty, testCase.MessageCount))

url, _ := url.ParseRequestURI(server.URL)
c := client.NewRESTClient(url, "x", nil, -1, -1)
c := client.NewRESTClient(url, unversioned.GroupVersion{Group: "x"}, nil, -1, -1)
req := c.Post().Resource("testing")
req.SetHeader(httpstream.HeaderProtocolVersion, StreamProtocolV2Name)
req.Param("command", "ls")
Expand Down Expand Up @@ -271,7 +272,7 @@ func TestRequestAttachRemoteCommand(t *testing.T) {
server := httptest.NewServer(fakeExecServer(t, i, testCase.Stdin, testCase.Stdout, testCase.Stderr, testCase.Error, testCase.Tty, 1))

url, _ := url.ParseRequestURI(server.URL)
c := client.NewRESTClient(url, "x", nil, -1, -1)
c := client.NewRESTClient(url, unversioned.GroupVersion{Group: "x"}, nil, -1, -1)
req := c.Post().Resource("testing")

conf := &client.Config{
Expand Down
44 changes: 22 additions & 22 deletions pkg/client/unversioned/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/api/validation"
"k8s.io/kubernetes/pkg/client/metrics"
"k8s.io/kubernetes/pkg/conversion/queryparams"
Expand Down Expand Up @@ -99,7 +100,7 @@ type Request struct {
selector labels.Selector
timeout time.Duration

apiVersion string
groupVersion unversioned.GroupVersion

// output
err error
Expand All @@ -111,15 +112,14 @@ type Request struct {
}

// NewRequest creates a new request helper object for accessing runtime.Objects on a server.
func NewRequest(client HTTPClient, verb string, baseURL *url.URL, apiVersion string,
codec runtime.Codec) *Request {
func NewRequest(client HTTPClient, verb string, baseURL *url.URL, groupVersion unversioned.GroupVersion, codec runtime.Codec) *Request {
return &Request{
client: client,
verb: verb,
baseURL: baseURL,
path: baseURL.Path,
apiVersion: apiVersion,
codec: codec,
client: client,
verb: verb,
baseURL: baseURL,
path: baseURL.Path,
groupVersion: groupVersion,
codec: codec,
}
}

Expand Down Expand Up @@ -307,25 +307,25 @@ func (r resourceTypeToFieldMapping) filterField(resourceType, field, value strin
return fMapping.filterField(field, value)
}

type versionToResourceToFieldMapping map[string]resourceTypeToFieldMapping
type versionToResourceToFieldMapping map[unversioned.GroupVersion]resourceTypeToFieldMapping

func (v versionToResourceToFieldMapping) filterField(apiVersion, resourceType, field, value string) (newField, newValue string, err error) {
rMapping, ok := v[apiVersion]
func (v versionToResourceToFieldMapping) filterField(groupVersion unversioned.GroupVersion, resourceType, field, value string) (newField, newValue string, err error) {
rMapping, ok := v[groupVersion]
if !ok {
glog.Warningf("Field selector: %v - %v - %v - %v: need to check if this is versioned correctly.", apiVersion, resourceType, field, value)
glog.Warningf("Field selector: %v - %v - %v - %v: need to check if this is versioned correctly.", groupVersion, resourceType, field, value)
return field, value, nil
}
newField, newValue, err = rMapping.filterField(resourceType, field, value)
if err != nil {
// This is only a warning until we find and fix all of the client's usages.
glog.Warningf("Field selector: %v - %v - %v - %v: need to check if this is versioned correctly.", apiVersion, resourceType, field, value)
glog.Warningf("Field selector: %v - %v - %v - %v: need to check if this is versioned correctly.", groupVersion, resourceType, field, value)
return field, value, nil
}
return newField, newValue, nil
}

var fieldMappings = versionToResourceToFieldMapping{
"v1": resourceTypeToFieldMapping{
v1.SchemeGroupVersion: resourceTypeToFieldMapping{
"nodes": clientFieldNameToAPIVersionFieldName{
ObjectNameField: ObjectNameField,
NodeUnschedulable: NodeUnschedulable,
Expand Down Expand Up @@ -370,13 +370,13 @@ func (r *Request) FieldsSelectorParam(s fields.Selector) *Request {
return r
}
s2, err := s.Transform(func(field, value string) (newField, newValue string, err error) {
return fieldMappings.filterField(r.apiVersion, r.resource, field, value)
return fieldMappings.filterField(r.groupVersion, r.resource, field, value)
})
if err != nil {
r.err = err
return r
}
return r.setParam(unversioned.FieldSelectorQueryParam(r.apiVersion), s2.String())
return r.setParam(unversioned.FieldSelectorQueryParam(r.groupVersion.String()), s2.String())
}

// LabelsSelectorParam adds the given selector as a query parameter
Expand All @@ -390,7 +390,7 @@ func (r *Request) LabelsSelectorParam(s labels.Selector) *Request {
if s.Empty() {
return r
}
return r.setParam(unversioned.LabelSelectorQueryParam(r.apiVersion), s.String())
return r.setParam(unversioned.LabelSelectorQueryParam(r.groupVersion.String()), s.String())
}

// UintParam creates a query parameter with the given value.
Expand All @@ -416,7 +416,7 @@ func (r *Request) VersionedParams(obj runtime.Object, convertor runtime.ObjectCo
if r.err != nil {
return r
}
versioned, err := convertor.ConvertToVersion(obj, r.apiVersion)
versioned, err := convertor.ConvertToVersion(obj, r.groupVersion.String())
if err != nil {
r.err = err
return r
Expand All @@ -430,14 +430,14 @@ func (r *Request) VersionedParams(obj runtime.Object, convertor runtime.ObjectCo
for _, value := range v {
// TODO: Move it to setParam method, once we get rid of
// FieldSelectorParam & LabelSelectorParam methods.
if k == unversioned.LabelSelectorQueryParam(r.apiVersion) && value == "" {
if k == unversioned.LabelSelectorQueryParam(r.groupVersion.String()) && value == "" {
// Don't set an empty selector for backward compatibility.
// Since there is no way to get the difference between empty
// and unspecified string, we don't set it to avoid having
// labelSelector= param in every request.
continue
}
if k == unversioned.FieldSelectorQueryParam(r.apiVersion) {
if k == unversioned.FieldSelectorQueryParam(r.groupVersion.String()) {
if value == "" {
// Don't set an empty selector for backward compatibility.
// Since there is no way to get the difference between empty
Expand All @@ -453,7 +453,7 @@ func (r *Request) VersionedParams(obj runtime.Object, convertor runtime.ObjectCo
}
filteredSelector, err := selector.Transform(
func(field, value string) (newField, newValue string, err error) {
return fieldMappings.filterField(r.apiVersion, r.resource, field, value)
return fieldMappings.filterField(r.groupVersion, r.resource, field, value)
})
if err != nil {
r.err = fmt.Errorf("untransformable field selector: %v", err)
Expand Down
Loading

0 comments on commit 0fd17b7

Please sign in to comment.