mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
Merge pull request #1115 from csrwng/use_codec_in_printing_and_parsing
Use codec to encode/decode api objects in client and kubecfg parser
This commit is contained in:
commit
c61bc58683
@ -105,7 +105,7 @@ func readConfig(storage string) []byte {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Fatalf("Unable to read %v: %v\n", *config, err)
|
glog.Fatalf("Unable to read %v: %v\n", *config, err)
|
||||||
}
|
}
|
||||||
data, err = parser.ToWireFormat(data, storage)
|
data, err = parser.ToWireFormat(data, storage, runtime.DefaultCodec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Fatalf("Error parsing %v as an object for %v: %v\n", *config, storage, err)
|
glog.Fatalf("Error parsing %v as an object for %v: %v\n", *config, storage, err)
|
||||||
}
|
}
|
||||||
|
@ -99,7 +99,7 @@ type Client struct {
|
|||||||
// to a URL will prepend the server path. Returns an error if host cannot be converted to a
|
// to a URL will prepend the server path. Returns an error if host cannot be converted to a
|
||||||
// valid URL.
|
// valid URL.
|
||||||
func New(host string, auth *AuthInfo) (*Client, error) {
|
func New(host string, auth *AuthInfo) (*Client, error) {
|
||||||
restClient, err := NewRESTClient(host, auth, "/api/v1beta1/")
|
restClient, err := NewRESTClient(host, auth, "/api/v1beta1/", runtime.DefaultCodec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -143,11 +143,12 @@ type RESTClient struct {
|
|||||||
Sync bool
|
Sync bool
|
||||||
PollPeriod time.Duration
|
PollPeriod time.Duration
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
|
Codec runtime.Codec
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// such as Get, Put, Post, and Delete on specified paths.
|
||||||
func NewRESTClient(host string, auth *AuthInfo, path string) (*RESTClient, error) {
|
func NewRESTClient(host string, auth *AuthInfo, path string, c runtime.Codec) (*RESTClient, error) {
|
||||||
prefix, err := normalizePrefix(host, path)
|
prefix, err := normalizePrefix(host, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -171,6 +172,7 @@ func NewRESTClient(host string, auth *AuthInfo, path string) (*RESTClient, error
|
|||||||
Sync: false,
|
Sync: false,
|
||||||
PollPeriod: time.Second * 2,
|
PollPeriod: time.Second * 2,
|
||||||
Timeout: time.Second * 20,
|
Timeout: time.Second * 20,
|
||||||
|
Codec: c,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ func TestValidatesHostParameter(t *testing.T) {
|
|||||||
"host/server": {"", "", true},
|
"host/server": {"", "", true},
|
||||||
}
|
}
|
||||||
for k, expected := range testCases {
|
for k, expected := range testCases {
|
||||||
c, err := NewRESTClient(k, nil, "/api/v1beta1/")
|
c, err := NewRESTClient(k, nil, "/api/v1beta1/", runtime.DefaultCodec)
|
||||||
switch {
|
switch {
|
||||||
case err == nil && expected.Err:
|
case err == nil && expected.Err:
|
||||||
t.Errorf("expected error but was nil")
|
t.Errorf("expected error but was nil")
|
||||||
|
@ -206,7 +206,7 @@ func (r *Request) Body(obj interface{}) *Request {
|
|||||||
case io.Reader:
|
case io.Reader:
|
||||||
r.body = t
|
r.body = t
|
||||||
case runtime.Object:
|
case runtime.Object:
|
||||||
data, err := runtime.DefaultCodec.Encode(t)
|
data, err := r.c.Codec.Encode(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.err = err
|
r.err = err
|
||||||
return r
|
return r
|
||||||
@ -301,14 +301,15 @@ func (r *Request) Do() Result {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Result{respBody, err}
|
return Result{respBody, err, r.c.Codec}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Result contains the result of calling Request.Do().
|
// Result contains the result of calling Request.Do().
|
||||||
type Result struct {
|
type Result struct {
|
||||||
body []byte
|
body []byte
|
||||||
err error
|
err error
|
||||||
|
codec runtime.Codec
|
||||||
}
|
}
|
||||||
|
|
||||||
// Raw returns the raw result.
|
// Raw returns the raw result.
|
||||||
@ -321,7 +322,7 @@ func (r Result) Get() (runtime.Object, error) {
|
|||||||
if r.err != nil {
|
if r.err != nil {
|
||||||
return nil, r.err
|
return nil, r.err
|
||||||
}
|
}
|
||||||
return runtime.DefaultCodec.Decode(r.body)
|
return r.codec.Decode(r.body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Into stores the result into obj, if possible.
|
// Into stores the result into obj, if possible.
|
||||||
@ -329,7 +330,7 @@ func (r Result) Into(obj runtime.Object) error {
|
|||||||
if r.err != nil {
|
if r.err != nil {
|
||||||
return r.err
|
return r.err
|
||||||
}
|
}
|
||||||
return runtime.DefaultCodec.DecodeInto(r.body, obj)
|
return r.codec.DecodeInto(r.body, obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error returns the error executing the request, nil if no error occurred.
|
// Error returns the error executing the request, nil if no error occurred.
|
||||||
|
@ -38,18 +38,18 @@ func NewParser(objectMap map[string]runtime.Object) *Parser {
|
|||||||
|
|
||||||
// ToWireFormat takes input 'data' as either json or yaml, checks that it parses as the
|
// ToWireFormat takes input 'data' as either json or yaml, checks that it parses as the
|
||||||
// appropriate object type, and returns json for sending to the API or an error.
|
// appropriate object type, and returns json for sending to the API or an error.
|
||||||
func (p *Parser) ToWireFormat(data []byte, storage string) ([]byte, error) {
|
func (p *Parser) ToWireFormat(data []byte, storage string, c runtime.Codec) ([]byte, error) {
|
||||||
prototypeType, found := p.storageToType[storage]
|
prototypeType, found := p.storageToType[storage]
|
||||||
if !found {
|
if !found {
|
||||||
return nil, fmt.Errorf("unknown storage type: %v", storage)
|
return nil, fmt.Errorf("unknown storage type: %v", storage)
|
||||||
}
|
}
|
||||||
|
|
||||||
obj := reflect.New(prototypeType).Interface().(runtime.Object)
|
obj := reflect.New(prototypeType).Interface().(runtime.Object)
|
||||||
err := runtime.DefaultCodec.DecodeInto(data, obj)
|
err := c.DecodeInto(data, obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return runtime.DefaultCodec.Encode(obj)
|
return c.Encode(obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) SupportedWireStorage() []string {
|
func (p *Parser) SupportedWireStorage() []string {
|
||||||
|
@ -26,7 +26,7 @@ import (
|
|||||||
|
|
||||||
func TestParseBadStorage(t *testing.T) {
|
func TestParseBadStorage(t *testing.T) {
|
||||||
p := NewParser(map[string]runtime.Object{})
|
p := NewParser(map[string]runtime.Object{})
|
||||||
_, err := p.ToWireFormat([]byte("{}"), "badstorage")
|
_, err := p.ToWireFormat([]byte("{}"), "badstorage", runtime.DefaultCodec)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected error, received none")
|
t.Errorf("Expected error, received none")
|
||||||
}
|
}
|
||||||
@ -37,8 +37,8 @@ func DoParseTest(t *testing.T, storage string, obj runtime.Object, p *Parser) {
|
|||||||
yamlData, _ := yaml.Marshal(obj)
|
yamlData, _ := yaml.Marshal(obj)
|
||||||
t.Logf("Intermediate yaml:\n%v\n", string(yamlData))
|
t.Logf("Intermediate yaml:\n%v\n", string(yamlData))
|
||||||
t.Logf("Intermediate json:\n%v\n", string(jsonData))
|
t.Logf("Intermediate json:\n%v\n", string(jsonData))
|
||||||
jsonGot, jsonErr := p.ToWireFormat(jsonData, storage)
|
jsonGot, jsonErr := p.ToWireFormat(jsonData, storage, runtime.DefaultCodec)
|
||||||
yamlGot, yamlErr := p.ToWireFormat(yamlData, storage)
|
yamlGot, yamlErr := p.ToWireFormat(yamlData, storage, runtime.DefaultCodec)
|
||||||
|
|
||||||
if jsonErr != nil {
|
if jsonErr != nil {
|
||||||
t.Errorf("json err: %#v", jsonErr)
|
t.Errorf("json err: %#v", jsonErr)
|
||||||
|
Loading…
Reference in New Issue
Block a user