From 1c31f5df019be9cb6fcde00c5c2624b37c5f27f5 Mon Sep 17 00:00:00 2001 From: "M. Mert Yildiran" Date: Sun, 5 Feb 2023 08:41:53 +0300 Subject: [PATCH] :bug: Fix a data race in pod watching --- cmd/tapRunner.go | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/cmd/tapRunner.go b/cmd/tapRunner.go index bb032b259..dd42b2bc8 100644 --- a/cmd/tapRunner.go +++ b/cmd/tapRunner.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "regexp" + "sync" "time" "github.com/kubeshark/kubeshark/docker" @@ -34,11 +35,18 @@ type tapState struct { var state tapState var connector *connect.Connector -var hubPodReady bool -var frontPodReady bool -var proxyDone bool + +type Readiness struct { + Hub bool + Front bool + Proxy bool + sync.Mutex +} + +var ready *Readiness func tap() { + ready = &Readiness{} state.startTime = time.Now() docker.SetRegistry(config.Config.Tap.Docker.Registry) 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 { isPodReady = true - hubPodReady = true + + ready.Lock() + ready.Hub = true + ready.Unlock() postHubStarted(ctx, kubernetesProvider, cancel) } + ready.Lock() + proxyDone := ready.Proxy + hubPodReady := ready.Hub + frontPodReady := ready.Front + ready.Unlock() + if !proxyDone && hubPodReady && frontPodReady { - proxyDone = true + ready.Lock() + ready.Proxy = true + ready.Unlock() postFrontStarted(ctx, kubernetesProvider, cancel) } case kubernetes.EventBookmark: @@ -258,11 +277,21 @@ func watchFrontPod(ctx context.Context, kubernetesProvider *kubernetes.Provider, if modifiedPod.Status.Phase == core.PodRunning && !isPodReady { 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 { - proxyDone = true + ready.Lock() + ready.Proxy = true + ready.Unlock() postFrontStarted(ctx, kubernetesProvider, cancel) } case kubernetes.EventBookmark: