-
Notifications
You must be signed in to change notification settings - Fork 0
Scan every proposed configuration commit #37
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
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,76 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/actions/scaleset" | ||
| ) | ||
|
|
||
| type scaleSetAdmin interface { | ||
| GetRunnerGroupByName(context.Context, string) (*scaleset.RunnerGroup, error) | ||
| GetRunnerScaleSet(context.Context, int, string) (*scaleset.RunnerScaleSet, error) | ||
| GetRunnerScaleSetByID(context.Context, int) (*scaleset.RunnerScaleSet, error) | ||
| DeleteRunnerScaleSet(context.Context, int) error | ||
| } | ||
|
|
||
| func removeIdleScaleSet(ctx context.Context, cfg Config, client scaleSetAdmin) (bool, error) { | ||
| runnerGroupID := 1 | ||
| if cfg.RunnerGroup != scaleset.DefaultRunnerGroup { | ||
| group, err := client.GetRunnerGroupByName(ctx, cfg.RunnerGroup) | ||
| if err != nil { | ||
| return false, fmt.Errorf("find runner group: %w", err) | ||
| } | ||
| runnerGroupID = group.ID | ||
| } | ||
| set, err := client.GetRunnerScaleSet(ctx, runnerGroupID, cfg.ScaleSetName) | ||
| if err != nil { | ||
| return false, fmt.Errorf("find runner scale set: %w", err) | ||
| } | ||
| if set == nil { | ||
| return false, nil | ||
| } | ||
| if set.Name != cfg.ScaleSetName || set.RunnerGroupID != runnerGroupID { | ||
| return false, fmt.Errorf("runner scale set identity does not match the selected configuration") | ||
| } | ||
| if set.Statistics == nil { | ||
| set, err = client.GetRunnerScaleSetByID(ctx, set.ID) | ||
| if err != nil { | ||
| return false, fmt.Errorf("read runner scale set statistics: %w", err) | ||
| } | ||
| if set == nil || set.Name != cfg.ScaleSetName || set.RunnerGroupID != runnerGroupID { | ||
| return false, fmt.Errorf("runner scale set identity changed while checking statistics") | ||
| } | ||
| } | ||
| if set.Statistics == nil { | ||
| return false, fmt.Errorf("runner scale set has no idle-state statistics") | ||
| } | ||
| if *set.Statistics != (scaleset.RunnerScaleSetStatistic{}) { | ||
| return false, fmt.Errorf("runner scale set is not idle") | ||
| } | ||
| if err := client.DeleteRunnerScaleSet(ctx, set.ID); err != nil { | ||
| return false, fmt.Errorf("delete idle runner scale set: %w", err) | ||
| } | ||
| return true, nil | ||
| } | ||
|
|
||
| func deleteIdleScaleSet(ctx context.Context) error { | ||
| cfg, err := configFromEnv() | ||
| if err != nil { | ||
| return fmt.Errorf("configuration: %w", err) | ||
| } | ||
| client, err := cfg.scaleSetClient() | ||
| if err != nil { | ||
| return fmt.Errorf("create scale-set client: %w", err) | ||
| } | ||
| deleted, err := removeIdleScaleSet(ctx, cfg, client) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if deleted { | ||
| fmt.Printf("deleted idle runner scale set %q\n", cfg.ScaleSetName) | ||
| } else { | ||
| fmt.Printf("runner scale set %q is already absent\n", cfg.ScaleSetName) | ||
| } | ||
| return nil | ||
| } |
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,82 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/actions/scaleset" | ||
| ) | ||
|
|
||
| type fakeScaleSetAdmin struct { | ||
| group *scaleset.RunnerGroup | ||
| set *scaleset.RunnerScaleSet | ||
| detail *scaleset.RunnerScaleSet | ||
| deleted bool | ||
| } | ||
|
|
||
| func (f *fakeScaleSetAdmin) GetRunnerGroupByName(context.Context, string) (*scaleset.RunnerGroup, error) { | ||
| return f.group, nil | ||
| } | ||
|
|
||
| func (f *fakeScaleSetAdmin) GetRunnerScaleSet(context.Context, int, string) (*scaleset.RunnerScaleSet, error) { | ||
| return f.set, nil | ||
| } | ||
|
|
||
| func (f *fakeScaleSetAdmin) GetRunnerScaleSetByID(context.Context, int) (*scaleset.RunnerScaleSet, error) { | ||
| return f.detail, nil | ||
| } | ||
|
|
||
| func (f *fakeScaleSetAdmin) DeleteRunnerScaleSet(context.Context, int) error { | ||
| f.deleted = true | ||
| return nil | ||
| } | ||
|
|
||
| func TestRemoveIdleScaleSet(t *testing.T) { | ||
| cfg := Config{RunnerGroup: "trusted-private-ci", ScaleSetName: "docker-ci-example"} | ||
| idle := scaleset.RunnerScaleSetStatistic{} | ||
| newAdmin := func(statistics *scaleset.RunnerScaleSetStatistic) *fakeScaleSetAdmin { | ||
| return &fakeScaleSetAdmin{ | ||
| group: &scaleset.RunnerGroup{ID: 7, Name: cfg.RunnerGroup}, | ||
| set: &scaleset.RunnerScaleSet{ID: 42, Name: cfg.ScaleSetName, RunnerGroupID: 7, Statistics: statistics}, | ||
| } | ||
| } | ||
|
|
||
| admin := newAdmin(&idle) | ||
| deleted, err := removeIdleScaleSet(context.Background(), cfg, admin) | ||
| if err != nil || !deleted || !admin.deleted { | ||
| t.Fatalf("idle scale set was not deleted: deleted=%v called=%v err=%v", deleted, admin.deleted, err) | ||
| } | ||
|
|
||
| absent := newAdmin(&idle) | ||
| absent.set = nil | ||
| deleted, err = removeIdleScaleSet(context.Background(), cfg, absent) | ||
| if err != nil || deleted || absent.deleted { | ||
| t.Fatalf("absent scale set should be a no-op: deleted=%v called=%v err=%v", deleted, absent.deleted, err) | ||
| } | ||
|
|
||
| withoutStatistics := newAdmin(nil) | ||
| withoutStatistics.detail = &scaleset.RunnerScaleSet{ID: 42, Name: cfg.ScaleSetName, RunnerGroupID: 7, Statistics: &idle} | ||
| deleted, err = removeIdleScaleSet(context.Background(), cfg, withoutStatistics) | ||
| if err != nil || !deleted || !withoutStatistics.deleted { | ||
| t.Fatalf("detailed idle statistics were not used: deleted=%v called=%v err=%v", deleted, withoutStatistics.deleted, err) | ||
| } | ||
|
|
||
| busy := map[string]scaleset.RunnerScaleSetStatistic{ | ||
| "available_jobs": {TotalAvailableJobs: 1}, | ||
| "acquired_jobs": {TotalAcquiredJobs: 1}, | ||
| "assigned_jobs": {TotalAssignedJobs: 1}, | ||
| "running_jobs": {TotalRunningJobs: 1}, | ||
| "registered_runners": {TotalRegisteredRunners: 1}, | ||
| "busy_runners": {TotalBusyRunners: 1}, | ||
| "idle_runners": {TotalIdleRunners: 1}, | ||
| } | ||
| for name, statistics := range busy { | ||
| t.Run(name, func(t *testing.T) { | ||
| admin := newAdmin(&statistics) | ||
| deleted, err := removeIdleScaleSet(context.Background(), cfg, admin) | ||
| if err == nil || deleted || admin.deleted { | ||
| t.Fatalf("non-idle scale set was deletable: deleted=%v called=%v err=%v", deleted, admin.deleted, err) | ||
| } | ||
| }) | ||
| } | ||
| } |
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
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
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.