mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
First part of parsing settings client-side
This commit is contained in:
parent
5b3c6f1965
commit
881613e3f5
@ -250,7 +250,7 @@ func DeleteController(name string, client client.ClientInterface) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if controller.DesiredState.Replicas != 0 {
|
if controller.DesiredState.Replicas != 0 {
|
||||||
return fmt.Errorf("controller has non-zero replicas (%d)", controller.DesiredState.Replicas)
|
return fmt.Errorf("controller has non-zero replicas (%d), please stop it first", controller.DesiredState.Replicas)
|
||||||
}
|
}
|
||||||
return client.DeleteReplicationController(name)
|
return client.DeleteReplicationController(name)
|
||||||
}
|
}
|
||||||
|
@ -150,6 +150,7 @@ func TestDoRequest(t *testing.T) {
|
|||||||
fakeHandler := util.FakeHandler{
|
fakeHandler := util.FakeHandler{
|
||||||
StatusCode: 200,
|
StatusCode: 200,
|
||||||
ResponseBody: expectedBody,
|
ResponseBody: expectedBody,
|
||||||
|
T: t,
|
||||||
}
|
}
|
||||||
testServer := httptest.NewTLSServer(&fakeHandler)
|
testServer := httptest.NewTLSServer(&fakeHandler)
|
||||||
request, _ := http.NewRequest("GET", testServer.URL+"/foo/bar", nil)
|
request, _ := http.NewRequest("GET", testServer.URL+"/foo/bar", nil)
|
||||||
|
41
pkg/cloudcfg/parse.go
Normal file
41
pkg/cloudcfg/parse.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package cloudcfg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"gopkg.in/v1/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
var storageToType = map[string]reflect.Type{
|
||||||
|
"pods": reflect.TypeOf(api.Pod{}),
|
||||||
|
"services": reflect.TypeOf(api.Service{}),
|
||||||
|
"replicationControllers": reflect.TypeOf(api.ReplicationController{}),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
func ToWireFormat(data []byte, storage string) ([]byte, error) {
|
||||||
|
prototypeType, found := storageToType[storage]
|
||||||
|
if !found {
|
||||||
|
return nil, fmt.Errorf("unknown storage type: %v", storage)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try parsing as json and yaml
|
||||||
|
parsed_json := reflect.New(prototypeType).Interface()
|
||||||
|
json_err := json.Unmarshal(data, parsed_json)
|
||||||
|
parsed_yaml := reflect.New(prototypeType).Interface()
|
||||||
|
yaml_err := yaml.Unmarshal(data, parsed_yaml)
|
||||||
|
|
||||||
|
if json_err != nil && yaml_err != nil {
|
||||||
|
return nil, fmt.Errorf("Could not parse input as json (error: %v) or yaml (error: %v", json_err, yaml_err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if json_err != nil {
|
||||||
|
return json.Marshal(parsed_json)
|
||||||
|
}
|
||||||
|
return json.Marshal(parsed_yaml)
|
||||||
|
}
|
88
pkg/cloudcfg/parse_test.go
Normal file
88
pkg/cloudcfg/parse_test.go
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
package cloudcfg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"gopkg.in/v1/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseBadStorage(t *testing.T) {
|
||||||
|
_, err := ToWireFormat([]byte("{}"), "badstorage")
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected error, received none")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DoParseTest(t *testing.T, storage string, obj interface{}) {
|
||||||
|
json_data, _ := json.Marshal(obj)
|
||||||
|
yaml_data, _ := yaml.Marshal(obj)
|
||||||
|
|
||||||
|
json_got, json_err := ToWireFormat(json_data, storage)
|
||||||
|
yaml_got, yaml_err := ToWireFormat(yaml_data, storage)
|
||||||
|
|
||||||
|
if json_err != nil {
|
||||||
|
t.Errorf("json err: %#v", json_err)
|
||||||
|
}
|
||||||
|
if yaml_err != nil {
|
||||||
|
t.Errorf("yaml err: %#v", yaml_err)
|
||||||
|
}
|
||||||
|
t.Logf("Intermediate yaml:\n%v\n", string(yaml_data))
|
||||||
|
if string(json_got) != string(json_data) {
|
||||||
|
t.Errorf("json output didn't match:\nGot:\n%v\n\nWanted:\n%v\n",
|
||||||
|
string(json_got), string(json_data))
|
||||||
|
}
|
||||||
|
if string(yaml_got) != string(json_data) {
|
||||||
|
t.Errorf("yaml parsed output didn't match:\nGot:\n%v\n\nWanted:\n%v\n",
|
||||||
|
string(yaml_got), string(json_data))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParsePod(t *testing.T) {
|
||||||
|
DoParseTest(t, "pods", api.Pod{
|
||||||
|
JSONBase: api.JSONBase{ID: "test pod"},
|
||||||
|
DesiredState: api.PodState{
|
||||||
|
Manifest: api.ContainerManifest{
|
||||||
|
Id: "My manifest",
|
||||||
|
Containers: []api.Container{
|
||||||
|
{Name: "my container"},
|
||||||
|
},
|
||||||
|
Volumes: []api.Volume{
|
||||||
|
{Name: "volume"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseService(t *testing.T) {
|
||||||
|
DoParseTest(t, "services", api.Service{
|
||||||
|
JSONBase: api.JSONBase{ID: "my service"},
|
||||||
|
Port: 8080,
|
||||||
|
Labels: map[string]string{
|
||||||
|
"area": "staging",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseController(t *testing.T) {
|
||||||
|
DoParseTest(t, "replicationControllers", api.ReplicationController{
|
||||||
|
DesiredState: api.ReplicationControllerState{
|
||||||
|
Replicas: 9001,
|
||||||
|
PodTemplate: api.PodTemplate{
|
||||||
|
DesiredState: api.PodState{
|
||||||
|
Manifest: api.ContainerManifest{
|
||||||
|
Id: "My manifest",
|
||||||
|
Containers: []api.Container{
|
||||||
|
{Name: "my container"},
|
||||||
|
},
|
||||||
|
Volumes: []api.Volume{
|
||||||
|
{Name: "volume"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
@ -17,7 +17,6 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -26,12 +25,18 @@ import (
|
|||||||
type TestInterface interface {
|
type TestInterface interface {
|
||||||
Errorf(format string, args ...interface{})
|
Errorf(format string, args ...interface{})
|
||||||
}
|
}
|
||||||
|
type LogInterface interface {
|
||||||
|
Logf(format string, args ...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
// FakeHandler is to assist in testing HTTP requests.
|
// FakeHandler is to assist in testing HTTP requests.
|
||||||
type FakeHandler struct {
|
type FakeHandler struct {
|
||||||
RequestReceived *http.Request
|
RequestReceived *http.Request
|
||||||
StatusCode int
|
StatusCode int
|
||||||
ResponseBody string
|
ResponseBody string
|
||||||
|
// For logging - you can use a *testing.T
|
||||||
|
// This will keep log messages associated with the test.
|
||||||
|
T LogInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
|
func (f *FakeHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
|
||||||
@ -40,8 +45,8 @@ func (f *FakeHandler) ServeHTTP(response http.ResponseWriter, request *http.Requ
|
|||||||
response.Write([]byte(f.ResponseBody))
|
response.Write([]byte(f.ResponseBody))
|
||||||
|
|
||||||
bodyReceived, err := ioutil.ReadAll(request.Body)
|
bodyReceived, err := ioutil.ReadAll(request.Body)
|
||||||
if err != nil {
|
if err != nil && f.T != nil {
|
||||||
log.Printf("Received read error: %#v", err)
|
f.T.Logf("Received read error: %#v", err)
|
||||||
}
|
}
|
||||||
f.ResponseBody = string(bodyReceived)
|
f.ResponseBody = string(bodyReceived)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user