Skip to content

Commit

Permalink
fix: prevent xdsIR updates from overwriting RateLimit configs from ot…
Browse files Browse the repository at this point in the history
…her xdsIR

Signed-off-by: Kensei Nakada <[email protected]>
  • Loading branch information
sanposhiho committed Jul 7, 2024
1 parent 9a2a7f6 commit b716627
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion internal/globalratelimit/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/envoyproxy/gateway/internal/message"
"github.com/envoyproxy/gateway/internal/xds/translator"
"github.com/envoyproxy/gateway/internal/xds/types"
cacheTypes "github.com/envoyproxy/go-control-plane/pkg/cache/types"

Check failure on line 34 in internal/globalratelimit/runner/runner.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/envoyproxy/gateway) (gci)
)

const (
Expand Down Expand Up @@ -110,6 +111,9 @@ func (r *Runner) serveXdsConfigServer(ctx context.Context) {
}

func (r *Runner) subscribeAndTranslate(ctx context.Context) {
// rateLimitConfigsCache is a cache of the rate limit config, which is keyed by the xdsIR key.
rateLimitConfigsCache := map[string][]cacheTypes.Resource{}

Check warning on line 115 in internal/globalratelimit/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/globalratelimit/runner/runner.go#L115

Added line #L115 was not covered by tests

// Subscribe to resources.
message.HandleSubscription(message.Metadata{Runner: string(egv1a1.LogComponentGlobalRateLimitRunner), Message: "xds-ir"}, r.XdsIR.Subscribe(ctx),
func(update message.Update[string, *ir.Xds], errChan chan error) {
Expand All @@ -120,6 +124,7 @@ func (r *Runner) subscribeAndTranslate(ctx context.Context) {
r.Logger.Error(err, "failed to update the config snapshot")
errChan <- err
}
delete(rateLimitConfigsCache, update.Key)

Check warning on line 127 in internal/globalratelimit/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/globalratelimit/runner/runner.go#L127

Added line #L127 was not covered by tests
} else {
// Translate to ratelimit xDS Config.
rvt, err := r.translate(update.Value)
Expand All @@ -129,7 +134,14 @@ func (r *Runner) subscribeAndTranslate(ctx context.Context) {

// Update ratelimit xDS config cache.
if rvt != nil {
r.updateSnapshot(ctx, rvt.XdsResources)
// Build XdsResources to use for the snapshot update from the cache.
xdsResourcesToUpdate := types.XdsResources{}
rateLimitConfigsCache[update.Key] = rvt.XdsResources[resourcev3.RateLimitConfigType]
for _, xdsR := range rateLimitConfigsCache {
xdsResourcesToUpdate[resourcev3.RateLimitConfigType] = append(xdsResourcesToUpdate[resourcev3.RateLimitConfigType], xdsR...)

Check warning on line 141 in internal/globalratelimit/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/globalratelimit/runner/runner.go#L138-L141

Added lines #L138 - L141 were not covered by tests
}

r.updateSnapshot(ctx, xdsResourcesToUpdate)

Check warning on line 144 in internal/globalratelimit/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/globalratelimit/runner/runner.go#L144

Added line #L144 was not covered by tests
}
}
},
Expand Down

0 comments on commit b716627

Please sign in to comment.