forked from envoyproxy/gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* `RateLimitInfraIR` - holds the config to manage the ratelimit service. * `global-ratelimit` runner that subscribes to `XdsIR` messages and translates it to `RateLimitInfraIR`. * enhance the `infrastructure` runner to subscribe to `RateLimitInfraIR` messages and translate it into platform specific ratelimit resources. Relates to envoyproxy#670 Signed-off-by: Arko Dasgupta <arko@tetrate.io>
- Loading branch information
Showing
10 changed files
with
303 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright Envoy Gateway Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// The full text of the Apache license is available in the LICENSE file at | ||
// the root of the repo. | ||
|
||
package runner | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/envoyproxy/gateway/internal/envoygateway/config" | ||
"github.com/envoyproxy/gateway/internal/ir" | ||
"github.com/envoyproxy/gateway/internal/message" | ||
"github.com/envoyproxy/gateway/internal/xds/translator" | ||
) | ||
|
||
type Config struct { | ||
config.Server | ||
XdsIR *message.XdsIR | ||
RateLimitInfraIR *message.RateLimitInfraIR | ||
} | ||
|
||
type Runner struct { | ||
Config | ||
} | ||
|
||
func (r *Runner) Name() string { | ||
return "global-ratelimit" | ||
} | ||
|
||
func New(cfg *Config) *Runner { | ||
return &Runner{Config: *cfg} | ||
} | ||
|
||
// Start starts the infrastructure runner | ||
func (r *Runner) Start(ctx context.Context) error { | ||
r.Logger = r.Logger.WithValues("runner", r.Name()) | ||
go r.subscribeAndTranslate(ctx) | ||
r.Logger.Info("started") | ||
return nil | ||
} | ||
|
||
func (r *Runner) subscribeAndTranslate(ctx context.Context) { | ||
// Subscribe | ||
xdsIRCh := r.XdsIR.Subscribe(ctx) | ||
for ctx.Err() == nil { | ||
var xdsIRs []*ir.Xds | ||
snapshot := <-xdsIRCh | ||
r.Logger.Info("received a notification") | ||
for _, value := range snapshot.State { | ||
xdsIRs = append(xdsIRs, value) | ||
} | ||
// Translate to ratelimit infra IR | ||
result, err := r.translate(xdsIRs) | ||
if err != nil { | ||
r.Logger.Error(err, "failed to translate xds ir") | ||
} else { | ||
if result == nil { | ||
r.RateLimitInfraIR.Delete(r.Name()) | ||
} else { | ||
// Publish ratelimit infra IR | ||
r.RateLimitInfraIR.Store(r.Name(), result) | ||
} | ||
} | ||
} | ||
r.Logger.Info("subscriber shutting down") | ||
} | ||
|
||
func (r *Runner) translate(xdsIRs []*ir.Xds) (*ir.RateLimitInfra, error) { | ||
rlInfra := new(ir.RateLimitInfra) | ||
|
||
// Return empty IR if ratelimit has not been enabled yet in the EnvoyGateway API | ||
if r.EnvoyGateway.RateLimit == nil { | ||
return nil, nil | ||
} | ||
|
||
for _, xdsIR := range xdsIRs { | ||
for _, listener := range xdsIR.HTTP { | ||
config := translator.BuildRateLimitServiceConfig(listener) | ||
if config != nil { | ||
str, err := translator.GetRateLimitServiceConfigStr(config) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get rate limit config string: %w", err) | ||
} | ||
c := &ir.RateLimitServiceConfig{ | ||
Name: listener.Name, | ||
Config: str, | ||
} | ||
rlInfra.Configs = append(rlInfra.Configs, c) | ||
} | ||
} | ||
} | ||
|
||
rlInfra.Backend = &ir.RateLimitDBBackend{ | ||
Redis: &ir.RateLimitRedis{ | ||
URL: r.EnvoyGateway.RateLimit.Backend.Redis.URL, | ||
}, | ||
} | ||
|
||
return rlInfra, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright Envoy Gateway Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// The full text of the Apache license is available in the LICENSE file at | ||
// the root of the repo. | ||
|
||
package ir | ||
|
||
// RateLimitInfra defines managed rate limit service infrastructure. | ||
// +k8s:deepcopy-gen=true | ||
type RateLimitInfra struct { | ||
// Rate limit service configuration | ||
Configs []*RateLimitServiceConfig | ||
// Backend holds configuration associated with the backend database. | ||
Backend *RateLimitDBBackend | ||
} | ||
|
||
// RateLimitServiceConfig holds the rate limit service configurations | ||
// defined here https://github.com/envoyproxy/ratelimit#configuration-1 | ||
// +k8s:deepcopy-gen=true | ||
type RateLimitServiceConfig struct { | ||
// Name of the config file. | ||
Name string | ||
// Config contents saved as a YAML string. | ||
Config string | ||
} | ||
|
||
// RateLimitDBBackend defines the database backend properties | ||
// associated with the rate limit service. | ||
// +k8s:deepcopy-gen=true | ||
type RateLimitDBBackend struct { | ||
// Redis backend details. | ||
Redis *RateLimitRedis | ||
} | ||
|
||
// RateLimitRedis defines the redis database configuration. | ||
// +k8s:deepcopy-gen=true | ||
type RateLimitRedis struct { | ||
// URL of the Redis Database. | ||
URL string | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.