-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
Separate healthz server from metrics server in kube-proxy #44968
Merged
k8s-github-robot
merged 4 commits into
kubernetes:master
from
MrHohn:kube-proxy-healthcheck
May 8, 2017
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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 |
---|---|---|
|
@@ -22,13 +22,16 @@ import ( | |
"net/http" | ||
"strings" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/golang/glog" | ||
"github.com/renstrom/dedent" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
clientv1 "k8s.io/client-go/pkg/api/v1" | ||
"k8s.io/client-go/tools/record" | ||
"k8s.io/client-go/util/clock" | ||
"k8s.io/kubernetes/pkg/api" | ||
) | ||
|
||
|
@@ -233,3 +236,92 @@ func (hcs *server) SyncEndpoints(newEndpoints map[types.NamespacedName]int) erro | |
} | ||
return nil | ||
} | ||
|
||
// HealthzUpdater allows callers to update healthz timestamp only. | ||
type HealthzUpdater interface { | ||
UpdateTimestamp() | ||
} | ||
|
||
// HealthzServer returns 200 "OK" by default. Once timestamp has been | ||
// updated, it verifies we don't exceed max no respond duration since | ||
// last update. | ||
type HealthzServer struct { | ||
listener Listener | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you need locking? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The plan is to use sync/atomic.Value for concurrent read/write. I think it should be sufficient? |
||
httpFactory HTTPServerFactory | ||
clock clock.Clock | ||
|
||
addr string | ||
port int32 | ||
healthTimeout time.Duration | ||
|
||
lastUpdated atomic.Value | ||
} | ||
|
||
// NewDefaultHealthzServer returns a default healthz http server. | ||
func NewDefaultHealthzServer(addr string, healthTimeout time.Duration) *HealthzServer { | ||
return newHealthzServer(nil, nil, nil, addr, healthTimeout) | ||
} | ||
|
||
func newHealthzServer(listener Listener, httpServerFactory HTTPServerFactory, c clock.Clock, addr string, healthTimeout time.Duration) *HealthzServer { | ||
if listener == nil { | ||
listener = stdNetListener{} | ||
} | ||
if httpServerFactory == nil { | ||
httpServerFactory = stdHTTPServerFactory{} | ||
} | ||
if c == nil { | ||
c = clock.RealClock{} | ||
} | ||
return &HealthzServer{ | ||
listener: listener, | ||
httpFactory: httpServerFactory, | ||
clock: c, | ||
addr: addr, | ||
healthTimeout: healthTimeout, | ||
} | ||
} | ||
|
||
// UpdateTimestamp updates the lastUpdated timestamp. | ||
func (hs *HealthzServer) UpdateTimestamp() { | ||
hs.lastUpdated.Store(hs.clock.Now()) | ||
} | ||
|
||
// Run starts the healthz http server and returns. | ||
func (hs *HealthzServer) Run() { | ||
serveMux := http.NewServeMux() | ||
serveMux.Handle("/healthz", healthzHandler{hs: hs}) | ||
server := hs.httpFactory.New(hs.addr, serveMux) | ||
listener, err := hs.listener.Listen(hs.addr) | ||
if err != nil { | ||
glog.Errorf("Failed to start healthz on %s: %v", hs.addr, err) | ||
return | ||
} | ||
go func() { | ||
glog.V(3).Infof("Starting goroutine for healthz on %s", hs.addr) | ||
if err := server.Serve(listener); err != nil { | ||
glog.Errorf("Healhz closed: %v", err) | ||
return | ||
} | ||
glog.Errorf("Unexpected healhz closed.") | ||
}() | ||
} | ||
|
||
type healthzHandler struct { | ||
hs *HealthzServer | ||
} | ||
|
||
func (h healthzHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { | ||
lastUpdated := time.Time{} | ||
if val := h.hs.lastUpdated.Load(); val != nil { | ||
lastUpdated = val.(time.Time) | ||
} | ||
currentTime := h.hs.clock.Now() | ||
|
||
resp.Header().Set("Content-Type", "application/json") | ||
if !lastUpdated.IsZero() && currentTime.After(lastUpdated.Add(h.hs.healthTimeout)) { | ||
resp.WriteHeader(http.StatusServiceUnavailable) | ||
} else { | ||
resp.WriteHeader(http.StatusOK) | ||
} | ||
fmt.Fprintf(resp, fmt.Sprintf(`{"lastUpdated": %q,"currentTime": %q}`, lastUpdated, currentTime)) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why don't we use these constants to generate the strings in the defaults above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.