Skip to content

Commit a557c18

Browse files
committed
CLEANUP/MINOR: add fallback to AddMapEntry during map sync
Improve resilience during runtime map synchronization by checking if `SetMapEntry` fails with an "entry not found" error. If it does, fallback to using `AddMapEntry`. This avoids potential errors and prevents unnecessary reloads when adding new lines to maps.
1 parent 94081b2 commit a557c18

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

k8s/gate/haproxy/routes-maps.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"log/slog"
2020
"regexp"
21+
"strings"
2122

2223
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/haproxy/storage/maps"
2324
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/logging"
@@ -248,19 +249,22 @@ func (b *RouteMgrImpl) runtimeMapSync() error {
248249
}
249250
// else update the entry
250251
b.topManager.logger.LogAttrs(context.Background(), slog.LevelDebug, "Set map [runtime] entry", slog.String("map", mapData.RelativeFileName), slog.String("key", key))
251-
// TODO: fix here, it should be or SetMapEntry or AddMapEntry if the entry is new
252-
// This will work only if we change the backend, any addition of a new line will fail and trigger a reload
253-
// To fix if we want to avoid reloads
252+
254253
err := runtimeClient.SetMapEntry(mapID, key, routeValue)
255254
if err != nil {
256-
metrics.MapStorageOperations.WithLabelValues("runtime_set", "error").Inc()
257-
b.topManager.logger.LogAttrs(context.Background(), slog.LevelError,
258-
"[failure] Set map [runtime] entry",
259-
slog.String("map", mapData.RelativeFileName),
260-
slog.String("key", key),
261-
logging.LogAttrError(err),
262-
)
263-
return err
255+
if strings.Contains(err.Error(), "entry not found") {
256+
err = runtimeClient.AddMapEntry(mapID, key, routeValue)
257+
}
258+
if err != nil {
259+
metrics.MapStorageOperations.WithLabelValues("runtime_set", "error").Inc()
260+
b.topManager.logger.LogAttrs(context.Background(), slog.LevelError,
261+
"[failure] Set map [runtime] entry",
262+
slog.String("map", mapData.RelativeFileName),
263+
slog.String("key", key),
264+
logging.LogAttrError(err),
265+
)
266+
return err
267+
}
264268
}
265269
metrics.MapStorageOperations.WithLabelValues("runtime_set", "ok").Inc()
266270
b.topManager.logger.LogAttrs(context.Background(), slog.LevelDebug,

0 commit comments

Comments
 (0)