Skip to content

Commit 8821341

Browse files
committed
use cached client from the controller manager
Signed-off-by: Huabing (Robin) Zhao <zhaohuabing@gmail.com>
1 parent 5ed1c4c commit 8821341

11 files changed

Lines changed: 336 additions & 282 deletions

File tree

internal/cmd/server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111

1212
"github.com/spf13/cobra"
13+
"sigs.k8s.io/controller-runtime/pkg/client"
1314

1415
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
1516
"github.com/envoyproxy/gateway/internal/admin"
@@ -177,6 +178,10 @@ func startRunners(ctx context.Context, cfg *config.Server, runnerErrors *message
177178
// The Elected channel is used to block the tasks that are waiting for the leader to be elected.
178179
// It will be closed once the leader is elected in the controller manager.
179180
cfg.Elected = make(chan struct{})
181+
// ProviderClient is a channel for sharing the k8s provider client between the provider runner and the infrastructure runner.
182+
// It will be initialized if the provider type is kubernetes, and the provider runner will send the client to this channel once it's created.
183+
// The infrastructure runner will receive the client from this channel when it starts, and use it to create the manager.
184+
cfg.ProviderClient = make(chan client.Client, 1)
180185

181186
// Setup the Extension Manager
182187
var extMgr types.Manager

internal/envoygateway/config/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"errors"
1010
"io"
1111

12+
"sigs.k8s.io/controller-runtime/pkg/client"
13+
1214
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
1315
"github.com/envoyproxy/gateway/api/v1alpha1/validation"
1416
"github.com/envoyproxy/gateway/internal/logging"
@@ -39,6 +41,10 @@ type Server struct {
3941
Logger logging.Logger
4042
// Elected chan is used to signal when an EG instance is elected as leader.
4143
Elected chan struct{}
44+
// ProviderClient is a channel for sharing the k8s provider client between the provider runner and the infrastructure runner.
45+
// It will be initialized if the provider type is kubernetes, and the provider runner will send the client to this channel once it's created.
46+
// The infrastructure runner will receive the client from this channel when it starts, and use it to create the manager.
47+
ProviderClient chan client.Client
4248
// Stdout is the writer for standard output.
4349
Stdout io.Writer
4450
// Stderr is the writer for error output.

internal/infrastructure/kubernetes/infra_resource.go

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,9 @@ func (i *Infra) deleteServiceAccount(ctx context.Context, r ResourceRender) (err
455455
// deleteDeployment deletes the Envoy Deployment in the kube api server, if it exists.
456456
func (i *Infra) deleteDeployment(ctx context.Context, r ResourceRender) (err error) {
457457
var (
458-
name, ns = r.Name(), r.Namespace()
459-
deployment = &appsv1.Deployment{
458+
name, ns = r.Name(), r.Namespace()
459+
recordDeleteMetric = true
460+
deployment = &appsv1.Deployment{
460461
ObjectMeta: metav1.ObjectMeta{
461462
Namespace: ns,
462463
Name: name,
@@ -470,25 +471,22 @@ func (i *Infra) deleteDeployment(ctx context.Context, r ResourceRender) (err err
470471
}
471472
)
472473

473-
// Check if any Deployments exist before attempting deletion to avoid
474-
// incrementing delete metrics on no-op reconciles.
474+
// DeleteAllOf always runs because this cached read may miss live objects and
475+
// must not decide whether deletion is skipped. It only suppresses success
476+
// metrics for likely no-op reconciles.
475477
deployList := &appsv1.DeploymentList{}
476-
if err = i.Client.List(ctx, deployList, &client.ListOptions{
478+
if listErr := i.Client.List(ctx, deployList, &client.ListOptions{
477479
Namespace: ns,
478480
LabelSelector: r.LabelSelector(),
479-
}); err != nil {
480-
resourceDeleteTotal.WithFailure(metrics.ReasonError, labels...).Increment()
481-
return err
482-
}
483-
if len(deployList.Items) == 0 {
484-
return nil
481+
}); listErr == nil && len(deployList.Items) == 0 {
482+
recordDeleteMetric = false
485483
}
486484

487485
defer func() {
488-
if err == nil {
486+
if err == nil && recordDeleteMetric {
489487
resourceDeleteDurationSeconds.With(labels...).Record(time.Since(startTime).Seconds())
490488
resourceDeleteTotal.WithSuccess(labels...).Increment()
491-
} else {
489+
} else if err != nil {
492490
resourceDeleteTotal.WithFailure(metrics.ReasonError, labels...).Increment()
493491
}
494492
}()
@@ -504,8 +502,9 @@ func (i *Infra) deleteDeployment(ctx context.Context, r ResourceRender) (err err
504502
// deleteDaemonSet deletes the Envoy DaemonSet in the kube api server, if it exists.
505503
func (i *Infra) deleteDaemonSet(ctx context.Context, r ResourceRender) (err error) {
506504
var (
507-
name, ns = r.Name(), r.Namespace()
508-
daemonSet = &appsv1.DaemonSet{
505+
name, ns = r.Name(), r.Namespace()
506+
recordDeleteMetric = true
507+
daemonSet = &appsv1.DaemonSet{
509508
ObjectMeta: metav1.ObjectMeta{
510509
Namespace: ns,
511510
Name: name,
@@ -519,25 +518,22 @@ func (i *Infra) deleteDaemonSet(ctx context.Context, r ResourceRender) (err erro
519518
}
520519
)
521520

522-
// Check if any DaemonSets exist before attempting deletion to avoid
523-
// incrementing delete metrics on no-op reconciles.
521+
// DeleteAllOf always runs because this cached read may miss live objects and
522+
// must not decide whether deletion is skipped. It only suppresses success
523+
// metrics for likely no-op reconciles.
524524
dsList := &appsv1.DaemonSetList{}
525-
if err = i.Client.List(ctx, dsList, &client.ListOptions{
525+
if listErr := i.Client.List(ctx, dsList, &client.ListOptions{
526526
Namespace: ns,
527527
LabelSelector: r.LabelSelector(),
528-
}); err != nil {
529-
resourceDeleteTotal.WithFailure(metrics.ReasonError, labels...).Increment()
530-
return err
531-
}
532-
if len(dsList.Items) == 0 {
533-
return nil
528+
}); listErr == nil && len(dsList.Items) == 0 {
529+
recordDeleteMetric = false
534530
}
535531

536532
defer func() {
537-
if err == nil {
533+
if err == nil && recordDeleteMetric {
538534
resourceDeleteDurationSeconds.With(labels...).Record(time.Since(startTime).Seconds())
539535
resourceDeleteTotal.WithSuccess(labels...).Increment()
540-
} else {
536+
} else if err != nil {
541537
resourceDeleteTotal.WithFailure(metrics.ReasonError, labels...).Increment()
542538
}
543539
}()
@@ -623,8 +619,9 @@ func (i *Infra) deleteService(ctx context.Context, r ResourceRender) (err error)
623619
// deleteHpa deletes the Horizontal Pod Autoscaler associated to its renderer, if it exists.
624620
func (i *Infra) deleteHPA(ctx context.Context, r ResourceRender) (err error) {
625621
var (
626-
name, ns = r.Name(), r.Namespace()
627-
hpa = &autoscalingv2.HorizontalPodAutoscaler{
622+
name, ns = r.Name(), r.Namespace()
623+
recordDeleteMetric = true
624+
hpa = &autoscalingv2.HorizontalPodAutoscaler{
628625
ObjectMeta: metav1.ObjectMeta{
629626
Namespace: ns,
630627
Name: name,
@@ -638,25 +635,22 @@ func (i *Infra) deleteHPA(ctx context.Context, r ResourceRender) (err error) {
638635
}
639636
)
640637

641-
// Check if any HPAs exist before attempting deletion to avoid
642-
// incrementing delete metrics on no-op reconciles.
638+
// DeleteAllOf always runs because this cached read may miss live objects and
639+
// must not decide whether deletion is skipped. It only suppresses success
640+
// metrics for likely no-op reconciles.
643641
hpaList := &autoscalingv2.HorizontalPodAutoscalerList{}
644-
if err = i.Client.List(ctx, hpaList, &client.ListOptions{
642+
if listErr := i.Client.List(ctx, hpaList, &client.ListOptions{
645643
Namespace: ns,
646644
LabelSelector: r.LabelSelector(),
647-
}); err != nil {
648-
resourceDeleteTotal.WithFailure(metrics.ReasonError, labels...).Increment()
649-
return err
650-
}
651-
if len(hpaList.Items) == 0 {
652-
return nil
645+
}); listErr == nil && len(hpaList.Items) == 0 {
646+
recordDeleteMetric = false
653647
}
654648

655649
defer func() {
656-
if err == nil {
650+
if err == nil && recordDeleteMetric {
657651
resourceDeleteDurationSeconds.With(labels...).Record(time.Since(startTime).Seconds())
658652
resourceDeleteTotal.WithSuccess(labels...).Increment()
659-
} else {
653+
} else if err != nil {
660654
resourceDeleteTotal.WithFailure(metrics.ReasonError, labels...).Increment()
661655
}
662656
}()
@@ -672,8 +666,9 @@ func (i *Infra) deleteHPA(ctx context.Context, r ResourceRender) (err error) {
672666
// deletePDB deletes the PodDistribution budget associated to its renderer, if it exists.
673667
func (i *Infra) deletePDB(ctx context.Context, r ResourceRender) (err error) {
674668
var (
675-
name, ns = r.Name(), r.Namespace()
676-
pdb = &policyv1.PodDisruptionBudget{
669+
name, ns = r.Name(), r.Namespace()
670+
recordDeleteMetric = true
671+
pdb = &policyv1.PodDisruptionBudget{
677672
ObjectMeta: metav1.ObjectMeta{
678673
Namespace: ns,
679674
Name: name,
@@ -687,25 +682,22 @@ func (i *Infra) deletePDB(ctx context.Context, r ResourceRender) (err error) {
687682
}
688683
)
689684

690-
// Check if any PDBs exist before attempting deletion to avoid
691-
// incrementing delete metrics on no-op reconciles.
685+
// DeleteAllOf always runs because this cached read may miss live objects and
686+
// must not decide whether deletion is skipped. It only suppresses success
687+
// metrics for likely no-op reconciles.
692688
pdbList := &policyv1.PodDisruptionBudgetList{}
693-
if err = i.Client.List(ctx, pdbList, &client.ListOptions{
689+
if listErr := i.Client.List(ctx, pdbList, &client.ListOptions{
694690
Namespace: ns,
695691
LabelSelector: r.LabelSelector(),
696-
}); err != nil {
697-
resourceDeleteTotal.WithFailure(metrics.ReasonError, labels...).Increment()
698-
return err
699-
}
700-
if len(pdbList.Items) == 0 {
701-
return nil
692+
}); listErr == nil && len(pdbList.Items) == 0 {
693+
recordDeleteMetric = false
702694
}
703695

704696
defer func() {
705-
if err == nil {
697+
if err == nil && recordDeleteMetric {
706698
resourceDeleteDurationSeconds.With(labels...).Record(time.Since(startTime).Seconds())
707699
resourceDeleteTotal.WithSuccess(labels...).Increment()
708-
} else {
700+
} else if err != nil {
709701
resourceDeleteTotal.WithFailure(metrics.ReasonError, labels...).Increment()
710702
}
711703
}()

0 commit comments

Comments
 (0)