Skip to content

Commit

Permalink
[YUNIKORN-333] rate limit predicate events (#240)
Browse files Browse the repository at this point in the history
Reduce the number of events published to the K8s event system when a
predicate check fails.
The rate is now limited to 1 event per second.

Fixes: #240
  • Loading branch information
HuangTing-Yao authored and wilfred-s committed Mar 16, 2021
1 parent 4607f37 commit 8f276d2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.7.0
go.uber.org/zap v1.13.0
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
gopkg.in/yaml.v2 v2.2.8
gotest.tools v2.2.0+incompatible
k8s.io/api v0.16.13
Expand Down
11 changes: 9 additions & 2 deletions pkg/plugin/predicates/predictor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
"fmt"
"strings"
"sync"
"time"

"go.uber.org/zap"
"golang.org/x/time/rate"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kubernetes/pkg/scheduler/algorithm/predicates"
Expand Down Expand Up @@ -71,6 +73,9 @@ var reservationPredicates = []string{
predicates.MatchNodeSelectorPred, // node selector check
}

var limit = rate.Every(time.Second)
var limiter = rate.NewLimiter(limit, 1)

type Predictor struct {
fitPredicateMap map[string]factory.FitPredicateFactory
fitPredicateFunctions map[string]predicates.FitPredicate
Expand Down Expand Up @@ -278,8 +283,10 @@ func (p *Predictor) predicatesAllocate(pod *v1.Pod, meta predicates.PredicateMet
}

if !fit {
events.GetRecorder().Eventf(pod, v1.EventTypeWarning,
"FailedScheduling", "%v", reasons)
if limiter.Allow() {
events.GetRecorder().Eventf(pod, v1.EventTypeWarning,
"FailedScheduling", "%v", reasons)
}
return fmt.Errorf("predicate %s cannot be satisfied, reason: %v", predicateKey, reasons)
}
}
Expand Down

0 comments on commit 8f276d2

Please sign in to comment.