Skip to content

Commit

Permalink
Use kubernetes watch for tracking challenge updates
Browse files Browse the repository at this point in the history
  • Loading branch information
J12934 committed Nov 24, 2024
1 parent 712de54 commit 39488ee
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
36 changes: 27 additions & 9 deletions balancer/pkg/scoring/scoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
"github.com/juice-shop/multi-juicer/balancer/pkg/bundle"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
)

const updateInterval = 5 * time.Second

type TeamScore struct {
Name string `json:"name"`
Score int `json:"score"`
Expand Down Expand Up @@ -69,16 +68,35 @@ func (s *ScoringService) GetTopScores() []*TeamScore {

// TrackScoresWorker is a worker that runs in the background and cheks the scores of all JuiceShop instances every 5 seconds
func (s *ScoringService) StartingScoringWorker() {
for {
context := context.Background()
time.Sleep(updateInterval)
watcher, err := s.bundle.ClientSet.AppsV1().Deployments(s.bundle.RuntimeEnvironment.Namespace).Watch(context.Background(), metav1.ListOptions{
LabelSelector: "app.kubernetes.io/name=juice-shop,app.kubernetes.io/part-of=multi-juicer",
})

err := s.CalculateAndCacheScoreBoard(context)
if err != nil {
s.bundle.Log.Printf("Failed to calculate the score board. Claculation will be automatically retried in %ds : %v", updateInterval, err)
continue
if err != nil {
s.bundle.Log.Printf("Failed to start the watcher for JuiceShop deployments: %v", err)
panic(err)
}
defer watcher.Stop()

for event := range watcher.ResultChan() {
switch event.Type {
case watch.Added, watch.Modified:
deployment := event.Object.(*appsv1.Deployment)
score := calculateScore(s.bundle, deployment, cachedChallengesMap)
s.currentScoresMutex.Lock()
s.currentScores[score.Name] = score
s.currentScoresMutex.Unlock()
case watch.Deleted:
deployment := event.Object.(*appsv1.Deployment)
team := deployment.Labels["team"]
s.currentScoresMutex.Lock()
delete(s.currentScores, team)
s.currentScoresMutex.Unlock()
default:
s.bundle.Log.Printf("Unknown event type: %v", event.Type)
}
}
s.bundle.Log.Printf("Watcher for JuiceShop deployments has been closed. Exiting the watcher.")
}

func (s *ScoringService) CalculateAndCacheScoreBoard(context context.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion helm/multi-juicer/templates/balancer/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
rules:
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "create", "list", "delete", "patch"]
verbs: ["get", "create", "list", "delete", "patch", "watch"]
- apiGroups: [""] # "" indicates the core API group
resources: ["services"]
verbs: ["get", "create", "delete"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ default values render correctly:
- list
- delete
- patch
- watch
- apiGroups:
- ""
resources:
Expand Down Expand Up @@ -773,6 +774,7 @@ full values render out correctly:
- list
- delete
- patch
- watch
- apiGroups:
- ""
resources:
Expand Down Expand Up @@ -3404,6 +3406,7 @@ production notes work correctly:
- list
- delete
- patch
- watch
- apiGroups:
- ""
resources:
Expand Down

0 comments on commit 39488ee

Please sign in to comment.