Skip to content

Commit

Permalink
feat: add support to not scale GitHub runner on default runner labels (
Browse files Browse the repository at this point in the history
…#6251)

Signed-off-by: Leevi Lehtonen <[email protected]>
  • Loading branch information
leevilehtonen authored Nov 5, 2024
1 parent 96c2ba3 commit 1e90416
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Here is an overview of all new **experimental** features:
- **Elasticsearch Scaler**: Support Query at the Elasticsearch scaler ([#6216](https://github.com/kedacore/keda/issues/6216))
- **Etcd Scaler**: Add username and password support for etcd ([#6199](https://github.com/kedacore/keda/pull/6199))
- **GCP Scalers**: Added custom time horizon in GCP scalers ([#5778](https://github.com/kedacore/keda/issues/5778))
- **GitHub Scaler**: Add support to not scale on default runner labels ([#6127](https://github.com/kedacore/keda/issues/6127))
- **GitHub Scaler**: Fixed pagination, fetching repository list ([#5738](https://github.com/kedacore/keda/issues/5738))
- **Grafana dashboard**: Fix dashboard to handle wildcard scaledObject variables ([#6214](https://github.com/kedacore/keda/issues/6214))
- **Kafka**: Allow disabling FAST negotation when using Kerberos ([#6188](https://github.com/kedacore/keda/issues/6188))
Expand Down
41 changes: 33 additions & 8 deletions pkg/scalers/github_runner_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type githubRunnerMetadata struct {
personalAccessToken *string
repos []string
labels []string
noDefaultLabels bool
targetWorkflowQueueLength int64
triggerIndex int
applicationID *int64
Expand Down Expand Up @@ -372,8 +373,8 @@ func getValueFromMetaOrEnv(key string, metadata map[string]string, env map[strin
}

// getInt64ValueFromMetaOrEnv returns the value of the given key from the metadata or the environment variables
func getInt64ValueFromMetaOrEnv(key string, config *scalersconfig.ScalerConfig) (int64, error) {
sInt, err := getValueFromMetaOrEnv(key, config.TriggerMetadata, config.ResolvedEnv)
func getInt64ValueFromMetaOrEnv(key string, metadata map[string]string, env map[string]string) (int64, error) {
sInt, err := getValueFromMetaOrEnv(key, metadata, env)
if err != nil {
return -1, fmt.Errorf("error parsing %s: %w", key, err)
}
Expand All @@ -385,6 +386,20 @@ func getInt64ValueFromMetaOrEnv(key string, config *scalersconfig.ScalerConfig)
return goodInt, nil
}

// getInt64ValueFromMetaOrEnv returns the value of the given key from the metadata or the environment variables
func getBoolValueFromMetaOrEnv(key string, metadata map[string]string, env map[string]string) (bool, error) {
sBool, err := getValueFromMetaOrEnv(key, metadata, env)
if err != nil {
return false, fmt.Errorf("error parsing %s: %w", key, err)
}

goodBool, err := strconv.ParseBool(sBool)
if err != nil {
return false, fmt.Errorf("error parsing %s: %w", key, err)
}
return goodBool, nil
}

func parseGitHubRunnerMetadata(config *scalersconfig.ScalerConfig) (*githubRunnerMetadata, error) {
meta := &githubRunnerMetadata{}
meta.targetWorkflowQueueLength = defaultTargetWorkflowQueueLength
Expand All @@ -401,7 +416,7 @@ func parseGitHubRunnerMetadata(config *scalersconfig.ScalerConfig) (*githubRunne
return nil, err
}

if val, err := getInt64ValueFromMetaOrEnv("targetWorkflowQueueLength", config); err == nil && val != -1 {
if val, err := getInt64ValueFromMetaOrEnv("targetWorkflowQueueLength", config.TriggerMetadata, config.ResolvedEnv); err == nil && val != -1 {
meta.targetWorkflowQueueLength = val
} else {
meta.targetWorkflowQueueLength = defaultTargetWorkflowQueueLength
Expand All @@ -411,6 +426,12 @@ func parseGitHubRunnerMetadata(config *scalersconfig.ScalerConfig) (*githubRunne
meta.labels = strings.Split(val, ",")
}

if val, err := getBoolValueFromMetaOrEnv("noDefaultLabels", config.TriggerMetadata, config.ResolvedEnv); err == nil {
meta.noDefaultLabels = val
} else {
meta.noDefaultLabels = false
}

if val, err := getValueFromMetaOrEnv("repos", config.TriggerMetadata, config.ResolvedEnv); err == nil && val != "" {
meta.repos = strings.Split(val, ",")
}
Expand Down Expand Up @@ -448,12 +469,12 @@ func setupGitHubApp(config *scalersconfig.ScalerConfig) (*int64, *int64, *string
var instID *int64
var appKey *string

appIDVal, appIDErr := getInt64ValueFromMetaOrEnv("applicationID", config)
appIDVal, appIDErr := getInt64ValueFromMetaOrEnv("applicationID", config.TriggerMetadata, config.ResolvedEnv)
if appIDErr == nil && appIDVal != -1 {
appID = &appIDVal
}

instIDVal, instIDErr := getInt64ValueFromMetaOrEnv("installationID", config)
instIDVal, instIDErr := getInt64ValueFromMetaOrEnv("installationID", config.TriggerMetadata, config.ResolvedEnv)
if instIDErr == nil && instIDVal != -1 {
instID = &instIDVal
}
Expand Down Expand Up @@ -625,9 +646,13 @@ func contains(s []string, e string) bool {
}

// canRunnerMatchLabels check Agent Label array will match runner label array
func canRunnerMatchLabels(jobLabels []string, runnerLabels []string) bool {
func canRunnerMatchLabels(jobLabels []string, runnerLabels []string, noDefaultLabels bool) bool {
allLabels := runnerLabels
if !noDefaultLabels {
allLabels = append(allLabels, reservedLabels...)
}
for _, jobLabel := range jobLabels {
if !contains(runnerLabels, jobLabel) && !contains(reservedLabels, jobLabel) {
if !contains(allLabels, jobLabel) {
return false
}
}
Expand Down Expand Up @@ -665,7 +690,7 @@ func (s *githubRunnerScaler) GetWorkflowQueueLength(ctx context.Context) (int64,
return -1, err
}
for _, job := range jobs {
if (job.Status == "queued" || job.Status == "in_progress") && canRunnerMatchLabels(job.Labels, s.metadata.labels) {
if (job.Status == "queued" || job.Status == "in_progress") && canRunnerMatchLabels(job.Labels, s.metadata.labels, s.metadata.noDefaultLabels) {
queueCount++
}
}
Expand Down
Loading

0 comments on commit 1e90416

Please sign in to comment.