pomerium/internal/zero/healthcheck/checks.go
Denis Mishin 0503b41108
zero/connect: add re-run health checks command (#5219)
* zero/connect: add run health checks and shutdown commands

* fix proto

* trigger re-run on command

* add handler

* rename runPeriodicHealthChecksLeased
2024-08-22 16:17:53 -04:00

58 lines
1.3 KiB
Go

package healthcheck
import (
"context"
"fmt"
"sync/atomic"
"golang.org/x/sync/errgroup"
"github.com/pomerium/pomerium/config"
configpb "github.com/pomerium/pomerium/pkg/grpc/config"
"github.com/pomerium/pomerium/pkg/grpc/databroker"
)
type Checker struct {
bootstrap config.Source
databrokerClient databroker.DataBrokerServiceClient
forceCheck chan struct{}
configs atomic.Value
}
func NewChecker(
bootstrap config.Source,
databrokerClient databroker.DataBrokerServiceClient,
) *Checker {
c := &Checker{
bootstrap: bootstrap,
databrokerClient: databrokerClient,
forceCheck: make(chan struct{}, 1),
}
return c
}
func (c *Checker) Run(ctx context.Context) error {
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error { c.Scheduler(ctx); return nil })
eg.Go(func() error { return c.ConfigSyncer(ctx) })
return eg.Wait()
}
func (c *Checker) ForceCheck() {
select {
case c.forceCheck <- struct{}{}:
default:
}
}
func getConfig(records []*databroker.Record) ([]*configpb.Config, error) {
var cfgs []*configpb.Config
for _, record := range records {
cfg := new(configpb.Config)
if err := record.Data.UnmarshalTo(cfg); err != nil {
return nil, fmt.Errorf("error unmarshalling config: %w", err)
}
cfgs = append(cfgs, cfg)
}
return cfgs, nil
}