Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/flow/internal/controller/config_argument.go
4095 views
1
package controller
2
3
import (
4
"fmt"
5
"sync"
6
7
"github.com/grafana/agent/pkg/river/ast"
8
"github.com/grafana/agent/pkg/river/vm"
9
)
10
11
type ArgumentConfigNode struct {
12
label string
13
nodeID string
14
componentName string
15
16
mut sync.RWMutex
17
block *ast.BlockStmt // Current River blocks to derive config from
18
eval *vm.Evaluator
19
defaultValue any
20
optional bool
21
}
22
23
var _ BlockNode = (*ArgumentConfigNode)(nil)
24
25
// NewArgumentConfigNode creates a new ArgumentConfigNode from an initial ast.BlockStmt.
26
// The underlying config isn't applied until Evaluate is called.
27
func NewArgumentConfigNode(block *ast.BlockStmt, globals ComponentGlobals) *ArgumentConfigNode {
28
return &ArgumentConfigNode{
29
label: block.Label,
30
nodeID: BlockComponentID(block).String(),
31
componentName: block.GetBlockName(),
32
33
block: block,
34
eval: vm.New(block.Body),
35
}
36
}
37
38
type argumentBlock struct {
39
Optional bool `river:"optional,attr,optional"`
40
Default any `river:"default,attr,optional"`
41
}
42
43
// Evaluate implements BlockNode and updates the arguments for the managed config block
44
// by re-evaluating its River block with the provided scope. The managed config block
45
// will be built the first time Evaluate is called.
46
//
47
// Evaluate will return an error if the River block cannot be evaluated or if
48
// decoding to arguments fails.
49
func (cn *ArgumentConfigNode) Evaluate(scope *vm.Scope) error {
50
cn.mut.Lock()
51
defer cn.mut.Unlock()
52
53
var argument argumentBlock
54
if err := cn.eval.Evaluate(scope, &argument); err != nil {
55
return fmt.Errorf("decoding River: %w", err)
56
}
57
58
cn.defaultValue = argument.Default
59
cn.optional = argument.Optional
60
61
return nil
62
}
63
64
func (cn *ArgumentConfigNode) Optional() bool {
65
cn.mut.RLock()
66
defer cn.mut.RUnlock()
67
return cn.optional
68
}
69
70
func (cn *ArgumentConfigNode) Default() any {
71
cn.mut.RLock()
72
defer cn.mut.RUnlock()
73
return cn.defaultValue
74
}
75
76
func (cn *ArgumentConfigNode) Label() string { return cn.label }
77
78
// Block implements BlockNode and returns the current block of the managed config node.
79
func (cn *ArgumentConfigNode) Block() *ast.BlockStmt {
80
cn.mut.RLock()
81
defer cn.mut.RUnlock()
82
return cn.block
83
}
84
85
// NodeID implements dag.Node and returns the unique ID for the config node.
86
func (cn *ArgumentConfigNode) NodeID() string { return cn.nodeID }
87
88