Merge pull request #2163 from hashicorp/irindos-clusterid-upstreams
feat(worker): Map cluster ID in config to upstream address
This commit is contained in:
4 files changed
+45
-9
No files matched your search
@@ -32,6 +32,7 @@ import (
|
||||
"github.com/hashicorp/go-secure-stdlib/mlock"
|
||||
"github.com/hashicorp/go-secure-stdlib/parseutil"
|
||||
"github.com/hashicorp/go-secure-stdlib/pluginutil/v2"
|
||||
"github.com/hashicorp/go-uuid"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
"go.uber.org/atomic"
|
||||
@@ -330,6 +331,14 @@ func (c *Command) Run(args []string) int {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if c.Config.HCPBClusterId != "" {
|
||||
_, err := uuid.ParseUUID(c.Config.HCPBClusterId)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Invalid HCPB cluster id %q: %w", c.Config.HCPBClusterId, err).Error())
|
||||
return base.CommandUserError
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := c.SetupListeners(c.UI, c.Config.SharedConfig, []string{"api", "cluster", "proxy", "ops"}); err != nil {
|
||||
c.UI.Error(err.Error())
|
||||
|
||||
@@ -124,6 +124,9 @@ type Config struct {
|
||||
|
||||
// Plugin-related options
|
||||
Plugins Plugins `hcl:"plugins"`
|
||||
|
||||
// Internal field for use with HCP deployments. Used if controllers/ initial_upstreams is not set
|
||||
HCPBClusterId string `hcl:"hcp_boundary_cluster_id"`
|
||||
}
|
||||
|
||||
type Controller struct {
|
||||
|
||||
@@ -35,6 +35,8 @@ import (
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
const hcpbUrlSuffix = ".proxy.boundary.hashicorp.cloud"
|
||||
|
||||
// StartControllerConnections starts up the resolver and initiates controller
|
||||
// connection client creation
|
||||
func (w *Worker) StartControllerConnections() error {
|
||||
@@ -57,7 +59,13 @@ func (w *Worker) StartControllerConnections() error {
|
||||
}
|
||||
|
||||
if len(initialAddrs) == 0 {
|
||||
return errors.New("no initial controller addresses found")
|
||||
if w.conf.RawConfig.HCPBClusterId != "" {
|
||||
clusterAddress := fmt.Sprintf("%s%s", w.conf.RawConfig.HCPBClusterId, hcpbUrlSuffix)
|
||||
initialAddrs = append(initialAddrs, resolver.Address{Addr: clusterAddress})
|
||||
event.WriteSysEvent(context.TODO(), op, fmt.Sprintf("Setting HCPB Cluster address %s as upstream address", clusterAddress))
|
||||
} else {
|
||||
return errors.New("no initial controller addresses found")
|
||||
}
|
||||
}
|
||||
|
||||
w.Resolver().InitialState(resolver.State{
|
||||
@@ -101,8 +109,10 @@ func (w *Worker) controllerDialerFunc() func(context.Context, string) (net.Conn,
|
||||
event.WriteError(ctx, op, err)
|
||||
}
|
||||
|
||||
if !w.everAuthenticated.Load() && err == nil && conn != nil {
|
||||
w.everAuthenticated.Store(true)
|
||||
if err == nil && conn != nil {
|
||||
if !w.everAuthenticated.Load() {
|
||||
w.everAuthenticated.Store(true)
|
||||
}
|
||||
event.WriteSysEvent(ctx, op, "worker has successfully authenticated")
|
||||
}
|
||||
|
||||
|
||||
@@ -3,21 +3,23 @@ package worker
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/servers"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/daemon/worker/common"
|
||||
"github.com/hashicorp/boundary/internal/daemon/worker/session"
|
||||
pbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
|
||||
"github.com/hashicorp/boundary/internal/observability/event"
|
||||
"github.com/hashicorp/boundary/internal/servers"
|
||||
"github.com/hashicorp/go-secure-stdlib/strutil"
|
||||
"google.golang.org/grpc/resolver"
|
||||
)
|
||||
|
||||
type LastStatusInformation struct {
|
||||
*pbs.StatusResponse
|
||||
StatusTime time.Time
|
||||
StatusTime time.Time
|
||||
LastCalculatedUpstreams []string
|
||||
}
|
||||
|
||||
func (w *Worker) startStatusTicking(cancelCtx context.Context) {
|
||||
@@ -199,14 +201,26 @@ func (w *Worker) sendWorkerStatus(cancelCtx context.Context) {
|
||||
} else {
|
||||
w.updateTags.Store(false)
|
||||
// This may be nil if we are in a multiple hop scenario
|
||||
var addrs []resolver.Address
|
||||
var newUpstreams []string
|
||||
if len(result.CalculatedUpstreams) > 0 {
|
||||
addrs := make([]resolver.Address, 0, len(result.CalculatedUpstreams))
|
||||
addrs = make([]resolver.Address, 0, len(result.CalculatedUpstreams))
|
||||
for _, v := range result.CalculatedUpstreams {
|
||||
addrs = append(addrs, resolver.Address{Addr: v.Address})
|
||||
newUpstreams = append(newUpstreams, v.Address)
|
||||
}
|
||||
lastStatus := w.lastStatusSuccess.Load().(*LastStatusInformation)
|
||||
// Compare upstreams; update resolver if there is a difference, and emit an event with old and new addresses
|
||||
if lastStatus != nil && !strutil.EquivalentSlices(lastStatus.LastCalculatedUpstreams, newUpstreams) {
|
||||
upstreamsMessage := fmt.Sprintf("Upstreams has changed; old upstreams were: %s, new upstreams are: %s", lastStatus.LastCalculatedUpstreams, newUpstreams)
|
||||
event.WriteSysEvent(context.TODO(), op, upstreamsMessage)
|
||||
w.Resolver().UpdateState(resolver.State{Addresses: addrs})
|
||||
} else if lastStatus == nil {
|
||||
w.Resolver().UpdateState(resolver.State{Addresses: addrs})
|
||||
event.WriteSysEvent(context.TODO(), op, fmt.Sprintf("Upstreams after first status set to: %s", newUpstreams))
|
||||
}
|
||||
w.Resolver().UpdateState(resolver.State{Addresses: addrs})
|
||||
}
|
||||
w.lastStatusSuccess.Store(&LastStatusInformation{StatusResponse: result, StatusTime: time.Now()})
|
||||
w.lastStatusSuccess.Store(&LastStatusInformation{StatusResponse: result, StatusTime: time.Now(), LastCalculatedUpstreams: newUpstreams})
|
||||
|
||||
for _, request := range result.GetJobsRequests() {
|
||||
switch request.GetRequestType() {
|
||||
|
||||
Reference in new issue
Block a user