Path: blob/main/pkg/flow/internal/controller/config_export.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 ExportConfigNode struct {11label string12nodeID string13componentName string1415mut sync.RWMutex16block *ast.BlockStmt // Current River blocks to derive config from17eval *vm.Evaluator18value any19}2021var _ BlockNode = (*ExportConfigNode)(nil)2223// NewExportConfigNode creates a new ExportConfigNode from an initial ast.BlockStmt.24// The underlying config isn't applied until Evaluate is called.25func NewExportConfigNode(block *ast.BlockStmt, globals ComponentGlobals) *ExportConfigNode {26return &ExportConfigNode{27label: block.Label,28nodeID: BlockComponentID(block).String(),29componentName: block.GetBlockName(),3031block: block,32eval: vm.New(block.Body),33}34}3536type exportBlock struct {37Value any `river:"value,attr"`38}3940// Evaluate implements BlockNode and updates the arguments for the managed config block41// by re-evaluating its River block with the provided scope. The managed config block42// will be built the first time Evaluate is called.43//44// Evaluate will return an error if the River block cannot be evaluated or if45// decoding to arguments fails.46func (cn *ExportConfigNode) Evaluate(scope *vm.Scope) error {47cn.mut.Lock()48defer cn.mut.Unlock()4950var export exportBlock51if err := cn.eval.Evaluate(scope, &export); err != nil {52return fmt.Errorf("decoding River: %w", err)53}54cn.value = export.Value55return nil56}5758func (cn *ExportConfigNode) Label() string { return cn.label }5960// Value returns the value of the export.61func (cn *ExportConfigNode) Value() any {62cn.mut.RLock()63defer cn.mut.RUnlock()64return cn.value65}6667// Block implements BlockNode and returns the current block of the managed config node.68func (cn *ExportConfigNode) Block() *ast.BlockStmt {69cn.mut.RLock()70defer cn.mut.RUnlock()71return cn.block72}7374// NodeID implements dag.Node and returns the unique ID for the config node.75func (cn *ExportConfigNode) NodeID() string { return cn.nodeID }767778