-
Notifications
You must be signed in to change notification settings - Fork 5
Auto-standby #183
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
Auto-standby #183
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
cb66913
Add Linux auto-standby controller and e2e coverage
sjmiller609 14cff19
Merge branch 'main' into codex/auto-standby-e2e
sjmiller609 d2892cb
Expose auto-standby policy in Stainless config
sjmiller609 d301651
Address auto-standby review feedback
sjmiller609 875dd25
Default-skip compression standby integration test
sjmiller609 4043a13
Clarify auto-standby update metadata handling
sjmiller609 5c1df95
Wire auto-standby controller into API app
sjmiller609 789a08c
Make auto-standby event-driven and observable
sjmiller609 16e6c20
Add periodic auto-standby snapshot sync
sjmiller609 767b8a2
Fix auto-standby review follow-ups
sjmiller609 73758cd
Format auto-standby files for CI
sjmiller609 e9bf333
Add scope for auto-standby status route
sjmiller609 53f2017
Consolidate instance lifecycle subscriptions
sjmiller609 134628d
Add config for lifecycle event buffer size
sjmiller609 165502c
Merge consolidate instance lifecycle bus
sjmiller609 91fa483
Merge remote-tracking branch 'origin/main' into codex/auto-standby-e2e
sjmiller609 c81e780
Merge remote-tracking branch 'origin/main' into codex/auto-standby-e2e
sjmiller609 9e480be
Fix Linux conntrack event test encoding
sjmiller609 ed02ddb
Clear stale auto-standby runtime on ineligible updates
sjmiller609 6f3c229
Preserve auto-standby runtime on lifecycle events
sjmiller609 76caba7
Clone snapshot policy in metadata copies
sjmiller609 896ee1a
Harden auto-standby startup and reconnects
sjmiller609 f9800fe
Add auto-standby timer and lifecycle tests
sjmiller609 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/kernel/hypeman/lib/autostandby" | ||
| "github.com/kernel/hypeman/lib/oapi" | ||
| "github.com/samber/lo" | ||
| ) | ||
|
|
||
| func toDomainAutoStandbyPolicy(policy *oapi.AutoStandbyPolicy) (*autostandby.Policy, error) { | ||
| if policy == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| out := &autostandby.Policy{} | ||
| if policy.Enabled != nil { | ||
| out.Enabled = *policy.Enabled | ||
| } | ||
| if policy.IdleTimeout != nil { | ||
| out.IdleTimeout = *policy.IdleTimeout | ||
| } | ||
| if policy.IgnoreSourceCidrs != nil { | ||
| out.IgnoreSourceCIDRs = append([]string(nil), (*policy.IgnoreSourceCidrs)...) | ||
| } | ||
| if policy.IgnoreDestinationPorts != nil { | ||
| out.IgnoreDestinationPorts = make([]uint16, 0, len(*policy.IgnoreDestinationPorts)) | ||
| for _, port := range *policy.IgnoreDestinationPorts { | ||
| if port < 1 || port > 65535 { | ||
| return nil, fmt.Errorf("auto_standby.ignore_destination_ports must be between 1 and 65535") | ||
| } | ||
| out.IgnoreDestinationPorts = append(out.IgnoreDestinationPorts, uint16(port)) | ||
| } | ||
| } | ||
|
|
||
| return out, nil | ||
| } | ||
|
|
||
| func toOAPIAutoStandbyPolicy(policy *autostandby.Policy) *oapi.AutoStandbyPolicy { | ||
| if policy == nil { | ||
| return nil | ||
| } | ||
|
|
||
| out := &oapi.AutoStandbyPolicy{ | ||
| Enabled: lo.ToPtr(policy.Enabled), | ||
| } | ||
| if policy.IdleTimeout != "" { | ||
| out.IdleTimeout = lo.ToPtr(policy.IdleTimeout) | ||
| } | ||
| if len(policy.IgnoreSourceCIDRs) > 0 { | ||
| out.IgnoreSourceCidrs = lo.ToPtr(append([]string(nil), policy.IgnoreSourceCIDRs...)) | ||
| } | ||
| if len(policy.IgnoreDestinationPorts) > 0 { | ||
| ports := make([]int, 0, len(policy.IgnoreDestinationPorts)) | ||
| for _, port := range policy.IgnoreDestinationPorts { | ||
| ports = append(ports, int(port)) | ||
| } | ||
| out.IgnoreDestinationPorts = &ports | ||
| } | ||
|
|
||
| return out | ||
| } | ||
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,79 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/kernel/hypeman/lib/autostandby" | ||
| "github.com/kernel/hypeman/lib/instances" | ||
| "github.com/kernel/hypeman/lib/logger" | ||
| "github.com/kernel/hypeman/lib/oapi" | ||
| "github.com/samber/lo" | ||
| ) | ||
|
|
||
| func (s *ApiService) GetAutoStandbyStatus(ctx context.Context, request oapi.GetAutoStandbyStatusRequestObject) (oapi.GetAutoStandbyStatusResponseObject, error) { | ||
| log := logger.FromContext(ctx) | ||
|
|
||
| inst, err := s.InstanceManager.GetInstance(ctx, request.Id) | ||
| if err != nil { | ||
| if err == instances.ErrNotFound || err == instances.ErrAmbiguousName { | ||
| return oapi.GetAutoStandbyStatus404JSONResponse{ | ||
| Code: "not_found", | ||
| Message: "instance not found", | ||
| }, nil | ||
| } | ||
| log.ErrorContext(ctx, "failed to resolve instance for auto-standby status", "instance_id", request.Id, "error", err) | ||
| return oapi.GetAutoStandbyStatus500JSONResponse{ | ||
| Code: "internal_error", | ||
| Message: "failed to load instance", | ||
| }, nil | ||
| } | ||
|
|
||
| snapshot := autostandby.StatusSnapshot{ | ||
| Supported: false, | ||
| Configured: inst.AutoStandby != nil, | ||
| Enabled: inst.AutoStandby != nil && inst.AutoStandby.Enabled, | ||
| TrackingMode: "conntrack_events_v4_tcp", | ||
| Status: autostandby.StatusUnsupported, | ||
| Reason: autostandby.ReasonUnsupportedPlatform, | ||
| } | ||
| if s.AutoStandbyController != nil { | ||
| snapshot = s.AutoStandbyController.Describe(instanceToAutoStandby(*inst)) | ||
| } | ||
|
|
||
| return oapi.GetAutoStandbyStatus200JSONResponse(toOAPIAutoStandbyStatus(snapshot)), nil | ||
| } | ||
|
|
||
| func instanceToAutoStandby(inst instances.Instance) autostandby.Instance { | ||
| return autostandby.Instance{ | ||
| ID: inst.Id, | ||
| Name: inst.Name, | ||
| State: string(inst.State), | ||
| NetworkEnabled: inst.NetworkEnabled, | ||
| IP: inst.IP, | ||
| HasVGPU: inst.GPUProfile != "" || inst.GPUMdevUUID != "", | ||
| AutoStandby: inst.AutoStandby, | ||
| } | ||
| } | ||
|
|
||
| func toOAPIAutoStandbyStatus(status autostandby.StatusSnapshot) oapi.AutoStandbyStatus { | ||
| out := oapi.AutoStandbyStatus{ | ||
| ActiveInboundConnections: status.ActiveInboundCount, | ||
| Configured: status.Configured, | ||
| Eligible: status.Eligible, | ||
| Enabled: status.Enabled, | ||
| Reason: oapi.AutoStandbyStatusReason(status.Reason), | ||
| Status: oapi.AutoStandbyStatusStatus(status.Status), | ||
| Supported: status.Supported, | ||
| TrackingMode: status.TrackingMode, | ||
| } | ||
| if status.IdleTimeout != "" { | ||
| out.IdleTimeout = lo.ToPtr(status.IdleTimeout) | ||
| } | ||
| out.IdleSince = status.IdleSince | ||
| out.LastInboundActivityAt = status.LastInboundActivityAt | ||
| out.NextStandbyAt = status.NextStandbyAt | ||
| if status.CountdownRemaining != nil { | ||
| out.CountdownRemaining = lo.ToPtr(status.CountdownRemaining.String()) | ||
| } | ||
| return out | ||
| } |
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,170 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/netip" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/kernel/hypeman/lib/autostandby" | ||
| "github.com/kernel/hypeman/lib/instances" | ||
| "github.com/kernel/hypeman/lib/oapi" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type captureStatusManager struct { | ||
| instances.Manager | ||
| instance *instances.Instance | ||
| err error | ||
| } | ||
|
|
||
| func (m *captureStatusManager) GetInstance(context.Context, string) (*instances.Instance, error) { | ||
| if m.err != nil { | ||
| return nil, m.err | ||
| } | ||
| return m.instance, nil | ||
| } | ||
|
|
||
| type statusStore struct { | ||
| instances []autostandby.Instance | ||
| runtime map[string]*autostandby.Runtime | ||
| events chan autostandby.InstanceEvent | ||
| } | ||
|
|
||
| func (s *statusStore) ListInstances(context.Context) ([]autostandby.Instance, error) { | ||
| return append([]autostandby.Instance(nil), s.instances...), nil | ||
| } | ||
|
|
||
| func (s *statusStore) StandbyInstance(context.Context, string) error { return nil } | ||
|
|
||
| func (s *statusStore) SetRuntime(_ context.Context, id string, runtime *autostandby.Runtime) error { | ||
| if s.runtime == nil { | ||
| s.runtime = make(map[string]*autostandby.Runtime) | ||
| } | ||
| s.runtime[id] = runtime | ||
| return nil | ||
| } | ||
|
|
||
| func (s *statusStore) SubscribeInstanceEvents() (<-chan autostandby.InstanceEvent, func(), error) { | ||
| if s.events == nil { | ||
| s.events = make(chan autostandby.InstanceEvent) | ||
| } | ||
| return s.events, func() {}, nil | ||
| } | ||
|
|
||
| type statusConnectionSource struct { | ||
| connections []autostandby.Connection | ||
| } | ||
|
|
||
| func (s *statusConnectionSource) ListConnections(context.Context) ([]autostandby.Connection, error) { | ||
| return append([]autostandby.Connection(nil), s.connections...), nil | ||
| } | ||
|
|
||
| func (s *statusConnectionSource) OpenStream(context.Context) (autostandby.ConnectionStream, error) { | ||
| return &statusConnectionStream{ | ||
| events: make(chan autostandby.ConnectionEvent), | ||
| errs: make(chan error), | ||
| }, nil | ||
| } | ||
|
|
||
| type statusConnectionStream struct { | ||
| events chan autostandby.ConnectionEvent | ||
| errs chan error | ||
| } | ||
|
|
||
| func (s *statusConnectionStream) Events() <-chan autostandby.ConnectionEvent { return s.events } | ||
|
|
||
| func (s *statusConnectionStream) Errors() <-chan error { return s.errs } | ||
|
|
||
| func (s *statusConnectionStream) Close() error { return nil } | ||
|
|
||
| func TestGetAutoStandbyStatusUnsupportedWithoutController(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| base := newTestService(t) | ||
| base.InstanceManager = &captureStatusManager{ | ||
| Manager: base.InstanceManager, | ||
| instance: &instances.Instance{ | ||
| StoredMetadata: instances.StoredMetadata{ | ||
| Id: "inst-1", | ||
| Name: "inst-1", | ||
| NetworkEnabled: true, | ||
| IP: "192.168.100.10", | ||
| AutoStandby: &autostandby.Policy{Enabled: true, IdleTimeout: "5m"}, | ||
| }, | ||
| State: instances.StateRunning, | ||
| }, | ||
| } | ||
|
|
||
| resp, err := base.GetAutoStandbyStatus(ctx(), oapi.GetAutoStandbyStatusRequestObject{Id: "inst-1"}) | ||
| require.NoError(t, err) | ||
|
|
||
| statusResp, ok := resp.(oapi.GetAutoStandbyStatus200JSONResponse) | ||
| require.True(t, ok) | ||
| assert.False(t, statusResp.Supported) | ||
| assert.Equal(t, oapi.AutoStandbyStatusStatusUnsupported, statusResp.Status) | ||
| assert.Equal(t, oapi.AutoStandbyStatusReasonUnsupportedPlatform, statusResp.Reason) | ||
| } | ||
|
|
||
| func TestGetAutoStandbyStatusActive(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| inst := &instances.Instance{ | ||
| StoredMetadata: instances.StoredMetadata{ | ||
| Id: "inst-2", | ||
| Name: "inst-2", | ||
| NetworkEnabled: true, | ||
| IP: "192.168.100.20", | ||
| AutoStandby: &autostandby.Policy{Enabled: true, IdleTimeout: "5m"}, | ||
| }, | ||
| State: instances.StateRunning, | ||
| } | ||
|
|
||
| now := time.Date(2026, 4, 6, 12, 0, 0, 0, time.UTC) | ||
| store := &statusStore{ | ||
| instances: []autostandby.Instance{{ | ||
| ID: "inst-2", | ||
| Name: "inst-2", | ||
| State: autostandby.StateRunning, | ||
| NetworkEnabled: true, | ||
| IP: "192.168.100.20", | ||
| AutoStandby: &autostandby.Policy{Enabled: true, IdleTimeout: "5m"}, | ||
| }}, | ||
| } | ||
| source := &statusConnectionSource{connections: []autostandby.Connection{{ | ||
| OriginalSourceIP: mustStatusAddr("1.2.3.4"), | ||
| OriginalSourcePort: 51234, | ||
| OriginalDestinationIP: mustStatusAddr("192.168.100.20"), | ||
| OriginalDestinationPort: 8080, | ||
| TCPState: autostandby.TCPStateEstablished, | ||
| }}} | ||
| controller := autostandby.NewController(store, source, autostandby.ControllerOptions{ | ||
| Now: func() time.Time { return now }, | ||
| }) | ||
| require.NoError(t, controller.Run(withCanceledContext(t))) | ||
|
|
||
| base := newTestService(t) | ||
| base.InstanceManager = &captureStatusManager{Manager: base.InstanceManager, instance: inst} | ||
| base.AutoStandbyController = controller | ||
|
|
||
| resp, err := base.GetAutoStandbyStatus(ctx(), oapi.GetAutoStandbyStatusRequestObject{Id: "inst-2"}) | ||
| require.NoError(t, err) | ||
|
|
||
| statusResp, ok := resp.(oapi.GetAutoStandbyStatus200JSONResponse) | ||
| require.True(t, ok) | ||
| assert.True(t, statusResp.Supported) | ||
| assert.Equal(t, oapi.AutoStandbyStatusStatusActive, statusResp.Status) | ||
| assert.Equal(t, 1, statusResp.ActiveInboundConnections) | ||
| } | ||
|
|
||
| func withCanceledContext(t *testing.T) context.Context { | ||
| t.Helper() | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| cancel() | ||
| return ctx | ||
| } | ||
|
|
||
| func mustStatusAddr(raw string) netip.Addr { | ||
| return netip.MustParseAddr(raw) | ||
| } |
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
Oops, something went wrong.
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.