From ba6905efb21a5ce1320762fcc2cc625d0dfde6ab Mon Sep 17 00:00:00 2001 From: William Dumont Date: Fri, 17 Jan 2025 14:51:46 +0100 Subject: [PATCH 1/2] add stability lvl to config blocks --- internal/runtime/foreach_test.go | 3 ++- internal/runtime/import_test.go | 8 +++--- .../internal/controller/loader_test.go | 13 +++++++++ .../internal/controller/node_config.go | 27 ++++++++++++++++++- 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/internal/runtime/foreach_test.go b/internal/runtime/foreach_test.go index b5a76c18b4..00ffda4bb5 100644 --- a/internal/runtime/foreach_test.go +++ b/internal/runtime/foreach_test.go @@ -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" @@ -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) diff --git a/internal/runtime/import_test.go b/internal/runtime/import_test.go index 5cde86c0c6..147c47d981 100644 --- a/internal/runtime/import_test.go +++ b/internal/runtime/import_test.go @@ -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) @@ -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()) @@ -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{}, }) diff --git a/internal/runtime/internal/controller/loader_test.go b/internal/runtime/internal/controller/loader_test.go index 98e1849f36..e8ac3ee446 100644 --- a/internal/runtime/internal/controller/loader_test.go +++ b/internal/runtime/internal/controller/loader_test.go @@ -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) { diff --git a/internal/runtime/internal/controller/node_config.go b/internal/runtime/internal/controller/node_config.go index c0eb346244..56acc3ae98 100644 --- a/internal/runtime/internal/controller/node_config.go +++ b/internal/runtime/internal/controller/node_config.go @@ -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" @@ -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 @@ -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()), @@ -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. From fc6bf9b9f2d40a92fe544c0d0f79d30f3defb9bf Mon Sep 17 00:00:00 2001 From: William Dumont Date: Fri, 17 Jan 2025 16:13:50 +0100 Subject: [PATCH 2/2] fix import git test --- internal/runtime/import_git_test.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/runtime/import_git_test.go b/internal/runtime/import_git_test.go index 4a7ff50d87..4b628c26e1 100644 --- a/internal/runtime/import_git_test.go +++ b/internal/runtime/import_git_test.go @@ -14,6 +14,7 @@ import ( "testing" "time" + "github.com/grafana/alloy/internal/featuregate" "github.com/grafana/alloy/internal/vcs" "github.com/stretchr/testify/require" ) @@ -55,7 +56,7 @@ testImport.add "cc" { runGit(t, testRepo, "commit", "-m \"test\"") defer verifyNoGoroutineLeaks(t) - ctrl, f := setup(t, main, nil) + ctrl, f := setup(t, main, nil, featuregate.StabilityPublicPreview) err = ctrl.LoadSource(f, nil, "") require.NoError(t, err) ctx, cancel := context.WithCancel(context.Background()) @@ -122,7 +123,7 @@ testImport.add "cc" { runGit(t, testRepo, "commit", "-m \"test\"") defer verifyNoGoroutineLeaks(t) - ctrl, f := setup(t, main, nil) + ctrl, f := setup(t, main, nil, featuregate.StabilityPublicPreview) err = ctrl.LoadSource(f, nil, "") require.NoError(t, err) ctx, cancel := context.WithCancel(context.Background()) @@ -205,7 +206,7 @@ testImport.add "cc" { runGit(t, testRepo, "commit", "-m \"test2\"") defer verifyNoGoroutineLeaks(t) - ctrl, f := setup(t, main, nil) + ctrl, f := setup(t, main, nil, featuregate.StabilityPublicPreview) err = ctrl.LoadSource(f, nil, "") require.NoError(t, err) ctx, cancel := context.WithCancel(context.Background()) @@ -269,7 +270,7 @@ testImport.add "cc" { defer verifyNoGoroutineLeaks(t) - ctrl, f := setup(t, main, nil) + ctrl, f := setup(t, main, nil, featuregate.StabilityPublicPreview) err = ctrl.LoadSource(f, nil, "") require.NoError(t, err) ctx, cancel := context.WithCancel(context.Background()) @@ -354,7 +355,7 @@ testImport.add "cc" { runGit(t, testRepo, "commit", "-m \"test\"") defer verifyNoGoroutineLeaks(t) - ctrl, f := setup(t, main, nil) + ctrl, f := setup(t, main, nil, featuregate.StabilityPublicPreview) err = ctrl.LoadSource(f, nil, "") expectedErr := vcs.InvalidRevisionError{ Revision: "nonexistent",