Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
c38c6f0d
Commit
c38c6f0d
authored
Mar 03, 2015
by
Fabio Yeon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix the etcd version check and have it return the version string.
parent
bdb307cc
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
8 deletions
+71
-8
etcd_tools.go
pkg/tools/etcd_tools.go
+23
-8
etcd_tools_test.go
pkg/tools/etcd_tools_test.go
+48
-0
No files found.
pkg/tools/etcd_tools.go
View file @
c38c6f0d
...
...
@@ -17,6 +17,7 @@ limitations under the License.
package
tools
import
(
"encoding/json"
"errors"
"fmt"
"io/ioutil"
...
...
@@ -24,7 +25,6 @@ import (
"os/exec"
"reflect"
"strconv"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
...
...
@@ -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
{
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
)
{
...
...
@@ -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
()
...
...
pkg/tools/etcd_tools_test.go
View file @
c38c6f0d
...
...
@@ -19,6 +19,8 @@ package tools
import
(
"errors"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"sync"
"testing"
...
...
@@ -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
{
...
...
@@ -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
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment