diff --git a/agent/config/builder.go b/agent/config/builder.go index e67409635184..667d822480ac 100644 --- a/agent/config/builder.go +++ b/agent/config/builder.go @@ -1134,6 +1134,15 @@ func (b *builder) build() (rt RuntimeConfig, err error) { return RuntimeConfig{}, fmt.Errorf("cache.entry_fetch_rate must be strictly positive, was: %v", rt.Cache.EntryFetchRate) } + // TODO(CC-6389): Remove once resource-apis is no longer considered experimental and is supported by HCP + if stringslice.Contains(rt.Experiments, consul.CatalogResourceExperimentName) && rt.IsCloudEnabled() { + // Allow override of this check for development/testing purposes. Should not be used in production + override, err := strconv.ParseBool(os.Getenv("CONSUL_OVERRIDE_HCP_RESOURCE_APIS_CHECK")) + if err != nil || !override { + return RuntimeConfig{}, fmt.Errorf("`experiments` cannot include 'resource-apis' when HCP `cloud` configuration is set") + } + } + if rt.UIConfig.MetricsProvider == "prometheus" { // Handle defaulting for the built-in version of prometheus. if len(rt.UIConfig.MetricsProxy.PathAllowlist) == 0 { @@ -2550,7 +2559,7 @@ func (b *builder) cloudConfigVal(v Config) hcpconfig.CloudConfig { val := hcpconfig.CloudConfig{ ResourceID: os.Getenv("HCP_RESOURCE_ID"), } - // Node id might get overriden in setup.go:142 + // Node id might get overridden in setup.go:142 nodeID := stringVal(v.NodeID) val.NodeID = types.NodeID(nodeID) val.NodeName = b.nodeName(v.NodeName) diff --git a/agent/config/builder_test.go b/agent/config/builder_test.go index 2cc3e3148c37..6f8fdc9598a0 100644 --- a/agent/config/builder_test.go +++ b/agent/config/builder_test.go @@ -575,3 +575,72 @@ func TestBuidler_hostMetricsWithCloud(t *testing.T) { require.NotNil(t, cfg) require.True(t, cfg.Telemetry.EnableHostMetrics) } + +func TestBuilder_WarnCloudConfigWithResourceApis(t *testing.T) { + tests := []struct { + name string + hcl string + expectErr bool + override bool + }{ + { + name: "base_case", + hcl: ``, + }, + { + name: "resource-apis_no_cloud", + hcl: `experiments = ["resource-apis"]`, + }, + { + name: "cloud-config_no_experiments", + hcl: `cloud{ resource_id = "abc" client_id = "abc" client_secret = "abc"}`, + }, + { + name: "cloud-config_resource-apis_experiment", + hcl: ` + experiments = ["resource-apis"] + cloud{ resource_id = "abc" client_id = "abc" client_secret = "abc"}`, + expectErr: true, + }, + { + name: "cloud-config_other_experiment", + hcl: ` + experiments = ["test"] + cloud{ resource_id = "abc" client_id = "abc" client_secret = "abc"}`, + }, + { + name: "cloud-config_resource-apis_experiment_override", + hcl: ` + experiments = ["resource-apis"] + cloud{ resource_id = "abc" client_id = "abc" client_secret = "abc"}`, + override: true, + }, + } + for _, tc := range tests { + // using dev mode skips the need for a data dir + devMode := true + builderOpts := LoadOpts{ + DevMode: &devMode, + Overrides: []Source{ + FileSource{ + Name: "overrides", + Format: "hcl", + Data: tc.hcl, + }, + }, + } + if tc.override { + os.Setenv("CONSUL_OVERRIDE_HCP_RESOURCE_APIS_CHECK", "1") + } + _, err := Load(builderOpts) + if tc.override { + os.Unsetenv("CONSUL_OVERRIDE_HCP_RESOURCE_APIS_CHECK") + } + if tc.expectErr { + require.Error(t, err) + require.Contains(t, err.Error(), "cannot include 'resource-apis' when HCP") + } else { + require.NoError(t, err) + } + } +}