zero: restart config reconciliation when databroker storage is changed (#4623)

This commit is contained in:
Denis Mishin 2023-10-12 11:19:57 -04:00 committed by Kenneth Jenkins
parent 60ab9dafbe
commit 0e1061d813
5 changed files with 231 additions and 10 deletions

View file

@ -7,12 +7,14 @@ package reconciler
import (
"context"
"fmt"
"time"
"golang.org/x/sync/errgroup"
"golang.org/x/time/rate"
"github.com/pomerium/pomerium/internal/atomicutil"
"github.com/pomerium/pomerium/pkg/grpc/databroker"
connect_mux "github.com/pomerium/zero-sdk/connect-mux"
)
@ -40,6 +42,11 @@ func Run(ctx context.Context, opts ...Option) error {
}
c.periodicUpdateInterval.Store(config.checkForUpdateIntervalWhenDisconnected)
return c.runMainLoop(ctx)
}
// RunLeased implements the databroker.LeaseHandler interface
func (c *service) RunLeased(ctx context.Context) error {
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error { return c.watchUpdates(ctx) })
eg.Go(func() error { return c.SyncLoop(ctx) })
@ -47,6 +54,44 @@ func Run(ctx context.Context, opts ...Option) error {
return eg.Wait()
}
// GetDataBrokerServiceClient implements the databroker.LeaseHandler interface.
func (c *service) GetDataBrokerServiceClient() databroker.DataBrokerServiceClient {
return c.config.databrokerClient
}
func (c *service) runMainLoop(ctx context.Context) error {
leaser := databroker.NewLeaser("zero-reconciler", time.Second*30, c)
return RunWithRestart(ctx, func(ctx context.Context) error {
return leaser.Run(ctx)
}, c.databrokerChangeMonitor)
}
// databrokerChangeMonitor runs infinite sync loop to see if there is any change in databroker
func (c *service) databrokerChangeMonitor(ctx context.Context) error {
_, recordVersion, serverVersion, err := databroker.InitialSync(ctx, c.GetDataBrokerServiceClient(), &databroker.SyncLatestRequest{
Type: BundleCacheEntryRecordType,
})
if err != nil {
return fmt.Errorf("error during initial sync: %w", err)
}
stream, err := c.GetDataBrokerServiceClient().Sync(ctx, &databroker.SyncRequest{
Type: BundleCacheEntryRecordType,
ServerVersion: serverVersion,
RecordVersion: recordVersion,
})
if err != nil {
return fmt.Errorf("error calling sync: %w", err)
}
for {
_, err := stream.Recv()
if err != nil {
return fmt.Errorf("error receiving record: %w", err)
}
}
}
// run is a main control loop.
// it is very simple and sequential download and reconcile.
// it may be later optimized by splitting between download and reconciliation process,