move ServerVersion to DiscoveryClient

pull/6/head
Chao Xu 2016-01-05 11:55:34 -08:00
parent 6cbedf2f0b
commit 144b5acd08
11 changed files with 41 additions and 70 deletions

View File

@ -27,7 +27,6 @@ import (
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/version"
)
// Interface holds the methods for clients of Kubernetes,
@ -38,7 +37,6 @@ type Interface interface {
ReplicationControllersNamespacer
ServicesNamespacer
EndpointsNamespacer
VersionInterface
NodesInterface
EventNamespacer
LimitRangesNamespacer
@ -113,33 +111,13 @@ func (c *Client) ComponentStatuses() ComponentStatusInterface {
return newComponentStatuses(c)
}
// VersionInterface has a method to retrieve the server version.
type VersionInterface interface {
ServerVersion() (*version.Info, error)
}
// Client is the implementation of a Kubernetes client.
type Client struct {
*RESTClient
*ExtensionsClient
// TODO: remove this when we re-structure pkg/client.
*DiscoveryClient
}
// ServerVersion retrieves and parses the server's version.
func (c *Client) ServerVersion() (*version.Info, error) {
body, err := c.Get().AbsPath("/version").Do().Raw()
if err != nil {
return nil, err
}
var info version.Info
err = json.Unmarshal(body, &info)
if err != nil {
return nil, fmt.Errorf("got '%s': %v", string(body), err)
}
return &info, nil
}
// SwaggerSchemaInterface has a method to retrieve the swagger schema. Used in
// client.Interface
type SwaggerSchemaInterface interface {

View File

@ -49,7 +49,7 @@ func TestGetServerVersion(t *testing.T) {
defer server.Close()
client := NewOrDie(&Config{Host: server.URL})
got, err := client.ServerVersion()
got, err := client.Discovery().ServerVersion()
if err != nil {
t.Fatalf("unexpected encoding error: %v", err)
}

View File

@ -17,11 +17,14 @@ limitations under the License.
package unversioned
import (
"encoding/json"
"fmt"
"net/url"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/version"
)
// DiscoveryInterface holds the methods that discover server-supported API groups,
@ -29,6 +32,7 @@ import (
type DiscoveryInterface interface {
ServerGroupsInterface
ServerResourcesInterface
ServerVersionInterface
}
// ServerGroupsInterface has methods for obtaining supported groups on the API server
@ -46,6 +50,12 @@ type ServerResourcesInterface interface {
ServerResources() (map[string]*unversioned.APIResourceList, error)
}
// ServerVersionInterface has a method for retrieving the server's version.
type ServerVersionInterface interface {
// ServerVersion retrieves and parses the server's version (git version).
ServerVersion() (*version.Info, error)
}
// DiscoveryClient implements the functions that dicovery server-supported API groups,
// versions and resources.
type DiscoveryClient struct {
@ -138,6 +148,20 @@ func (d *DiscoveryClient) ServerResources() (map[string]*unversioned.APIResource
return result, nil
}
// ServerVersion retrieves and parses the server's version (git version).
func (d *DiscoveryClient) ServerVersion() (*version.Info, error) {
body, err := d.Get().AbsPath("/version").Do().Raw()
if err != nil {
return nil, err
}
var info version.Info
err = json.Unmarshal(body, &info)
if err != nil {
return nil, fmt.Errorf("got '%s': %v", string(body), err)
}
return &info, nil
}
func setDiscoveryDefaults(config *Config) error {
config.Prefix = ""
config.GroupVersion = nil

View File

@ -17,12 +17,10 @@ limitations under the License.
package unversioned
import (
"encoding/json"
"fmt"
"k8s.io/kubernetes/pkg/api/latest"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/version"
)
// Interface holds the experimental methods for clients of Kubernetes
@ -30,7 +28,6 @@ import (
// Features of Extensions group are not supported and may be changed or removed in
// incompatible ways at any time.
type ExtensionsInterface interface {
VersionInterface
HorizontalPodAutoscalersNamespacer
ScaleNamespacer
DaemonSetsNamespacer
@ -48,20 +45,6 @@ type ExtensionsClient struct {
*RESTClient
}
// ServerVersion retrieves and parses the server's version.
func (c *ExtensionsClient) ServerVersion() (*version.Info, error) {
body, err := c.Get().AbsPath("/version").Do().Raw()
if err != nil {
return nil, err
}
var info version.Info
err = json.Unmarshal(body, &info)
if err != nil {
return nil, fmt.Errorf("got '%s': %v", string(body), err)
}
return &info, nil
}
func (c *ExtensionsClient) HorizontalPodAutoscalers(namespace string) HorizontalPodAutoscalerInterface {
return newHorizontalPodAutoscalers(c, namespace)
}

View File

@ -157,7 +157,7 @@ func MatchesServerVersion(client *Client, c *Config) error {
}
}
clientVersion := version.Get()
serverVersion, err := client.ServerVersion()
serverVersion, err := client.Discovery().ServerVersion()
if err != nil {
return fmt.Errorf("couldn't read version from server: %v\n", err)
}

View File

@ -23,7 +23,6 @@ import (
"github.com/emicklei/go-restful/swagger"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/registered"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
client "k8s.io/kubernetes/pkg/client/unversioned"
@ -279,29 +278,6 @@ func (c *Fake) Discovery() client.DiscoveryInterface {
return &FakeDiscovery{c}
}
func (c *Fake) ServerVersion() (*version.Info, error) {
action := ActionImpl{}
action.Verb = "get"
action.Resource = "version"
c.Invokes(action, nil)
versionInfo := version.Get()
return &versionInfo, nil
}
func (c *Fake) ServerAPIVersions() (*unversioned.APIVersions, error) {
action := ActionImpl{}
action.Verb = "get"
action.Resource = "apiversions"
c.Invokes(action, nil)
gvStrings := []string{}
for _, gv := range registered.EnabledVersions() {
gvStrings = append(gvStrings, gv.String())
}
return &unversioned.APIVersions{Versions: gvStrings}, nil
}
func (c *Fake) ComponentStatuses() client.ComponentStatusInterface {
return &FakeComponentStatuses{Fake: c}
}
@ -386,3 +362,13 @@ func (c *FakeDiscovery) ServerResources() (map[string]*unversioned.APIResourceLi
func (c *FakeDiscovery) ServerGroups() (*unversioned.APIGroupList, error) {
return nil, nil
}
func (c *FakeDiscovery) ServerVersion() (*version.Info, error) {
action := ActionImpl{}
action.Verb = "get"
action.Resource = "version"
c.Invokes(action, nil)
versionInfo := version.Get()
return &versionInfo, nil
}

View File

@ -26,7 +26,7 @@ import (
)
func GetServerVersion(w io.Writer, kubeClient client.Interface) {
serverVersion, err := kubeClient.ServerVersion()
serverVersion, err := kubeClient.Discovery().ServerVersion()
if err != nil {
fmt.Printf("Couldn't read server version from server: %v\n", err)
os.Exit(1)

View File

@ -326,7 +326,7 @@ func testMasterUpgrade(ip, v string, mUp func(v string) error) {
}
func checkMasterVersion(c *client.Client, want string) error {
v, err := c.ServerVersion()
v, err := c.Discovery().ServerVersion()
if err != nil {
return fmt.Errorf("checkMasterVersion() couldn't get the master version: %v", err)
}

View File

@ -972,7 +972,7 @@ func (r podResponseChecker) checkAllResponses() (done bool, err error) {
// version.
//
// TODO(18726): This should be incorporated into client.VersionInterface.
func serverVersionGTE(v semver.Version, c client.VersionInterface) (bool, error) {
func serverVersionGTE(v semver.Version, c client.ServerVersionInterface) (bool, error) {
serverVersion, err := c.ServerVersion()
if err != nil {
return false, fmt.Errorf("Unable to get server version: %v", err)

View File

@ -220,7 +220,7 @@ func contactOthers(state *State) {
log.Fatalf("Unable to create client; error: %v\n", err)
}
// Double check that that worked by getting the server version.
if v, err := client.ServerVersion(); err != nil {
if v, err := client.Discovery().ServerVersion(); err != nil {
log.Fatalf("Unable to get server version: %v\n", err)
} else {
log.Printf("Server version: %#v\n", v)

View File

@ -45,7 +45,7 @@ func TestClient(t *testing.T) {
framework.DeleteAllEtcdKeys()
client := client.NewOrDie(&client.Config{Host: s.URL, GroupVersion: testapi.Default.GroupVersion()})
info, err := client.ServerVersion()
info, err := client.Discovery().ServerVersion()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}