Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the etcd version check and have it return the version string. #4991

Merged
merged 1 commit into from
Mar 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions pkg/tools/etcd_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ limitations under the License.
package tools

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"reflect"
"strconv"
"strings"

"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
Expand Down Expand Up @@ -418,20 +418,35 @@ func (h *EtcdHelper) AtomicUpdate(key string, ptrToType runtime.Object, ignoreNo
}
}

func checkEtcd(host string) error {
// GetEtcdVersion performs a version check against the provided Etcd server, returning a triplet
// of the release version, internal version, and error (if any).
func GetEtcdVersion(host string) (releaseVersion, internalVersion string, err error) {
response, err := http.Get(host + "/version")
if err != nil {
return err
return "", "", err
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
return "", "", err
}
if !strings.HasPrefix(string(body), "etcd") {
return fmt.Errorf("unknown server: %s", string(body))

var dat map[string]interface{}
if err := json.Unmarshal(body, &dat); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we now requiring etcd version >= 2.0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No explicit version check yet, but this code will enable it. The previous version was just broken. It seems at some point etcd started returning a simple JSON version object for the version request.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unmarshall will return an error with etcd v0.4.6 where /version still returns etcd 0.4.6 so this is essentially a > 0.4.6 minimum version check. Is that a concern? The transition may have happened at etcd v2.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be a concern with the current usage of this code (see NewEtcdClientStartServerIfNecessary below). But, could be an issue if other start relying on this code for a true version check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

return "", "", fmt.Errorf("unknown server: %s", string(body))
}
return nil
if obj := dat["releaseVersion"]; obj != nil {
if s, ok := obj.(string); ok {
releaseVersion = s
}
}
if obj := dat["internalVersion"]; obj != nil {
if s, ok := obj.(string); ok {
internalVersion = s
}
}
return
}

func startEtcd() (*exec.Cmd, error) {
Expand All @@ -444,7 +459,7 @@ func startEtcd() (*exec.Cmd, error) {
}

func NewEtcdClientStartServerIfNecessary(server string) (EtcdClient, error) {
err := checkEtcd(server)
_, _, err := GetEtcdVersion(server)
if err != nil {
glog.Infof("Failed to find etcd, attempting to start.")
_, err := startEtcd()
Expand Down
48 changes: 48 additions & 0 deletions pkg/tools/etcd_tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package tools
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"sync"
"testing"
Expand All @@ -29,6 +31,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/coreos/go-etcd/etcd"
"github.com/stretchr/testify/assert"
)

type TestResource struct {
Expand Down Expand Up @@ -608,3 +611,48 @@ func TestAtomicUpdate_CreateCollision(t *testing.T) {
t.Errorf("Some of the writes were lost. Stored value: %d", stored.Value)
}
}

func TestGetEtcdVersion_ValidVersion(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{\"releaseVersion\":\"2.0.3\",\"internalVersion\":\"2\"}")
}))
defer testServer.Close()

var relVersion string
var intVersion string
var err error
if relVersion, intVersion, err = GetEtcdVersion(testServer.URL); err != nil {
t.Errorf("Unexpected error: %v", err)
}
assert.Equal(t, "2.0.3", relVersion, "Unexpected external version")
assert.Equal(t, "2", intVersion, "Unexpected internal version")
assert.Nil(t, err)
}

func TestGetEtcdVersion_UnknownVersion(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{\"unknownAttribute\":\"foobar\",\"internalVersion\":\"2\"}")
}))
defer testServer.Close()

var relVersion string
var intVersion string
var err error
if relVersion, intVersion, err = GetEtcdVersion(testServer.URL); err != nil {
t.Errorf("Unexpected error: %v", err)
}
assert.Equal(t, "", relVersion, "Unexpected external version")
assert.Equal(t, "2", intVersion, "Unexpected internal version")
assert.Nil(t, err)
}

func TestGetEtcdVersion_ErrorStatus(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
defer testServer.Close()

var err error
_, _, err = GetEtcdVersion(testServer.URL)
assert.NotNil(t, err)
}