2020-06-10 20:47:35 +00:00
|
|
|
package autoconf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-08-31 17:12:17 +00:00
|
|
|
"sync"
|
2020-06-10 20:47:35 +00:00
|
|
|
"time"
|
|
|
|
|
2020-10-01 23:02:32 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
2020-06-10 20:47:35 +00:00
|
|
|
"github.com/hashicorp/consul/agent/config"
|
2020-08-31 17:12:17 +00:00
|
|
|
"github.com/hashicorp/consul/agent/token"
|
2020-10-01 23:02:32 +00:00
|
|
|
"github.com/hashicorp/consul/lib/retry"
|
2020-06-10 20:47:35 +00:00
|
|
|
"github.com/hashicorp/consul/logging"
|
2020-07-23 15:24:20 +00:00
|
|
|
"github.com/hashicorp/consul/proto/pbautoconf"
|
2020-07-28 19:31:48 +00:00
|
|
|
)
|
2020-06-10 20:47:35 +00:00
|
|
|
|
|
|
|
// AutoConfig is all the state necessary for being able to parse a configuration
|
|
|
|
// as well as perform the necessary RPCs to perform Agent Auto Configuration.
|
|
|
|
type AutoConfig struct {
|
2020-08-31 17:12:17 +00:00
|
|
|
sync.Mutex
|
|
|
|
|
2020-08-12 16:35:30 +00:00
|
|
|
acConfig Config
|
2020-07-30 15:37:18 +00:00
|
|
|
logger hclog.Logger
|
2020-08-31 17:12:17 +00:00
|
|
|
cache Cache
|
2020-10-01 23:02:32 +00:00
|
|
|
waiter *retry.Waiter
|
2020-07-30 15:37:18 +00:00
|
|
|
config *config.RuntimeConfig
|
|
|
|
autoConfigResponse *pbautoconf.AutoConfigResponse
|
2020-08-10 17:03:33 +00:00
|
|
|
autoConfigSource config.Source
|
2020-08-31 17:12:17 +00:00
|
|
|
|
|
|
|
running bool
|
|
|
|
done chan struct{}
|
|
|
|
// cancel is used to cancel the entire AutoConfig
|
|
|
|
// go routine. This is the main field protected
|
|
|
|
// by the mutex as it being non-nil indicates that
|
|
|
|
// the go routine has been started and is stoppable.
|
|
|
|
// note that it doesn't indcate that the go routine
|
|
|
|
// is currently running.
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
|
|
|
// cancelWatches is used to cancel the existing
|
|
|
|
// cache watches regarding the agents certificate. This is
|
|
|
|
// mainly only necessary when the Agent token changes.
|
|
|
|
cancelWatches context.CancelFunc
|
|
|
|
|
|
|
|
// cacheUpdates is the chan used to have the cache
|
|
|
|
// send us back events
|
|
|
|
cacheUpdates chan cache.UpdateEvent
|
|
|
|
|
|
|
|
// tokenUpdates is the struct used to receive
|
|
|
|
// events from the token store when the Agent
|
|
|
|
// token is updated.
|
|
|
|
tokenUpdates token.Notifier
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 16:35:30 +00:00
|
|
|
// New creates a new AutoConfig object for providing automatic Consul configuration.
|
|
|
|
func New(config Config) (*AutoConfig, error) {
|
|
|
|
switch {
|
|
|
|
case config.Loader == nil:
|
|
|
|
return nil, fmt.Errorf("must provide a config loader")
|
|
|
|
case config.DirectRPC == nil:
|
2020-07-28 19:31:48 +00:00
|
|
|
return nil, fmt.Errorf("must provide a direct RPC delegate")
|
2020-08-31 17:12:17 +00:00
|
|
|
case config.Cache == nil:
|
|
|
|
return nil, fmt.Errorf("must provide a cache")
|
|
|
|
case config.TLSConfigurator == nil:
|
|
|
|
return nil, fmt.Errorf("must provide a TLS configurator")
|
|
|
|
case config.Tokens == nil:
|
|
|
|
return nil, fmt.Errorf("must provide a token store")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.FallbackLeeway == 0 {
|
|
|
|
config.FallbackLeeway = 10 * time.Second
|
|
|
|
}
|
|
|
|
if config.FallbackRetry == 0 {
|
|
|
|
config.FallbackRetry = time.Minute
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 19:31:48 +00:00
|
|
|
logger := config.Logger
|
2020-06-10 20:47:35 +00:00
|
|
|
if logger == nil {
|
|
|
|
logger = hclog.NewNullLogger()
|
|
|
|
} else {
|
|
|
|
logger = logger.Named(logging.AutoConfig)
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:35:30 +00:00
|
|
|
if config.Waiter == nil {
|
2020-10-01 05:14:21 +00:00
|
|
|
config.Waiter = &retry.Waiter{
|
|
|
|
MinFailures: 1,
|
|
|
|
MaxWait: 10 * time.Minute,
|
|
|
|
Jitter: retry.NewJitter(25),
|
|
|
|
}
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 20:01:32 +00:00
|
|
|
if err := config.EnterpriseConfig.validateAndFinalize(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:35:30 +00:00
|
|
|
return &AutoConfig{
|
2020-08-31 17:12:17 +00:00
|
|
|
acConfig: config,
|
|
|
|
logger: logger,
|
2020-08-12 16:35:30 +00:00
|
|
|
}, nil
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadConfig will parse the current configuration and inject any
|
|
|
|
// auto-config sources if present into the correct place in the parsing chain.
|
|
|
|
func (ac *AutoConfig) ReadConfig() (*config.RuntimeConfig, error) {
|
2020-08-31 17:12:17 +00:00
|
|
|
ac.Lock()
|
|
|
|
defer ac.Unlock()
|
2020-12-21 18:25:32 +00:00
|
|
|
result, err := ac.acConfig.Loader(ac.autoConfigSource)
|
2020-06-10 20:47:35 +00:00
|
|
|
if err != nil {
|
2020-12-21 18:25:32 +00:00
|
|
|
return result.RuntimeConfig, err
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 18:25:32 +00:00
|
|
|
for _, w := range result.Warnings {
|
2020-06-10 20:47:35 +00:00
|
|
|
ac.logger.Warn(w)
|
|
|
|
}
|
|
|
|
|
2020-12-21 18:25:32 +00:00
|
|
|
ac.config = result.RuntimeConfig
|
|
|
|
return ac.config, nil
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitialConfiguration will perform a one-time RPC request to the configured servers
|
2020-07-23 15:24:20 +00:00
|
|
|
// to retrieve various cluster wide configurations. See the proto/pbautoconf/auto_config.proto
|
2020-06-10 20:47:35 +00:00
|
|
|
// file for a complete reference of what configurations can be applied in this manner.
|
|
|
|
// The returned configuration will be the new configuration with any auto-config settings
|
|
|
|
// already applied. If AutoConfig is not enabled this method will just parse any
|
|
|
|
// local configuration and return the built runtime configuration.
|
|
|
|
//
|
|
|
|
// The context passed in can be used to cancel the retrieval of the initial configuration
|
|
|
|
// like when receiving a signal during startup.
|
|
|
|
func (ac *AutoConfig) InitialConfiguration(ctx context.Context) (*config.RuntimeConfig, error) {
|
2021-05-17 20:01:32 +00:00
|
|
|
if err := ac.maybeLoadConfig(); err != nil {
|
|
|
|
return nil, err
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
switch {
|
|
|
|
case ac.config.AutoConfig.Enabled:
|
|
|
|
resp, err := ac.readPersistedAutoConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-10 20:47:35 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
if resp == nil {
|
|
|
|
ac.logger.Info("retrieving initial agent auto configuration remotely")
|
|
|
|
resp, err = ac.getInitialConfiguration(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 20:47:35 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
ac.logger.Debug("updating auto-config settings")
|
|
|
|
if err = ac.recordInitialConfiguration(resp); err != nil {
|
2020-06-10 20:47:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
// re-read the configuration now that we have our initial auto-config
|
|
|
|
config, err := ac.ReadConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-10 20:47:35 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
ac.config = config
|
|
|
|
return ac.config, nil
|
|
|
|
case ac.config.AutoEncryptTLS:
|
|
|
|
certs, err := ac.autoEncryptInitialCerts(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ac.setInitialTLSCertificates(certs); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ac.logger.Info("automatically upgraded to TLS")
|
|
|
|
return ac.config, nil
|
|
|
|
default:
|
|
|
|
return ac.config, nil
|
|
|
|
}
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 20:01:32 +00:00
|
|
|
// maybeLoadConfig will read the Consul configuration using the
|
|
|
|
// provided config loader if and only if the config field of
|
|
|
|
// the struct is nil. When it does this it will fill in that
|
|
|
|
// field. If the config field already is non-nil then this
|
|
|
|
// is a noop.
|
|
|
|
func (ac *AutoConfig) maybeLoadConfig() error {
|
|
|
|
if ac.config == nil {
|
|
|
|
config, err := ac.ReadConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ac.config = config
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-10 20:47:35 +00:00
|
|
|
// introToken is responsible for determining the correct intro token to use
|
2020-07-07 19:39:04 +00:00
|
|
|
// when making the initial AutoConfig.InitialConfiguration RPC request.
|
2020-06-10 20:47:35 +00:00
|
|
|
func (ac *AutoConfig) introToken() (string, error) {
|
|
|
|
conf := ac.config.AutoConfig
|
|
|
|
// without an intro token or intro token file we cannot do anything
|
|
|
|
if conf.IntroToken == "" && conf.IntroTokenFile == "" {
|
|
|
|
return "", fmt.Errorf("neither intro_token or intro_token_file settings are not configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
token := conf.IntroToken
|
|
|
|
if token == "" {
|
|
|
|
// load the intro token from the file
|
|
|
|
content, err := ioutil.ReadFile(conf.IntroTokenFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to read intro token from file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
token = string(content)
|
|
|
|
|
|
|
|
if token == "" {
|
|
|
|
return "", fmt.Errorf("intro_token_file did not contain any token")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
// recordInitialConfiguration is responsible for recording the AutoConfigResponse from
|
|
|
|
// the AutoConfig.InitialConfiguration RPC. It is an all-in-one function to do the following
|
2022-10-21 19:58:06 +00:00
|
|
|
// - update the Agent token in the token store
|
2020-08-31 17:12:17 +00:00
|
|
|
func (ac *AutoConfig) recordInitialConfiguration(resp *pbautoconf.AutoConfigResponse) error {
|
|
|
|
ac.autoConfigResponse = resp
|
2020-06-10 20:47:35 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
ac.autoConfigSource = config.LiteralSource{
|
|
|
|
Name: autoConfigFileName,
|
|
|
|
Config: translateConfig(resp.Config),
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
// we need to re-read the configuration to determine what the correct ACL
|
|
|
|
// token to push into the token store is. Any user provided token will override
|
|
|
|
// any AutoConfig generated token.
|
|
|
|
config, err := ac.ReadConfig()
|
2020-06-10 20:47:35 +00:00
|
|
|
if err != nil {
|
2020-08-31 17:12:17 +00:00
|
|
|
return fmt.Errorf("failed to fully resolve configuration: %w", err)
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
// ignoring the return value which would indicate a change in the token
|
2020-08-25 04:10:12 +00:00
|
|
|
_ = ac.acConfig.Tokens.UpdateAgentToken(config.ACLTokens.ACLAgentToken, token.TokenSourceConfig)
|
2020-06-10 20:47:35 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
// extra a structs.SignedResponse from the AutoConfigResponse for use in cache prepopulation
|
|
|
|
signed, err := extractSignedResponse(resp)
|
2020-06-10 20:47:35 +00:00
|
|
|
if err != nil {
|
2020-08-31 17:12:17 +00:00
|
|
|
return fmt.Errorf("failed to extract certificates from the auto-config response: %w", err)
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
// prepopulate the cache
|
|
|
|
if err = ac.populateCertificateCache(signed); err != nil {
|
|
|
|
return fmt.Errorf("failed to populate the cache with certificate responses: %w", err)
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
// update the TLS configurator with the latest certificates
|
|
|
|
if err := ac.updateTLSFromResponse(resp); err != nil {
|
|
|
|
return err
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
return ac.persistAutoConfig(resp)
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getInitialConfigurationOnce will perform full server to TCPAddr resolution and
|
2020-07-07 19:39:04 +00:00
|
|
|
// loop through each host trying to make the AutoConfig.InitialConfiguration RPC call. When
|
2020-06-10 20:47:35 +00:00
|
|
|
// successful the bool return will be true and the err value will indicate whether we
|
|
|
|
// successfully recorded the auto config settings (persisted to disk and stored internally
|
|
|
|
// on the AutoConfig object)
|
2020-07-28 19:31:48 +00:00
|
|
|
func (ac *AutoConfig) getInitialConfigurationOnce(ctx context.Context, csr string, key string) (*pbautoconf.AutoConfigResponse, error) {
|
2020-06-10 20:47:35 +00:00
|
|
|
token, err := ac.introToken()
|
|
|
|
if err != nil {
|
2020-07-28 19:31:48 +00:00
|
|
|
return nil, err
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:24:20 +00:00
|
|
|
request := pbautoconf.AutoConfigRequest{
|
2020-06-10 20:47:35 +00:00
|
|
|
Datacenter: ac.config.Datacenter,
|
|
|
|
Node: ac.config.NodeName,
|
|
|
|
Segment: ac.config.SegmentName,
|
2021-12-01 19:32:34 +00:00
|
|
|
Partition: ac.config.PartitionOrEmpty(),
|
2020-06-10 20:47:35 +00:00
|
|
|
JWT: token,
|
2020-07-28 19:31:48 +00:00
|
|
|
CSR: csr,
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 19:31:48 +00:00
|
|
|
var resp pbautoconf.AutoConfigResponse
|
2020-06-10 20:47:35 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
servers, err := ac.autoConfigHosts()
|
2020-06-10 20:47:35 +00:00
|
|
|
if err != nil {
|
2020-07-28 19:31:48 +00:00
|
|
|
return nil, err
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range servers {
|
|
|
|
// try each IP to see if we can successfully make the request
|
|
|
|
for _, addr := range ac.resolveHost(s) {
|
|
|
|
if ctx.Err() != nil {
|
2020-07-28 19:31:48 +00:00
|
|
|
return nil, ctx.Err()
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 19:39:04 +00:00
|
|
|
ac.logger.Debug("making AutoConfig.InitialConfiguration RPC", "addr", addr.String())
|
2020-08-12 16:35:30 +00:00
|
|
|
if err = ac.acConfig.DirectRPC.RPC(ac.config.Datacenter, ac.config.NodeName, &addr, "AutoConfig.InitialConfiguration", &request, &resp); err != nil {
|
2020-07-07 19:39:04 +00:00
|
|
|
ac.logger.Error("AutoConfig.InitialConfiguration RPC failed", "addr", addr.String(), "error", err)
|
2020-06-10 20:47:35 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-08-31 17:12:17 +00:00
|
|
|
ac.logger.Debug("AutoConfig.InitialConfiguration RPC was successful")
|
2020-06-10 20:47:35 +00:00
|
|
|
|
2020-07-28 19:31:48 +00:00
|
|
|
// update the Certificate with the private key we generated locally
|
|
|
|
if resp.Certificate != nil {
|
|
|
|
resp.Certificate.PrivateKeyPEM = key
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp, nil
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
return nil, fmt.Errorf("No server successfully responded to the auto-config request")
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getInitialConfiguration implements a loop to retry calls to getInitialConfigurationOnce.
|
|
|
|
// It uses the RetryWaiter on the AutoConfig object to control how often to attempt
|
|
|
|
// the initial configuration process. It is also canceallable by cancelling the provided context.
|
2020-08-31 17:12:17 +00:00
|
|
|
func (ac *AutoConfig) getInitialConfiguration(ctx context.Context) (*pbautoconf.AutoConfigResponse, error) {
|
2020-07-28 19:31:48 +00:00
|
|
|
// generate a CSR
|
|
|
|
csr, key, err := ac.generateCSR()
|
|
|
|
if err != nil {
|
2020-08-31 17:12:17 +00:00
|
|
|
return nil, err
|
2020-07-28 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
ac.acConfig.Waiter.Reset()
|
2020-06-10 20:47:35 +00:00
|
|
|
for {
|
2020-10-01 05:14:21 +00:00
|
|
|
resp, err := ac.getInitialConfigurationOnce(ctx, csr, key)
|
|
|
|
switch {
|
|
|
|
case err == nil && resp != nil:
|
|
|
|
return resp, nil
|
|
|
|
case err != nil:
|
|
|
|
ac.logger.Error(err.Error())
|
|
|
|
default:
|
|
|
|
ac.logger.Error("No error returned when fetching configuration from the servers but no response was either")
|
|
|
|
}
|
2020-08-31 17:12:17 +00:00
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
if err := ac.acConfig.Waiter.Wait(ctx); err != nil {
|
|
|
|
ac.logger.Info("interrupted during initial auto configuration", "err", err)
|
|
|
|
return nil, err
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 19:31:48 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
func (ac *AutoConfig) Start(ctx context.Context) error {
|
|
|
|
ac.Lock()
|
|
|
|
defer ac.Unlock()
|
2020-07-28 19:31:48 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
if !ac.config.AutoConfig.Enabled && !ac.config.AutoEncryptTLS {
|
|
|
|
return nil
|
2020-07-28 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
if ac.running || ac.cancel != nil {
|
|
|
|
return fmt.Errorf("AutoConfig is already running")
|
2020-07-28 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
// create the top level context to control the go
|
|
|
|
// routine executing the `run` method
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-07-28 19:31:48 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
// create the channel to get cache update events through
|
|
|
|
// really we should only ever get 10 updates
|
|
|
|
ac.cacheUpdates = make(chan cache.UpdateEvent, 10)
|
2020-07-28 19:31:48 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
// setup the cache watches
|
|
|
|
cancelCertWatches, err := ac.setupCertificateCacheWatches(ctx)
|
2020-07-28 19:31:48 +00:00
|
|
|
if err != nil {
|
2020-08-31 17:12:17 +00:00
|
|
|
cancel()
|
|
|
|
return fmt.Errorf("error setting up cache watches: %w", err)
|
2020-07-28 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
// start the token update notifier
|
|
|
|
ac.tokenUpdates = ac.acConfig.Tokens.Notify(token.TokenKindAgent)
|
2020-07-28 19:31:48 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
// store the cancel funcs
|
|
|
|
ac.cancel = cancel
|
|
|
|
ac.cancelWatches = cancelCertWatches
|
2020-07-28 19:31:48 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
ac.running = true
|
|
|
|
ac.done = make(chan struct{})
|
|
|
|
go ac.run(ctx, ac.done)
|
2020-07-28 19:31:48 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
ac.logger.Info("auto-config started")
|
2020-07-28 19:31:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
func (ac *AutoConfig) Done() <-chan struct{} {
|
|
|
|
ac.Lock()
|
|
|
|
defer ac.Unlock()
|
2020-07-28 19:31:48 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
if ac.done != nil {
|
|
|
|
return ac.done
|
2020-07-28 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
// return a closed channel to indicate that we are already done
|
|
|
|
done := make(chan struct{})
|
|
|
|
close(done)
|
|
|
|
return done
|
2020-07-28 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
func (ac *AutoConfig) IsRunning() bool {
|
|
|
|
ac.Lock()
|
|
|
|
defer ac.Unlock()
|
|
|
|
return ac.running
|
2020-07-28 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AutoConfig) Stop() bool {
|
2020-08-31 17:12:17 +00:00
|
|
|
ac.Lock()
|
|
|
|
defer ac.Unlock()
|
2020-07-28 19:31:48 +00:00
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
if !ac.running {
|
2020-07-28 19:31:48 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
if ac.cancel != nil {
|
|
|
|
ac.cancel()
|
2020-07-30 15:37:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
return true
|
2020-07-30 15:37:18 +00:00
|
|
|
}
|