Path: blob/main/pkg/flow/internal/controller/config_argument.go
4095 views
package controller12import (3"fmt"4"sync"56"github.com/grafana/agent/pkg/river/ast"7"github.com/grafana/agent/pkg/river/vm"8)910type ArgumentConfigNode struct {11label string12nodeID string13componentName string1415mut sync.RWMutex16block *ast.BlockStmt // Current River blocks to derive config from17eval *vm.Evaluator18defaultValue any19optional bool20}2122var _ BlockNode = (*ArgumentConfigNode)(nil)2324// NewArgumentConfigNode creates a new ArgumentConfigNode from an initial ast.BlockStmt.25// The underlying config isn't applied until Evaluate is called.26func NewArgumentConfigNode(block *ast.BlockStmt, globals ComponentGlobals) *ArgumentConfigNode {27return &ArgumentConfigNode{28label: block.Label,29nodeID: BlockComponentID(block).String(),30componentName: block.GetBlockName(),3132block: block,33eval: vm.New(block.Body),34}35}3637type argumentBlock struct {38Optional bool `river:"optional,attr,optional"`39Default any `river:"default,attr,optional"`40}4142// Evaluate implements BlockNode and updates the arguments for the managed config block43// by re-evaluating its River block with the provided scope. The managed config block44// will be built the first time Evaluate is called.45//46// Evaluate will return an error if the River block cannot be evaluated or if47// decoding to arguments fails.48func (cn *ArgumentConfigNode) Evaluate(scope *vm.Scope) error {49cn.mut.Lock()50defer cn.mut.Unlock()5152var argument argumentBlock53if err := cn.eval.Evaluate(scope, &argument); err != nil {54return fmt.Errorf("decoding River: %w", err)55}5657cn.defaultValue = argument.Default58cn.optional = argument.Optional5960return nil61}6263func (cn *ArgumentConfigNode) Optional() bool {64cn.mut.RLock()65defer cn.mut.RUnlock()66return cn.optional67}6869func (cn *ArgumentConfigNode) Default() any {70cn.mut.RLock()71defer cn.mut.RUnlock()72return cn.defaultValue73}7475func (cn *ArgumentConfigNode) Label() string { return cn.label }7677// Block implements BlockNode and returns the current block of the managed config node.78func (cn *ArgumentConfigNode) Block() *ast.BlockStmt {79cn.mut.RLock()80defer cn.mut.RUnlock()81return cn.block82}8384// NodeID implements dag.Node and returns the unique ID for the config node.85func (cn *ArgumentConfigNode) NodeID() string { return cn.nodeID }868788