Skip to content

Commit ef71abb

Browse files
committed
Address code review; fix unnecessary updates
1 parent bb5ab5f commit ef71abb

File tree

5 files changed

+32
-15
lines changed

5 files changed

+32
-15
lines changed

apis/v1alpha2/nginxproxy_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ type DaemonSetSpec struct {
411411
Container ContainerSpec `json:"container,omitempty"`
412412
}
413413

414-
// Pod defines Pod-specific fields.
414+
// PodSpec defines Pod-specific fields.
415415
type PodSpec struct {
416416
// TerminationGracePeriodSeconds is the optional duration in seconds the pod needs to terminate gracefully.
417417
// Value must be non-negative integer. The value zero indicates stop immediately via
@@ -454,7 +454,7 @@ type PodSpec struct {
454454
TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
455455
}
456456

457-
// Container defines container fields for the nginx container.
457+
// ContainerSpec defines container fields for the nginx container.
458458
type ContainerSpec struct {
459459
// Image is the nginx image to use.
460460
//
@@ -512,7 +512,7 @@ const (
512512
PullIfNotPresent PullPolicy = PullPolicy(corev1.PullIfNotPresent)
513513
)
514514

515-
// Service is the configuration for the nginx Service.
515+
// ServiceSpec is the configuration for the nginx Service.
516516
type ServiceSpec struct {
517517
// ServiceType describes ingress method for the service.
518518
//

internal/mode/static/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type eventHandlerConfig struct {
4141
ctx context.Context
4242
// nginxUpdater updates nginx configuration using the NGINX agent.
4343
nginxUpdater agent.NginxUpdater
44-
// provisioner handles provisioning and deprovisioning nginx resources.
44+
// nginxProvisioner handles provisioning and deprovisioning nginx resources.
4545
nginxProvisioner provisioner.Provisioner
4646
// metricsCollector collects metrics for this controller.
4747
metricsCollector handlerMetricsCollector

internal/mode/static/provisioner/provisioner.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ func (p *NginxProvisioner) RegisterGateway(
281281
resourceName string,
282282
) error {
283283
gatewayNSName := client.ObjectKeyFromObject(gateway.Source)
284-
p.store.registerResourceInGatewayConfig(gatewayNSName, gateway)
284+
if updated := p.store.registerResourceInGatewayConfig(gatewayNSName, gateway); !updated {
285+
return nil
286+
}
285287

286288
if gateway.Valid {
287289
if err := p.provisionNginx(ctx, resourceName, gateway.Source, gateway.EffectiveNginxProxy); err != nil {

internal/mode/static/provisioner/store.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package provisioner
22

33
import (
4+
"reflect"
45
"strings"
56
"sync"
67

@@ -62,7 +63,11 @@ func (s *store) getGateway(nsName types.NamespacedName) *gatewayv1.Gateway {
6263
return s.gateways[nsName]
6364
}
6465

65-
func (s *store) registerResourceInGatewayConfig(gatewayNSName types.NamespacedName, object interface{}) {
66+
// registerResourceInGatewayConfig adds or updates the provided resource in the tracking map.
67+
// If the object being updated is the Gateway, check if anything that we care about changed. This ensures that
68+
// we don't attempt to update nginx resources when the main event handler triggers this call with an unrelated event
69+
// (like a Route update) that shouldn't result in nginx resource changes.
70+
func (s *store) registerResourceInGatewayConfig(gatewayNSName types.NamespacedName, object interface{}) bool {
6671
s.lock.Lock()
6772
defer s.lock.Unlock()
6873

@@ -73,8 +78,9 @@ func (s *store) registerResourceInGatewayConfig(gatewayNSName types.NamespacedNa
7378
Gateway: obj,
7479
}
7580
} else {
81+
changed := gatewayChanged(cfg.Gateway, obj)
7682
cfg.Gateway = obj
77-
s.nginxResources[gatewayNSName] = cfg
83+
return changed
7884
}
7985
case *appsv1.Deployment:
8086
if cfg, ok := s.nginxResources[gatewayNSName]; !ok {
@@ -83,7 +89,6 @@ func (s *store) registerResourceInGatewayConfig(gatewayNSName types.NamespacedNa
8389
}
8490
} else {
8591
cfg.Deployment = obj
86-
s.nginxResources[gatewayNSName] = cfg
8792
}
8893
case *corev1.Service:
8994
if cfg, ok := s.nginxResources[gatewayNSName]; !ok {
@@ -92,7 +97,6 @@ func (s *store) registerResourceInGatewayConfig(gatewayNSName types.NamespacedNa
9297
}
9398
} else {
9499
cfg.Service = obj
95-
s.nginxResources[gatewayNSName] = cfg
96100
}
97101
case *corev1.ServiceAccount:
98102
if cfg, ok := s.nginxResources[gatewayNSName]; !ok {
@@ -101,7 +105,6 @@ func (s *store) registerResourceInGatewayConfig(gatewayNSName types.NamespacedNa
101105
}
102106
} else {
103107
cfg.ServiceAccount = obj
104-
s.nginxResources[gatewayNSName] = cfg
105108
}
106109
case *corev1.ConfigMap:
107110
if cfg, ok := s.nginxResources[gatewayNSName]; !ok {
@@ -117,13 +120,25 @@ func (s *store) registerResourceInGatewayConfig(gatewayNSName types.NamespacedNa
117120
} else {
118121
if strings.HasSuffix(obj.GetName(), nginxIncludesConfigMapNameSuffix) {
119122
cfg.BootstrapConfigMap = obj
120-
s.nginxResources[gatewayNSName] = cfg
121123
} else if strings.HasSuffix(obj.GetName(), nginxAgentConfigMapNameSuffix) {
122124
cfg.AgentConfigMap = obj
123-
s.nginxResources[gatewayNSName] = cfg
124125
}
125126
}
126127
}
128+
129+
return true
130+
}
131+
132+
func gatewayChanged(original, updated *graph.Gateway) bool {
133+
if original.Valid != updated.Valid {
134+
return true
135+
}
136+
137+
if !reflect.DeepEqual(original.Source, updated.Source) {
138+
return true
139+
}
140+
141+
return !reflect.DeepEqual(original.EffectiveNginxProxy, updated.EffectiveNginxProxy)
127142
}
128143

129144
func (s *store) getNginxResourcesForGateway(nsName types.NamespacedName) *NginxResources {

site/content/reference/api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,7 +1995,7 @@ sigs.k8s.io/gateway-api/apis/v1alpha2.PolicyStatus
19951995
<a href="#gateway.nginx.org/v1alpha2.DeploymentSpec">DeploymentSpec</a>)
19961996
</p>
19971997
<p>
1998-
<p>Container defines container fields for the nginx container.</p>
1998+
<p>ContainerSpec defines container fields for the nginx container.</p>
19991999
</p>
20002000
<table class="table table-bordered table-striped">
20012001
<thead>
@@ -2809,7 +2809,7 @@ be unique across all targetRef entries in the ObservabilityPolicy.</p>
28092809
<a href="#gateway.nginx.org/v1alpha2.DeploymentSpec">DeploymentSpec</a>)
28102810
</p>
28112811
<p>
2812-
<p>Pod defines Pod-specific fields.</p>
2812+
<p>PodSpec defines Pod-specific fields.</p>
28132813
</p>
28142814
<table class="table table-bordered table-striped">
28152815
<thead>
@@ -3130,7 +3130,7 @@ IP address in the X-Forwarded-For HTTP header.
31303130
<a href="#gateway.nginx.org/v1alpha2.KubernetesSpec">KubernetesSpec</a>)
31313131
</p>
31323132
<p>
3133-
<p>Service is the configuration for the nginx Service.</p>
3133+
<p>ServiceSpec is the configuration for the nginx Service.</p>
31343134
</p>
31353135
<table class="table table-bordered table-striped">
31363136
<thead>

0 commit comments

Comments
 (0)