Skip to content

Commit

Permalink
use gohc for healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurHlt committed Oct 23, 2023
1 parent 5e27549 commit 440ff99
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 891 deletions.
2 changes: 1 addition & 1 deletion app/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (a *App) loadGrpcServer() error {
grpcServer := grpc.NewServer(grpcOptions...)

reflection.Register(grpcServer)
serv, err := gslb.NewServer(a.consulClient, a.gslocConsul)
serv, err := gslb.NewServer(a.consulClient, a.gslocConsul, a.cnf.HealthCheckConfig.Plugins)
if err != nil {
return fmt.Errorf("agent: failed to create gslb server: %v", err)
}
Expand Down
41 changes: 38 additions & 3 deletions config/hc.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package config

import (
"fmt"
"os/exec"
)

type HealthCheckConfig struct {
HealthcheckAddress string `yaml:"healthcheck_address"`
HealthcheckAuth *BasicAuth `yaml:"healthcheck_auth"`
AllowOnlyLocalhost bool `yaml:"allow_only_localhost"`
HealthcheckAddress string `yaml:"healthcheck_address"`
HealthcheckAuth *BasicAuth `yaml:"healthcheck_auth"`
AllowOnlyLocalhost bool `yaml:"allow_only_localhost"`
Plugins []*PluginHealthCheckConfig `yaml:"plugins"`
}

func (c *HealthCheckConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand All @@ -14,3 +20,32 @@ func (c *HealthCheckConfig) UnmarshalYAML(unmarshal func(interface{}) error) err
}
return nil
}

type PluginHealthCheckConfig struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
Path string `yaml:"path"`
Args []string `yaml:"args"`
}

func (c *PluginHealthCheckConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain PluginHealthCheckConfig
err := unmarshal((*plain)(c))
if err != nil {
return err
}
if c.Name == "" {
return fmt.Errorf("missing name in plugin")
}
if c.Description == "" {
return fmt.Errorf("missing description for plugin %s", c.Name)
}
if c.Path == "" {
return fmt.Errorf("missing path for plugin %s", c.Name)
}
_, err = exec.LookPath(c.Path)
if err != nil {
return fmt.Errorf("unable to find path %s for plugin %s", c.Path, c.Name)
}
return nil
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/miekg/dns v1.1.56
github.com/onsi/ginkgo/v2 v2.13.0
github.com/onsi/gomega v1.28.0
github.com/orange-cloudfoundry/gsloc-go-sdk v0.5.0
github.com/orange-cloudfoundry/gsloc-go-sdk v0.6.0
github.com/oschwald/geoip2-golang v1.9.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.17.0
Expand All @@ -29,6 +29,7 @@ require (
)

require (
github.com/ArthurHlt/gohc v1.0.0 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand All @@ -39,6 +40,7 @@ require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ cloud.google.com/go/compute v1.23.0 h1:tP41Zoavr8ptEqaW6j+LQOnyBBhO7OkOMAGrgLopT
cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY=
github.com/ArthurHlt/emitter v1.1.0 h1:DeD1o+EriC1jUAkrimMgGue5+erUqoy4SLoJ0wN5nEc=
github.com/ArthurHlt/emitter v1.1.0/go.mod h1:iWgeciGVKYVJ/QcnrRqfxVA5SJ0lLbFKyrNpGkv393c=
github.com/ArthurHlt/gohc v1.0.0 h1:qC839QHBFXxltTjt/aPRVYxuFmgIG+NqQf9JqmaPIEM=
github.com/ArthurHlt/gohc v1.0.0/go.mod h1:0ImzwC7e80W06I8vmGKWV5hycbs2pL3lwRZ1MM2xaA4=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0=
github.com/alecthomas/kong v0.8.1 h1:acZdn3m4lLRobeh3Zi2S2EpnXTd1mOL6U7xVml+vfkY=
Expand Down Expand Up @@ -69,6 +71,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
Expand Down Expand Up @@ -165,6 +169,8 @@ github.com/onsi/gomega v1.28.0 h1:i2rg/p9n/UqIDAMFUJ6qIUUMcsqOuUHgbpbu235Vr1c=
github.com/onsi/gomega v1.28.0/go.mod h1:A1H2JE76sI14WIP57LMKj7FVfCHx3g3BcZVjJG8bjX8=
github.com/orange-cloudfoundry/gsloc-go-sdk v0.5.0 h1:bEX3nbxvmcPWqFacoWCsX5yoNYiWyMx1ibDlZqc67rE=
github.com/orange-cloudfoundry/gsloc-go-sdk v0.5.0/go.mod h1:t7I60OATKiFc45BOEmKv/RRAKdD943uFwhWS1hu55sA=
github.com/orange-cloudfoundry/gsloc-go-sdk v0.6.0 h1:aL3QdTYzAFKerwOVD7qg2vqm5S2GA9AnhNHjqu2fpvM=
github.com/orange-cloudfoundry/gsloc-go-sdk v0.6.0/go.mod h1:t7I60OATKiFc45BOEmKv/RRAKdD943uFwhWS1hu55sA=
github.com/oschwald/geoip2-golang v1.9.0 h1:uvD3O6fXAXs+usU+UGExshpdP13GAqp4GBrzN7IgKZc=
github.com/oschwald/geoip2-golang v1.9.0/go.mod h1:BHK6TvDyATVQhKNbQBdrj9eAvuwOMi2zSFXizL3K81Y=
github.com/oschwald/maxminddb-golang v1.11.0 h1:aSXMqYR/EPNjGE8epgqwDay+P30hCBZIveY0WZbAWh0=
Expand Down Expand Up @@ -245,10 +251,13 @@ go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTV
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand All @@ -272,6 +281,7 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -300,9 +310,11 @@ golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d h1:VBu5YqKPv6XiJ199exd8Br+Aetz+o08F+PLMnwJQHAY=
Expand Down
5 changes: 5 additions & 0 deletions gslb/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func (s *Server) SetEntry(ctx context.Context, request *gslbsvc.SetEntryRequest)
return nil, status.Errorf(codes.InvalidArgument, "invalid request: %v", err)
}

err = s.validatePluginHealthCheck(request.GetHealthcheck())
if err != nil {
return nil, err
}

request.Entry.Fqdn = dns.CanonicalName(request.GetEntry().GetFqdn())

signedEntry := &entries.SignedEntry{
Expand Down
21 changes: 21 additions & 0 deletions gslb/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gslb
import (
"context"
"github.com/miekg/dns"
hcconf "github.com/orange-cloudfoundry/gsloc-go-sdk/gsloc/api/config/healthchecks/v1"
gslbsvc "github.com/orange-cloudfoundry/gsloc-go-sdk/gsloc/services/gslb/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -21,6 +22,11 @@ func (s *Server) SetHealthCheck(ctx context.Context, request *gslbsvc.SetHealthC
return nil, err
}

err = s.validatePluginHealthCheck(request.GetHealthcheck())
if err != nil {
return nil, err
}

signedEntry.Healthcheck = request.GetHealthcheck()
err = s.setSignedEntry(signedEntry)
if err != nil {
Expand Down Expand Up @@ -49,3 +55,18 @@ func (s *Server) GetHealthCheck(ctx context.Context, request *gslbsvc.GetHealthC
Healthcheck: signedEntry.Healthcheck,
}, nil
}

func (s *Server) validatePluginHealthCheck(healthcheck *hcconf.HealthCheck) error {
if healthcheck == nil {
return nil
}
if healthcheck.GetPluginHealthCheck() == nil {
return nil
}
for _, plugin := range s.hcPlugins {
if plugin.Name == healthcheck.GetPluginHealthCheck().GetName() {
return nil
}
}
return status.Errorf(codes.InvalidArgument, "plugin %s not found", healthcheck.GetPluginHealthCheck().GetName())
}
15 changes: 15 additions & 0 deletions gslb/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/samber/lo"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"sort"
)

Expand Down Expand Up @@ -59,3 +60,17 @@ func (s *Server) ListDcs(ctx context.Context, request *gslbsvc.ListDcsRequest) (
sort.Strings(dcs)
return &gslbsvc.ListDcsResponse{Dcs: dcs}, nil
}

func (s *Server) ListPluginHealthChecks(context.Context, *emptypb.Empty) (*gslbsvc.ListPluginHealthChecksResponse, error) {
infos := make([]*gslbsvc.PluginHealthCheckInfo, 0)
for _, plugin := range s.hcPlugins {
infos = append(infos, &gslbsvc.PluginHealthCheckInfo{
Name: plugin.Name,
Description: plugin.Description,
})
}

return &gslbsvc.ListPluginHealthChecksResponse{
PluginHealthChecks: infos,
}, nil
}
5 changes: 4 additions & 1 deletion gslb/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ package gslb
import (
consul "github.com/hashicorp/consul/api"
gslbsvc "github.com/orange-cloudfoundry/gsloc-go-sdk/gsloc/services/gslb/v1"
"github.com/orange-cloudfoundry/gsloc/config"
"github.com/orange-cloudfoundry/gsloc/disco"
)

type Server struct {
consulClient *consul.Client
gslocConsul *disco.GslocConsul
hcPlugins []*config.PluginHealthCheckConfig
gslbsvc.UnimplementedGSLBServer
}

func NewServer(consulClient *consul.Client, gslocConsul *disco.GslocConsul) (*Server, error) {
func NewServer(consulClient *consul.Client, gslocConsul *disco.GslocConsul, plugins []*config.PluginHealthCheckConfig) (*Server, error) {
s := &Server{
consulClient: consulClient,
gslocConsul: gslocConsul,
hcPlugins: plugins,
}
return s, nil
}
97 changes: 0 additions & 97 deletions healthchecks/grpc.go

This file was deleted.

Loading

0 comments on commit 440ff99

Please sign in to comment.