Skip to content

Commit

Permalink
add stability lvl to config blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
wildum committed Jan 17, 2025
1 parent 30766dd commit ba6905e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
3 changes: 2 additions & 1 deletion internal/runtime/foreach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/grafana/alloy/internal/component"
"github.com/grafana/alloy/internal/featuregate"
"github.com/grafana/alloy/internal/runtime"
alloy_runtime "github.com/grafana/alloy/internal/runtime"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -103,7 +104,7 @@ func buildTestForEach(t *testing.T, filename string) testForEachFile {
func testConfigForEach(t *testing.T, config string, reloadConfig string, update func(), expectedMetrics *string, expectedDurationMetrics *int) {
defer verifyNoGoroutineLeaks(t)
reg := prometheus.NewRegistry()
ctrl, f := setup(t, config, reg)
ctrl, f := setup(t, config, reg, featuregate.StabilityExperimental)

err := ctrl.LoadSource(f, nil, "")
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions internal/runtime/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func TestImportError(t *testing.T) {

func testConfig(t *testing.T, config string, reloadConfig string, update func()) {
defer verifyNoGoroutineLeaks(t)
ctrl, f := setup(t, config, nil)
ctrl, f := setup(t, config, nil, featuregate.StabilityPublicPreview)

err := ctrl.LoadSource(f, nil, "")
require.NoError(t, err)
Expand Down Expand Up @@ -352,7 +352,7 @@ func testConfig(t *testing.T, config string, reloadConfig string, update func())

func testConfigError(t *testing.T, config string, expectedError string) {
defer verifyNoGoroutineLeaks(t)
ctrl, f := setup(t, config, nil)
ctrl, f := setup(t, config, nil, featuregate.StabilityPublicPreview)
err := ctrl.LoadSource(f, nil, "")
require.ErrorContains(t, err, expectedError)
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -369,13 +369,13 @@ func testConfigError(t *testing.T, config string, expectedError string) {
}()
}

func setup(t *testing.T, config string, reg prometheus.Registerer) (*alloy_runtime.Runtime, *alloy_runtime.Source) {
func setup(t *testing.T, config string, reg prometheus.Registerer, stability featuregate.Stability) (*alloy_runtime.Runtime, *alloy_runtime.Source) {
s, err := logging.New(os.Stderr, logging.DefaultOptions)
require.NoError(t, err)
ctrl := alloy_runtime.New(alloy_runtime.Options{
Logger: s,
DataPath: t.TempDir(),
MinStability: featuregate.StabilityPublicPreview,
MinStability: stability,
Reg: reg,
Services: []service.Service{},
})
Expand Down
13 changes: 13 additions & 0 deletions internal/runtime/internal/controller/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,19 @@ func TestLoader(t *testing.T) {
diags = applyFromContent(t, l, nil, nil, []byte(invalidFile))
require.ErrorContains(t, diags.ErrorOrNil(), `block declare.a already declared at TestLoader/Declare_block_redefined_after_reload:2:4`)
})

t.Run("Foreach incorrect feature stability", func(t *testing.T) {
invalidFile := `
foreach "a" {
collection = [5]
var = "item"
template {}
}
`
l := controller.NewLoader(newLoaderOptions())
diags := applyFromContent(t, l, nil, []byte(invalidFile), nil)
require.ErrorContains(t, diags.ErrorOrNil(), `config block "foreach" is at stability level "experimental", which is below the minimum allowed stability level "public-preview". Use --stability.level command-line flag to enable "experimental"`)
})
}

func TestLoader_Services(t *testing.T) {
Expand Down
27 changes: 26 additions & 1 deletion internal/runtime/internal/controller/node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"fmt"

"github.com/grafana/alloy/internal/featuregate"
"github.com/grafana/alloy/internal/runtime/internal/importsource"
"github.com/grafana/alloy/syntax/ast"
"github.com/grafana/alloy/syntax/diag"
Expand All @@ -16,9 +17,26 @@ const (
foreachID = "foreach"
)

// Add config blocks that are not GA. Config blocks that are not specified here are considered GA.
var configBlocksUnstable = map[string]featuregate.Stability{
foreachID: featuregate.StabilityExperimental,
}

// NewConfigNode creates a new ConfigNode from an initial ast.BlockStmt.
// The underlying config isn't applied until Evaluate is called.
func NewConfigNode(block *ast.BlockStmt, globals ComponentGlobals, customReg *CustomComponentRegistry) (BlockNode, diag.Diagnostics) {
var diags diag.Diagnostics

if err := checkFeatureStability(block.GetBlockName(), globals.MinStability); err != nil {
diags.Add(diag.Diagnostic{
Severity: diag.SeverityLevelError,
Message: err.Error(),
StartPos: ast.StartPos(block).Position(),
EndPos: ast.EndPos(block).Position(),
})
return nil, diags
}

switch block.GetBlockName() {
case argumentBlockID:
return NewArgumentConfigNode(block, globals), nil
Expand All @@ -33,7 +51,6 @@ func NewConfigNode(block *ast.BlockStmt, globals ComponentGlobals, customReg *Cu
case foreachID:
return NewForeachConfigNode(block, globals, customReg), nil
default:
var diags diag.Diagnostics
diags.Add(diag.Diagnostic{
Severity: diag.SeverityLevelError,
Message: fmt.Sprintf("invalid config block type %s while creating new config node", block.GetBlockName()),
Expand All @@ -44,6 +61,14 @@ func NewConfigNode(block *ast.BlockStmt, globals ComponentGlobals, customReg *Cu
}
}

func checkFeatureStability(blockName string, minStability featuregate.Stability) error {
blockStability, exist := configBlocksUnstable[blockName]
if exist {
return featuregate.CheckAllowed(blockStability, minStability, fmt.Sprintf("config block %q", blockName))
}
return nil
}

// ConfigNodeMap represents the config BlockNodes in their explicit types.
// This is helpful when validating node conditions specific to config node
// types.
Expand Down

0 comments on commit ba6905e

Please sign in to comment.