mirror of
https://github.com/actions/actions-runner-controller.git
synced 2025-12-10 19:50:30 +00:00
Option to consider runner group visibility on scale based on webhook (#1062)
This will work on GHES but GitHub Enterprise Cloud due to excessive GitHub API calls required. More work is needed, like adding a cache layer to the GitHub client, to make it usable on GitHub Enterprise Cloud. Fixes additional cases from https://github.com/actions-runner-controller/actions-runner-controller/pull/1012 If GitHub auth is provided in the webhooks controller then runner groups with custom visibility are supported. Otherwise, all runner groups will be assumed to be visible to all repositories `getScaleUpTargetWithFunction()` will check if there is an HRA available with the following flow: 1. Search for **repository** HRAs - if so it ends here 2. Get available HRAs in k8s 3. Compute visible runner groups a. If GitHub auth is provided - get all the runner groups that are visible to the repository of the incoming webhook using GitHub API calls. b. If GitHub auth is not provided - assume all runner groups are visible to all repositories 4. Search for **default organization** runners (a.k.a runners from organization's visible default runner group) with matching labels 5. Search for **default enterprise** runners (a.k.a runners from enterprise's visible default runner group) with matching labels 6. Search for **custom organization runner groups** with matching labels 7. Search for **custom enterprise runner groups** with matching labels Co-authored-by: Yusuke Kuoka <ykuoka@gmail.com>
This commit is contained in:
committed by
GitHub
parent
b509eb4388
commit
d0d316252e
@@ -38,6 +38,7 @@ import (
|
||||
|
||||
"github.com/actions-runner-controller/actions-runner-controller/api/v1alpha1"
|
||||
"github.com/actions-runner-controller/actions-runner-controller/github"
|
||||
"github.com/actions-runner-controller/actions-runner-controller/simulator"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -476,37 +477,14 @@ func (autoscaler *HorizontalRunnerAutoscalerGitHubWebhook) getScaleUpTargetWithF
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Search for organization runner HRAs in default runner group
|
||||
if target, err := scaleTarget(owner); err != nil {
|
||||
log.Error(err, "finding organizational runner", "organization", owner)
|
||||
return nil, err
|
||||
} else if target != nil {
|
||||
log.Info("job scale up target is organizational runners", "organization", owner)
|
||||
return target, nil
|
||||
}
|
||||
|
||||
if enterprise != "" {
|
||||
// Search for enterprise runner HRAs in default runner group
|
||||
if target, err := scaleTarget(enterpriseKey(enterprise)); err != nil {
|
||||
log.Error(err, "finding enterprise runner", "enterprise", enterprise)
|
||||
return nil, err
|
||||
} else if target != nil {
|
||||
log.Info("scale up target is default enterprise runners", "enterprise", enterprise)
|
||||
return target, nil
|
||||
}
|
||||
}
|
||||
|
||||
// At this point there were no default organization/enterprise runners available to use, try now
|
||||
// searching in runner groups
|
||||
|
||||
// We need to get the potential runner groups first to avoid spending API queries needless. Once/if GitHub improves an
|
||||
// Find the potential runner groups first to avoid spending API queries needless. Once/if GitHub improves an
|
||||
// API to find related/linked runner groups from a specific repository this logic could be removed
|
||||
availableEnterpriseGroups, availableOrganizationGroups, err := autoscaler.getPotentialGroupsFromHRAs(ctx, enterprise, owner)
|
||||
managedRunnerGroups, err := autoscaler.getManagedRunnerGroupsFromHRAs(ctx, enterprise, owner)
|
||||
if err != nil {
|
||||
log.Error(err, "finding potential organization runner groups from HRAs", "organization", owner)
|
||||
log.Error(err, "finding potential organization/enterprise runner groups from HRAs", "organization", owner)
|
||||
return nil, err
|
||||
}
|
||||
if len(availableEnterpriseGroups) == 0 && len(availableOrganizationGroups) == 0 {
|
||||
if managedRunnerGroups.IsEmpty() {
|
||||
log.V(1).Info("no repository/organizational/enterprise runner found",
|
||||
"repository", repositoryRunnerKey,
|
||||
"organization", owner,
|
||||
@@ -514,57 +492,88 @@ func (autoscaler *HorizontalRunnerAutoscalerGitHubWebhook) getScaleUpTargetWithF
|
||||
)
|
||||
}
|
||||
|
||||
var enterpriseGroups []string
|
||||
var organizationGroups []string
|
||||
var visibleGroups *simulator.VisibleRunnerGroups
|
||||
if autoscaler.GitHubClient != nil {
|
||||
simu := &simulator.Simulator{
|
||||
Client: autoscaler.GitHubClient,
|
||||
}
|
||||
// Get available organization runner groups and enterprise runner groups for a repository
|
||||
// These are the sum of runner groups with repository access = All repositories plus
|
||||
// runner groups where owner/repo has access to
|
||||
enterpriseGroups, organizationGroups, err = autoscaler.GitHubClient.GetRunnerGroupsFromRepository(ctx, owner, repositoryRunnerKey, availableEnterpriseGroups, availableOrganizationGroups)
|
||||
log.V(1).Info("Searching in runner groups", "enterprise.groups", enterpriseGroups, "organization.groups", organizationGroups)
|
||||
// These are the sum of runner groups with repository access = All repositories and runner groups
|
||||
// where owner/repo has access to as well. The list will include default runner group also if it has access to
|
||||
visibleGroups, err = simu.GetRunnerGroupsVisibleToRepository(ctx, owner, repositoryRunnerKey, managedRunnerGroups)
|
||||
log.V(1).Info("Searching in runner groups", "groups", visibleGroups)
|
||||
if err != nil {
|
||||
log.Error(err, "Unable to find runner groups from repository", "organization", owner, "repository", repo)
|
||||
return nil, nil
|
||||
return nil, fmt.Errorf("error while finding visible runner groups: %v", err)
|
||||
}
|
||||
} else {
|
||||
// For backwards compatibility if GitHub authentication is not configured, we assume all runner groups have
|
||||
// visibility=all to honor the previous implementation, therefore any available enterprise/organization runner
|
||||
// is a potential target for scaling
|
||||
enterpriseGroups = availableEnterpriseGroups
|
||||
organizationGroups = availableOrganizationGroups
|
||||
// is a potential target for scaling. This will also avoid doing extra API calls caused by
|
||||
// GitHubClient.GetRunnerGroupsVisibleToRepository in case users are not using custom visibility on their runner
|
||||
// groups or they are using only default runner groups
|
||||
visibleGroups = managedRunnerGroups
|
||||
}
|
||||
|
||||
for _, group := range organizationGroups {
|
||||
if target, err := scaleTarget(organizationalRunnerGroupKey(owner, group)); err != nil {
|
||||
log.Error(err, "finding organizational runner group", "organization", owner)
|
||||
return nil, err
|
||||
} else if target != nil {
|
||||
log.Info(fmt.Sprintf("job scale up target is organizational runner group %s", target.Name), "organization", owner)
|
||||
return target, nil
|
||||
scaleTargetKey := func(rg simulator.RunnerGroup) string {
|
||||
switch rg.Kind {
|
||||
case simulator.Default:
|
||||
switch rg.Scope {
|
||||
case simulator.Organization:
|
||||
return owner
|
||||
case simulator.Enterprise:
|
||||
return enterpriseKey(enterprise)
|
||||
}
|
||||
case simulator.Custom:
|
||||
switch rg.Scope {
|
||||
case simulator.Organization:
|
||||
return organizationalRunnerGroupKey(owner, rg.Name)
|
||||
case simulator.Enterprise:
|
||||
return enterpriseRunnerGroupKey(enterprise, rg.Name)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
for _, group := range enterpriseGroups {
|
||||
if target, err := scaleTarget(enterpriseRunnerGroupKey(enterprise, group)); err != nil {
|
||||
log.Error(err, "finding enterprise runner group", "enterprise", owner)
|
||||
return nil, err
|
||||
} else if target != nil {
|
||||
log.Info(fmt.Sprintf("job scale up target is enterprise runner group %s", target.Name), "enterprise", owner)
|
||||
return target, nil
|
||||
log.Info("groups", "groups", visibleGroups)
|
||||
|
||||
var t *ScaleTarget
|
||||
|
||||
traverseErr := visibleGroups.Traverse(func(rg simulator.RunnerGroup) (bool, error) {
|
||||
key := scaleTargetKey(rg)
|
||||
|
||||
target, err := scaleTarget(key)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err, "finding runner group", "enterprise", enterprise, "organization", owner, "repository", repo, "key", key)
|
||||
return false, err
|
||||
} else if target == nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
t = target
|
||||
log.Info("job scale up target found", "enterprise", enterprise, "organization", owner, "repository", repo, "key", key)
|
||||
|
||||
return true, nil
|
||||
})
|
||||
|
||||
if traverseErr != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.V(1).Info("no repository/organizational/enterprise runner found",
|
||||
"repository", repositoryRunnerKey,
|
||||
"organization", owner,
|
||||
"enterprises", enterprise,
|
||||
)
|
||||
return nil, nil
|
||||
if t == nil {
|
||||
log.V(1).Info("no repository/organizational/enterprise runner found",
|
||||
"repository", repositoryRunnerKey,
|
||||
"organization", owner,
|
||||
"enterprise", enterprise,
|
||||
)
|
||||
}
|
||||
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func (autoscaler *HorizontalRunnerAutoscalerGitHubWebhook) getPotentialGroupsFromHRAs(ctx context.Context, enterprise, org string) ([]string, []string, error) {
|
||||
var enterpriseRunnerGroups []string
|
||||
var orgRunnerGroups []string
|
||||
func (autoscaler *HorizontalRunnerAutoscalerGitHubWebhook) getManagedRunnerGroupsFromHRAs(ctx context.Context, enterprise, org string) (*simulator.VisibleRunnerGroups, error) {
|
||||
groups := simulator.NewVisibleRunnerGroups()
|
||||
ns := autoscaler.Namespace
|
||||
|
||||
var defaultListOpts []client.ListOption
|
||||
@@ -579,36 +588,57 @@ func (autoscaler *HorizontalRunnerAutoscalerGitHubWebhook) getPotentialGroupsFro
|
||||
|
||||
var hraList v1alpha1.HorizontalRunnerAutoscalerList
|
||||
if err := autoscaler.List(ctx, &hraList, opts...); err != nil {
|
||||
return orgRunnerGroups, enterpriseRunnerGroups, err
|
||||
return groups, err
|
||||
}
|
||||
|
||||
for _, hra := range hraList.Items {
|
||||
switch hra.Spec.ScaleTargetRef.Kind {
|
||||
var o, e, g string
|
||||
|
||||
kind := hra.Spec.ScaleTargetRef.Kind
|
||||
switch kind {
|
||||
case "RunnerSet":
|
||||
var rs v1alpha1.RunnerSet
|
||||
if err := autoscaler.Client.Get(context.Background(), types.NamespacedName{Namespace: hra.Namespace, Name: hra.Spec.ScaleTargetRef.Name}, &rs); err != nil {
|
||||
return orgRunnerGroups, enterpriseRunnerGroups, err
|
||||
}
|
||||
if rs.Spec.Organization == org && rs.Spec.Group != "" {
|
||||
orgRunnerGroups = append(orgRunnerGroups, rs.Spec.Group)
|
||||
}
|
||||
if rs.Spec.Enterprise == enterprise && rs.Spec.Group != "" {
|
||||
enterpriseRunnerGroups = append(enterpriseRunnerGroups, rs.Spec.Group)
|
||||
return groups, err
|
||||
}
|
||||
o, e, g = rs.Spec.Organization, rs.Spec.Enterprise, rs.Spec.Group
|
||||
case "RunnerDeployment", "":
|
||||
var rd v1alpha1.RunnerDeployment
|
||||
if err := autoscaler.Client.Get(context.Background(), types.NamespacedName{Namespace: hra.Namespace, Name: hra.Spec.ScaleTargetRef.Name}, &rd); err != nil {
|
||||
return orgRunnerGroups, enterpriseRunnerGroups, err
|
||||
}
|
||||
if rd.Spec.Template.Spec.Organization == org && rd.Spec.Template.Spec.Group != "" {
|
||||
orgRunnerGroups = append(orgRunnerGroups, rd.Spec.Template.Spec.Group)
|
||||
}
|
||||
if rd.Spec.Template.Spec.Enterprise == enterprise && rd.Spec.Template.Spec.Group != "" {
|
||||
enterpriseRunnerGroups = append(enterpriseRunnerGroups, rd.Spec.Template.Spec.Group)
|
||||
return groups, err
|
||||
}
|
||||
o, e, g = rd.Spec.Template.Spec.Organization, rd.Spec.Template.Spec.Enterprise, rd.Spec.Template.Spec.Group
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported scale target kind: %v", kind)
|
||||
}
|
||||
|
||||
if g == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if e == "" && o == "" {
|
||||
autoscaler.Log.V(1).Info(
|
||||
"invalid runner group config in scale target: spec.group must be set along with either spec.enterprise or spec.organization",
|
||||
"scaleTargetKind", kind,
|
||||
"group", g,
|
||||
"enterprise", e,
|
||||
"organization", o,
|
||||
)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if e != enterprise && o != org {
|
||||
continue
|
||||
}
|
||||
|
||||
rg := simulator.NewRunnerGroupFromProperties(e, o, g)
|
||||
|
||||
if err := groups.Add(rg); err != nil {
|
||||
return groups, fmt.Errorf("failed adding visible group from HRA %s/%s: %w", hra.Namespace, hra.Name, err)
|
||||
}
|
||||
}
|
||||
return enterpriseRunnerGroups, orgRunnerGroups, nil
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
func (autoscaler *HorizontalRunnerAutoscalerGitHubWebhook) getJobScaleTarget(ctx context.Context, name string, labels []string) (*ScaleTarget, error) {
|
||||
|
||||
Reference in New Issue
Block a user