Initial worker porting steps (#232)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-08-12 00:40:39 -04:00
1 parent 73a38b1433
commit 203e2b5dc2
41 files changed
+2462 -645

No files matched your search

+65 -10
View File
@@ -4,15 +4,21 @@ import (
"context"
"crypto/rand"
"fmt"
"sync"
"github.com/hashicorp/boundary/internal/auth/password"
"github.com/hashicorp/boundary/internal/authtoken"
"github.com/hashicorp/boundary/internal/cmd/config"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/host/static"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/servers"
"github.com/hashicorp/boundary/internal/servers/controller/common"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/helper/base62"
"github.com/hashicorp/vault/sdk/helper/mlock"
"github.com/patrickmn/go-cache"
ua "go.uber.org/atomic"
)
type Controller struct {
@@ -21,25 +27,46 @@ type Controller struct {
baseContext context.Context
baseCancel context.CancelFunc
started ua.Bool
workerAuthCache *cache.Cache
// Used for testing
workerStatusUpdateTimes *sync.Map
// Repo factory methods
IamRepoFn common.IamRepoFactory
StaticHostRepoFn common.StaticRepoFactory
AuthTokenRepoFn common.AuthTokenRepoFactory
IamRepoFn common.IamRepoFactory
StaticHostRepoFn common.StaticRepoFactory
AuthTokenRepoFn common.AuthTokenRepoFactory
ServersRepoFn common.ServersRepoFactory
PasswordAuthRepoFn common.PasswordAuthRepoFactory
clusterAddress string
}
func New(conf *Config) (*Controller, error) {
c := &Controller{
conf: conf,
logger: conf.Logger.Named("controller"),
conf: conf,
logger: conf.Logger.Named("controller"),
workerStatusUpdateTimes: new(sync.Map),
}
c.started.Store(false)
if conf.SecureRandomReader == nil {
conf.SecureRandomReader = rand.Reader
}
var err error
if conf.RawConfig.Controller == nil {
conf.RawConfig.Controller = new(config.Controller)
}
if conf.RawConfig.Controller.Name == "" {
if conf.RawConfig.Controller.Name, err = base62.Random(10); err != nil {
return nil, fmt.Errorf("error auto-generating controller name: %w", err)
}
}
if !conf.RawConfig.DisableMlock {
// Ensure our memory usage is locked into physical RAM
if err := mlock.LockMemory(); err != nil {
@@ -56,8 +83,6 @@ func New(conf *Config) (*Controller, error) {
}
}
c.baseContext, c.baseCancel = context.WithCancel(context.Background())
// Set up repo stuff
dbase := db.New(c.conf.Database)
c.IamRepoFn = func() (*iam.Repository, error) {
@@ -69,23 +94,53 @@ func New(conf *Config) (*Controller, error) {
c.AuthTokenRepoFn = func() (*authtoken.Repository, error) {
return authtoken.NewRepository(dbase, dbase, c.conf.ControllerKMS)
}
c.ServersRepoFn = func() (*servers.Repository, error) {
return servers.NewRepository(c.logger.Named("servers.repository"), dbase, dbase, c.conf.ControllerKMS)
}
c.PasswordAuthRepoFn = func() (*password.Repository, error) {
return password.NewRepository(dbase, dbase, c.conf.ControllerKMS)
}
c.workerAuthCache = cache.New(0, 0)
return c, nil
}
func (c *Controller) Start() error {
if c.started.Load() {
c.logger.Info("already started, skipping")
return nil
}
c.baseContext, c.baseCancel = context.WithCancel(context.Background())
if err := c.startListeners(); err != nil {
return fmt.Errorf("error starting controller listeners: %w", err)
}
c.startStatusTicking(c.baseContext)
c.started.Store(true)
return nil
}
func (c *Controller) Shutdown() error {
if err := c.stopListeners(); err != nil {
func (c *Controller) Shutdown(serversOnly bool) error {
if !c.started.Load() {
c.logger.Info("already shut down, skipping")
return nil
}
c.baseCancel()
if err := c.stopListeners(serversOnly); err != nil {
return fmt.Errorf("error stopping controller listeners: %w", err)
}
c.clusterAddress = ""
c.started.Store(false)
return nil
}
// WorkerStatusUpdateTimes returns the map, which specifically is held in _this_
// controller, not the DB. It's used in tests to verify that a given controller
// is receiving updates from an expected set of workers, to test out balancing
// and auto reconnection.
func (c *Controller) WorkerStatusUpdateTimes() *sync.Map {
return c.workerStatusUpdateTimes
}