From 460abc97f18fe527205461e53687fa2e65e9961f Mon Sep 17 00:00:00 2001 From: Roman Dmytrenko Date: Fri, 19 Jul 2024 00:09:28 +0300 Subject: [PATCH] address PR feedback Signed-off-by: Roman Dmytrenko --- internal/storage/fs/git/store.go | 2 +- internal/storage/fs/oci/store.go | 2 +- internal/storage/fs/snapshot.go | 25 +++++++++++++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/internal/storage/fs/git/store.go b/internal/storage/fs/git/store.go index a00901c3cf..890038e4d4 100644 --- a/internal/storage/fs/git/store.go +++ b/internal/storage/fs/git/store.go @@ -355,5 +355,5 @@ func (s *SnapshotStore) buildSnapshot(ctx context.Context, hash plumbing.Hash) ( } } - return storagefs.SnapshotFromFS(s.logger, gfs) + return storagefs.SnapshotFromFS(s.logger, gfs, storagefs.WithEtag(hash.String())) } diff --git a/internal/storage/fs/oci/store.go b/internal/storage/fs/oci/store.go index 7c797fd7fe..12f1c56377 100644 --- a/internal/storage/fs/oci/store.go +++ b/internal/storage/fs/oci/store.go @@ -89,7 +89,7 @@ func (s *SnapshotStore) update(ctx context.Context) (bool, error) { return false, nil } - snap, err := storagefs.SnapshotFromFiles(s.logger, resp.Files) + snap, err := storagefs.SnapshotFromFiles(s.logger, resp.Files, storagefs.WithEtag(resp.Digest.Hex())) if err != nil { return false, err } diff --git a/internal/storage/fs/snapshot.go b/internal/storage/fs/snapshot.go index c2acc6319c..a73e5819f5 100644 --- a/internal/storage/fs/snapshot.go +++ b/internal/storage/fs/snapshot.go @@ -68,14 +68,31 @@ func newNamespace(key, name string, created *timestamppb.Timestamp) *namespace { type SnapshotOption struct { validatorOption []validation.FeaturesValidatorOption + etagFn EtagFn } +type EtagFn func(stat fs.FileInfo) string + func WithValidatorOption(opts ...validation.FeaturesValidatorOption) containers.Option[SnapshotOption] { return func(so *SnapshotOption) { so.validatorOption = opts } } +func WithEtag(etag string) containers.Option[SnapshotOption] { + return func(so *SnapshotOption) { + so.etagFn = func(stat fs.FileInfo) string { return etag } + } +} + +func WithFileInfoEtag() containers.Option[SnapshotOption] { + return func(so *SnapshotOption) { + so.etagFn = func(stat fs.FileInfo) string { + return fmt.Sprintf("%x-%x", stat.ModTime().Unix(), stat.Size()) + } + } +} + // SnapshotFromFS is a convenience function for building a snapshot // directly from an implementation of fs.FS using the list state files // function to source the relevant Flipt configuration files. @@ -119,6 +136,7 @@ func SnapshotFromFiles(logger *zap.Logger, files []fs.File, opts ...containers.O } var so SnapshotOption + containers.ApplyAll(&so, WithFileInfoEtag()) containers.ApplyAll(&so, opts...) for _, fi := range files { @@ -162,7 +180,9 @@ func WalkDocuments(logger *zap.Logger, src fs.FS, fn func(*ext.Document) error) } defer fi.Close() - docs, err := documentsFromFile(fi, SnapshotOption{}) + var so SnapshotOption + containers.ApplyAll(&so, WithFileInfoEtag()) + docs, err := documentsFromFile(fi, so) if err != nil { return err } @@ -227,7 +247,8 @@ func documentsFromFile(fi fs.File, opts SnapshotOption) ([]*ext.Document, error) if doc.Namespace == "" { doc.Namespace = "default" } - doc.Etag = fmt.Sprintf("%x-%x", stat.ModTime().Unix(), stat.Size()) + + doc.Etag = opts.etagFn(stat) docs = append(docs, doc) }