Skip to content

Commit 97c2e50

Browse files
committed
address comments
Signed-off-by: Huabing (Robin) Zhao <zhaohuabing@gmail.com>
1 parent 88c09ae commit 97c2e50

3 files changed

Lines changed: 32 additions & 37 deletions

File tree

internal/infrastructure/kubernetes/ratelimit_infra_test.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,23 @@ import (
1616
"sigs.k8s.io/controller-runtime/pkg/client"
1717

1818
"github.com/envoyproxy/gateway/internal/infrastructure/kubernetes/ratelimit"
19-
"github.com/envoyproxy/gateway/internal/provider/kubernetes"
2019
)
2120

2221
func createRateLimitTLSSecret(t *testing.T, client client.Client) {
23-
_, secretErr := kubernetes.CreateOrUpdateSecrets(context.Background(), client, []corev1.Secret{
24-
{
25-
Type: corev1.SecretTypeTLS,
26-
TypeMeta: metav1.TypeMeta{
27-
Kind: "Secret",
28-
APIVersion: "v1",
29-
},
30-
ObjectMeta: metav1.ObjectMeta{
31-
Name: "ratelimit-cert",
32-
Namespace: "envoy-gateway-system",
33-
Labels: map[string]string{
34-
"control-plane": "envoy-gateway",
35-
},
22+
secretErr := client.Create(context.Background(), &corev1.Secret{
23+
Type: corev1.SecretTypeTLS,
24+
TypeMeta: metav1.TypeMeta{
25+
Kind: "Secret",
26+
APIVersion: "v1",
27+
},
28+
ObjectMeta: metav1.ObjectMeta{
29+
Name: "ratelimit-cert",
30+
Namespace: "envoy-gateway-system",
31+
Labels: map[string]string{
32+
"control-plane": "envoy-gateway",
3633
},
3734
},
38-
}, false)
35+
})
3936
require.NoError(t, secretErr)
4037
}
4138

internal/infrastructure/manager.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ import (
2020
"github.com/envoyproxy/gateway/internal/message"
2121
)
2222

23-
var (
24-
// globalClient is the cached client used by infrastructure managers
25-
globalClient client.Client
26-
)
23+
// globalClient is the cached client used by infrastructure managers
24+
var globalClient client.Client
2725

2826
// SetGlobalClient sets the global cached client for infrastructure managers.
29-
// This should be called by the provider after creating the manager.
27+
// This should be called by the kubernetes provider after creating the manager.
3028
func SetGlobalClient(cli client.Client) {
3129
globalClient = cli
3230
}

internal/infrastructure/runner/runner.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package runner
77

88
import (
99
"context"
10+
"sync"
1011

1112
"github.com/telepresenceio/watchable"
1213
"k8s.io/utils/ptr"
@@ -27,6 +28,8 @@ type Config struct {
2728
type Runner struct {
2829
Config
2930
mgr infrastructure.Manager
31+
32+
rateLimitInfraOnce sync.Once
3033
}
3134

3235
// Close implements Runner interface.
@@ -52,6 +55,7 @@ func (r *Runner) Start(ctx context.Context) (err error) {
5255
return nil
5356
}
5457
errNotifier := message.RunnerErrorNotifier{RunnerName: r.Name(), RunnerErrors: r.RunnerErrors}
58+
5559
r.mgr, err = infrastructure.NewManager(ctx, &r.Server, r.Logger, errNotifier)
5660
if err != nil {
5761
r.Logger.Error(err, "failed to create new manager")
@@ -64,17 +68,6 @@ func (r *Runner) Start(ctx context.Context) (err error) {
6468
sub := r.InfraIR.Subscribe(ctx)
6569
go r.updateProxyInfraFromSubscription(ctx, sub)
6670

67-
// Enable global ratelimit if it has been configured.
68-
if r.EnvoyGateway.RateLimit != nil {
69-
go r.enableRateLimitInfra(ctx)
70-
} else {
71-
// Delete the ratelimit infra if it exists.
72-
go func() {
73-
if err := r.mgr.DeleteRateLimitInfra(ctx); err != nil {
74-
r.Logger.Error(err, "failed to delete ratelimit infra")
75-
}
76-
}()
77-
}
7871
r.Logger.Info("started")
7972
<-ctx.Done()
8073
r.InfraIR.Close()
@@ -132,13 +125,20 @@ func (r *Runner) updateProxyInfraFromSubscription(ctx context.Context, sub <-cha
132125
errChan <- err
133126
}
134127
}
128+
129+
// Handle rate limit infra once on first IR update.
130+
r.rateLimitInfraOnce.Do(func() {
131+
if r.EnvoyGateway.RateLimit != nil {
132+
if err := r.mgr.CreateOrUpdateRateLimitInfra(ctx); err != nil {
133+
r.Logger.Error(err, "failed to create ratelimit infra")
134+
}
135+
} else {
136+
if err := r.mgr.DeleteRateLimitInfra(ctx); err != nil {
137+
r.Logger.Error(err, "failed to delete ratelimit infra")
138+
}
139+
}
140+
})
135141
},
136142
)
137143
r.Logger.Info("infra subscriber shutting down")
138144
}
139-
140-
func (r *Runner) enableRateLimitInfra(ctx context.Context) {
141-
if err := r.mgr.CreateOrUpdateRateLimitInfra(ctx); err != nil {
142-
r.Logger.Error(err, "failed to create ratelimit infra")
143-
}
144-
}

0 commit comments

Comments
 (0)