Remove HCP Manager dependency from Link Controller

The Link controller does not need to have HCP Manager
as a dependency anymore, so this removes that dependency
in order to simplify the design.
pull/20401/head
Nick Cellino 2024-02-05 21:01:54 -05:00
parent 1b5e05fb95
commit 89c795e158
7 changed files with 17 additions and 41 deletions

View File

@ -913,6 +913,7 @@ func NewServer(config *Config, flat Deps, externalGRPCServer *grpc.Server,
hcpLinkWatchCh,
hcpclient.NewClient,
bootstrap.LoadManagementToken,
flat.HCP.Config,
flat.HCP.DataDir,
)
}
@ -1023,7 +1024,6 @@ func (s *Server) registerControllers(deps Deps, proxyUpdater ProxyUpdater) error
ResourceApisEnabled: s.useV2Resources,
HCPAllowV2ResourceApis: s.hcpAllowV2Resources,
CloudConfig: deps.HCP.Config,
HCPManager: s.hcpManager,
},
)

View File

@ -29,6 +29,7 @@ func MonitorHCPLink(
hcpLinkEventCh chan *pbresource.WatchEvent,
hcpClientFn func(cfg config.CloudConfig) (hcpclient.Client, error),
loadMgmtTokenFn func(ctx context.Context, logger hclog.Logger, hcpClient hcpclient.Client, dataDir string) (string, error),
cloudConfig config.CloudConfig,
dataDir string,
) {
for watchEvent := range hcpLinkEventCh {
@ -65,13 +66,12 @@ func MonitorHCPLink(
// fields that are provided by the link. This ensures that:
// 1. The HCP configuration (i.e., how to connect to HCP) is preserved
// 2. The Consul agent's node ID and node name are preserved
existingCfg := m.GetCloudConfig()
newCfg := config.CloudConfig{
ResourceID: link.ResourceId,
ClientID: link.ClientId,
ClientSecret: link.ClientSecret,
}
mergedCfg := config.Merge(existingCfg, newCfg)
mergedCfg := config.Merge(cloudConfig, newCfg)
hcpClient, err := hcpClientFn(mergedCfg)
if err != nil {
logger.Error("error creating HCP client", "error", err)

View File

@ -51,7 +51,7 @@ func TestMonitorHCPLink_Ok(t *testing.T) {
defer wg.Done()
MonitorHCPLink(
ctx, hclog.New(&hclog.LoggerOptions{Output: io.Discard}), mgr, linkWatchCh, mockHcpClientFn,
loadMgmtTokenFn, dataDir,
loadMgmtTokenFn, config.CloudConfig{}, dataDir,
)
}()
@ -135,8 +135,7 @@ func TestMonitorHCPLink_Ok_ReadOnly(t *testing.T) {
defer wg.Done()
MonitorHCPLink(
ctx, hclog.New(&hclog.LoggerOptions{Output: io.Discard}), mgr, linkWatchCh, mockHcpClientFn,
loadMgmtTokenFn,
"",
loadMgmtTokenFn, config.CloudConfig{}, "",
)
}()

View File

@ -47,7 +47,7 @@ func TestManager_MonitorHCPLink(t *testing.T) {
require.False(t, mgr.isRunning())
go MonitorHCPLink(
ctx, hclog.New(&hclog.LoggerOptions{Output: io.Discard}), mgr, linkWatchCh, mockHcpClientFn, loadMgmtTokenFn,
"",
config.CloudConfig{}, "",
)
// Set up a link

View File

@ -14,7 +14,6 @@ import (
gnmmod "github.com/hashicorp/hcp-sdk-go/clients/cloud-global-network-manager-service/preview/2022-02-15/models"
"github.com/hashicorp/consul/agent/hcp"
hcpclient "github.com/hashicorp/consul/agent/hcp/client"
"github.com/hashicorp/consul/agent/hcp/config"
"github.com/hashicorp/consul/internal/controller"
@ -43,7 +42,6 @@ func LinkController(
hcpAllowV2ResourceApis bool,
hcpClientFn HCPClientFn,
cfg config.CloudConfig,
hcpManager hcp.Manager,
) *controller.Controller {
return controller.NewController("link", pbhcp.LinkType).
WithInitializer(
@ -56,7 +54,7 @@ func LinkController(
resourceApisEnabled: resourceApisEnabled,
hcpAllowV2ResourceApis: hcpAllowV2ResourceApis,
hcpClientFn: hcpClientFn,
hcpManager: hcpManager,
cloudConfig: cfg,
},
)
}
@ -65,7 +63,7 @@ type linkReconciler struct {
resourceApisEnabled bool
hcpAllowV2ResourceApis bool
hcpClientFn HCPClientFn
hcpManager hcp.Manager
cloudConfig config.CloudConfig
}
func hcpAccessLevelToConsul(level *gnmmod.HashicorpCloudGlobalNetworkManager20220215ClusterConsulAccessLevel) pbhcp.AccessLevel {
@ -123,9 +121,8 @@ func (r *linkReconciler) Reconcile(ctx context.Context, rt controller.Runtime, r
// fields that are provided by the link. This ensures that:
// 1. The HCP configuration (i.e., how to connect to HCP) is preserved
// 2. The Consul agent's node ID and node name are preserved
existingCfg := r.hcpManager.GetCloudConfig()
newCfg := CloudConfigFromLink(&link)
cfg := config.Merge(existingCfg, newCfg)
cfg := config.Merge(r.cloudConfig, newCfg)
hcpClient, err := r.hcpClientFn(cfg)
if err != nil {
rt.Logger.Error("error creating HCP client", "error", err)

View File

@ -15,7 +15,6 @@ import (
gnmmod "github.com/hashicorp/hcp-sdk-go/clients/cloud-global-network-manager-service/preview/2022-02-15/models"
svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing"
"github.com/hashicorp/consul/agent/hcp"
hcpclient "github.com/hashicorp/consul/agent/hcp/client"
"github.com/hashicorp/consul/agent/hcp/config"
"github.com/hashicorp/consul/internal/controller"
@ -82,15 +81,11 @@ func (suite *controllerSuite) TestController_Ok() {
AccessLevel: &readWrite,
}, nil)
hcpMgr := hcp.NewMockManager(suite.T())
hcpMgr.EXPECT().GetCloudConfig().Return(config.CloudConfig{})
mgr.Register(LinkController(
false,
false,
mockClientFn,
config.CloudConfig{},
hcpMgr,
))
mgr.SetRaftLeader(true)
go mgr.Run(suite.ctx)
@ -132,15 +127,11 @@ func (suite *controllerSuite) TestController_Initialize() {
ResourceID: types.GenerateTestResourceID(suite.T()),
}
hcpMgr := hcp.NewMockManager(suite.T())
hcpMgr.EXPECT().GetCloudConfig().Return(cloudCfg)
mgr.Register(LinkController(
false,
false,
mockClientFn,
cloudCfg,
hcpMgr,
))
mgr.SetRaftLeader(true)
go mgr.Run(suite.ctx)
@ -172,13 +163,11 @@ func (suite *controllerSuite) TestControllerResourceApisEnabled_LinkDisabled() {
mgr := controller.NewManager(suite.client, suite.rt.Logger)
_, mockClientFunc := mockHcpClientFn(suite.T())
hcpMgr := hcp.NewMockManager(suite.T())
mgr.Register(LinkController(
true,
false,
mockClientFunc,
config.CloudConfig{},
hcpMgr,
))
mgr.SetRaftLeader(true)
go mgr.Run(suite.ctx)
@ -205,15 +194,11 @@ func (suite *controllerSuite) TestControllerResourceApisEnabledWithOverride_Link
HCPPortalURL: "http://test.com",
}, nil)
hcpMgr := hcp.NewMockManager(suite.T())
hcpMgr.EXPECT().GetCloudConfig().Return(config.CloudConfig{})
mgr.Register(LinkController(
true,
true,
mockClientFunc,
config.CloudConfig{},
hcpMgr,
))
mgr.SetRaftLeader(true)
@ -260,15 +245,11 @@ func (suite *controllerSuite) TestController_GetClusterError() {
mockClient, mockClientFunc := mockHcpClientFn(t)
mockClient.EXPECT().GetCluster(mock.Anything).Return(nil, tc.expectErr)
hcpMgr := hcp.NewMockManager(t)
hcpMgr.EXPECT().GetCloudConfig().Return(config.CloudConfig{})
mgr.Register(LinkController(
true,
true,
mockClientFunc,
config.CloudConfig{},
hcpMgr,
))
mgr.SetRaftLeader(true)

View File

@ -4,7 +4,6 @@
package controllers
import (
"github.com/hashicorp/consul/agent/hcp"
"github.com/hashicorp/consul/agent/hcp/config"
"github.com/hashicorp/consul/internal/controller"
"github.com/hashicorp/consul/internal/hcp/internal/controllers/link"
@ -15,17 +14,17 @@ type Dependencies struct {
CloudConfig config.CloudConfig
ResourceApisEnabled bool
HCPAllowV2ResourceApis bool
HCPManager *hcp.HCPManager
}
func Register(mgr *controller.Manager, deps Dependencies) {
mgr.Register(link.LinkController(
deps.ResourceApisEnabled,
deps.HCPAllowV2ResourceApis,
link.DefaultHCPClientFn,
deps.CloudConfig,
deps.HCPManager,
))
mgr.Register(
link.LinkController(
deps.ResourceApisEnabled,
deps.HCPAllowV2ResourceApis,
link.DefaultHCPClientFn,
deps.CloudConfig,
),
)
mgr.Register(telemetrystate.TelemetryStateController(link.DefaultHCPClientFn))
}