Skip to content

Commit

Permalink
fix data race in hotreload.
Browse files Browse the repository at this point in the history
  • Loading branch information
guettli committed Dec 16, 2024
1 parent c2c5f60 commit 63ed4db
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions hcloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,13 @@ func Test_updateHcloudCredentials(t *testing.T) {
_, err = i.InstanceExists(context.TODO(), node)
require.NoError(t, err)

oldCounter := hotreload.HcloudTokenReloadCounter
oldCounter := hotreload.GetHcloudReloadCounter()
token2 := "22222ZHpPptyhJzZyHw2Pqu4g9gTqDvEceYpngPf79jNZXCeTYQ4uArypFM3nh75"
err = writeCredentials(secretsDir, token2)
require.NoError(t, err)
start := time.Now()
for {
if hotreload.HcloudTokenReloadCounter > oldCounter {
if hotreload.GetHcloudReloadCounter() > oldCounter {
break
}
if time.Since(start) > time.Second*3 {
Expand Down
25 changes: 18 additions & 7 deletions internal/hotreload/hotreload.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"path/filepath"
"strings"
"sync"
"sync/atomic"

fsnotify "github.com/fsnotify/fsnotify"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
Expand All @@ -23,18 +22,30 @@ var (
oldRobotPassword string
oldHcloudToken string

// RobotReloadCounter gets incremented when the credentials get reloaded.
// robotReloadCounter gets incremented when the credentials get reloaded.
// Mosty used for testing.
RobotReloadCounter uint64
robotReloadCounter uint64

// HcloudTokenReloadCounter gets incremented when the credentials get reloaded.
// hcloudTokenReloadCounter gets incremented when the credentials get reloaded.
// Mosty used for testing.
HcloudTokenReloadCounter uint64
hcloudTokenReloadCounter uint64

hcloudMutex sync.Mutex
robotMutex sync.Mutex
)

func GetRobotReloadCounter() uint64 {
robotMutex.Lock()
defer robotMutex.Unlock()
return robotReloadCounter
}

func GetHcloudReloadCounter() uint64 {
hcloudMutex.Lock()
defer hcloudMutex.Unlock()
return hcloudTokenReloadCounter
}

// Watch the mounted secrets. Reload the credentials, when the files get updated. The robotClient can be nil.
func Watch(hetznerSecretDirectory string, hcloudClient *hcloud.Client, robotClient robotclient.Client) error {
watcher, err := fsnotify.NewWatcher()
Expand Down Expand Up @@ -112,7 +123,7 @@ func loadRobotCredentials(hetznerSecretDirectory string, robotClient robotclient
}
oldRobotUser = username
oldRobotPassword = password
atomic.AddUint64(&RobotReloadCounter, 1)
robotReloadCounter++
err = robotClient.SetCredentials(username, password)
if err != nil {
return fmt.Errorf("SetCredentials: %w", err)
Expand Down Expand Up @@ -160,7 +171,7 @@ func loadHcloudCredentials(hetznerSecretDirectory string, hcloudClient *hcloud.C
return nil
}
oldHcloudToken = token
atomic.AddUint64(&HcloudTokenReloadCounter, 1)
hcloudTokenReloadCounter++
hcloud.WithToken(token)(hcloudClient)
klog.Infof("Hetzner Cloud token updated to new value: %s...", token[:5])
return nil
Expand Down
6 changes: 3 additions & 3 deletions internal/robot/client/cache/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ func Test_updateRobotCredentials(t *testing.T) {
require.NoError(t, err)
require.Len(t, servers, 1)

oldCount := hotreload.RobotReloadCounter
oldCount := hotreload.GetRobotReloadCounter()
err = writeCredentials(tmp, "user2", "password2")
require.NoError(t, err)
start := time.Now()
for {
// if hotreload.RobotReloadCounter > oldCount {
if hotreload.RobotReloadCounter > oldCount {
// if hotreload.robotReloadCounter > oldCount {
if hotreload.GetRobotReloadCounter() > oldCount {
break
}
if time.Since(start) > time.Second*3 {
Expand Down

0 comments on commit 63ed4db

Please sign in to comment.