mirror of
https://github.com/status-im/consul.git
synced 2025-02-01 08:27:20 +00:00
7900544249
* Move config-dependent methods to separate package In order to reuse the fetching and file creation part of the bootstrap package, move the code that would cause cyclical dependencies to a different package. * Export needed bootstrap methods and variables Also add back validating persisted config and update tests. * Add support to check for just management token Add a new method that fetches the bootstrap configuration only if there isn't a valid management token file instead of checking for all the hcp-config files. * Pass data dir as a dependency to link controller The link controller needs to check the data directory for the hcp-config files. * Fetch bootstrap config for token in controller Load the management token when reconciling a link resource, which will fetch the agent boostrap configuration if the token is not already persisted locally. Skip this step if the cluster is in read-only mode. * Validate resource ID format in link creation * Handle unauthorized and forbidden errors Check for 401 and 403s when making GNM requests, exit bootstrap fetch loop and return specific failure statuses for link. * Move test function to a testing file * Log load and status write errors
91 lines
2.4 KiB
Go
91 lines
2.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package hcp
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/armon/go-metrics"
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/consul/agent/hcp/client"
|
|
"github.com/hashicorp/consul/agent/hcp/config"
|
|
"github.com/hashicorp/consul/agent/hcp/scada"
|
|
"github.com/hashicorp/consul/agent/hcp/telemetry"
|
|
)
|
|
|
|
// Deps contains the interfaces that the rest of Consul core depends on for HCP integration.
|
|
type Deps struct {
|
|
Config config.CloudConfig
|
|
Client client.Client
|
|
Provider scada.Provider
|
|
Sink metrics.MetricSink
|
|
TelemetryProvider *hcpProviderImpl
|
|
DataDir string
|
|
}
|
|
|
|
func NewDeps(cfg config.CloudConfig, logger hclog.Logger, dataDir string) (Deps, error) {
|
|
ctx := context.Background()
|
|
ctx = hclog.WithContext(ctx, logger)
|
|
|
|
hcpClient, err := client.NewClient(cfg)
|
|
if err != nil {
|
|
return Deps{}, fmt.Errorf("failed to init client: %w", err)
|
|
}
|
|
|
|
provider, err := scada.New(logger.Named("scada"))
|
|
if err != nil {
|
|
return Deps{}, fmt.Errorf("failed to init scada: %w", err)
|
|
}
|
|
|
|
metricsProvider := NewHCPProvider(ctx)
|
|
if err != nil {
|
|
logger.Error("failed to init HCP metrics provider", "error", err)
|
|
return Deps{}, fmt.Errorf("failed to init HCP metrics provider: %w", err)
|
|
}
|
|
|
|
metricsClient := client.NewMetricsClient(ctx, metricsProvider)
|
|
|
|
sink, err := sink(ctx, metricsClient, metricsProvider)
|
|
if err != nil {
|
|
// Do not prevent server start if sink init fails, only log error.
|
|
logger.Error("failed to init sink", "error", err)
|
|
}
|
|
|
|
return Deps{
|
|
Config: cfg,
|
|
Client: hcpClient,
|
|
Provider: provider,
|
|
Sink: sink,
|
|
TelemetryProvider: metricsProvider,
|
|
DataDir: dataDir,
|
|
}, nil
|
|
}
|
|
|
|
// sink initializes an OTELSink which forwards Consul metrics to HCP.
|
|
// This step should not block server initialization, errors are returned, only to be logged.
|
|
func sink(
|
|
ctx context.Context,
|
|
metricsClient telemetry.MetricsClient,
|
|
cfgProvider *hcpProviderImpl,
|
|
) (metrics.MetricSink, error) {
|
|
logger := hclog.FromContext(ctx)
|
|
|
|
reader := telemetry.NewOTELReader(metricsClient, cfgProvider)
|
|
sinkOpts := &telemetry.OTELSinkOpts{
|
|
Reader: reader,
|
|
ConfigProvider: cfgProvider,
|
|
}
|
|
|
|
sink, err := telemetry.NewOTELSink(ctx, sinkOpts)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create OTELSink: %w", err)
|
|
}
|
|
|
|
logger.Debug("initialized HCP metrics sink")
|
|
|
|
return sink, nil
|
|
}
|