Skip to content

Commit

Permalink
Serve API version list, test with an integration test.
Browse files Browse the repository at this point in the history
  • Loading branch information
lavalamp committed Oct 29, 2014
1 parent 1da5c44 commit dca7363
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -426,6 +437,7 @@ func main() {
runReplicationControllerTest,
runAtomicPutTest,
runServiceTest,
runAPIVersionsTest,
}
var wg sync.WaitGroup
wg.Add(len(testFuncs))
Expand Down
7 changes: 7 additions & 0 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
5 changes: 5 additions & 0 deletions pkg/client/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions pkg/master/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit dca7363

Please sign in to comment.