forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b6ff69
commit 41c6680
Showing
8 changed files
with
339 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
Copyright 2014 Google Inc. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kubelet | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" | ||
"github.com/golang/glog" | ||
) | ||
|
||
type HealthChecker interface { | ||
IsHealthy(container api.Container) (bool, error) | ||
} | ||
|
||
type httpDoInterface interface { | ||
Get(string) (*http.Response, error) | ||
} | ||
|
||
func MakeHealthChecker() HealthChecker { | ||
return &MuxHealthChecker{ | ||
checkers: map[string]HealthChecker{ | ||
"http": &HTTPHealthChecker{ | ||
client: &http.Client{}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
type MuxHealthChecker struct { | ||
checkers map[string]HealthChecker | ||
} | ||
|
||
func (m *MuxHealthChecker) IsHealthy(container api.Container) (bool, error) { | ||
checker, ok := m.checkers[container.LivenessProbe.Type] | ||
if !ok || checker == nil { | ||
glog.Warningf("Failed to find health checker for %s %s", container.Name, container.LivenessProbe.Type) | ||
return true, nil | ||
} | ||
return checker.IsHealthy(container) | ||
} | ||
|
||
type HTTPHealthChecker struct { | ||
client httpDoInterface | ||
} | ||
|
||
func (h *HTTPHealthChecker) findPort(container api.Container, portName string) int64 { | ||
for _, port := range container.Ports { | ||
if port.Name == portName { | ||
// TODO This means you can only health check exposed ports | ||
return int64(port.HostPort) | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func (h *HTTPHealthChecker) IsHealthy(container api.Container) (bool, error) { | ||
params := container.LivenessProbe.HTTPGet | ||
port := h.findPort(container, params.Port) | ||
if port == -1 { | ||
var err error | ||
port, err = strconv.ParseInt(params.Port, 10, 0) | ||
if err != nil { | ||
return true, err | ||
} | ||
} | ||
var host string | ||
if len(params.Host) > 0 { | ||
host = params.Host | ||
} else { | ||
host = "localhost" | ||
} | ||
url := fmt.Sprintf("http://%s:%d%s", host, port, params.Path) | ||
res, err := h.client.Get(url) | ||
if res != nil && res.Body != nil { | ||
defer res.Body.Close() | ||
} | ||
if err != nil { | ||
// At this point, if it fails, its either a policy (unlikely) or HTTP protocol (likely) error. | ||
return false, nil | ||
} | ||
return res.StatusCode == http.StatusOK, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
Copyright 2014 Google Inc. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kubelet | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" | ||
) | ||
|
||
type fakeHttpClient struct { | ||
req string | ||
res http.Response | ||
err error | ||
} | ||
|
||
func (f *fakeHttpClient) Get(url string) (*http.Response, error) { | ||
f.req = url | ||
return &f.res, f.err | ||
} | ||
|
||
func TestHttpHealth(t *testing.T) { | ||
fakeClient := fakeHttpClient{ | ||
res: http.Response{ | ||
StatusCode: http.StatusOK, | ||
}, | ||
} | ||
|
||
check := HTTPHealthChecker{ | ||
client: &fakeClient, | ||
} | ||
|
||
container := api.Container{ | ||
LivenessProbe: api.LivenessProbe{ | ||
HTTPGet: api.HTTPGetProbe{ | ||
Port: "8080", | ||
Path: "/foo/bar", | ||
}, | ||
Type: "http", | ||
}, | ||
} | ||
|
||
ok, err := check.IsHealthy(container) | ||
if !ok { | ||
t.Error("Unexpected unhealthy") | ||
} | ||
if err != nil { | ||
t.Errorf("Unexpected error: %#v", err) | ||
} | ||
if fakeClient.req != "http://localhost:8080/foo/bar" { | ||
t.Errorf("Unexpected url: %s", fakeClient.req) | ||
} | ||
} | ||
|
||
func TestFindPort(t *testing.T) { | ||
container := api.Container{ | ||
Ports: []api.Port{ | ||
{ | ||
Name: "foo", | ||
HostPort: 8080, | ||
}, | ||
{ | ||
Name: "bar", | ||
HostPort: 9000, | ||
}, | ||
}, | ||
} | ||
check := HTTPHealthChecker{} | ||
validatePort(t, check.findPort(container, "foo"), 8080) | ||
} | ||
|
||
func validatePort(t *testing.T, port int64, expectedPort int64) { | ||
if port != expectedPort { | ||
t.Errorf("Unexpected port: %d, expected: %d", port, expectedPort) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.