-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add readiness endpoint #15
Draft
nocturnalastro
wants to merge
2
commits into
k8snetworkplumbingwg:main
Choose a base branch
from
nocturnalastro:readiness
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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 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 |
---|---|---|
@@ -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 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 |
---|---|---|
|
@@ -97,6 +97,15 @@ func NewProcessManager() *ProcessManager { | |
} | ||
} | ||
|
||
// NewDaemonForTests is used by unit tests | ||
func NewDaemonForTests(tracker *ReadyTracker, processManager *ProcessManager) *Daemon { | ||
tracker.processManager = processManager | ||
return &Daemon{ | ||
readyTracker: tracker, | ||
processManager: processManager, | ||
} | ||
} | ||
|
||
// SetTestProfileProcess ... | ||
func (p *ProcessManager) SetTestProfileProcess(name string, ifaces config.IFaces, socketPath, | ||
processConfigPath string, nodeProfile ptpv1.PtpProfile) { | ||
|
@@ -151,27 +160,28 @@ func (p *ProcessManager) UpdateSynceConfig(config *synce.Relations) { | |
} | ||
|
||
type ptpProcess struct { | ||
name string | ||
ifaces config.IFaces | ||
processSocketPath string | ||
processConfigPath string | ||
configName string | ||
messageTag string | ||
eventCh chan event.EventChannel | ||
exitCh chan bool | ||
execMutex sync.Mutex | ||
stopped bool | ||
logFilterRegex string | ||
cmd *exec.Cmd | ||
depProcess []process // these are list of dependent process which needs to be started/stopped if the parent process is starts/stops | ||
nodeProfile ptpv1.PtpProfile | ||
parentClockClass float64 | ||
pmcCheck bool | ||
clockType event.ClockType | ||
ptpClockThreshold *ptpv1.PtpClockThreshold | ||
haProfile map[string][]string // stores list of interface name for each profile | ||
syncERelations *synce.Relations | ||
c *net.Conn | ||
name string | ||
ifaces config.IFaces | ||
processSocketPath string | ||
processConfigPath string | ||
configName string | ||
messageTag string | ||
eventCh chan event.EventChannel | ||
exitCh chan bool | ||
execMutex sync.Mutex | ||
stopped bool | ||
logFilterRegex string | ||
cmd *exec.Cmd | ||
depProcess []process // these are list of dependent process which needs to be started/stopped if the parent process is starts/stops | ||
nodeProfile ptpv1.PtpProfile | ||
parentClockClass float64 | ||
pmcCheck bool | ||
clockType event.ClockType | ||
ptpClockThreshold *ptpv1.PtpClockThreshold | ||
haProfile map[string][]string // stores list of interface name for each profile | ||
syncERelations *synce.Relations | ||
c *net.Conn | ||
hasCollectedMetrics bool | ||
} | ||
|
||
func (p *ptpProcess) Stopped() bool { | ||
|
@@ -202,6 +212,7 @@ type Daemon struct { | |
ptpUpdate *LinuxPTPConfUpdate | ||
|
||
processManager *ProcessManager | ||
readyTracker *ReadyTracker | ||
|
||
hwconfigs *[]ptpv1.HwConfig | ||
|
||
|
@@ -230,13 +241,20 @@ func New( | |
refreshNodePtpDevice *bool, | ||
closeManager chan bool, | ||
pmcPollInterval int, | ||
tracker *ReadyTracker, | ||
) *Daemon { | ||
if !stdoutToSocket { | ||
RegisterMetrics(nodeName) | ||
} | ||
InitializeOffsetMaps() | ||
pluginManager := registerPlugins(plugins) | ||
eventChannel := make(chan event.EventChannel, 100) | ||
pm := &ProcessManager{ | ||
process: nil, | ||
eventChannel: eventChannel, | ||
ptpEventHandler: event.Init(nodeName, stdoutToSocket, eventSocket, eventChannel, closeManager, Offset, ClockState, ClockClassMetrics), | ||
} | ||
tracker.processManager = pm | ||
return &Daemon{ | ||
nodeName: nodeName, | ||
namespace: namespace, | ||
|
@@ -248,12 +266,9 @@ func New( | |
refreshNodePtpDevice: refreshNodePtpDevice, | ||
pmcPollInterval: pmcPollInterval, | ||
//TODO:Enable only for GM | ||
processManager: &ProcessManager{ | ||
process: nil, | ||
eventChannel: eventChannel, | ||
ptpEventHandler: event.Init(nodeName, stdoutToSocket, eventSocket, eventChannel, closeManager, Offset, ClockState, ClockClassMetrics), | ||
}, | ||
stopCh: stopCh, | ||
processManager: pm, | ||
readyTracker: tracker, | ||
stopCh: stopCh, | ||
} | ||
} | ||
|
||
|
@@ -305,9 +320,10 @@ func printWhenNotNil(p interface{}, description string) { | |
} | ||
} | ||
|
||
// SetProcessManager ... | ||
// SetProcessManager in tests | ||
func (dn *Daemon) SetProcessManager(p *ProcessManager) { | ||
dn.processManager = p | ||
dn.readyTracker.processManager = p | ||
} | ||
|
||
// Delete all socket and config files | ||
|
@@ -329,6 +345,8 @@ func (dn *Daemon) cleanupTempFiles() error { | |
} | ||
|
||
func (dn *Daemon) applyNodePTPProfiles() error { | ||
dn.readyTracker.setConfig(false) | ||
|
||
glog.Infof("in applyNodePTPProfiles") | ||
for _, p := range dn.processManager.process { | ||
if p != nil { | ||
|
@@ -343,6 +361,7 @@ func (dn *Daemon) applyNodePTPProfiles() error { | |
} | ||
} | ||
p.depProcess = nil | ||
p.hasCollectedMetrics = false | ||
//cleanup metrics | ||
deleteMetrics(p.ifaces, p.haProfile, p.name, p.configName) | ||
if p.name == syncEProcessName && p.syncERelations != nil { | ||
|
@@ -427,6 +446,7 @@ func (dn *Daemon) applyNodePTPProfiles() error { | |
} | ||
dn.pluginManager.PopulateHwConfig(dn.hwconfigs) | ||
*dn.refreshNodePtpDevice = true | ||
dn.readyTracker.setConfig(true) | ||
return nil | ||
} | ||
|
||
|
@@ -1009,6 +1029,7 @@ func (p *ptpProcess) processPTPMetrics(output string) { | |
p.ProcessSynceEvents(logEntry) | ||
} else { | ||
configName, source, ptpOffset, clockState, iface := extractMetrics(p.messageTag, p.name, p.ifaces, output) | ||
p.hasCollectedMetrics = true | ||
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. you would know if there are metrics only if you have ptpOffset, I think That coould be good start. But if events are enable dthen extract metrics is moved to cloud-event-proxy and it will not enter into this condition . |
||
if iface != "" { // for ptp4l/phc2sys this function only update metrics | ||
var values map[event.ValueType]interface{} | ||
ifaceName := masterOffsetIface.getByAlias(configName, iface).name | ||
|
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,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.
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.
good catch