mirror of https://github.com/k3s-io/k3s
Serve API version list, test with an integration test.
parent
1da5c444e8
commit
dca7363459
|
@ -251,6 +251,17 @@ func runReplicationControllerTest(c *client.Client) {
|
|||
glog.Infof("Pods created")
|
||||
}
|
||||
|
||||
func runAPIVersionsTest(c *client.Client) {
|
||||
v, err := c.ServerAPIVersions()
|
||||
if err != nil {
|
||||
glog.Fatalf("failed to get api versions: %v", err)
|
||||
}
|
||||
if e, a := []string{"v1beta1", "v1beta2"}, v.Versions; !reflect.DeepEqual(e, a) {
|
||||
glog.Fatalf("Expected version list '%v', got '%v'", e, a)
|
||||
}
|
||||
glog.Infof("Version test passed")
|
||||
}
|
||||
|
||||
func runAtomicPutTest(c *client.Client) {
|
||||
var svc api.Service
|
||||
err := c.Post().Path("services").Body(
|
||||
|
@ -426,6 +437,7 @@ func main() {
|
|||
runReplicationControllerTest,
|
||||
runAtomicPutTest,
|
||||
runServiceTest,
|
||||
runAPIVersionsTest,
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(testFuncs))
|
||||
|
|
|
@ -135,6 +135,13 @@ func handleVersion(w http.ResponseWriter, req *http.Request) {
|
|||
writeRawJSON(http.StatusOK, version.Get(), w)
|
||||
}
|
||||
|
||||
// APIVersionHandler returns a handler which will list the provided versions as available.
|
||||
func APIVersionHandler(versions ...string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
writeRawJSON(http.StatusOK, version.APIVersions{Versions: versions}, w)
|
||||
})
|
||||
}
|
||||
|
||||
// writeJSON renders an object as JSON to the response.
|
||||
func writeJSON(statusCode int, codec runtime.Codec, object runtime.Object, w http.ResponseWriter) {
|
||||
output, err := codec.Encode(object)
|
||||
|
|
|
@ -62,6 +62,7 @@ func (c *Client) Services(namespace string) ServiceInterface {
|
|||
// VersionInterface has a method to retrieve the server version.
|
||||
type VersionInterface interface {
|
||||
ServerVersion() (*version.Info, error)
|
||||
ServerAPIVersions() (*version.APIVersions, error)
|
||||
}
|
||||
|
||||
// APIStatus is exposed by errors that can be converted to an api.Status object
|
||||
|
@ -88,3 +89,17 @@ func (c *Client) ServerVersion() (*version.Info, error) {
|
|||
}
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
// ServerAPIVersions retrieves and parses the list of API versions the server supports.
|
||||
func (c *Client) ServerAPIVersions() (*version.APIVersions, error) {
|
||||
body, err := c.Get().AbsPath("/api").Do().Raw()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var v version.APIVersions
|
||||
err = json.Unmarshal(body, &v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Got '%s': %v", string(body), err)
|
||||
}
|
||||
return &v, nil
|
||||
}
|
||||
|
|
|
@ -70,3 +70,8 @@ func (c *Fake) ServerVersion() (*version.Info, error) {
|
|||
versionInfo := version.Get()
|
||||
return &versionInfo, nil
|
||||
}
|
||||
|
||||
func (c *Fake) ServerAPIVersions() (*version.APIVersions, error) {
|
||||
c.Actions = append(c.Actions, FakeAction{Action: "get-apiversions", Value: nil})
|
||||
return &version.APIVersions{Versions: []string{"v1beta1", "v1beta2"}}, nil
|
||||
}
|
||||
|
|
|
@ -190,6 +190,8 @@ func (m *Master) init(c *Config) {
|
|||
}
|
||||
apiserver.NewAPIGroup(m.API_v1beta1()).InstallREST(m.mux, c.APIPrefix+"/v1beta1")
|
||||
apiserver.NewAPIGroup(m.API_v1beta2()).InstallREST(m.mux, c.APIPrefix+"/v1beta2")
|
||||
versionHandler := apiserver.APIVersionHandler("v1beta1", "v1beta2")
|
||||
m.mux.Handle(c.APIPrefix, versionHandler)
|
||||
apiserver.InstallSupport(m.mux)
|
||||
if c.EnableLogsSupport {
|
||||
apiserver.InstallLogsSupport(m.mux)
|
||||
|
|
|
@ -45,3 +45,9 @@ func Get() Info {
|
|||
func (info Info) String() string {
|
||||
return info.GitVersion
|
||||
}
|
||||
|
||||
// APIVersions lists the api versions that are available, to allow
|
||||
// version negotiation.
|
||||
type APIVersions struct {
|
||||
Versions []string `json:"versions" yaml:"versions"`
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue