Add NegotiatedSerializer to config

This commit is contained in:
Wojciech Tyczynski 2016-04-26 08:28:15 +02:00
parent 43b644ea6f
commit b4c83022e3
21 changed files with 63 additions and 50 deletions

View File

@ -204,7 +204,8 @@ func setConfigDefaults(config *$.Config|raw$) error {
config.GroupVersion = &copyGroupVersion config.GroupVersion = &copyGroupVersion
//} //}
config.Codec = $.codecs|raw$.LegacyCodec(*config.GroupVersion) config.NegotiatedSerializer = $.codecs|raw$
if config.QPS == 0 { if config.QPS == 0 {
config.QPS = 5 config.QPS = 5
} }
@ -232,11 +233,7 @@ func setConfigDefaults(config *$.Config|raw$) error {
config.GroupVersion = &copyGroupVersion config.GroupVersion = &copyGroupVersion
//} //}
codec, ok := $.codecs|raw$.SerializerForFileExtension("json") config.NegotiatedSerializer = $.codecs|raw$
if !ok {
return $.Errorf|raw$("unable to find serializer for JSON")
}
config.Codec = codec
if config.QPS == 0 { if config.QPS == 0 {
config.QPS = 5 config.QPS = 5

View File

@ -80,7 +80,8 @@ func setConfigDefaults(config *restclient.Config) error {
config.GroupVersion = &copyGroupVersion config.GroupVersion = &copyGroupVersion
//} //}
config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion) config.NegotiatedSerializer = api.Codecs
if config.QPS == 0 { if config.QPS == 0 {
config.QPS = 5 config.QPS = 5
} }

View File

@ -52,6 +52,7 @@ var (
Extensions TestGroup Extensions TestGroup
Apps TestGroup Apps TestGroup
Federation TestGroup Federation TestGroup
NegotiatedSerializer = api.Codecs
) )
type TestGroup struct { type TestGroup struct {

View File

@ -80,7 +80,8 @@ func setConfigDefaults(config *restclient.Config) error {
config.GroupVersion = &copyGroupVersion config.GroupVersion = &copyGroupVersion
//} //}
config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion) config.NegotiatedSerializer = api.Codecs
if config.QPS == 0 { if config.QPS == 0 {
config.QPS = 5 config.QPS = 5
} }

View File

@ -155,7 +155,8 @@ func setConfigDefaults(config *restclient.Config) error {
config.GroupVersion = &copyGroupVersion config.GroupVersion = &copyGroupVersion
//} //}
config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion) config.NegotiatedSerializer = api.Codecs
if config.QPS == 0 { if config.QPS == 0 {
config.QPS = 5 config.QPS = 5
} }

View File

@ -110,7 +110,8 @@ func setConfigDefaults(config *restclient.Config) error {
config.GroupVersion = &copyGroupVersion config.GroupVersion = &copyGroupVersion
//} //}
config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion) config.NegotiatedSerializer = api.Codecs
if config.QPS == 0 { if config.QPS == 0 {
config.QPS = 5 config.QPS = 5
} }

View File

@ -149,7 +149,8 @@ func setConfigDefaults(config *restclient.Config) error {
config.GroupVersion = &copyGroupVersion config.GroupVersion = &copyGroupVersion
//} //}
config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion) config.NegotiatedSerializer = api.Codecs
if config.QPS == 0 { if config.QPS == 0 {
config.QPS = 5 config.QPS = 5
} }

View File

@ -114,7 +114,8 @@ func setConfigDefaults(config *restclient.Config) error {
config.GroupVersion = &copyGroupVersion config.GroupVersion = &copyGroupVersion
//} //}
config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion) config.NegotiatedSerializer = api.Codecs
if config.QPS == 0 { if config.QPS == 0 {
config.QPS = 5 config.QPS = 5
} }

View File

@ -17,7 +17,6 @@ limitations under the License.
package v1 package v1
import ( import (
fmt "fmt"
api "k8s.io/kubernetes/pkg/api" api "k8s.io/kubernetes/pkg/api"
registered "k8s.io/kubernetes/pkg/apimachinery/registered" registered "k8s.io/kubernetes/pkg/apimachinery/registered"
restclient "k8s.io/kubernetes/pkg/client/restclient" restclient "k8s.io/kubernetes/pkg/client/restclient"
@ -150,11 +149,7 @@ func setConfigDefaults(config *restclient.Config) error {
config.GroupVersion = &copyGroupVersion config.GroupVersion = &copyGroupVersion
//} //}
codec, ok := api.Codecs.SerializerForFileExtension("json") config.NegotiatedSerializer = api.Codecs
if !ok {
return fmt.Errorf("unable to find serializer for JSON")
}
config.Codec = codec
if config.QPS == 0 { if config.QPS == 0 {
config.QPS = 5 config.QPS = 5

View File

@ -17,7 +17,6 @@ limitations under the License.
package v1beta1 package v1beta1
import ( import (
fmt "fmt"
api "k8s.io/kubernetes/pkg/api" api "k8s.io/kubernetes/pkg/api"
registered "k8s.io/kubernetes/pkg/apimachinery/registered" registered "k8s.io/kubernetes/pkg/apimachinery/registered"
restclient "k8s.io/kubernetes/pkg/client/restclient" restclient "k8s.io/kubernetes/pkg/client/restclient"
@ -115,11 +114,7 @@ func setConfigDefaults(config *restclient.Config) error {
config.GroupVersion = &copyGroupVersion config.GroupVersion = &copyGroupVersion
//} //}
codec, ok := api.Codecs.SerializerForFileExtension("json") config.NegotiatedSerializer = api.Codecs
if !ok {
return fmt.Errorf("unable to find serializer for JSON")
}
config.Codec = codec
if config.QPS == 0 { if config.QPS == 0 {
config.QPS = 5 config.QPS = 5

View File

@ -63,7 +63,7 @@ type RESTClient struct {
// NewRESTClient creates a new RESTClient. This client performs generic REST functions // NewRESTClient creates a new RESTClient. This client performs generic REST functions
// such as Get, Put, Post, and Delete on specified paths. Codec controls encoding and // such as Get, Put, Post, and Delete on specified paths. Codec controls encoding and
// decoding of responses from the server. // decoding of responses from the server.
func NewRESTClient(baseURL *url.URL, versionedAPIPath string, config ContentConfig, maxQPS float32, maxBurst int, rateLimiter flowcontrol.RateLimiter, client *http.Client) *RESTClient { func NewRESTClient(baseURL *url.URL, versionedAPIPath string, config ContentConfig, maxQPS float32, maxBurst int, rateLimiter flowcontrol.RateLimiter, client *http.Client) (*RESTClient, error) {
base := *baseURL base := *baseURL
if !strings.HasSuffix(base.Path, "/") { if !strings.HasSuffix(base.Path, "/") {
base.Path += "/" base.Path += "/"
@ -90,7 +90,7 @@ func NewRESTClient(baseURL *url.URL, versionedAPIPath string, config ContentConf
contentConfig: config, contentConfig: config,
Throttle: throttle, Throttle: throttle,
Client: client, Client: client,
} }, nil
} }
// GetRateLimiter returns rate limier for a given client, or nil if it's called on a nil client // GetRateLimiter returns rate limier for a given client, or nil if it's called on a nil client
@ -122,7 +122,8 @@ func readExpBackoffConfig() BackoffManager {
// Verb begins a request with a verb (GET, POST, PUT, DELETE). // Verb begins a request with a verb (GET, POST, PUT, DELETE).
// //
// Example usage of RESTClient's request building interface: // Example usage of RESTClient's request building interface:
// c := NewRESTClient(url, codec) // c, err := NewRESTClient(...)
// if err != nil { ... }
// resp, err := c.Verb("GET"). // resp, err := c.Verb("GET").
// Path("pods"). // Path("pods").
// SelectorParam("labels", "area=staging"). // SelectorParam("labels", "area=staging").

View File

@ -130,9 +130,15 @@ type ContentConfig struct {
// a RESTClient directly. When initializing a Client, will be set with the default // a RESTClient directly. When initializing a Client, will be set with the default
// code version. // code version.
GroupVersion *unversioned.GroupVersion GroupVersion *unversioned.GroupVersion
// NegotiatedSerializer is used for obtaining encoders and decoders for multiple
// supported media types.
NegotiatedSerializer runtime.NegotiatedSerializer
// Codec specifies the encoding and decoding behavior for runtime.Objects passed // Codec specifies the encoding and decoding behavior for runtime.Objects passed
// to a RESTClient or Client. Required when initializing a RESTClient, optional // to a RESTClient or Client. Required when initializing a RESTClient, optional
// when initializing a Client. // when initializing a Client.
//
// DEPRECATED: Please use NegotiatedSerializer instead.
Codec runtime.Codec Codec runtime.Codec
} }
@ -144,8 +150,8 @@ func RESTClientFor(config *Config) (*RESTClient, error) {
if config.GroupVersion == nil { if config.GroupVersion == nil {
return nil, fmt.Errorf("GroupVersion is required when initializing a RESTClient") return nil, fmt.Errorf("GroupVersion is required when initializing a RESTClient")
} }
if config.Codec == nil { if config.NegotiatedSerializer == nil {
return nil, fmt.Errorf("Codec is required when initializing a RESTClient") return nil, fmt.Errorf("NegotiatedSerializer is required when initializing a RESTClient")
} }
baseURL, versionedAPIPath, err := defaultServerUrlFor(config) baseURL, versionedAPIPath, err := defaultServerUrlFor(config)
@ -163,16 +169,14 @@ func RESTClientFor(config *Config) (*RESTClient, error) {
httpClient = &http.Client{Transport: transport} httpClient = &http.Client{Transport: transport}
} }
client := NewRESTClient(baseURL, versionedAPIPath, config.ContentConfig, config.QPS, config.Burst, config.RateLimiter, httpClient) return NewRESTClient(baseURL, versionedAPIPath, config.ContentConfig, config.QPS, config.Burst, config.RateLimiter, httpClient)
return client, nil
} }
// UnversionedRESTClientFor is the same as RESTClientFor, except that it allows // UnversionedRESTClientFor is the same as RESTClientFor, except that it allows
// the config.Version to be empty. // the config.Version to be empty.
func UnversionedRESTClientFor(config *Config) (*RESTClient, error) { func UnversionedRESTClientFor(config *Config) (*RESTClient, error) {
if config.Codec == nil { if config.NegotiatedSerializer == nil {
return nil, fmt.Errorf("Codec is required when initializing a RESTClient") return nil, fmt.Errorf("NeogitatedSerializer is required when initializing a RESTClient")
} }
baseURL, versionedAPIPath, err := defaultServerUrlFor(config) baseURL, versionedAPIPath, err := defaultServerUrlFor(config)
@ -196,8 +200,7 @@ func UnversionedRESTClientFor(config *Config) (*RESTClient, error) {
versionConfig.GroupVersion = &v versionConfig.GroupVersion = &v
} }
client := NewRESTClient(baseURL, versionedAPIPath, versionConfig, config.QPS, config.Burst, config.RateLimiter, httpClient) return NewRESTClient(baseURL, versionedAPIPath, versionConfig, config.QPS, config.Burst, config.RateLimiter, httpClient)
return client, nil
} }
// SetKubernetesDefaults sets default values on the provided client config for accessing the // SetKubernetesDefaults sets default values on the provided client config for accessing the

View File

@ -87,13 +87,13 @@ func TestSetKubernetesDefaultsUserAgent(t *testing.T) {
} }
func TestRESTClientRequires(t *testing.T) { func TestRESTClientRequires(t *testing.T) {
if _, err := RESTClientFor(&Config{Host: "127.0.0.1", ContentConfig: ContentConfig{Codec: testapi.Default.Codec()}}); err == nil { if _, err := RESTClientFor(&Config{Host: "127.0.0.1", ContentConfig: ContentConfig{NegotiatedSerializer: testapi.NegotiatedSerializer}}); err == nil {
t.Errorf("unexpected non-error") t.Errorf("unexpected non-error")
} }
if _, err := RESTClientFor(&Config{Host: "127.0.0.1", ContentConfig: ContentConfig{GroupVersion: testapi.Default.GroupVersion()}}); err == nil { if _, err := RESTClientFor(&Config{Host: "127.0.0.1", ContentConfig: ContentConfig{GroupVersion: testapi.Default.GroupVersion()}}); err == nil {
t.Errorf("unexpected non-error") t.Errorf("unexpected non-error")
} }
if _, err := RESTClientFor(&Config{Host: "127.0.0.1", ContentConfig: ContentConfig{GroupVersion: testapi.Default.GroupVersion(), Codec: testapi.Default.Codec()}}); err != nil { if _, err := RESTClientFor(&Config{Host: "127.0.0.1", ContentConfig: ContentConfig{GroupVersion: testapi.Default.GroupVersion(), NegotiatedSerializer: testapi.NegotiatedSerializer}}); err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
} }

View File

@ -1308,5 +1308,9 @@ func testRESTClient(t testing.TB, srv *httptest.Server) *RESTClient {
} }
} }
versionedAPIPath := testapi.Default.ResourcePath("", "", "") versionedAPIPath := testapi.Default.ResourcePath("", "", "")
return NewRESTClient(baseURL, versionedAPIPath, ContentConfig{GroupVersion: testapi.Default.GroupVersion(), Codec: testapi.Default.Codec()}, 0, 0, nil, nil) client, err := NewRESTClient(baseURL, versionedAPIPath, ContentConfig{GroupVersion: testapi.Default.GroupVersion(), Codec: testapi.Default.Codec()}, 0, 0, nil, nil)
if err != nil {
t.Fatalf("failed to create a client: %v", err)
}
return client
} }

View File

@ -72,6 +72,7 @@ func setAppsDefaults(config *restclient.Config) error {
//} //}
config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion) config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion)
config.NegotiatedSerializer = api.Codecs
if config.QPS == 0 { if config.QPS == 0 {
config.QPS = 5 config.QPS = 5
} }

View File

@ -73,6 +73,7 @@ func setAutoscalingDefaults(config *restclient.Config) error {
//} //}
config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion) config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion)
config.NegotiatedSerializer = api.Codecs
if config.QPS == 0 { if config.QPS == 0 {
config.QPS = 5 config.QPS = 5
} }

View File

@ -73,6 +73,7 @@ func setBatchDefaults(config *restclient.Config) error {
//} //}
config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion) config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion)
config.NegotiatedSerializer = api.Codecs
if config.QPS == 0 { if config.QPS == 0 {
config.QPS = 5 config.QPS = 5
} }

View File

@ -127,6 +127,7 @@ func setExtensionsDefaults(config *restclient.Config) error {
//} //}
config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion) config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion)
config.NegotiatedSerializer = api.Codecs
if config.QPS == 0 { if config.QPS == 0 {
config.QPS = 5 config.QPS = 5
} }

View File

@ -231,6 +231,9 @@ func SetKubernetesDefaults(config *restclient.Config) error {
// TODO: Unconditionally set the config.Version, until we fix the config. // TODO: Unconditionally set the config.Version, until we fix the config.
copyGroupVersion := g.GroupVersion copyGroupVersion := g.GroupVersion
config.GroupVersion = &copyGroupVersion config.GroupVersion = &copyGroupVersion
if config.NegotiatedSerializer == nil {
config.NegotiatedSerializer = api.Codecs
}
if config.Codec == nil { if config.Codec == nil {
config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion) config.Codec = api.Codecs.LegacyCodec(*config.GroupVersion)
} }

View File

@ -42,6 +42,7 @@ func TestSetKubernetesDefaults(t *testing.T) {
ContentConfig: restclient.ContentConfig{ ContentConfig: restclient.ContentConfig{
GroupVersion: testapi.Default.GroupVersion(), GroupVersion: testapi.Default.GroupVersion(),
Codec: testapi.Default.Codec(), Codec: testapi.Default.Codec(),
NegotiatedSerializer: testapi.NegotiatedSerializer,
}, },
QPS: 5, QPS: 5,
Burst: 10, Burst: 10,
@ -125,7 +126,7 @@ func TestHelperGetServerAPIVersions(t *testing.T) {
w.Write(output) w.Write(output)
})) }))
defer server.Close() defer server.Close()
got, err := restclient.ServerAPIVersions(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &unversioned.GroupVersion{Group: "invalid version", Version: "one"}, Codec: testapi.Default.Codec()}}) got, err := restclient.ServerAPIVersions(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &unversioned.GroupVersion{Group: "invalid version", Version: "one"}, NegotiatedSerializer: testapi.NegotiatedSerializer}})
if err != nil { if err != nil {
t.Fatalf("unexpected encoding error: %v", err) t.Fatalf("unexpected encoding error: %v", err)
} }

View File

@ -212,7 +212,10 @@ func TestStream(t *testing.T) {
server := httptest.NewServer(fakeServer(t, name, exec, testCase.Stdin, testCase.Stdout, testCase.Stderr, testCase.Error, testCase.Tty, testCase.MessageCount, testCase.ServerProtocols)) server := httptest.NewServer(fakeServer(t, name, exec, testCase.Stdin, testCase.Stdout, testCase.Stderr, testCase.Error, testCase.Tty, testCase.MessageCount, testCase.ServerProtocols))
url, _ := url.ParseRequestURI(server.URL) url, _ := url.ParseRequestURI(server.URL)
c := restclient.NewRESTClient(url, "", restclient.ContentConfig{GroupVersion: &unversioned.GroupVersion{Group: "x"}}, -1, -1, nil, nil) c, err := restclient.NewRESTClient(url, "", restclient.ContentConfig{GroupVersion: &unversioned.GroupVersion{Group: "x"}}, -1, -1, nil, nil)
if err != nil {
t.Fatalf("failed to create a client: %v", err)
}
req := c.Post().Resource("testing") req := c.Post().Resource("testing")
if exec { if exec {