zero/bundle-download: update metadata (#5212)

* zero/download: refresh metadata

* fix cmp
This commit is contained in:
Denis Mishin 2024-08-22 16:18:17 -04:00 committed by GitHub
parent 0503b41108
commit 99d7a73cef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 143 additions and 13 deletions

View file

@ -20,6 +20,7 @@ import (
"github.com/pomerium/pomerium/internal/log"
"github.com/pomerium/pomerium/internal/retry"
zero "github.com/pomerium/pomerium/internal/zero/api"
"github.com/pomerium/pomerium/pkg/grpc/databroker"
)
@ -140,17 +141,27 @@ func (c *service) syncBundle(ctx context.Context, key string) error {
return fmt.Errorf("download bundle: %w", err)
}
if result.NotModified {
log.Ctx(ctx).Debug().Str("bundle", key).Msg("bundle not changed")
if result.ContentUpdated {
return c.syncUpdatedBundle(ctx, key, cached, result, fd)
}
if !result.MetadataUpdated {
log.Ctx(ctx).Debug().Str("id", key).Msg("bundle not updated")
return nil
}
if cached == nil {
return fmt.Errorf("invalid state: bundle metadata updated but no cached entry")
}
return c.getUpdatedMetadata(ctx, key, *cached, result)
}
func (c *service) syncUpdatedBundle(ctx context.Context, key string, cached *BundleCacheEntry, result *zero.DownloadResult, fd ReadWriteSeekCloser) error {
log.Ctx(ctx).Debug().Str("bundle", key).
Interface("cached-entry", cached).
Interface("current-entry", result.DownloadConditional).
Msg("bundle updated")
_, err = fd.Seek(0, io.SeekStart)
_, err := fd.Seek(0, io.SeekStart)
if err != nil {
return fmt.Errorf("seek to start: %w", err)
}
@ -184,6 +195,41 @@ func (c *service) syncBundle(ctx context.Context, key string) error {
return nil
}
func (c *service) getUpdatedMetadata(ctx context.Context, key string, cached BundleCacheEntry, result *zero.DownloadResult) error {
log.Ctx(ctx).Debug().Str("bundle", key).
Interface("cached-entry", cached).
Interface("current-entry", result.DownloadConditional).
Msg("bundle metadata updated")
result, err := c.config.api.HeadClusterResourceBundle(ctx, key, cached.ETag)
if err != nil {
return fmt.Errorf("get bundle metadata: %w", err)
}
current := BundleCacheEntry{
DownloadConditional: *result.DownloadConditional,
RecordTypes: cached.GetRecordTypes(),
}
log.Ctx(ctx).Debug().
Str("bundle", key).
Strs("record_types", current.RecordTypes).
Str("etag", current.ETag).
Str("last_modified", current.LastModified).
Interface("metadata", result.Metadata).
Msg("metadata updated")
err = c.SetBundleCacheEntry(ctx, key, current)
if err != nil {
err = fmt.Errorf("set bundle cache entry: %w", err)
c.ReportBundleAppliedFailure(key, fmt.Errorf("set bundle cache entry: %w", err))
return err
}
c.ReportBundleAppliedSuccess(key, result.Metadata)
return nil
}
func strUnion(a, b []string) []string {
m := make(map[string]struct{}, len(a)+len(b))
for _, s := range a {