Skip to content

Commit

Permalink
plugin: Fix nil deref on nil metrics (#909)
Browse files Browse the repository at this point in the history
The autoscaler-agent <-> scheduler plugin protocol allows the
autoscaler-agent to skip the metrics field if there aren't any, which
typically occurs right after the VM is created.

With the changes from #750, we assumed that .Metrics will always be
non-nil, meaning that it panics when .Metrics *is* actually nil.
  • Loading branch information
sharnoff authored Apr 16, 2024
1 parent 76ceab2 commit 9c7f4f8
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions pkg/plugin/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,14 @@ func (e *AutoscaleEnforcer) handleAgentRequest(
}
// check that nil-ness of req.Metrics.{LoadAverage5Min,MemoryUsageBytes} match what's expected
// for the protocol version.
if (req.Metrics.LoadAverage5Min != nil) != (req.Metrics.MemoryUsageBytes != nil) {
return nil, 400, fmt.Errorf("presence of metrics.loadAvg5M must match presence of metrics.memoryUsageBytes")
} else if req.Metrics.LoadAverage5Min == nil && req.ProtoVersion.IncludesExtendedMetrics() {
return nil, 400, fmt.Errorf("nil metrics.{loadAvg5M,memoryUsageBytes} not supported for protocol version %v", req.ProtoVersion)
} else if req.Metrics.LoadAverage5Min != nil && !req.ProtoVersion.IncludesExtendedMetrics() {
return nil, 400, fmt.Errorf("non-nil metrics.{loadAvg5M,memoryUsageBytes} not supported for protocol version %v", req.ProtoVersion)
if req.Metrics != nil {
if (req.Metrics.LoadAverage5Min != nil) != (req.Metrics.MemoryUsageBytes != nil) {
return nil, 400, fmt.Errorf("presence of metrics.loadAvg5M must match presence of metrics.memoryUsageBytes")
} else if req.Metrics.LoadAverage5Min == nil && req.ProtoVersion.IncludesExtendedMetrics() {
return nil, 400, fmt.Errorf("nil metrics.{loadAvg5M,memoryUsageBytes} not supported for protocol version %v", req.ProtoVersion)
} else if req.Metrics.LoadAverage5Min != nil && !req.ProtoVersion.IncludesExtendedMetrics() {
return nil, 400, fmt.Errorf("non-nil metrics.{loadAvg5M,memoryUsageBytes} not supported for protocol version %v", req.ProtoVersion)
}
}

e.state.lock.Lock()
Expand Down

0 comments on commit 9c7f4f8

Please sign in to comment.