Skip to content

Commit

Permalink
always return error in status unless node is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
maximpertsov committed Aug 20, 2024
1 parent ff43ecc commit b102850
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions resource/graph_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,15 @@ func (w *GraphNode) ResourceStatus() Status {
return w.resourceStatus()
}

func (w *GraphNode) getLoggerOrGlobal() logging.Logger {
if w.logger == nil {
// This node has not yet been configured with a logger - use the global logger as
// a fall-back.
return logging.Global()
}
return w.logger
}

func (w *GraphNode) resourceStatus() Status {
var resName Name
if w.current == nil {
Expand All @@ -545,25 +554,17 @@ func (w *GraphNode) resourceStatus() Status {
resName = w.current.Name()
}

var err error
if w.state == NodeStateUnhealthy {
err = w.lastErr
if err == nil && w.logger != nil {
// an unhealthy node should always have a non-nil error - log a warning if this
// invariant is violated.
w.logger.Warnw("an unhealthy node doesn't have an error", "resource", resName)
}
} else if w.lastErr != nil && w.logger != nil {
// a recovered node should not have an error - log a warning if this invariant is
// violated.
var logf = w.logger.Warnf
if w.state == NodeStateRemoving {
// a previously-unhealthy node can be marked for removal but still retain the
// error - this is not surprising so log at the debug level.
logf = w.logger.Debugf
}
logf("a node has an error but is not unhealthy",
"resource", resName, "state", w.state.String(), "error", w.lastErr)
err := w.lastErr
logger := w.getLoggerOrGlobal()

// check invariants between state and error
switch {
case w.state == NodeStateUnhealthy && w.lastErr == nil:
logger.Warnw("an unhealthy node doesn't have an error", "resource", resName)
case w.state == NodeStateUnhealthy && w.lastErr != nil:
logger.Warnw("a ready node still has an error", "resource", resName, "error", err)
// do not return leftover error in status if the node is ready
err = nil
}

return Status{
Expand Down

0 comments on commit b102850

Please sign in to comment.