mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 12:15:52 +00:00
Make apiserver work with new encode/decode
This commit is contained in:
parent
d7b4915111
commit
14361e336a
@ -17,7 +17,6 @@ limitations under the License.
|
||||
package apiserver
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
@ -27,6 +26,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
)
|
||||
@ -130,7 +130,7 @@ func (server *ApiServer) notFound(req *http.Request, w http.ResponseWriter) {
|
||||
|
||||
func (server *ApiServer) write(statusCode int, object interface{}, w http.ResponseWriter) {
|
||||
w.WriteHeader(statusCode)
|
||||
output, err := json.MarshalIndent(object, "", " ")
|
||||
output, err := api.EncodeIndent(object)
|
||||
if err != nil {
|
||||
server.error(err, w)
|
||||
return
|
||||
|
@ -26,9 +26,14 @@ import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
)
|
||||
|
||||
func init() {
|
||||
api.AddKnownTypes(Simple{}, SimpleList{})
|
||||
}
|
||||
|
||||
// TODO: This doesn't reduce typing enough to make it worth the less readable errors. Remove.
|
||||
func expectNoError(t *testing.T, err error) {
|
||||
if err != nil {
|
||||
@ -37,11 +42,13 @@ func expectNoError(t *testing.T, err error) {
|
||||
}
|
||||
|
||||
type Simple struct {
|
||||
Name string
|
||||
JSONBase api.JSONBase `json:",inline"`
|
||||
Name string
|
||||
}
|
||||
|
||||
type SimpleList struct {
|
||||
Items []Simple
|
||||
JSONBase api.JSONBase `json:",inline"`
|
||||
Items []Simple
|
||||
}
|
||||
|
||||
type SimpleRESTStorage struct {
|
||||
@ -54,7 +61,7 @@ type SimpleRESTStorage struct {
|
||||
}
|
||||
|
||||
func (storage *SimpleRESTStorage) List(labels.Selector) (interface{}, error) {
|
||||
result := SimpleList{
|
||||
result := &SimpleList{
|
||||
Items: storage.list,
|
||||
}
|
||||
return result, storage.err
|
||||
@ -71,7 +78,7 @@ func (storage *SimpleRESTStorage) Delete(id string) (<-chan interface{}, error)
|
||||
|
||||
func (storage *SimpleRESTStorage) Extract(body []byte) (interface{}, error) {
|
||||
var item Simple
|
||||
json.Unmarshal(body, &item)
|
||||
api.DecodeInto(body, &item)
|
||||
return item, storage.err
|
||||
}
|
||||
|
||||
@ -90,7 +97,7 @@ func extractBody(response *http.Response, object interface{}) (string, error) {
|
||||
if err != nil {
|
||||
return string(body), err
|
||||
}
|
||||
err = json.Unmarshal(body, object)
|
||||
err = api.DecodeInto(body, object)
|
||||
return string(body), err
|
||||
}
|
||||
|
||||
@ -150,6 +157,7 @@ func TestNonEmptyList(t *testing.T) {
|
||||
body, err := extractBody(resp, &listOut)
|
||||
if len(listOut.Items) != 1 {
|
||||
t.Errorf("Unexpected response: %#v", listOut)
|
||||
return
|
||||
}
|
||||
if listOut.Items[0].Name != simpleStorage.list[0].Name {
|
||||
t.Errorf("Unexpected data: %#v, %s", listOut.Items[0], string(body))
|
||||
|
Loading…
Reference in New Issue
Block a user