Skip to content

Commit

Permalink
fix gateway deployment controller leader election event handler leak (i…
Browse files Browse the repository at this point in the history
  • Loading branch information
hzxuzhonghu authored Dec 13, 2022
1 parent 8203053 commit 4813d52
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions pilot/pkg/config/kube/gateway/deploymentcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ type DeploymentController struct {
patcher patcher
gatewayLister lister.GatewayLister
gatewayClassLister lister.GatewayClassLister

serviceInformer cache.SharedIndexInformer
serviceHandle cache.ResourceEventHandlerRegistration
deploymentInformer cache.SharedIndexInformer
deploymentHandle cache.ResourceEventHandlerRegistration
gwInformer cache.SharedIndexInformer
gwHandle cache.ResourceEventHandlerRegistration
gwClassInformer cache.SharedIndexInformer
gwClassHandle cache.ResourceEventHandlerRegistration
}

// Patcher is a function that abstracts patching logic. This is largely because client-go fakes do not handle patching
Expand Down Expand Up @@ -111,7 +120,8 @@ func NewDeploymentController(client kube.Client) *DeploymentController {

// Use the full informer, since we are already fetching all Services for other purposes
// If we somehow stop watching Services in the future we can add a label selector like below.
_, _ = client.KubeInformer().Core().V1().Services().Informer().
dc.serviceInformer = client.KubeInformer().Core().V1().Services().Informer()
dc.serviceHandle, _ = client.KubeInformer().Core().V1().Services().Informer().
AddEventHandler(handler)

// For Deployments, this is the only controller watching. We can filter to just the deployments we care about
Expand All @@ -124,12 +134,14 @@ func NewDeploymentController(client kube.Client) *DeploymentController {
)
})
_ = deployInformer.SetTransform(kube.StripUnusedFields)
_, _ = deployInformer.AddEventHandler(handler)
dc.deploymentHandle, _ = deployInformer.AddEventHandler(handler)
dc.deploymentInformer = deployInformer

// Use the full informer; we are already watching all Gateways for the core Istiod logic
_, _ = gw.Informer().AddEventHandler(controllers.ObjectHandler(dc.queue.AddObject))
_, _ = gwc.Informer().AddEventHandler(controllers.ObjectHandler(func(o controllers.Object) {
o.GetName()
dc.gwInformer = gw.Informer()
dc.gwClassHandle, _ = dc.gwInformer.AddEventHandler(controllers.ObjectHandler(dc.queue.AddObject))
dc.gwClassInformer = gwc.Informer()
dc.gwClassHandle, _ = dc.gwClassInformer.AddEventHandler(controllers.ObjectHandler(func(o controllers.Object) {
gws, _ := dc.gatewayLister.List(klabels.Everything())
for _, g := range gws {
if string(g.Spec.GatewayClassName) == o.GetName() {
Expand All @@ -143,6 +155,10 @@ func NewDeploymentController(client kube.Client) *DeploymentController {

func (d *DeploymentController) Run(stop <-chan struct{}) {
d.queue.Run(stop)
_ = d.serviceInformer.RemoveEventHandler(d.serviceHandle)
_ = d.deploymentInformer.RemoveEventHandler(d.deploymentHandle)
_ = d.gwInformer.RemoveEventHandler(d.gwHandle)
_ = d.gwClassInformer.RemoveEventHandler(d.gwClassHandle)
}

// Reconcile takes in the name of a Gateway and ensures the cluster is in the desired state
Expand Down

0 comments on commit 4813d52

Please sign in to comment.