Skip to content

Commit

Permalink
Gh patch rates (kedacore#4786)
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Turrado <jorge.turrado@scrm.lidl>
  • Loading branch information
Eldarrin authored and JorTurFer committed Jul 27, 2023
1 parent f71b895 commit 39963d6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio

- **General**: Metrics server exposes Prometheus metrics ([#4776](https://github.com/kedacore/keda/issues/4776))
- **AWS Pod Identity Authentication**: Use `default` service account if the workload doesn't set it ([#4767](https://github.com/kedacore/keda/issues/4767))
- **GitHub Runner Scaler**: Fix rate checking on GHEC when HTTP 200 ([#4786](https://github.com/kedacore/keda/issues/4786))
- **Pulsar Scaler**: Fix `msgBacklogThreshold` field being named wrongly as `msgBacklog` ([#4681](https://github.com/kedacore/keda/issues/4681))

### Deprecations
Expand Down
16 changes: 9 additions & 7 deletions pkg/scalers/github_runner_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,17 @@ func getGithubRequest(ctx context.Context, url string, metadata *githubRunnerMet
}
_ = r.Body.Close()

if r.StatusCode != 200 || r.Header.Get("X-RateLimit-Remaining") == "" {
return []byte{}, fmt.Errorf("the GitHub REST API returned error. url: %s status: %d response: %s", url, r.StatusCode, string(b))
}
if r.StatusCode != 200 {
if r.Header.Get("X-RateLimit-Remaining") != "" {
githubAPIRemaining, _ := strconv.Atoi(r.Header.Get("X-RateLimit-Remaining"))

githubAPIRemaining, _ := strconv.Atoi(r.Header.Get("X-RateLimit-Remaining"))
if githubAPIRemaining == 0 {
resetTime, _ := strconv.ParseInt(r.Header.Get("X-RateLimit-Reset"), 10, 64)
return []byte{}, fmt.Errorf("GitHub API rate limit exceeded, resets at %s", time.Unix(resetTime, 0))
}
}

if githubAPIRemaining == 0 {
resetTime, _ := strconv.ParseInt(r.Header.Get("X-RateLimit-Reset"), 10, 64)
return []byte{}, fmt.Errorf("GitHub API rate limit exceeded, resets at %s", time.Unix(resetTime, 0))
return []byte{}, fmt.Errorf("the GitHub REST API returned error. url: %s status: %d response: %s", url, r.StatusCode, string(b))
}

return b, nil
Expand Down
9 changes: 5 additions & 4 deletions pkg/scalers/github_runner_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,16 @@ func buildQueueJSON() []byte {

func apiStubHandler(hasRateLeft bool) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
futureReset := time.Now()
futureReset = futureReset.Add(time.Minute * 30)
w.Header().Set("X-RateLimit-Reset", fmt.Sprint(futureReset.Unix()))
if hasRateLeft {
w.Header().Set("X-RateLimit-Remaining", "50")
w.WriteHeader(http.StatusOK)
} else {
w.Header().Set("X-RateLimit-Remaining", "0")
w.WriteHeader(http.StatusForbidden)
}
futureReset := time.Now()
futureReset = futureReset.Add(time.Minute * 30)
w.Header().Set("X-RateLimit-Reset", fmt.Sprint(futureReset.Unix()))
w.WriteHeader(http.StatusOK)
if strings.HasSuffix(r.URL.String(), "jobs") {
_, _ = w.Write([]byte(testGhWFJobResponse))
}
Expand Down

0 comments on commit 39963d6

Please sign in to comment.