Skip to content

Commit cc072a4

Browse files
jukiezirain
andcommitted
Optimize pod cache (envoyproxy#6936)
* Optimize pod cache Signed-off-by: jukie <10012479+Jukie@users.noreply.github.com> * release note Signed-off-by: jukie <10012479+Jukie@users.noreply.github.com> * Remove retry Signed-off-by: jukie <10012479+Jukie@users.noreply.github.com> * cleanup Signed-off-by: jukie <10012479+Jukie@users.noreply.github.com> --------- Signed-off-by: jukie <10012479+Jukie@users.noreply.github.com> Signed-off-by: Isaac <10012479+jukie@users.noreply.github.com> Co-authored-by: zirain <zirain2009@gmail.com>
1 parent 4f05e61 commit cc072a4

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

internal/provider/kubernetes/kubernetes.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"fmt"
1111
"time"
1212

13+
corev1 "k8s.io/api/core/v1"
14+
"k8s.io/apimachinery/pkg/labels"
1315
"k8s.io/client-go/rest"
1416
"k8s.io/klog/v2"
1517
"k8s.io/utils/ptr"
@@ -26,6 +28,7 @@ import (
2628
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
2729
"github.com/envoyproxy/gateway/internal/envoygateway"
2830
ec "github.com/envoyproxy/gateway/internal/envoygateway/config"
31+
"github.com/envoyproxy/gateway/internal/infrastructure/kubernetes/proxy"
2932
"github.com/envoyproxy/gateway/internal/message"
3033
)
3134

@@ -112,6 +115,16 @@ func New(ctx context.Context, restCfg *rest.Config, svrCfg *ec.Server, resources
112115
mgrOpts.Cache.SyncPeriod = ptr.To(csp)
113116
}
114117

118+
// Limit the cache to only Envoy proxy Pods to reduce memory and sync churn.
119+
// ProxyTopologyInjector is the only component that interacts with Pods.
120+
if mgrOpts.Cache.ByObject == nil {
121+
mgrOpts.Cache.ByObject = map[client.Object]cache.ByObject{}
122+
}
123+
124+
mgrOpts.Cache.ByObject[&corev1.Pod{}] = cache.ByObject{
125+
Label: labels.SelectorFromSet(proxy.EnvoyAppLabel()),
126+
}
127+
115128
if svrCfg.EnvoyGateway.NamespaceMode() {
116129
mgrOpts.Cache.DefaultNamespaces = make(map[string]cache.Config)
117130
for _, watchNS := range svrCfg.EnvoyGateway.Provider.Kubernetes.Watch.Namespaces {
@@ -134,9 +147,10 @@ func New(ctx context.Context, restCfg *rest.Config, svrCfg *ec.Server, resources
134147
if svrCfg.EnvoyGateway.Provider.Kubernetes.TopologyInjector == nil || !ptr.Deref(svrCfg.EnvoyGateway.Provider.Kubernetes.TopologyInjector.Disable, false) {
135148
mgr.GetWebhookServer().Register("/inject-pod-topology", &webhook.Admission{
136149
Handler: &ProxyTopologyInjector{
137-
Client: mgr.GetClient(),
138-
Logger: svrCfg.Logger.WithName("proxy-topology-injector"),
139-
Decoder: admission.NewDecoder(mgr.GetScheme()),
150+
Client: mgr.GetClient(),
151+
APIReader: mgr.GetAPIReader(),
152+
Logger: svrCfg.Logger.WithName("proxy-topology-injector"),
153+
Decoder: admission.NewDecoder(mgr.GetScheme()),
140154
},
141155
})
142156
}

internal/provider/kubernetes/topology_injector.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222

2323
type ProxyTopologyInjector struct {
2424
client.Client
25-
Decoder admission.Decoder
26-
27-
Logger logging.Logger
25+
APIReader client.Reader
26+
Decoder admission.Decoder
27+
Logger logging.Logger
2828
}
2929

3030
func (m *ProxyTopologyInjector) Handle(ctx context.Context, req admission.Request) admission.Response {
@@ -50,9 +50,13 @@ func (m *ProxyTopologyInjector) Handle(ctx context.Context, req admission.Reques
5050

5151
pod := &corev1.Pod{}
5252
if err := m.Get(ctx, podName, pod); err != nil {
53-
logger.Error(err, "get pod failed", "pod", podName.String())
54-
topologyInjectorEventsTotal.WithFailure(metrics.ReasonError).Increment()
55-
return admission.Allowed("internal error, skipped")
53+
// Cache isn't guaranteed to be updated yet so if m.Get() fails
54+
// try getting the pod from API server directly.
55+
if err = m.APIReader.Get(ctx, podName, pod); err != nil {
56+
logger.Error(err, "get pod failed", "pod", podName.String())
57+
topologyInjectorEventsTotal.WithFailure(metrics.ReasonError).Increment()
58+
return admission.Allowed("internal error, skipped")
59+
}
5660
}
5761

5862
// Skip non-proxy pods

release-notes/current.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ bug fixes: |
1818
Fixed the controller cannot read the EnvoyProxy attached gatewayclass only.
1919
Fixed indexer and controller crashing when BackendTrafficPolicy has a redirect response override.
2020
Fixed Lua validator log level to be suppressed by default.
21+
Fixed ProxyTopologyInjector cache sync race condition that caused injection failures
2122
2223
# Enhancements that improve performance.
2324
performance improvements: |

0 commit comments

Comments
 (0)