Add health endpoint for worker (#2442)
* Return worker process information in the health endpoint. Experimental: The worker_info and returned worker_process_info fields are experimental and can change or be removed without notice. When a user passes in "worker_info=1" or "worker_info=true" and a worker is running the returned json will be returned with a key named "worker_process_info" which value is a json object with information about the worker process.
This commit is contained in:
19 files changed
+848
-401
No files matched your search
@@ -815,7 +815,7 @@ func (c *Command) Run(args []string) int {
|
||||
return base.CommandCliError
|
||||
}
|
||||
|
||||
opsServer, err := ops.NewServer(c.Logger, c.controller, c.Listeners...)
|
||||
opsServer, err := ops.NewServer(c.Logger, c.controller, c.worker, c.Listeners...)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Failed to start ops listeners: %w", err).Error())
|
||||
return base.CommandCliError
|
||||
|
||||
@@ -492,7 +492,7 @@ func (c *Command) Run(args []string) int {
|
||||
return base.CommandCliError
|
||||
}
|
||||
|
||||
opsServer, err := ops.NewServer(c.Logger, c.controller, c.Listeners...)
|
||||
opsServer, err := ops.NewServer(c.Logger, c.controller, c.worker, c.Listeners...)
|
||||
if err != nil {
|
||||
c.UI.Error(err.Error())
|
||||
return base.CommandCliError
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller"
|
||||
"github.com/hashicorp/boundary/internal/daemon/worker"
|
||||
"github.com/hashicorp/go-cleanhttp"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/hashicorp/go-multierror"
|
||||
@@ -36,7 +37,7 @@ type opsBundle struct {
|
||||
|
||||
// NewServer iterates through all the listeners and sets up HTTP Servers for each, along with individual handlers.
|
||||
// If Controller is set-up, NewServer will set-up a health endpoint for it.
|
||||
func NewServer(l hclog.Logger, c *controller.Controller, listeners ...*base.ServerListener) (*Server, error) {
|
||||
func NewServer(l hclog.Logger, c *controller.Controller, w *worker.Worker, listeners ...*base.ServerListener) (*Server, error) {
|
||||
const op = "ops.NewServer()"
|
||||
if l == nil {
|
||||
return nil, fmt.Errorf("%s: missing logger", op)
|
||||
@@ -54,7 +55,7 @@ func NewServer(l hclog.Logger, c *controller.Controller, listeners ...*base.Serv
|
||||
return nil, fmt.Errorf("%s: missing ops listener", op)
|
||||
}
|
||||
|
||||
h, err := createOpsHandler(ln.Config, c)
|
||||
h, err := createOpsHandler(ln.Config, c, w)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -128,16 +129,30 @@ func (s *Server) WaitIfHealthExists(d time.Duration, ui cli.Ui) {
|
||||
<-time.After(d)
|
||||
}
|
||||
|
||||
func createOpsHandler(lncfg *listenerutil.ListenerConfig, c *controller.Controller) (http.Handler, error) {
|
||||
func createOpsHandler(lncfg *listenerutil.ListenerConfig, c *controller.Controller, w *worker.Worker) (http.Handler, error) {
|
||||
mux := http.NewServeMux()
|
||||
if c != nil && c.HealthService != nil {
|
||||
h, err := c.GetHealthHandler(lncfg)
|
||||
var h http.Handler
|
||||
var err error
|
||||
switch {
|
||||
case c != nil && c.HealthService != nil:
|
||||
h, err = c.GetHealthHandler(lncfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if w != nil {
|
||||
c.HealthService.SetWorkerProcessInformationFunc(w.HealthInformation)
|
||||
}
|
||||
case w != nil:
|
||||
h, err = w.GetHealthHandler()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if h != nil {
|
||||
// Shouldn't happen since this function should only be called when
|
||||
// either a controller or worker is starting up, but just to be safe.
|
||||
mux.Handle("/health", h)
|
||||
}
|
||||
|
||||
mux.Handle("/metrics", promhttp.Handler())
|
||||
return cleanhttp.PrintablePathCheckHandler(mux, nil), nil
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math/big"
|
||||
"net"
|
||||
@@ -17,16 +18,25 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/protobuf/jsonpb"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller/handlers/health"
|
||||
"github.com/hashicorp/boundary/internal/daemon/worker"
|
||||
pbs "github.com/hashicorp/boundary/internal/gen/ops/services"
|
||||
pbhealth "github.com/hashicorp/boundary/internal/gen/worker/health"
|
||||
"github.com/hashicorp/boundary/internal/server"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/hashicorp/go-secure-stdlib/base62"
|
||||
"github.com/hashicorp/go-secure-stdlib/configutil/v2"
|
||||
"github.com/hashicorp/go-secure-stdlib/listenerutil"
|
||||
"github.com/hashicorp/go-secure-stdlib/reloadutil"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/testing/protocmp"
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
)
|
||||
|
||||
func TestNewServer(t *testing.T) {
|
||||
@@ -34,6 +44,7 @@ func TestNewServer(t *testing.T) {
|
||||
name string
|
||||
logger hclog.Logger
|
||||
c *controller.Controller
|
||||
w *worker.Worker
|
||||
listeners []*base.ServerListener
|
||||
assertions func(t *testing.T, s *Server)
|
||||
expErr bool
|
||||
@@ -93,7 +104,7 @@ func TestNewServer(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s, err := NewServer(tt.logger, tt.c, tt.listeners...)
|
||||
s, err := NewServer(tt.logger, tt.c, tt.w, tt.listeners...)
|
||||
if tt.expErr {
|
||||
require.EqualError(t, err, tt.expErrMsg)
|
||||
require.Nil(t, s)
|
||||
@@ -265,7 +276,7 @@ func TestNewServerIntegration(t *testing.T) {
|
||||
err := bs.SetupListeners(nil, &configutil.SharedConfig{Listeners: tt.listeners}, []string{"ops"})
|
||||
require.NoError(t, err)
|
||||
|
||||
s, err := NewServer(hclog.Default(), nil, bs.Listeners...)
|
||||
s, err := NewServer(hclog.Default(), nil, nil, bs.Listeners...)
|
||||
if tt.expErr {
|
||||
require.EqualError(t, err, tt.expErrMsg)
|
||||
require.Nil(t, s)
|
||||
@@ -562,7 +573,7 @@ func TestHealthEndpointLifecycle(t *testing.T) {
|
||||
t.Cleanup(tc.Shutdown)
|
||||
|
||||
// Controller has started and is set onto our Command object, start ops.
|
||||
opsServer, err := NewServer(hclog.Default(), tc.Controller(), tc.Config().Listeners...)
|
||||
opsServer, err := NewServer(hclog.Default(), tc.Controller(), nil, tc.Config().Listeners...)
|
||||
require.NoError(t, err)
|
||||
opsServer.Start()
|
||||
|
||||
@@ -655,6 +666,7 @@ func TestCreateOpsHandler(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setupController bool
|
||||
setupWorker bool
|
||||
lncfg *listenerutil.ListenerConfig
|
||||
expErr bool
|
||||
expErrMsg string
|
||||
@@ -678,6 +690,64 @@ func TestCreateOpsHandler(t *testing.T) {
|
||||
rsp, err := http.Get("http://" + addr + "/health")
|
||||
require.NoError(t, err) // We can do the GET request
|
||||
require.Equal(t, http.StatusOK, rsp.StatusCode) // And the endpoint exists
|
||||
body, err := io.ReadAll(rsp.Body)
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, []byte("{}"), body)
|
||||
|
||||
rsp, err = http.Get("http://" + addr + "/health?worker_info=1")
|
||||
require.NoError(t, err) // We can do the GET request
|
||||
require.Equal(t, http.StatusOK, rsp.StatusCode) // And the endpoint exists
|
||||
body, err = io.ReadAll(rsp.Body)
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, []byte("{}"), body)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "worker set",
|
||||
setupWorker: true,
|
||||
lncfg: &listenerutil.ListenerConfig{},
|
||||
assertions: func(t *testing.T, addr string) {
|
||||
rsp, err := http.Get("http://" + addr + "/health")
|
||||
require.NoError(t, err) // We can do the GET request
|
||||
require.Equal(t, http.StatusOK, rsp.StatusCode) // And the endpoint exists
|
||||
body, err := io.ReadAll(rsp.Body)
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, []byte("{}"), body)
|
||||
|
||||
rsp, err = http.Get("http://" + addr + "/health?worker_info=1")
|
||||
require.NoError(t, err) // We can do the GET request
|
||||
require.Equal(t, http.StatusOK, rsp.StatusCode) // And the endpoint exists
|
||||
pbResp := &pbs.GetHealthResponse{}
|
||||
require.NoError(t, jsonpb.Unmarshal(rsp.Body, pbResp))
|
||||
want := &pbs.GetHealthResponse{WorkerProcessInfo: &pbhealth.HealthInfo{
|
||||
State: server.ActiveOperationalState.String(),
|
||||
ActiveSessionCount: wrapperspb.UInt32(0),
|
||||
}}
|
||||
assert.Empty(t, cmp.Diff(want, pbResp, protocmp.Transform()))
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "worker and controller set",
|
||||
setupController: true,
|
||||
setupWorker: true,
|
||||
lncfg: &listenerutil.ListenerConfig{},
|
||||
assertions: func(t *testing.T, addr string) {
|
||||
rsp, err := http.Get("http://" + addr + "/health")
|
||||
require.NoError(t, err) // We can do the GET request
|
||||
require.Equal(t, http.StatusOK, rsp.StatusCode) // And the endpoint exists
|
||||
body, err := io.ReadAll(rsp.Body)
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, []byte("{}"), body)
|
||||
rsp, err = http.Get("http://" + addr + "/health?worker_info=1")
|
||||
require.NoError(t, err) // We can do the GET request
|
||||
require.Equal(t, http.StatusOK, rsp.StatusCode) // And the endpoint exists
|
||||
pbResp := &pbs.GetHealthResponse{}
|
||||
require.NoError(t, jsonpb.Unmarshal(rsp.Body, pbResp))
|
||||
want := &pbs.GetHealthResponse{WorkerProcessInfo: &pbhealth.HealthInfo{
|
||||
State: server.ActiveOperationalState.String(),
|
||||
ActiveSessionCount: wrapperspb.UInt32(0),
|
||||
}}
|
||||
assert.Empty(t, cmp.Diff(want, pbResp, protocmp.Transform()))
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -698,8 +768,14 @@ func TestCreateOpsHandler(t *testing.T) {
|
||||
|
||||
c = tc.Controller()
|
||||
}
|
||||
var w *worker.Worker
|
||||
if tt.setupWorker {
|
||||
tc := worker.NewTestWorker(t, &worker.TestWorkerOpts{})
|
||||
t.Cleanup(tc.Shutdown)
|
||||
w = tc.Worker()
|
||||
}
|
||||
|
||||
h, err := createOpsHandler(tt.lncfg, c)
|
||||
h, err := createOpsHandler(tt.lncfg, c, w)
|
||||
if tt.expErr {
|
||||
require.EqualError(t, err, tt.expErrMsg)
|
||||
require.Nil(t, h)
|
||||
|
||||
Reference in new issue
Block a user