-
Notifications
You must be signed in to change notification settings - Fork 6
Add readiness endpoint #15
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
Merged
nocturnalastro
merged 2 commits into
k8snetworkplumbingwg:main
from
nocturnalastro:readiness
Mar 19, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
#!/bin/bash | ||
|
||
docker build -t ghcr.io/k8snetworkplumbingwg/linuxptp-daemon-image -f ./Dockerfile . | ||
CONTAINER_TOOL="${CONTAINER_TOOL:-docker}" | ||
IMAGE_NAME="${IMAGE_NAME:-linuxptp-daemon-image}" | ||
IMAGE_TAG_BASE="${IMAGE_TAG_BASE:-ghcr.io/k8snetworkplumbingwg/${IMAGE_NAME}}" | ||
VERSION="${VERSION:-latest}" | ||
IMG="${IMAGE_TAG_BASE}:${VERSION}" | ||
|
||
$CONTAINER_TOOL build -t "${IMG}" -f ./Dockerfile . |
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package daemon | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/golang/glog" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
utilwait "k8s.io/apimachinery/pkg/util/wait" | ||
) | ||
|
||
type ReadyTracker struct { | ||
mutex sync.Mutex | ||
config bool | ||
processManager *ProcessManager | ||
} | ||
|
||
func (rt *ReadyTracker) Ready() (bool, string) { | ||
rt.mutex.Lock() | ||
defer rt.mutex.Unlock() | ||
|
||
if !rt.config { | ||
return false, "Config not applied" | ||
} | ||
|
||
if len(rt.processManager.process) == 0 { | ||
return false, "No processes have started" | ||
} | ||
|
||
notRunning := strings.Builder{} | ||
noMetrics := strings.Builder{} | ||
for _, p := range rt.processManager.process { | ||
if p.Stopped() { | ||
if notRunning.Len() > 0 { | ||
notRunning.WriteString(", ") | ||
} | ||
notRunning.WriteString(p.name) | ||
} else if !p.hasCollectedMetrics { | ||
if noMetrics.Len() > 0 { | ||
noMetrics.WriteString(", ") | ||
} | ||
noMetrics.WriteString(p.name) | ||
} | ||
|
||
} | ||
if notRunning.Len() > 0 { | ||
return false, "Stopped process(es): " + notRunning.String() | ||
} | ||
|
||
if noMetrics.Len() > 0 { | ||
return false, "Process(es) have not yet collected metrics: " + noMetrics.String() | ||
} | ||
|
||
return true, "" | ||
} | ||
|
||
func (rt *ReadyTracker) setConfig(v bool) { | ||
rt.mutex.Lock() | ||
rt.config = v | ||
rt.mutex.Unlock() | ||
} | ||
|
||
type readyHandler struct { | ||
tracker *ReadyTracker | ||
} | ||
|
||
func (h readyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
if isReady, msg := h.tracker.Ready(); !isReady { | ||
w.WriteHeader(http.StatusServiceUnavailable) | ||
fmt.Fprintf(w, "503: %s\n", msg) | ||
} else { | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
} | ||
|
||
func StartReadyServer(bindAddress string, tracker *ReadyTracker) { | ||
glog.Info("Starting Ready Server") | ||
mux := http.NewServeMux() | ||
mux.Handle("/ready", readyHandler{tracker: tracker}) | ||
go utilwait.Until(func() { | ||
err := http.ListenAndServe(bindAddress, mux) | ||
if err != nil { | ||
utilruntime.HandleError(fmt.Errorf("starting metrics server failed: %v", err)) | ||
} | ||
}, 5*time.Second, utilwait.NeverStop) | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.