Skip to content

Commit 5dc8eab

Browse files
authored
[CC-7041] Update and start the SCADA provider in HCP manager (hashicorp#19976)
* Update SCADA provider version Also update mocks for SCADA provider. * Create SCADA provider w/o HCP config, then update Adds a placeholder config option to allow us to initialize a SCADA provider without the HCP configuration. Also adds an update method to then add the HCP configuration. We need this to be able to eventually always register a SCADA listener at startup before the HCP config values are known. * Pass cloud configuration to HCP manager Save the entire cloud configuration and pass it to the HCP manager. * Update and start SCADA provider in HCP manager Move config updating and starting to the HCP manager. The HCP manager will eventually be responsible for all processes that contribute to linking to HCP.
1 parent 0d57acc commit 5dc8eab

File tree

13 files changed

+300
-49
lines changed

13 files changed

+300
-49
lines changed

agent/agent.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -896,17 +896,6 @@ func (a *Agent) Start(ctx context.Context) error {
896896
}()
897897
}
898898

899-
if a.scadaProvider != nil {
900-
a.scadaProvider.UpdateMeta(map[string]string{
901-
"consul_server_id": string(a.config.NodeID),
902-
})
903-
904-
if err = a.scadaProvider.Start(); err != nil {
905-
a.baseDeps.Logger.Error("scada provider failed to start, some HashiCorp Cloud Platform functionality has been disabled",
906-
"error", err, "resource_id", a.config.Cloud.ResourceID)
907-
}
908-
}
909-
910899
return nil
911900
}
912901

@@ -1598,7 +1587,7 @@ func newConsulConfig(runtimeCfg *config.RuntimeConfig, logger hclog.Logger) (*co
15981587
cfg.RequestLimitsWriteRate = runtimeCfg.RequestLimitsWriteRate
15991588
cfg.Locality = runtimeCfg.StructLocality()
16001589

1601-
cfg.Cloud.ManagementToken = runtimeCfg.Cloud.ManagementToken
1590+
cfg.Cloud = runtimeCfg.Cloud
16021591

16031592
cfg.Reporting.License.Enabled = runtimeCfg.Reporting.License.Enabled
16041593

agent/agent_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6343,6 +6343,7 @@ func TestAgent_scadaProvider(t *testing.T) {
63436343
pvd.EXPECT().Listen(scada.CAPCoreAPI.Capability()).Return(l, nil).Once()
63446344
pvd.EXPECT().Stop().Return(nil).Once()
63456345
pvd.EXPECT().SessionStatus().Return("test")
6346+
pvd.EXPECT().UpdateHCPConfig(mock.Anything).Return(nil).Once()
63466347
a := TestAgent{
63476348
OverrideDeps: func(deps *BaseDeps) {
63486349
deps.HCP.Provider = pvd

agent/consul/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/hashicorp/consul/agent/checks"
1818
consulrate "github.com/hashicorp/consul/agent/consul/rate"
19+
hcpconfig "github.com/hashicorp/consul/agent/hcp/config"
1920
"github.com/hashicorp/consul/agent/structs"
2021
libserf "github.com/hashicorp/consul/lib/serf"
2122
"github.com/hashicorp/consul/tlsutil"
@@ -442,7 +443,7 @@ type Config struct {
442443

443444
Locality *structs.Locality
444445

445-
Cloud CloudConfig
446+
Cloud hcpconfig.CloudConfig
446447

447448
Reporting Reporting
448449

agent/consul/config_cloud.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

agent/consul/server.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,11 @@ func NewServer(config *Config, flat Deps, externalGRPCServer *grpc.Server,
586586
})
587587

588588
s.hcpManager = hcp.NewManager(hcp.ManagerConfig{
589-
Client: flat.HCP.Client,
590-
StatusFn: s.hcpServerStatus(flat),
591-
Logger: logger.Named("hcp_manager"),
589+
CloudConfig: s.config.Cloud,
590+
Client: flat.HCP.Client,
591+
StatusFn: s.hcpServerStatus(flat),
592+
Logger: logger.Named("hcp_manager"),
593+
SCADAProvider: flat.HCP.Provider,
592594
})
593595

594596
var recorder *middleware.RequestRecorder

agent/hcp/deps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func NewDeps(cfg config.CloudConfig, logger hclog.Logger) (Deps, error) {
3232
return Deps{}, fmt.Errorf("failed to init client: %w", err)
3333
}
3434

35-
provider, err := scada.New(cfg, logger.Named("scada"))
35+
provider, err := scada.New(logger.Named("scada"))
3636
if err != nil {
3737
return Deps{}, fmt.Errorf("failed to init scada: %w", err)
3838
}

agent/hcp/manager.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"time"
1010

1111
hcpclient "github.com/hashicorp/consul/agent/hcp/client"
12+
"github.com/hashicorp/consul/agent/hcp/config"
13+
"github.com/hashicorp/consul/agent/hcp/scada"
1214
"github.com/hashicorp/consul/lib"
1315
"github.com/hashicorp/go-hclog"
1416
)
@@ -19,7 +21,9 @@ var (
1921
)
2022

2123
type ManagerConfig struct {
22-
Client hcpclient.Client
24+
Client hcpclient.Client
25+
CloudConfig config.CloudConfig
26+
SCADAProvider scada.Provider
2327

2428
StatusFn StatusCallback
2529
MinInterval time.Duration
@@ -83,6 +87,15 @@ func (m *Manager) Run(ctx context.Context) {
8387
var err error
8488
m.logger.Debug("HCP manager starting")
8589

90+
// Update and start the SCADA provider
91+
err = m.startSCADAProvider()
92+
if err != nil {
93+
// Log the error but continue starting the manager. The SCADA provider
94+
// could potentially be updated later with a working configuration.
95+
m.logger.Error("scada provider failed to start, some HashiCorp Cloud Platform functionality has been disabled",
96+
"error", err)
97+
}
98+
8699
// immediately send initial update
87100
select {
88101
case <-ctx.Done():
@@ -116,6 +129,34 @@ func (m *Manager) Run(ctx context.Context) {
116129
}
117130
}
118131

132+
func (m *Manager) startSCADAProvider() error {
133+
provider := m.cfg.SCADAProvider
134+
if provider == nil {
135+
return nil
136+
}
137+
138+
// Update the SCADA provider configuration with HCP configurations
139+
m.logger.Debug("updating scada provider with HCP configuration")
140+
err := provider.UpdateHCPConfig(m.cfg.CloudConfig)
141+
if err != nil {
142+
m.logger.Error("failed to update scada provider with HCP configuration", "err", err)
143+
return err
144+
}
145+
146+
// Update the SCADA provider metadata
147+
provider.UpdateMeta(map[string]string{
148+
"consul_server_id": string(m.cfg.CloudConfig.NodeID),
149+
})
150+
151+
// Start the SCADA provider
152+
err = provider.Start()
153+
if err != nil {
154+
return err
155+
}
156+
157+
return nil
158+
}
159+
119160
func (m *Manager) UpdateConfig(cfg ManagerConfig) {
120161
m.cfgMu.Lock()
121162
defer m.cfgMu.Unlock()

agent/hcp/manager_test.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"time"
1010

1111
hcpclient "github.com/hashicorp/consul/agent/hcp/client"
12+
"github.com/hashicorp/consul/agent/hcp/config"
13+
"github.com/hashicorp/consul/agent/hcp/scada"
1214
"github.com/hashicorp/go-hclog"
1315
"github.com/stretchr/testify/mock"
1416
"github.com/stretchr/testify/require"
@@ -22,10 +24,26 @@ func TestManager_Run(t *testing.T) {
2224
}
2325
updateCh := make(chan struct{}, 1)
2426
client.EXPECT().PushServerStatus(mock.Anything, &hcpclient.ServerStatus{ID: t.Name()}).Return(nil).Once()
27+
28+
cloudCfg := config.CloudConfig{
29+
ResourceID: "organization/85702e73-8a3d-47dc-291c-379b783c5804/project/8c0547c0-10e8-1ea2-dffe-384bee8da634/hashicorp.consul.global-network-manager.cluster/test",
30+
NodeID: "node-1",
31+
}
32+
scadaM := scada.NewMockProvider(t)
33+
scadaM.EXPECT().UpdateHCPConfig(cloudCfg).Return(nil)
34+
scadaM.EXPECT().UpdateMeta(
35+
map[string]string{
36+
"consul_server_id": string(cloudCfg.NodeID),
37+
},
38+
).Return()
39+
scadaM.EXPECT().Start().Return(nil)
40+
2541
mgr := NewManager(ManagerConfig{
26-
Client: client,
27-
Logger: hclog.New(&hclog.LoggerOptions{Output: io.Discard}),
28-
StatusFn: statusF,
42+
Client: client,
43+
Logger: hclog.New(&hclog.LoggerOptions{Output: io.Discard}),
44+
StatusFn: statusF,
45+
CloudConfig: cloudCfg,
46+
SCADAProvider: scadaM,
2947
})
3048
mgr.testUpdateSent = updateCh
3149
ctx, cancel := context.WithCancel(context.Background())

agent/hcp/scada/mock_Provider.go

Lines changed: 124 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)