-
Notifications
You must be signed in to change notification settings - Fork 42
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
cnvm: Delete old snapshots on startup #3143
Changes from all commits
4eae248
9fb09be
a075e85
952133b
a122ec7
aadb8e1
08ecf74
e15f978
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,16 +19,23 @@ package vulnerability | |
|
||
import ( | ||
"context" | ||
"iter" | ||
"sync" | ||
"time" | ||
|
||
"github.com/elastic/cloudbeat/internal/infra/clog" | ||
"github.com/elastic/cloudbeat/internal/resources/providers/awslib/ec2" | ||
) | ||
|
||
const ( | ||
backgroundDeleteWorkers = 3 | ||
backgroundDeleteTimeout = 2 * 24 * time.Hour | ||
) | ||
|
||
type snapshotCreatorDeleter interface { | ||
CreateSnapshots(ctx context.Context, ins *ec2.Ec2Instance) ([]ec2.EBSSnapshot, error) | ||
DeleteSnapshot(ctx context.Context, snapshot ec2.EBSSnapshot) error | ||
IterOwnedSnapshots(ctx context.Context, before time.Time) iter.Seq[ec2.EBSSnapshot] | ||
} | ||
|
||
type SnapshotManager struct { | ||
|
@@ -64,7 +71,7 @@ func (s *SnapshotManager) CreateSnapshots(ctx context.Context, ins *ec2.Ec2Insta | |
|
||
func (s *SnapshotManager) DeleteSnapshot(ctx context.Context, snapshot ec2.EBSSnapshot) { | ||
runWithGrace(ctx, shutdownGracePeriod, func(ctx context.Context) { | ||
s.delete(ctx, snapshot) | ||
s.delete(ctx, snapshot, "DeleteSnapshot") | ||
}) | ||
|
||
s.lock.Lock() | ||
|
@@ -82,17 +89,45 @@ func (s *SnapshotManager) Cleanup(ctx context.Context) { | |
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
s.delete(ctx, snap) | ||
s.delete(ctx, snap, "Cleanup") | ||
}() | ||
} | ||
}) | ||
clear(s.snapshots) | ||
} | ||
|
||
func (s *SnapshotManager) delete(ctx context.Context, snapshot ec2.EBSSnapshot) { | ||
s.logger.Infof("VulnerabilityScanner.manager.DeleteSnapshot %s", snapshot.SnapshotId) | ||
func (s *SnapshotManager) DeleteOldSnapshots(ctx context.Context) { | ||
var wg sync.WaitGroup | ||
defer wg.Wait() | ||
|
||
ch := newContextualChan[ec2.EBSSnapshot]() | ||
defer ch.Close() | ||
|
||
wg.Add(backgroundDeleteWorkers) | ||
for range backgroundDeleteWorkers { | ||
go func() { | ||
defer wg.Done() | ||
for { | ||
snap, ok := ch.Read(ctx) | ||
if !ok { | ||
return | ||
} | ||
s.delete(ctx, snap, "DeleteOldSnapshots") | ||
} | ||
}() | ||
} | ||
for snapshot := range s.provider.IterOwnedSnapshots(ctx, time.Now().Add(-backgroundDeleteTimeout)) { | ||
if !ch.Write(ctx, snapshot) { | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (s *SnapshotManager) delete(ctx context.Context, snapshot ec2.EBSSnapshot, message string) { | ||
s.logger.Infof("VulnerabilityScanner.manager.%s %s", message, snapshot.SnapshotId) | ||
err := s.provider.DeleteSnapshot(ctx, snapshot) | ||
if err != nil { | ||
s.logger.Errorf("VulnerabilityScanner.manager.DeleteSnapshot %s error: %s", snapshot.SnapshotId, err) | ||
s.logger.Errorf("VulnerabilityScanner.manager.%s %s error: %s", message, snapshot.SnapshotId, err) | ||
} | ||
} | ||
|
||
|
@@ -109,3 +144,33 @@ func runWithGrace(ctx context.Context, grace time.Duration, f func(ctx context.C | |
defer stop() // if the callback finishes in time, stop the AfterFunc | ||
f(newCtx) // finally, call the actual callback! | ||
} | ||
|
||
type contextualChan[T any] struct { | ||
ch chan T | ||
} | ||
Comment on lines
+148
to
+150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ just sharing thoughts - not suggesting to change it ] eg instead of a wrapper just having something like that func SendToChannel[T any](ctx context.Context, ch chan<- T, t T) bool
func ReadFromChannel[T any](ctx context.Context, ch <-chan T) (T, bool) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to completely avoid exposing the channel to prevent misusing it. Both can be valid approaches if direct access to the channel is needed. |
||
|
||
func newContextualChan[T any]() contextualChan[T] { | ||
return contextualChan[T]{ch: make(chan T)} | ||
} | ||
|
||
func (s contextualChan[T]) Write(ctx context.Context, t T) bool { | ||
select { | ||
case <-ctx.Done(): | ||
return false | ||
case s.ch <- t: | ||
return true | ||
} | ||
} | ||
|
||
func (s contextualChan[T]) Read(ctx context.Context) (T, bool) { | ||
select { | ||
case t, ok := <-s.ch: | ||
return t, ok | ||
case <-ctx.Done(): | ||
return *new(T), false | ||
} | ||
} | ||
|
||
func (s contextualChan[T]) Close() { | ||
close(s.ch) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are already querying already the snapshots here:
Do we need this piece of filtering?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to filter for time anyway so the name check is there just as a sanity check.