Skip to content

Commit

Permalink
address PR feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Dmytrenko <[email protected]>
  • Loading branch information
erka committed Jul 18, 2024
1 parent 57854e9 commit 460abc9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/storage/fs/git/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
2 changes: 1 addition & 1 deletion internal/storage/fs/oci/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
25 changes: 23 additions & 2 deletions internal/storage/fs/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 460abc9

Please sign in to comment.