Skip to content

Commit 19ddf38

Browse files
committed
Add defaults
1 parent b577a4b commit 19ddf38

File tree

9 files changed

+37
-17
lines changed

9 files changed

+37
-17
lines changed

packages/api/internal/handlers/sandboxes_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (a *APIStore) GetSandboxes(c *gin.Context, params api.GetSandboxesParams) {
8686
return
8787
}
8888

89-
sandboxes := a.orchestrator.GetSandboxes(ctx, sandbox.WithTeamID(team.ID), sandbox.WithState(sandbox.StateRunning), sandbox.WithIsExpired(false))
89+
sandboxes := a.orchestrator.GetSandboxes(ctx, sandbox.WithTeamID(team.ID), sandbox.WithState(sandbox.StateRunning))
9090
runningSandboxes := getRunningSandboxes(sandboxes, metadataFilter)
9191

9292
// Sort sandboxes by start time descending

packages/api/internal/metrics/team.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (so *TeamObserver) Start(cache sandbox.Store) (err error) {
7777
// Register callbacks for team sandbox metrics
7878
so.registration, err = so.meter.RegisterCallback(
7979
func(ctx context.Context, obs metric.Observer) error {
80-
sbxs := cache.Items(sandbox.WithState(sandbox.StateRunning), sandbox.WithIsExpired(false))
80+
sbxs := cache.Items(sandbox.WithAllTeams())
8181
sbxsPerTeam := make(map[string]int64)
8282
for _, sbx := range sbxs {
8383
teamID := sbx.TeamID.String()

packages/api/internal/orchestrator/admin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (o *Orchestrator) AdminNodes() []*api.Node {
3939
}
4040
}
4141

42-
for _, sbx := range o.sandboxStore.Items(sandbox.WithState(sandbox.StateRunning), sandbox.WithIsExpired(false)) {
42+
for _, sbx := range o.sandboxStore.Items(sandbox.WithAllTeams()) {
4343
n, ok := apiNodes[sbx.NodeID]
4444
if !ok {
4545
zap.L().Error("node for sandbox wasn't found", logger.WithNodeID(sbx.NodeID), logger.WithSandboxID(sbx.SandboxID))
@@ -84,7 +84,7 @@ func (o *Orchestrator) AdminNodeDetail(clusterID uuid.UUID, nodeIDOrNomadNodeSho
8484
Metrics: metrics,
8585
}
8686

87-
for _, sbx := range o.sandboxStore.Items(sandbox.WithState(sandbox.StateRunning), sandbox.WithIsExpired(false)) {
87+
for _, sbx := range o.sandboxStore.Items(sandbox.WithAllTeams()) {
8888
if sbx.NodeID == n.ID && sbx.ClusterID == n.ClusterID {
8989
var metadata *api.SandboxMetadata
9090
if sbx.Metadata != nil {

packages/api/internal/orchestrator/analytics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (o *Orchestrator) reportLongRunningSandboxes(ctx context.Context) {
3535
zap.L().Info("Stopping node analytics reporting due to context cancellation")
3636
return
3737
case <-ticker.C:
38-
sandboxes := o.sandboxStore.Items(sandbox.WithState(sandbox.StateRunning), sandbox.WithIsExpired(false))
38+
sandboxes := o.sandboxStore.Items(sandbox.WithState(sandbox.StateRunning), sandbox.WithAllTeams())
3939
longRunningSandboxes := make([]sandbox.Sandbox, 0, len(sandboxes))
4040
for _, sandbox := range sandboxes {
4141
if time.Since(sandbox.StartTime) > oldSandboxThreshold {

packages/api/internal/orchestrator/evictor/evict.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (e *Evictor) Start(ctx context.Context) {
3131
case <-ctx.Done():
3232
return
3333
case <-time.After(50 * time.Millisecond):
34-
for _, item := range e.store.Items(sandbox.WithState(sandbox.StateRunning), sandbox.WithIsExpired(true)) {
34+
for _, item := range e.store.Items(sandbox.WithAllTeams(), sandbox.WithOnlyExpired(true)) {
3535
go func() {
3636
stateAction := sandbox.StateActionKill
3737
if item.AutoPause {

packages/api/internal/orchestrator/orchestrator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (o *Orchestrator) startStatusLogging(ctx context.Context) {
198198
}
199199

200200
zap.L().Info("API internal status",
201-
zap.Int("sandboxes_count", len(o.sandboxStore.Items(sandbox.WithState(sandbox.StateRunning), sandbox.WithIsExpired(false)))),
201+
zap.Int("sandboxes_count", len(o.sandboxStore.Items(sandbox.WithAllTeams()))),
202202
zap.Int("nodes_count", o.nodes.Count()),
203203
zap.Any("nodes", connectedNodes),
204204
)

packages/api/internal/sandbox/store.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,18 @@ type (
1212
)
1313

1414
type ItemsFilter struct {
15-
TeamID *uuid.UUID
16-
States *[]State
17-
IsExpired *bool
15+
TeamID *uuid.UUID
16+
States []State
17+
OnlyExpired bool
18+
}
19+
20+
func NewItemsFilter() *ItemsFilter {
21+
// Defaults to prevent accidental full scans
22+
return &ItemsFilter{
23+
States: nil,
24+
TeamID: &uuid.Nil,
25+
OnlyExpired: false,
26+
}
1827
}
1928

2029
type Store interface {
@@ -36,20 +45,31 @@ func WithTeamID(teamID uuid.UUID) ItemsOption {
3645
}
3746
}
3847

48+
func WithAllTeams() ItemsOption {
49+
return func(f *ItemsFilter) {
50+
f.TeamID = nil
51+
}
52+
}
53+
3954
func WithState(state State) ItemsOption {
4055
return func(f *ItemsFilter) {
41-
f.States = &[]State{state}
56+
f.States = []State{state}
4257
}
4358
}
4459

4560
func WithStates(states ...State) ItemsOption {
4661
return func(f *ItemsFilter) {
47-
f.States = &states
62+
if len(states) == 0 {
63+
f.States = nil
64+
return
65+
}
66+
67+
f.States = states
4868
}
4969
}
5070

51-
func WithIsExpired(isExpired bool) ItemsOption {
71+
func WithOnlyExpired(isExpired bool) ItemsOption {
5272
return func(f *ItemsFilter) {
53-
f.IsExpired = &isExpired
73+
f.OnlyExpired = isExpired
5474
}
5575
}

packages/api/internal/sandbox/store/memory/filters.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ func applyFilter(sbx sandbox.Sandbox, filter *sandbox.ItemsFilter) bool {
1111
if filter.TeamID != nil && sbx.TeamID != *filter.TeamID {
1212
return false
1313
}
14-
if filter.States != nil && !slices.Contains(*filter.States, sbx.State) {
14+
if filter.States != nil && !slices.Contains(filter.States, sbx.State) {
1515
return false
1616
}
17-
if filter.IsExpired != nil && sbx.IsExpired() != *filter.IsExpired {
17+
if sbx.IsExpired() != filter.OnlyExpired {
1818
return false
1919
}
2020
return true

packages/api/internal/sandbox/store/memory/operations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (s *Store) Remove(sandboxID string) {
7979
}
8080

8181
func (s *Store) Items(options ...sandbox.ItemsOption) []sandbox.Sandbox {
82-
filter := &sandbox.ItemsFilter{}
82+
filter := sandbox.NewItemsFilter()
8383
for _, opt := range options {
8484
opt(filter)
8585
}

0 commit comments

Comments
 (0)