Skip to content

Commit

Permalink
feat: implement single component health status
Browse files Browse the repository at this point in the history
  • Loading branch information
isometry committed Apr 2, 2024
1 parent f095a4d commit e651137
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 54 deletions.
9 changes: 8 additions & 1 deletion pkg/commands/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
var (
targetHost string
targetPort int
component string
tlsClient bool
insecureSkipVerify bool
clientTimeout time.Duration
Expand Down Expand Up @@ -94,7 +95,10 @@ func query(c *cobra.Command, _ []string) (err error) {

health := ph.NewHealthClient(conn)

status, err := health.Check(ctx, &ph.HealthCheckRequest{})
request := &ph.HealthCheckRequest{
Component: component,
}
status, err := health.Check(ctx, request)
if err != nil {
slog.Info("failed to check", slog.Any("error", err))
return err
Expand Down Expand Up @@ -132,6 +136,9 @@ func init() {
ClientCmd.Flags().IntVarP(&targetPort, "port", "p", 8080, "server port")
viper.BindPFlag("server", ClientCmd.Flags().Lookup("server"))

ClientCmd.Flags().StringVarP(&component, "component", "c", "", "component name")
viper.BindPFlag("component", ClientCmd.Flags().Lookup("component"))

ClientCmd.Flags().BoolVar(&tlsClient, "tls", false, "enable tls")
viper.BindPFlag("tls", ClientCmd.Flags().Lookup("tls"))

Expand Down
6 changes: 1 addition & 5 deletions pkg/provider/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net"
"time"

"github.com/mcuadros/go-defaults"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
Expand All @@ -22,6 +21,7 @@ import (
var TypeGRPC = "grpc"

type GRPC struct {
provider.UnimplementedProvider
Name string `mapstructure:"name"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Expand All @@ -45,10 +45,6 @@ func (i *GRPC) LogValue() slog.Value {
return slog.GroupValue(logAttr...)
}

func (i *GRPC) SetDefaults() {
defaults.SetDefaults(i)
}

func (i *GRPC) GetType() string {
return TypeGRPC
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/provider/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log/slog"
"time"

"github.com/mcuadros/go-defaults"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"

Expand All @@ -18,6 +17,7 @@ import (
const TypeHelm = "helm"

type Helm struct {
provider.UnimplementedProvider
Name string `mapstructure:"name"`
Chart string `mapstructure:"chart"`
Namespace string `mapstructure:"namespace"`
Expand All @@ -38,10 +38,6 @@ func (i *Helm) LogValue() slog.Value {
return slog.GroupValue(logAttr...)
}

func (i *Helm) SetDefaults() {
defaults.SetDefaults(i)
}

func (i *Helm) GetType() string {
return TypeHelm
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/provider/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"slices"
"time"

"github.com/mcuadros/go-defaults"
"google.golang.org/protobuf/types/known/anypb"

ph "github.com/isometry/platform-health/pkg/platform_health"
Expand All @@ -23,6 +22,7 @@ import (
const TypeHTTP = "http"

type HTTP struct {
provider.UnimplementedProvider
Name string `mapstructure:"name"`
URL string `mapstructure:"url"`
Method string `mapstructure:"method" default:"HEAD"`
Expand Down Expand Up @@ -53,10 +53,6 @@ func (i *HTTP) LogValue() slog.Value {
return slog.GroupValue(logAttr...)
}

func (i *HTTP) SetDefaults() {
defaults.SetDefaults(i)
}

func (i *HTTP) GetType() string {
return TypeHTTP
}
Expand Down
8 changes: 2 additions & 6 deletions pkg/provider/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

"github.com/mcuadros/go-defaults"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
Expand All @@ -25,6 +24,7 @@ import (
const TypeKubernetes = "kubernetes"

type Kubernetes struct {
provider.UnimplementedProvider
Group string `mapstructure:"group" default:"apps"`
Version string `mapstructure:"version" default:"v1"`
Kind string `mapstructure:"kind" default:"deployment"`
Expand Down Expand Up @@ -53,17 +53,13 @@ func (i *Kubernetes) LogValue() slog.Value {
slog.String("group", i.Group),
slog.String("version", i.Version),
slog.String("kind", i.Kind),
slog.String("name", i.Name),
slog.String("namespace", i.Namespace),
slog.String("name", i.Name),
slog.Any("timeout", i.Timeout),
}
return slog.GroupValue(logAttr...)
}

func (i *Kubernetes) SetDefaults() {
defaults.SetDefaults(i)
}

func (i *Kubernetes) GetType() string {
return TypeKubernetes
}
Expand Down
7 changes: 1 addition & 6 deletions pkg/provider/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"context"
"time"

"github.com/mcuadros/go-defaults"

ph "github.com/isometry/platform-health/pkg/platform_health"
"github.com/isometry/platform-health/pkg/provider"
)

const TypeMock = "mock"

type Mock struct {
provider.UnimplementedProvider
Name string `mapstructure:"name"`
Health ph.Status `mapstructure:"health" default:"1"`
Sleep time.Duration `mapstructure:"sleep" default:"1ns"`
Expand All @@ -22,10 +21,6 @@ func init() {
provider.Register(TypeMock, new(Mock))
}

func (i *Mock) SetDefaults() {
defaults.SetDefaults(i)
}

func (i *Mock) GetType() string {
return TypeMock
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sync"
"time"

"github.com/mcuadros/go-defaults"
"google.golang.org/protobuf/types/known/durationpb"

ph "github.com/isometry/platform-health/pkg/platform_health"
Expand All @@ -18,10 +19,33 @@ type Instance interface {
GetName() string
// GetHealth checks and returns the instance
GetHealth(context.Context) *ph.HealthCheckResponse
// SetComponent sets the component of the instance, only relevant for satellites
SetComponent(string)
// SetDefaults sets the default values for the instance
SetDefaults()
}

type UnimplementedProvider struct {
}

func (i *UnimplementedProvider) GetType() string {
panic("not implement")
}

func (i *UnimplementedProvider) GetName() string {
panic("not implement")
}

func (i *UnimplementedProvider) GetHealth(ctx context.Context) *ph.HealthCheckResponse {
panic("not implement")
}

func (i *UnimplementedProvider) SetComponent(string) {}

func (i *UnimplementedProvider) SetDefaults() {
defaults.SetDefaults(i)
}

// Config is the interface through which the provider configuration is retrieved.
type Config interface {
GetInstances() []Instance
Expand Down
22 changes: 12 additions & 10 deletions pkg/provider/satellite/satellite.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net"
"time"

"github.com/mcuadros/go-defaults"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
Expand All @@ -22,12 +21,14 @@ import (
const TypeSatellite = "satellite"

type Satellite struct {
Name string `mapstructure:"name"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
TLS bool `mapstructure:"tls"`
Insecure bool `mapstructure:"insecure"`
Timeout time.Duration `mapstructure:"timeout" default:"30s"`
provider.UnimplementedProvider
Name string `mapstructure:"name"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
TLS bool `mapstructure:"tls"`
Insecure bool `mapstructure:"insecure"`
Timeout time.Duration `mapstructure:"timeout" default:"30s"`
component string
}

func init() {
Expand All @@ -44,8 +45,8 @@ func (i *Satellite) LogValue() slog.Value {
return slog.GroupValue(logAttr...)
}

func (i *Satellite) SetDefaults() {
defaults.SetDefaults(i)
func (i *Satellite) SetComponent(component string) {
i.component = component
}

func (i *Satellite) GetType() string {
Expand Down Expand Up @@ -94,7 +95,8 @@ func (i *Satellite) GetHealth(ctx context.Context) *ph.HealthCheckResponse {

// Propagate already visited serverIds from context to enable loop detection
request := &ph.HealthCheckRequest{
Hops: server.HopsFromContext(ctx),
Component: i.component,
Hops: server.HopsFromContext(ctx),
}

status, err := ph.NewHealthClient(conn).Check(ctx, request)
Expand Down
7 changes: 1 addition & 6 deletions pkg/provider/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"net"
"time"

"github.com/mcuadros/go-defaults"

ph "github.com/isometry/platform-health/pkg/platform_health"
"github.com/isometry/platform-health/pkg/provider"
"github.com/isometry/platform-health/pkg/utils"
Expand All @@ -17,6 +15,7 @@ import (
const TypeTCP = "tcp"

type TCP struct {
provider.UnimplementedProvider
Name string `mapstructure:"name"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port" default:"80"`
Expand All @@ -39,10 +38,6 @@ func (i *TCP) LogValue() slog.Value {
return slog.GroupValue(logAttr...)
}

func (i *TCP) SetDefaults() {
defaults.SetDefaults(i)
}

func (i *TCP) GetType() string {
return TypeTCP
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/provider/tls/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"slices"
"time"

"github.com/mcuadros/go-defaults"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"

Expand All @@ -24,6 +23,7 @@ import (
const TypeTLS = "tls"

type TLS struct {
provider.UnimplementedProvider
Name string `mapstructure:"name"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port" default:"443"`
Expand Down Expand Up @@ -58,10 +58,6 @@ func (i *TLS) LogValue() slog.Value {
return slog.GroupValue(logAttr...)
}

func (i *TLS) SetDefaults() {
defaults.SetDefaults(i)
}

func (i *TLS) GetType() string {
return TypeTLS
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/provider/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

vault "github.com/hashicorp/vault/api"
"github.com/mcuadros/go-defaults"

ph "github.com/isometry/platform-health/pkg/platform_health"
"github.com/isometry/platform-health/pkg/provider"
Expand All @@ -16,6 +15,7 @@ import (
const TypeVault = "vault"

type Vault struct {
provider.UnimplementedProvider
Name string `mapstructure:"name"`
Address string `mapstructure:"address"`
Timeout time.Duration `mapstructure:"timeout" default:"1s"`
Expand All @@ -36,10 +36,6 @@ func (i *Vault) LogValue() slog.Value {
return slog.GroupValue(logAttr...)
}

func (i *Vault) SetDefaults() {
defaults.SetDefaults(i)
}

func (i *Vault) GetType() string {
return TypeVault
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net"
"slices"
"strings"
"time"

"google.golang.org/grpc"
Expand Down Expand Up @@ -111,6 +112,37 @@ func (s *PlatformHealthServer) Check(ctx context.Context, req *ph.HealthCheckReq

providerServices := s.Config.GetInstances()

componentPath := strings.Split(req.Component, "/")
if len(componentPath) > 1 {
switch len(componentPath) {
case 1:
// invalid component path
case 2:
for _, instance := range providerServices {
if instance.GetType() == componentPath[0] && instance.GetName() == componentPath[1] {
providerServices = []provider.Instance{instance}
goto singleComponent
}
}
// invalid component path
default:
for _, instance := range providerServices {
// "satellite" is a special case, as it is the only provider that can have multiple components
if instance.GetType() == "satellite" && instance.GetName() == componentPath[0] {
providerServices = []provider.Instance{instance}
instance.SetComponent(strings.Join(componentPath[1:], "/"))
goto singleComponent
}
}
// invalid component path
}
return &ph.HealthCheckResponse{
Status: ph.Status_UNKNOWN,
Message: "invalid component path",
}, nil
}
singleComponent:

start := time.Now()
platformServices, health := provider.Check(ctx, providerServices)
duration := durationpb.New(time.Since(start))
Expand Down

0 comments on commit e651137

Please sign in to comment.