🐛 Fix a data race in pod watching

This commit is contained in:
M. Mert Yildiran 2023-02-05 08:41:53 +03:00
parent 6e0f2aa10a
commit 1c31f5df01
No known key found for this signature in database
GPG Key ID: DA5D6DCBB758A461

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"regexp" "regexp"
"sync"
"time" "time"
"github.com/kubeshark/kubeshark/docker" "github.com/kubeshark/kubeshark/docker"
@ -34,11 +35,18 @@ type tapState struct {
var state tapState var state tapState
var connector *connect.Connector var connector *connect.Connector
var hubPodReady bool
var frontPodReady bool type Readiness struct {
var proxyDone bool Hub bool
Front bool
Proxy bool
sync.Mutex
}
var ready *Readiness
func tap() { func tap() {
ready = &Readiness{}
state.startTime = time.Now() state.startTime = time.Now()
docker.SetRegistry(config.Config.Tap.Docker.Registry) docker.SetRegistry(config.Config.Tap.Docker.Registry)
docker.SetTag(config.Config.Tap.Docker.Tag) docker.SetTag(config.Config.Tap.Docker.Tag)
@ -178,12 +186,23 @@ func watchHubPod(ctx context.Context, kubernetesProvider *kubernetes.Provider, c
if modifiedPod.Status.Phase == core.PodRunning && !isPodReady { if modifiedPod.Status.Phase == core.PodRunning && !isPodReady {
isPodReady = true isPodReady = true
hubPodReady = true
ready.Lock()
ready.Hub = true
ready.Unlock()
postHubStarted(ctx, kubernetesProvider, cancel) postHubStarted(ctx, kubernetesProvider, cancel)
} }
ready.Lock()
proxyDone := ready.Proxy
hubPodReady := ready.Hub
frontPodReady := ready.Front
ready.Unlock()
if !proxyDone && hubPodReady && frontPodReady { if !proxyDone && hubPodReady && frontPodReady {
proxyDone = true ready.Lock()
ready.Proxy = true
ready.Unlock()
postFrontStarted(ctx, kubernetesProvider, cancel) postFrontStarted(ctx, kubernetesProvider, cancel)
} }
case kubernetes.EventBookmark: case kubernetes.EventBookmark:
@ -258,11 +277,21 @@ func watchFrontPod(ctx context.Context, kubernetesProvider *kubernetes.Provider,
if modifiedPod.Status.Phase == core.PodRunning && !isPodReady { if modifiedPod.Status.Phase == core.PodRunning && !isPodReady {
isPodReady = true isPodReady = true
frontPodReady = true ready.Lock()
ready.Front = true
ready.Unlock()
} }
ready.Lock()
proxyDone := ready.Proxy
hubPodReady := ready.Hub
frontPodReady := ready.Front
ready.Unlock()
if !proxyDone && hubPodReady && frontPodReady { if !proxyDone && hubPodReady && frontPodReady {
proxyDone = true ready.Lock()
ready.Proxy = true
ready.Unlock()
postFrontStarted(ctx, kubernetesProvider, cancel) postFrontStarted(ctx, kubernetesProvider, cancel)
} }
case kubernetes.EventBookmark: case kubernetes.EventBookmark: