This starts the work of having a `NodeProvider` which is responsible for providing node details. It splits the responsibilities of node management off to a new controller. The primary change here is to add the framework pieces for node management and move the VK CLI to use this new controller. It also adds support for node leases where available. This can be enabled via the command line (disabled by default), but may fall back if we find that leaess aren't supported on the cluster.
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package source
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/token"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func scanToDeferLine(fileset *token.FileSet, node ast.Node, lineNum int) ast.Node {
|
|
var matchedNode ast.Node
|
|
ast.Inspect(node, func(node ast.Node) bool {
|
|
switch {
|
|
case node == nil || matchedNode != nil:
|
|
return false
|
|
case fileset.Position(node.End()).Line == lineNum:
|
|
if funcLit, ok := node.(*ast.FuncLit); ok {
|
|
matchedNode = funcLit
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
debug("defer line node: %s", debugFormatNode{matchedNode})
|
|
return matchedNode
|
|
}
|
|
|
|
func guessDefer(node ast.Node) (ast.Node, error) {
|
|
defers := collectDefers(node)
|
|
switch len(defers) {
|
|
case 0:
|
|
return nil, errors.New("failed to expression in defer")
|
|
case 1:
|
|
return defers[0].Call, nil
|
|
default:
|
|
return nil, errors.Errorf(
|
|
"ambiguous call expression: multiple (%d) defers in call block",
|
|
len(defers))
|
|
}
|
|
}
|
|
|
|
func collectDefers(node ast.Node) []*ast.DeferStmt {
|
|
var defers []*ast.DeferStmt
|
|
ast.Inspect(node, func(node ast.Node) bool {
|
|
if d, ok := node.(*ast.DeferStmt); ok {
|
|
defers = append(defers, d)
|
|
debug("defer: %s", debugFormatNode{d})
|
|
return false
|
|
}
|
|
return true
|
|
})
|
|
return defers
|
|
}
|