feat(controller): Add health endpoint (#1882)
* feat(controller): New health service and server This change also implements a new listener purpose, `ops`. `ops` (short for operations) is a new listener purpose that is responsible for serving Boundary's operations endpoints, like health and possibly metrics. At Startup: `ops` listeners get configured as late as possible to catch any errors with the startup process; At Shutdown: If the health endpoint has been set-up, we introduce a configurable delay in the process. This is to enable the health endpoint to report unhealthy for that amount of time. `ops` listeners are also the last listeners to be shutdown.
This commit is contained in:
25 files changed
+2034
-23
No files matched your search
@@ -25,6 +25,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/scheduler/job"
|
||||
"github.com/hashicorp/boundary/internal/servers"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/common"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/health"
|
||||
"github.com/hashicorp/boundary/internal/session"
|
||||
"github.com/hashicorp/boundary/internal/target"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
@@ -80,6 +81,10 @@ type Controller struct {
|
||||
kms *kms.Kms
|
||||
|
||||
enabledPlugins []base.EnabledPlugin
|
||||
|
||||
// Used to signal the Health Service to start
|
||||
// replying to queries with "503 Service Unavailable".
|
||||
HealthService *health.Service
|
||||
}
|
||||
|
||||
func New(ctx context.Context, conf *Config) (*Controller, error) {
|
||||
@@ -92,6 +97,7 @@ func New(ctx context.Context, conf *Config) (*Controller, error) {
|
||||
workerAuthCache: new(sync.Map),
|
||||
workerStatusUpdateTimes: new(sync.Map),
|
||||
enabledPlugins: conf.Server.EnabledPlugins,
|
||||
apiListeners: make([]*base.ServerListener, 0),
|
||||
}
|
||||
|
||||
c.started.Store(false)
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/auth/oidc"
|
||||
"github.com/hashicorp/boundary/internal/gen/controller/api/services"
|
||||
authpb "github.com/hashicorp/boundary/internal/gen/controller/auth"
|
||||
opsservices "github.com/hashicorp/boundary/internal/gen/controller/ops/services"
|
||||
"github.com/hashicorp/boundary/internal/observability/event"
|
||||
"github.com/hashicorp/boundary/internal/servers/common"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/auth"
|
||||
@@ -27,6 +28,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/credentiallibraries"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/credentialstores"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/groups"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/health"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/host_catalogs"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/host_sets"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/hosts"
|
||||
@@ -74,6 +76,28 @@ func (c *Controller) apiHandler(props HandlerProperties) (http.Handler, error) {
|
||||
return eventsHandler, err
|
||||
}
|
||||
|
||||
// GetHealthHandler returns a gRPC Gateway mux that is registered against the
|
||||
// controller's gRPC health service to make it accessible from an HTTP API.
|
||||
func (c *Controller) GetHealthHandler(lcfg *listenerutil.ListenerConfig) (http.Handler, error) {
|
||||
const op = "controller.(Controller).GetHealthHandler"
|
||||
if lcfg == nil {
|
||||
return nil, fmt.Errorf("%s: received nil listener config", op)
|
||||
}
|
||||
|
||||
healthGrpcGwMux := newGrpcGatewayMux()
|
||||
err := registerHealthGrpcGatewayEndpoint(c.baseContext, healthGrpcGwMux, gatewayDialOptions(c.apiGrpcServerListener)...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: failed to register health service handler: %w", op, err)
|
||||
}
|
||||
|
||||
wrapped := wrapHandlerWithCommonFuncs(healthGrpcGwMux, c, HandlerProperties{lcfg, c.baseContext})
|
||||
return common.WrapWithEventsHandler(wrapped, c.conf.Eventer, c.kms, lcfg)
|
||||
}
|
||||
|
||||
func registerHealthGrpcGatewayEndpoint(ctx context.Context, gwMux *runtime.ServeMux, dialOptions ...grpc.DialOption) error {
|
||||
return opsservices.RegisterHealthServiceHandlerFromEndpoint(ctx, gwMux, gatewayTarget, dialOptions)
|
||||
}
|
||||
|
||||
func (c *Controller) registerGrpcServices(s *grpc.Server) error {
|
||||
// We have to check against the current services because the gRPC lib treats a duplicate
|
||||
// register call as an error and os.Exits.
|
||||
@@ -193,6 +217,11 @@ func (c *Controller) registerGrpcServices(s *grpc.Server) error {
|
||||
}
|
||||
services.RegisterCredentialLibraryServiceServer(s, cl)
|
||||
}
|
||||
if _, ok := s.GetServiceInfo()[opsservices.HealthService_ServiceDesc.ServiceName]; !ok {
|
||||
hs := health.NewService()
|
||||
opsservices.RegisterHealthServiceServer(s, hs)
|
||||
c.HealthService = hs
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
pbs "github.com/hashicorp/boundary/internal/gen/controller/ops/services"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
pbs.UnimplementedHealthServiceServer
|
||||
|
||||
replyWithServiceUnavailable bool
|
||||
}
|
||||
|
||||
func NewService() *Service {
|
||||
s := Service{}
|
||||
return &s
|
||||
}
|
||||
|
||||
func (s *Service) GetHealth(ctx context.Context, req *pbs.GetHealthRequest) (*pbs.GetHealthResponse, error) {
|
||||
if s.replyWithServiceUnavailable {
|
||||
err := handlers.SetStatusCode(ctx, http.StatusServiceUnavailable)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pbs.GetHealthResponse{}, nil
|
||||
}
|
||||
|
||||
return &pbs.GetHealthResponse{}, nil
|
||||
}
|
||||
|
||||
// StartServiceUnavailableReplies gets returned to the caller of NewService.
|
||||
// When invoked, we start responding to any health queries with a 503.
|
||||
func (s *Service) StartServiceUnavailableReplies() {
|
||||
s.replyWithServiceUnavailable = true
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/gen/controller/ops/services"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
func TestGetHealth(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ctx context.Context
|
||||
serviceUnavailable bool
|
||||
expGetHealthResponse *services.GetHealthResponse
|
||||
expErr bool
|
||||
expErrMsg string
|
||||
}{
|
||||
{
|
||||
name: "healthy reply",
|
||||
ctx: context.Background(),
|
||||
serviceUnavailable: false,
|
||||
expGetHealthResponse: &services.GetHealthResponse{},
|
||||
expErr: false,
|
||||
},
|
||||
{
|
||||
name: "service unavailable reply",
|
||||
ctx: grpc.NewContextWithServerTransportStream(context.Background(), &testServerTransportStream{expHttpCode: "503"}),
|
||||
serviceUnavailable: true,
|
||||
expGetHealthResponse: &services.GetHealthResponse{},
|
||||
expErr: false,
|
||||
},
|
||||
{
|
||||
name: "get health error",
|
||||
ctx: context.Background(),
|
||||
serviceUnavailable: true,
|
||||
expErr: true,
|
||||
expErrMsg: "handlers.SetHttpStatusCode: internal error, unknown: error #500: rpc error: code = Internal desc = grpc: failed to fetch the stream from the context context.Background",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
hs := NewService()
|
||||
|
||||
if tt.serviceUnavailable {
|
||||
hs.StartServiceUnavailableReplies()
|
||||
}
|
||||
|
||||
rsp, err := hs.GetHealth(tt.ctx, &services.GetHealthRequest{})
|
||||
if tt.expErr {
|
||||
require.Error(t, err)
|
||||
require.EqualError(t, err, tt.expErrMsg)
|
||||
return
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, tt.expGetHealthResponse, rsp)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type testServerTransportStream struct {
|
||||
expHttpCode string
|
||||
}
|
||||
|
||||
func (s *testServerTransportStream) Method() string { return "" }
|
||||
func (s *testServerTransportStream) SendHeader(md metadata.MD) error { return nil }
|
||||
func (s *testServerTransportStream) SetTrailer(md metadata.MD) error { return nil }
|
||||
func (s *testServerTransportStream) SetHeader(md metadata.MD) error {
|
||||
codes, ok := md["x-http-code"]
|
||||
if !ok {
|
||||
return fmt.Errorf("x-http-code header not found")
|
||||
}
|
||||
if len(codes) != 1 {
|
||||
return fmt.Errorf("expected only one element in http codes, got %d", len(codes))
|
||||
}
|
||||
if codes[0] != s.expHttpCode {
|
||||
return fmt.Errorf("expected http code %q, got %q", s.expHttpCode, codes[0])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -576,7 +576,7 @@ func TestControllerConfig(t *testing.T, ctx context.Context, tc *TestController,
|
||||
for _, listener := range opts.Config.Listeners {
|
||||
listener.RandomPort = true
|
||||
}
|
||||
if err := tc.b.SetupListeners(nil, opts.Config.SharedConfig, []string{"api", "cluster"}); err != nil {
|
||||
if err := tc.b.SetupListeners(nil, opts.Config.SharedConfig, []string{"api", "cluster", "ops"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := tc.b.SetupControllerPublicClusterAddress(opts.Config, ""); err != nil {
|
||||
|
||||
Reference in new issue
Block a user