This repository was archived by the owner on Jan 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Change APIServer startup message #48
Merged
k8s-ci-robot
merged 9 commits into
kubernetes-retired:master
from
totherme:apiserver-startup-message
Mar 23, 2018
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
52a74ea
Change APIServer startup message
hoegaarden 5205b84
Check APIServer readiness using healthcheck API
320842d
Simplify ProcessState initialization
hoegaarden d8db11d
Stop poller go routine when timeout expired
hoegaarden 151bdde
Document thati use of StartMessage is discouraged
hoegaarden a0f4835
Document the rationale behind `CheckAPIServerIsReady`
hoegaarden 36d7cad
Simplify the poller handling and cancelation
hoegaarden b04870d
Make HealthCheckPollInterval configurable
hoegaarden 49018b7
Simplify tests a bit by pulling out URL handling
hoegaarden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"os/exec" | ||
|
@@ -16,7 +17,23 @@ import ( | |
|
||
type ProcessState struct { | ||
DefaultedProcessInput | ||
Session *gexec.Session | ||
Session *gexec.Session | ||
// Healthcheck Endpoint. If we get http.StatusOK from this endpoint, we | ||
// assume the process is ready to operate. E.g. "/healthz". If this is set, | ||
// we ignore StartMessage. | ||
HealthCheckEndpoint string | ||
// HealthCheckPollInterval is the interval which will be used for polling the | ||
// HealthCheckEndpoint. | ||
// If left empty it will default to 100 Milliseconds. | ||
HealthCheckPollInterval time.Duration | ||
// StartMessage is the message to wait for on stderr. If we recieve this | ||
// message, we assume the process is ready to operate. Ignored if | ||
// HealthCheckEndpoint is specified. | ||
// | ||
// The usage of StartMessage is discouraged, favour HealthCheckEndpoint | ||
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. Great! |
||
// instead! | ||
// | ||
// Deprecated: Use HealthCheckEndpoint in favour of StartMessage | ||
StartMessage string | ||
Args []string | ||
} | ||
|
@@ -86,17 +103,24 @@ func DoDefaulting( | |
return defaults, nil | ||
} | ||
|
||
type stopChannel chan struct{} | ||
|
||
func (ps *ProcessState) Start(stdout, stderr io.Writer) (err error) { | ||
command := exec.Command(ps.Path, ps.Args...) | ||
|
||
startDetectStream := gbytes.NewBuffer() | ||
detectedStart := startDetectStream.Detect(ps.StartMessage) | ||
ready := make(chan bool) | ||
timedOut := time.After(ps.StartTimeout) | ||
var pollerStopCh stopChannel | ||
|
||
if stderr == nil { | ||
stderr = startDetectStream | ||
if ps.HealthCheckEndpoint != "" { | ||
healthCheckURL := ps.URL | ||
healthCheckURL.Path = ps.HealthCheckEndpoint | ||
pollerStopCh = make(stopChannel) | ||
go pollURLUntilOK(healthCheckURL, ps.HealthCheckPollInterval, ready, pollerStopCh) | ||
} else { | ||
stderr = io.MultiWriter(startDetectStream, stderr) | ||
startDetectStream := gbytes.NewBuffer() | ||
ready = startDetectStream.Detect(ps.StartMessage) | ||
stderr = safeMultiWriter(stderr, startDetectStream) | ||
} | ||
|
||
ps.Session, err = gexec.Start(command, stdout, stderr) | ||
|
@@ -105,14 +129,49 @@ func (ps *ProcessState) Start(stdout, stderr io.Writer) (err error) { | |
} | ||
|
||
select { | ||
case <-detectedStart: | ||
case <-ready: | ||
return nil | ||
case <-timedOut: | ||
ps.Session.Terminate() | ||
if pollerStopCh != nil { | ||
close(pollerStopCh) | ||
} | ||
if ps.Session != nil { | ||
ps.Session.Terminate() | ||
} | ||
return fmt.Errorf("timeout waiting for process %s to start", path.Base(ps.Path)) | ||
} | ||
} | ||
|
||
func safeMultiWriter(writers ...io.Writer) io.Writer { | ||
safeWriters := []io.Writer{} | ||
for _, w := range writers { | ||
if w != nil { | ||
safeWriters = append(safeWriters, w) | ||
} | ||
} | ||
return io.MultiWriter(safeWriters...) | ||
} | ||
|
||
func pollURLUntilOK(url url.URL, interval time.Duration, ready chan bool, stopCh stopChannel) { | ||
if interval <= 0 { | ||
interval = 100 * time.Millisecond | ||
} | ||
for { | ||
res, err := http.Get(url.String()) | ||
if err == nil && res.StatusCode == http.StatusOK { | ||
ready <- true | ||
return | ||
} | ||
|
||
select { | ||
case <-stopCh: | ||
return | ||
default: | ||
time.Sleep(interval) | ||
} | ||
} | ||
} | ||
|
||
func (ps *ProcessState) Stop() error { | ||
if ps.Session == nil { | ||
return nil | ||
|
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.
Thank you!