Skip to content

Commit

Permalink
fix import namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
wildum committed Dec 21, 2023
1 parent 82da0f9 commit e352a3a
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 39 deletions.
16 changes: 6 additions & 10 deletions integration-tests/tests/module-file/loki.river
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import.file "logTarget" {
filename = "logfile.river"
}

import.file "write" {
filename = "loki_write.river"
}

declare "loki" {
argument "file" {}
loki.source.file "test" {
targets = argument.file.value
forward_to = [loki.write.test.receiver]
}

loki.write "test" {
endpoint {
url = "http://localhost:3100/loki/api/v1/push"
}
external_labels = {
test_name = "module_file",
}
forward_to = [write.loki_write.default.receiver]
}
write.loki_write "default" {}
}

13 changes: 13 additions & 0 deletions integration-tests/tests/module-file/loki_write.river
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
declare "loki_write" {
loki.write "test" {
endpoint {
url = "http://localhost:3100/loki/api/v1/push"
}
external_labels = {
test_name = "module_file",
}
}
export "receiver" {
value = loki.write.test.receiver
}
}
22 changes: 21 additions & 1 deletion pkg/flow/internal/controller/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func (l *Loader) wireModuleDependencies(g *dag.Graph, dc *DeclareComponentNode,
references = deps
} else {
var err error
references, err = GetModuleReferences(declareNode, l.importNodes, l.declareNodes, l.parentModuleDependencies)
references, err = GetModuleReferences(declareNode.content, l.importNodes, l.declareNodes, l.parentModuleDependencies)
if err != nil {
return err
}
Expand Down Expand Up @@ -883,6 +883,26 @@ func (l *Loader) getModuleInfo(fullName string, namespace string, module string)
if err != nil {
return moduleInfo, err
}

// The import node might need its nested imported content.
// We need to pass the correct content according to the namespace to respect the scope.
lastIndex := strings.LastIndex(module, ".")
if lastIndex != -1 {
scope := module[:lastIndex]
moduleInfo.moduleDependencies = make(map[string]string)
for importedMod, importedModContent := range node.importedContent {
if strings.HasPrefix(importedMod, scope) {
moduleInfo.moduleDependencies[strings.TrimPrefix(importedMod, scope+".")] = importedModContent
}
}
} else {
// In this case the declare is only at depth 1 which corresponds to the importedContent, so we can just pass everything.
moduleInfo.moduleDependencies = node.importedContent
}

if err != nil {
return moduleInfo, err
}
} else if c, ok := l.parentModuleDependencies[fullName]; ok {
content = c
} else {
Expand Down
4 changes: 2 additions & 2 deletions pkg/flow/internal/controller/module_references.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ type ModuleReference struct {
}

func GetModuleReferences(
declareNode *DeclareNode,
content string,
importNodes map[string]*ImportConfigNode,
declareNodes map[string]*DeclareNode,
parentModuleDependencies map[string]string,
) ([]ModuleReference, error) {
uniqueReferences := make(map[string]ModuleReference)
err := getModuleReferences(declareNode.content, importNodes, declareNodes, uniqueReferences, parentModuleDependencies)
err := getModuleReferences(content, importNodes, declareNodes, uniqueReferences, parentModuleDependencies)
if err != nil {
return nil, err
}
Expand Down
23 changes: 9 additions & 14 deletions pkg/flow/internal/controller/node_config_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ImportConfigNode struct {
source importsource.ImportSource
registry *prometheus.Registry
importedContent map[string]string
importConfigNodesChildren map[importsource.SourceType]map[string]*ImportConfigNode
importConfigNodesChildren map[string]*ImportConfigNode
OnComponentUpdate func(cn NodeWithDependants) // Informs controller that we need to reevaluate
logger log.Logger
inContentUpdate bool
Expand Down Expand Up @@ -173,16 +173,13 @@ func (cn *ImportConfigNode) processDeclareBlock(stmt *ast.BlockStmt, content str
// processDeclareBlock processes an import block.
func (cn *ImportConfigNode) processImportBlock(stmt *ast.BlockStmt, fullName string) {
sourceType := importsource.GetSourceType(fullName)
if _, ok := cn.importConfigNodesChildren[sourceType][stmt.Label]; ok {
if _, ok := cn.importConfigNodesChildren[stmt.Label]; ok {
level.Error(cn.logger).Log("msg", "import block redefined", "name", stmt.Label)
return
}
childGlobals := cn.globals
childGlobals.OnComponentUpdate = cn.OnChildrenContentUpdate
if cn.importConfigNodesChildren[sourceType] == nil {
cn.importConfigNodesChildren[sourceType] = make(map[string]*ImportConfigNode)
}
cn.importConfigNodesChildren[sourceType][stmt.Label] = NewImportConfigNode(stmt, childGlobals, sourceType)
cn.importConfigNodesChildren[stmt.Label] = NewImportConfigNode(stmt, childGlobals, sourceType)
}

// onContentUpdate is triggered every time the managed import component has new content.
Expand All @@ -192,7 +189,7 @@ func (cn *ImportConfigNode) onContentUpdate(content string) {
cn.inContentUpdate = true
cn.importedContent = make(map[string]string)
// TODO: We recreate the nodes when the content changes. Can we copy instead for optimization?
cn.importConfigNodesChildren = make(map[importsource.SourceType]map[string]*ImportConfigNode)
cn.importConfigNodesChildren = make(map[string]*ImportConfigNode)
node, err := parser.ParseFile(cn.label, []byte(content))
if err != nil {
level.Error(cn.logger).Log("msg", "failed to parse file on update", "err", err)
Expand All @@ -207,13 +204,11 @@ func (cn *ImportConfigNode) onContentUpdate(content string) {

// evaluateChildren evaluates the import nodes managed by this import node.
func (cn *ImportConfigNode) evaluateChildren() {
for _, sourceTypeChildren := range cn.importConfigNodesChildren {
for _, child := range sourceTypeChildren {
child.Evaluate(&vm.Scope{
Parent: nil,
Variables: make(map[string]interface{}),
})
}
for _, child := range cn.importConfigNodesChildren {
child.Evaluate(&vm.Scope{
Parent: nil,
Variables: make(map[string]interface{}),
})
}
}

Expand Down
Loading

0 comments on commit e352a3a

Please sign in to comment.