-
Notifications
You must be signed in to change notification settings - Fork 106
feat: Support blob type sources and GCS as an example of such source. #1366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
598a4c1
Add support for blob type sources.
alan-kut 0c7412a
Support for GCS source using Blob sync.
alan-kut 15d4070
fix(deps): update module connectrpc.com/otelconnect to v0.7.1 (#1367)
renovate[bot] 224a656
Fix lint and data race
alan-kut 186822e
Update docs with the information about GCS sync source
alan-kut 18952d8
Simplify the blob sync by using bucket.Download and validate bucket and
alan-kut 75c2ce9
Fix log message
alan-kut b03d802
Merge branch 'main' into support-blob
alan-kut 7d2f912
Merge branch 'main' into support-blob
beeme1mr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package blob | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/open-feature/flagd/core/pkg/logger" | ||
| "github.com/open-feature/flagd/core/pkg/sync" | ||
| "gocloud.dev/blob" | ||
| _ "gocloud.dev/blob/gcsblob" // needed to initialize GCS driver | ||
| ) | ||
|
|
||
| type Sync struct { | ||
| Bucket string | ||
| Object string | ||
| BlobURLMux *blob.URLMux | ||
| Cron Cron | ||
| Logger *logger.Logger | ||
| Interval uint32 | ||
| ready bool | ||
| lastUpdated time.Time | ||
| } | ||
|
|
||
| // Cron defines the behaviour required of a cron | ||
| type Cron interface { | ||
| AddFunc(spec string, cmd func()) error | ||
| Start() | ||
| Stop() | ||
| } | ||
|
|
||
| func (hs *Sync) Init(_ context.Context) error { | ||
| if hs.Bucket == "" { | ||
| return errors.New("no bucket string set") | ||
| } | ||
| if hs.Object == "" { | ||
| return errors.New("no object string set") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (hs *Sync) IsReady() bool { | ||
| return hs.ready | ||
| } | ||
|
|
||
| func (hs *Sync) Sync(ctx context.Context, dataSync chan<- sync.DataSync) error { | ||
| hs.Logger.Info(fmt.Sprintf("starting sync from %s/%s with interval %ds", hs.Bucket, hs.Object, hs.Interval)) | ||
| _ = hs.Cron.AddFunc(fmt.Sprintf("*/%d * * * *", hs.Interval), func() { | ||
| err := hs.sync(ctx, dataSync, false) | ||
| if err != nil { | ||
| hs.Logger.Warn(fmt.Sprintf("sync failed: %v", err)) | ||
| } | ||
| }) | ||
| // Initial fetch | ||
| hs.Logger.Debug(fmt.Sprintf("initial sync of the %s/%s", hs.Bucket, hs.Object)) | ||
| err := hs.sync(ctx, dataSync, false) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| hs.ready = true | ||
| hs.Cron.Start() | ||
| <-ctx.Done() | ||
| hs.Cron.Stop() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (hs *Sync) ReSync(ctx context.Context, dataSync chan<- sync.DataSync) error { | ||
| return hs.sync(ctx, dataSync, true) | ||
| } | ||
|
|
||
| func (hs *Sync) sync(ctx context.Context, dataSync chan<- sync.DataSync, skipCheckingModTime bool) error { | ||
| bucket, err := hs.getBucket(ctx) | ||
alan-kut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return fmt.Errorf("couldn't get bucket: %v", err) | ||
| } | ||
| defer bucket.Close() | ||
| var updated time.Time | ||
| if !skipCheckingModTime { | ||
| updated, err = hs.fetchObjectModificationTime(ctx, bucket) | ||
| if err != nil { | ||
| return fmt.Errorf("couldn't get object attributes: %v", err) | ||
| } | ||
| if hs.lastUpdated == updated { | ||
alan-kut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| hs.Logger.Debug("configuration hasn't changed, skipping fetching full object") | ||
| return nil | ||
| } | ||
| if hs.lastUpdated.After(updated) { | ||
| hs.Logger.Warn("configuration changed but the modification time decreased instead of increasing") | ||
| } | ||
| } | ||
| msg, err := hs.fetchObject(ctx, bucket) | ||
| if err != nil { | ||
| return fmt.Errorf("couldn't get object: %v", err) | ||
| } | ||
| hs.Logger.Debug(fmt.Sprintf("configuration updated: %s", msg)) | ||
| if !skipCheckingModTime { | ||
| hs.lastUpdated = updated | ||
| } | ||
| dataSync <- sync.DataSync{FlagData: msg, Source: hs.Bucket + hs.Object, Type: sync.ALL} | ||
| return nil | ||
| } | ||
|
|
||
| func (hs *Sync) getBucket(ctx context.Context) (*blob.Bucket, error) { | ||
| b, err := hs.BlobURLMux.OpenBucket(ctx, hs.Bucket) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error opening bucket %s: %v", hs.Bucket, err) | ||
| } | ||
| return b, nil | ||
| } | ||
|
|
||
| func (hs *Sync) fetchObjectModificationTime(ctx context.Context, bucket *blob.Bucket) (time.Time, error) { | ||
| if hs.Object == "" { | ||
| return time.Time{}, errors.New("no object string set") | ||
| } | ||
| attrs, err := bucket.Attributes(ctx, hs.Object) | ||
| if err != nil { | ||
| return time.Time{}, fmt.Errorf("error fetching attributes for object %s/%s: %w", hs.Bucket, hs.Object, err) | ||
| } | ||
| return attrs.ModTime, nil | ||
| } | ||
|
|
||
| func (hs *Sync) fetchObject(ctx context.Context, bucket *blob.Bucket) (string, error) { | ||
alan-kut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| buf := bytes.NewBuffer(nil) | ||
| err := bucket.Download(ctx, hs.Object, buf, nil) | ||
| if err != nil { | ||
| return "", fmt.Errorf("error downloading object %s/%s: %w", hs.Bucket, hs.Object, err) | ||
| } | ||
|
|
||
| return buf.String(), nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.