Revision: Rename to 'maglevd'; Refactor config structure
This commit is contained in:
@@ -2,7 +2,6 @@ package grpcapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
@@ -13,9 +12,9 @@ import (
|
||||
"git.ipng.ch/ipng/vpp-maglev/internal/health"
|
||||
)
|
||||
|
||||
// Server implements the HealthCheckerServer gRPC interface.
|
||||
// Server implements the MaglevServer gRPC interface.
|
||||
type Server struct {
|
||||
UnimplementedHealthCheckerServer
|
||||
UnimplementedMaglevServer
|
||||
checker *checker.Checker
|
||||
}
|
||||
|
||||
@@ -24,85 +23,88 @@ func NewServer(c *checker.Checker) *Server {
|
||||
return &Server{checker: c}
|
||||
}
|
||||
|
||||
// ListVIPs returns the names of all configured VIPs.
|
||||
func (s *Server) ListVIPs(_ context.Context, _ *ListVIPsRequest) (*ListVIPsResponse, error) {
|
||||
return &ListVIPsResponse{VipNames: s.checker.ListVIPs()}, nil
|
||||
// ListFrontends returns the names of all configured frontends.
|
||||
func (s *Server) ListFrontends(_ context.Context, _ *ListFrontendsRequest) (*ListFrontendsResponse, error) {
|
||||
return &ListFrontendsResponse{FrontendNames: s.checker.ListFrontends()}, nil
|
||||
}
|
||||
|
||||
// GetVIP returns configuration details for a single VIP.
|
||||
func (s *Server) GetVIP(_ context.Context, req *GetVIPRequest) (*VIPInfo, error) {
|
||||
vip, ok := s.checker.GetVIP(req.VipName)
|
||||
// GetFrontend returns configuration details for a single frontend.
|
||||
func (s *Server) GetFrontend(_ context.Context, req *GetFrontendRequest) (*FrontendInfo, error) {
|
||||
fe, ok := s.checker.GetFrontend(req.Name)
|
||||
if !ok {
|
||||
return nil, status.Errorf(codes.NotFound, "vip %q not found", req.VipName)
|
||||
return nil, status.Errorf(codes.NotFound, "frontend %q not found", req.Name)
|
||||
}
|
||||
return vipToProto(req.VipName, vip), nil
|
||||
return frontendToProto(req.Name, fe), nil
|
||||
}
|
||||
|
||||
// ListBackends returns health state for all backends of a VIP.
|
||||
func (s *Server) ListBackends(_ context.Context, req *ListBackendsRequest) (*ListBackendsResponse, error) {
|
||||
if _, ok := s.checker.GetVIP(req.VipName); !ok {
|
||||
return nil, status.Errorf(codes.NotFound, "vip %q not found", req.VipName)
|
||||
}
|
||||
backends := s.checker.ListBackends(req.VipName)
|
||||
resp := &ListBackendsResponse{}
|
||||
for _, b := range backends {
|
||||
resp.Backends = append(resp.Backends, backendToProto(b))
|
||||
}
|
||||
return resp, nil
|
||||
// ListBackends returns the names of all active backends.
|
||||
func (s *Server) ListBackends(_ context.Context, _ *ListBackendsRequest) (*ListBackendsResponse, error) {
|
||||
return &ListBackendsResponse{BackendNames: s.checker.ListBackends()}, nil
|
||||
}
|
||||
|
||||
// GetBackend returns health state for a specific VIP:backend tuple.
|
||||
// GetBackend returns health state for a backend by name.
|
||||
func (s *Server) GetBackend(_ context.Context, req *GetBackendRequest) (*BackendInfo, error) {
|
||||
b, ok := s.checker.GetBackend(req.VipName, req.BackendAddress)
|
||||
b, ok := s.checker.GetBackend(req.Name)
|
||||
if !ok {
|
||||
return nil, status.Errorf(codes.NotFound, "backend %q in vip %q not found",
|
||||
req.BackendAddress, req.VipName)
|
||||
return nil, status.Errorf(codes.NotFound, "backend %q not found", req.Name)
|
||||
}
|
||||
return backendToProto(b), nil
|
||||
}
|
||||
|
||||
// PauseBackend pauses health checking for a specific backend.
|
||||
// PauseBackend pauses health checking for a backend by name.
|
||||
func (s *Server) PauseBackend(_ context.Context, req *PauseResumeRequest) (*BackendInfo, error) {
|
||||
b, ok := s.checker.PauseBackend(req.VipName, req.BackendAddress)
|
||||
b, ok := s.checker.PauseBackend(req.Name)
|
||||
if !ok {
|
||||
return nil, status.Errorf(codes.NotFound, "backend %q in vip %q not found",
|
||||
req.BackendAddress, req.VipName)
|
||||
return nil, status.Errorf(codes.NotFound, "backend %q not found", req.Name)
|
||||
}
|
||||
return backendToProto(b), nil
|
||||
}
|
||||
|
||||
// ResumeBackend resumes health checking for a specific backend.
|
||||
// ResumeBackend resumes health checking for a backend by name.
|
||||
func (s *Server) ResumeBackend(_ context.Context, req *PauseResumeRequest) (*BackendInfo, error) {
|
||||
b, ok := s.checker.ResumeBackend(req.VipName, req.BackendAddress)
|
||||
b, ok := s.checker.ResumeBackend(req.Name)
|
||||
if !ok {
|
||||
return nil, status.Errorf(codes.NotFound, "backend %q in vip %q not found",
|
||||
req.BackendAddress, req.VipName)
|
||||
return nil, status.Errorf(codes.NotFound, "backend %q not found", req.Name)
|
||||
}
|
||||
return backendToProto(b), nil
|
||||
}
|
||||
|
||||
// WatchTransitions streams the current state of all backends on connect, then
|
||||
// ListHealthChecks returns the names of all configured health checks.
|
||||
func (s *Server) ListHealthChecks(_ context.Context, _ *ListHealthChecksRequest) (*ListHealthChecksResponse, error) {
|
||||
return &ListHealthChecksResponse{Names: s.checker.ListHealthChecks()}, nil
|
||||
}
|
||||
|
||||
// GetHealthCheck returns the full configuration for a health check by name.
|
||||
func (s *Server) GetHealthCheck(_ context.Context, req *GetHealthCheckRequest) (*HealthCheckInfo, error) {
|
||||
hc, ok := s.checker.GetHealthCheck(req.Name)
|
||||
if !ok {
|
||||
return nil, status.Errorf(codes.NotFound, "healthcheck %q not found", req.Name)
|
||||
}
|
||||
return healthCheckToProto(req.Name, hc), nil
|
||||
}
|
||||
|
||||
// WatchBackendEvents streams the current state of all backends on connect, then
|
||||
// streams live state transitions until the client disconnects.
|
||||
func (s *Server) WatchTransitions(_ *WatchRequest, stream HealthChecker_WatchTransitionsServer) error {
|
||||
func (s *Server) WatchBackendEvents(_ *WatchRequest, stream Maglev_WatchBackendEventsServer) error {
|
||||
// Send current state of all backends as synthetic events.
|
||||
for _, vipName := range s.checker.ListVIPs() {
|
||||
for _, b := range s.checker.ListBackends(vipName) {
|
||||
ev := &TransitionEvent{
|
||||
VipName: vipName,
|
||||
BackendAddress: b.Address.String(),
|
||||
Transition: &TransitionRecord{
|
||||
From: b.State.String(),
|
||||
To: b.State.String(),
|
||||
AtUnixNs: 0,
|
||||
},
|
||||
}
|
||||
if err := stream.Send(ev); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, name := range s.checker.ListBackends() {
|
||||
snap, ok := s.checker.GetBackend(name)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
ev := &BackendEvent{
|
||||
BackendName: name,
|
||||
Transition: &TransitionRecord{
|
||||
From: snap.Health.State.String(),
|
||||
To: snap.Health.State.String(),
|
||||
AtUnixNs: 0,
|
||||
},
|
||||
}
|
||||
if err := stream.Send(ev); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Subscribe to live transitions.
|
||||
ch, unsub := s.checker.Subscribe()
|
||||
defer unsub()
|
||||
|
||||
@@ -114,10 +116,9 @@ func (s *Server) WatchTransitions(_ *WatchRequest, stream HealthChecker_WatchTra
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
ev := &TransitionEvent{
|
||||
VipName: e.VIPName,
|
||||
BackendAddress: e.Backend.String(),
|
||||
Transition: transitionToProto(e.Transition),
|
||||
ev := &BackendEvent{
|
||||
BackendName: e.BackendName,
|
||||
Transition: transitionToProto(e.Transition),
|
||||
}
|
||||
if err := stream.Send(ev); err != nil {
|
||||
return err
|
||||
@@ -128,28 +129,71 @@ func (s *Server) WatchTransitions(_ *WatchRequest, stream HealthChecker_WatchTra
|
||||
|
||||
// ---- conversion helpers ----------------------------------------------------
|
||||
|
||||
func vipToProto(name string, v config.VIP) *VIPInfo {
|
||||
info := &VIPInfo{
|
||||
Name: name,
|
||||
Address: v.Address.String(),
|
||||
Protocol: v.Protocol,
|
||||
Port: uint32(v.Port),
|
||||
Description: v.Description,
|
||||
func frontendToProto(name string, fe config.Frontend) *FrontendInfo {
|
||||
return &FrontendInfo{
|
||||
Name: name,
|
||||
Address: fe.Address.String(),
|
||||
Protocol: fe.Protocol,
|
||||
Port: uint32(fe.Port),
|
||||
Description: fe.Description,
|
||||
BackendNames: fe.Backends,
|
||||
}
|
||||
for _, b := range v.Backends {
|
||||
info.Backends = append(info.Backends, b.String())
|
||||
}
|
||||
|
||||
func backendToProto(snap checker.BackendSnapshot) *BackendInfo {
|
||||
info := &BackendInfo{
|
||||
Name: snap.Health.Name,
|
||||
Address: snap.Health.Address.String(),
|
||||
State: snap.Health.State.String(),
|
||||
Enabled: snap.Config.Enabled,
|
||||
Weight: int32(snap.Config.Weight),
|
||||
Healthcheck: snap.Config.HealthCheck,
|
||||
}
|
||||
for _, t := range snap.Health.Transitions {
|
||||
info.Transitions = append(info.Transitions, transitionToProto(t))
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
func backendToProto(b *health.Backend) *BackendInfo {
|
||||
info := &BackendInfo{
|
||||
VipName: b.VIPName,
|
||||
Address: b.Address.String(),
|
||||
State: b.State.String(),
|
||||
func healthCheckToProto(name string, hc config.HealthCheck) *HealthCheckInfo {
|
||||
info := &HealthCheckInfo{
|
||||
Name: name,
|
||||
Type: hc.Type,
|
||||
Port: uint32(hc.Port),
|
||||
IntervalNs: hc.Interval.Nanoseconds(),
|
||||
FastIntervalNs: hc.FastInterval.Nanoseconds(),
|
||||
DownIntervalNs: hc.DownInterval.Nanoseconds(),
|
||||
TimeoutNs: hc.Timeout.Nanoseconds(),
|
||||
Rise: int32(hc.Rise),
|
||||
Fall: int32(hc.Fall),
|
||||
}
|
||||
for _, t := range b.Transitions {
|
||||
info.Transitions = append(info.Transitions, transitionToProto(t))
|
||||
if hc.ProbeIPv4Src != nil {
|
||||
info.ProbeIpv4Src = hc.ProbeIPv4Src.String()
|
||||
}
|
||||
if hc.ProbeIPv6Src != nil {
|
||||
info.ProbeIpv6Src = hc.ProbeIPv6Src.String()
|
||||
}
|
||||
if hc.HTTP != nil {
|
||||
re := ""
|
||||
if hc.HTTP.ResponseRegexp != nil {
|
||||
re = hc.HTTP.ResponseRegexp.String()
|
||||
}
|
||||
info.Http = &HTTPCheckParams{
|
||||
Path: hc.HTTP.Path,
|
||||
Host: hc.HTTP.Host,
|
||||
ResponseCodeMin: int32(hc.HTTP.ResponseCodeMin),
|
||||
ResponseCodeMax: int32(hc.HTTP.ResponseCodeMax),
|
||||
ResponseRegexp: re,
|
||||
ServerName: hc.HTTP.ServerName,
|
||||
InsecureSkipVerify: hc.HTTP.InsecureSkipVerify,
|
||||
}
|
||||
}
|
||||
if hc.TCP != nil {
|
||||
info.Tcp = &TCPCheckParams{
|
||||
Ssl: hc.TCP.SSL,
|
||||
ServerName: hc.TCP.ServerName,
|
||||
InsecureSkipVerify: hc.TCP.InsecureSkipVerify,
|
||||
}
|
||||
}
|
||||
return info
|
||||
}
|
||||
@@ -164,4 +208,3 @@ func transitionToProto(t health.Transition) *TransitionRecord {
|
||||
|
||||
// Ensure net.IP is imported (used via b.Address.String()).
|
||||
var _ = net.IP{}
|
||||
var _ = fmt.Sprintf
|
||||
|
||||
Reference in New Issue
Block a user