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
f4e97be7
Commit
f4e97be7
authored
Jun 23, 2015
by
Brendan Burns
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Switch to using the official etcd health check.
parent
f4e7b548
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
72 additions
and
4 deletions
+72
-4
validator.go
pkg/apiserver/validator.go
+8
-0
validator_test.go
pkg/apiserver/validator_test.go
+17
-3
master.go
pkg/master/master.go
+1
-1
etcd_helper.go
pkg/tools/etcd_helper.go
+17
-0
etcd_helper_test.go
pkg/tools/etcd_helper_test.go
+29
-0
No files found.
pkg/apiserver/validator.go
View file @
f4e97be7
...
@@ -33,11 +33,14 @@ type httpGet interface {
...
@@ -33,11 +33,14 @@ type httpGet interface {
Get
(
url
string
)
(
*
http
.
Response
,
error
)
Get
(
url
string
)
(
*
http
.
Response
,
error
)
}
}
type
ValidatorFn
func
([]
byte
)
error
type
Server
struct
{
type
Server
struct
{
Addr
string
Addr
string
Port
int
Port
int
Path
string
Path
string
EnableHTTPS
bool
EnableHTTPS
bool
Validate
ValidatorFn
}
}
type
ServerStatus
struct
{
type
ServerStatus
struct
{
...
@@ -85,5 +88,10 @@ func (server *Server) DoServerCheck(rt http.RoundTripper) (probe.Result, string,
...
@@ -85,5 +88,10 @@ func (server *Server) DoServerCheck(rt http.RoundTripper) (probe.Result, string,
return
probe
.
Failure
,
string
(
data
),
return
probe
.
Failure
,
string
(
data
),
fmt
.
Errorf
(
"unhealthy http status code: %d (%s)"
,
resp
.
StatusCode
,
resp
.
Status
)
fmt
.
Errorf
(
"unhealthy http status code: %d (%s)"
,
resp
.
StatusCode
,
resp
.
Status
)
}
}
if
server
.
Validate
!=
nil
{
if
err
:=
server
.
Validate
(
data
);
err
!=
nil
{
return
probe
.
Failure
,
string
(
data
),
err
}
}
return
probe
.
Success
,
string
(
data
),
nil
return
probe
.
Success
,
string
(
data
),
nil
}
}
pkg/apiserver/validator_test.go
View file @
f4e97be7
...
@@ -18,6 +18,7 @@ package apiserver
...
@@ -18,6 +18,7 @@ package apiserver
import
(
import
(
"bytes"
"bytes"
"errors"
"fmt"
"fmt"
"io/ioutil"
"io/ioutil"
"net/http"
"net/http"
...
@@ -37,6 +38,15 @@ func (f *fakeRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
...
@@ -37,6 +38,15 @@ func (f *fakeRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
return
f
.
resp
,
f
.
err
return
f
.
resp
,
f
.
err
}
}
func
alwaysError
([]
byte
)
error
{
return
errors
.
New
(
"test error"
)
}
func
matchError
(
data
[]
byte
)
error
{
if
string
(
data
)
==
"bar"
{
return
errors
.
New
(
"match error"
)
}
return
nil
}
func
TestValidate
(
t
*
testing
.
T
)
{
func
TestValidate
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
tests
:=
[]
struct
{
err
error
err
error
...
@@ -44,10 +54,13 @@ func TestValidate(t *testing.T) {
...
@@ -44,10 +54,13 @@ func TestValidate(t *testing.T) {
expectedStatus
probe
.
Result
expectedStatus
probe
.
Result
code
int
code
int
expectErr
bool
expectErr
bool
validator
ValidatorFn
}{
}{
{
fmt
.
Errorf
(
"test error"
),
""
,
probe
.
Unknown
,
500
/*ignored*/
,
true
},
{
fmt
.
Errorf
(
"test error"
),
""
,
probe
.
Unknown
,
500
/*ignored*/
,
true
,
nil
},
{
nil
,
"foo"
,
probe
.
Success
,
200
,
false
},
{
nil
,
"foo"
,
probe
.
Success
,
200
,
false
,
nil
},
{
nil
,
"foo"
,
probe
.
Failure
,
500
,
true
},
{
nil
,
"foo"
,
probe
.
Failure
,
500
,
true
,
nil
},
{
nil
,
"foo"
,
probe
.
Failure
,
200
,
true
,
alwaysError
},
{
nil
,
"foo"
,
probe
.
Success
,
200
,
false
,
matchError
},
}
}
s
:=
Server
{
Addr
:
"foo.com"
,
Port
:
8080
,
Path
:
"/healthz"
}
s
:=
Server
{
Addr
:
"foo.com"
,
Port
:
8080
,
Path
:
"/healthz"
}
...
@@ -60,6 +73,7 @@ func TestValidate(t *testing.T) {
...
@@ -60,6 +73,7 @@ func TestValidate(t *testing.T) {
StatusCode
:
test
.
code
,
StatusCode
:
test
.
code
,
},
},
}
}
s
.
Validate
=
test
.
validator
status
,
data
,
err
:=
s
.
DoServerCheck
(
fakeRT
)
status
,
data
,
err
:=
s
.
DoServerCheck
(
fakeRT
)
expect
:=
fmt
.
Sprintf
(
"http://%s:%d/healthz"
,
s
.
Addr
,
s
.
Port
)
expect
:=
fmt
.
Sprintf
(
"http://%s:%d/healthz"
,
s
.
Addr
,
s
.
Port
)
if
fakeRT
.
url
!=
expect
{
if
fakeRT
.
url
!=
expect
{
...
...
pkg/master/master.go
View file @
f4e97be7
...
@@ -715,7 +715,7 @@ func (m *Master) getServersToValidate(c *Config) map[string]apiserver.Server {
...
@@ -715,7 +715,7 @@ func (m *Master) getServersToValidate(c *Config) map[string]apiserver.Server {
addr
=
etcdUrl
.
Host
addr
=
etcdUrl
.
Host
port
=
4001
port
=
4001
}
}
serversToValidate
[
fmt
.
Sprintf
(
"etcd-%d"
,
ix
)]
=
apiserver
.
Server
{
Addr
:
addr
,
Port
:
port
,
Path
:
"/
v2/keys/"
}
serversToValidate
[
fmt
.
Sprintf
(
"etcd-%d"
,
ix
)]
=
apiserver
.
Server
{
Addr
:
addr
,
Port
:
port
,
Path
:
"/
health"
,
Validate
:
tools
.
EtcdHealthCheck
}
}
}
return
serversToValidate
return
serversToValidate
}
}
...
...
pkg/tools/etcd_helper.go
View file @
f4e97be7
...
@@ -17,6 +17,7 @@ limitations under the License.
...
@@ -17,6 +17,7 @@ limitations under the License.
package
tools
package
tools
import
(
import
(
"encoding/json"
"errors"
"errors"
"fmt"
"fmt"
"io/ioutil"
"io/ioutil"
...
@@ -712,3 +713,19 @@ func NewEtcdClientStartServerIfNecessary(server string) (EtcdClient, error) {
...
@@ -712,3 +713,19 @@ func NewEtcdClientStartServerIfNecessary(server string) (EtcdClient, error) {
servers
:=
[]
string
{
server
}
servers
:=
[]
string
{
server
}
return
etcd
.
NewClient
(
servers
),
nil
return
etcd
.
NewClient
(
servers
),
nil
}
}
type
etcdHealth
struct
{
// Note this has to be public so the json library can modify it.
Health
string
`json:health`
}
func
EtcdHealthCheck
(
data
[]
byte
)
error
{
obj
:=
etcdHealth
{}
if
err
:=
json
.
Unmarshal
(
data
,
&
obj
);
err
!=
nil
{
return
err
}
if
obj
.
Health
!=
"true"
{
return
fmt
.
Errorf
(
"Unhealthy status: %s"
,
obj
.
Health
)
}
return
nil
}
pkg/tools/etcd_helper_test.go
View file @
f4e97be7
...
@@ -856,3 +856,32 @@ func TestPrefixEtcdKey(t *testing.T) {
...
@@ -856,3 +856,32 @@ func TestPrefixEtcdKey(t *testing.T) {
assert
.
Equal
(
t
,
keyBefore
,
keyAfter
,
"Prefix incorrectly added by EtcdHelper"
)
assert
.
Equal
(
t
,
keyBefore
,
keyAfter
,
"Prefix incorrectly added by EtcdHelper"
)
}
}
func
TestEtcdHealthCheck
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
data
string
expectErr
bool
}{
{
data
:
"{
\"
health
\"
:
\"
true
\"
}"
,
expectErr
:
false
,
},
{
data
:
"{
\"
health
\"
:
\"
false
\"
}"
,
expectErr
:
true
,
},
{
data
:
"invalid json"
,
expectErr
:
true
,
},
}
for
_
,
test
:=
range
tests
{
err
:=
EtcdHealthCheck
([]
byte
(
test
.
data
))
if
err
!=
nil
&&
!
test
.
expectErr
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
if
err
==
nil
&&
test
.
expectErr
{
t
.
Error
(
"unexpected non-error"
)
}
}
}
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