You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "Hemanth1205 (via GitHub)" <gi...@apache.org> on 2026/03/14 17:57:09 UTC

[PR] fix: update publishService/statusAdress logic in gateway and ingress controller files (#2730) [apisix-ingress-controller]

Hemanth1205 opened a new pull request, #2732:
URL: https://github.com/apache/apisix-ingress-controller/pull/2732

   <!-- Please answer these questions before submitting a pull request -->
   
   ### Type of change:
   
   <!-- Please delete options that are not relevant. -->
   
   <!-- Select all the options from below that matches the type your PR best -->
   
   - [X] Bugfix
   - [ ] New feature provided
   - [ ] Improve performance
   - [ ] Backport patches
   - [ ] Documentation
   - [ ] Refactor
   - [ ] Chore
   - [ ] CI/CD or Tests
   
   ### What this PR does / why we need it:
   
   <!--- Why is this change required? What problem does it solve? -->
   <!--- If it fixes an open issue, please link to the issue here. -->
   This PR fixes #2730 
   
   ### Pre-submission checklist:
   
   <!--
   Please follow the requirements:
   1. Use Draft if the PR is not ready to be reviewed
   2. Test is required for the feat/fix PR, unless you have a good reason
   3. Doc is required for the feat PR
   4. Use a new commit to resolve review instead of `push -f`
   5. Use "request review" to notify the reviewer once you have resolved the review
   -->
   
   - [X] Did you explain what problem does this PR solve? Or what new features have been added?
   - [X] Have you added corresponding test cases?
   - [X] Have you modified the corresponding document?
   - [X] Is this PR backward compatible? **If it is not backward compatible, please discuss on the [mailing list](https://github.com/apache/apisix-ingress-controller#community) first**
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: update publishService/statusAdress logic in gateway and ingress controller files (#2730) [apisix-ingress-controller]

Posted by "Hemanth1205 (via GitHub)" <gi...@apache.org>.
Hemanth1205 commented on PR #2732:
URL: https://github.com/apache/apisix-ingress-controller/pull/2732#issuecomment-4096839937

   > ## Review Comments
   > ### 1. `ServiceIndexRef` semantic mismatch in ClusterIP branch
   > In `ingress_controller.go`, the new ClusterIP branch uses:
   > 
   > ```go
   > ingressList := &networkingv1.IngressList{}
   > if err := r.List(ctx, ingressList, client.MatchingFields{
   >     indexer.ServiceIndexRef: indexer.GenIndexKey(namespace, name),
   > }); err != nil {
   > ```
   > 
   > Here `namespace` and `name` come from the `publishService` config (i.e., the APISIX gateway's ClusterIP Service). However, `ServiceIndexRef` is built by `IngressServiceIndexFunc`, which indexes Ingress objects by their **backend services** in `spec.rules[].http.paths[].backend.service.name` — not by any arbitrary service reference.
   > 
   > This works **only** when some Ingress in the cluster has the APISIX ClusterIP Service as a backend in its routing rules (which is the scenario in #2730 — a cloud ALB Ingress fronting the APISIX ClusterIP Service). But this is an **implicit assumption** that isn't documented anywhere.
   > 
   > **Suggestions:**
   > 
   > * Add a code comment explaining this assumption explicitly (e.g., "This relies on another Ingress existing in the cluster whose `spec.rules` backends point at this ClusterIP service").
   > * Log at info/debug level when `ingressList.Items` is empty after the query, so users can diagnose why ClusterIP `publishService` isn't propagating status.
   > 
   > ### 2. `len()` short-circuit condition prevents status updates when values change
   > This is a **pre-existing issue** in `gateway_controller.go`, but the PR builds on top of it and now makes it worse by adding the `Type` field:
   > 
   > ```go
   > if len(gateway.Status.Addresses) != len(gatewayProxy.Spec.StatusAddress) {
   >     for _, addr := range gatewayProxy.Spec.StatusAddress {
   > ```
   > 
   > This condition only triggers an update when the **count** of addresses changes. If a user modifies a `statusAddress` value without changing the count (e.g., `1.2.3.4` → `example.com`), the status won't be updated — the Gateway will keep the old address and the old (or missing) `Type`.
   > 
   > Since this PR introduces the `Type` field on `GatewayStatusAddress`, this short-circuit becomes more impactful: even if the existing addresses lack a `Type` (pre-upgrade state), they won't be backfilled as long as the count matches.
   > 
   > **Suggestion:** Replace the `len()` comparison with a proper equality check, e.g.:
   > 
   > ```go
   > if !reflect.DeepEqual(gateway.Status.Addresses, addrs) {
   > ```
   > 
   > or build `addrs` unconditionally and let the comparison guard the actual update call (similar to how `ingress_controller.go` already does it with `reflect.DeepEqual` at line 739).
   
   @Baoyuantop , thanks for bringing this up. I've updated both the files with the suggested changes. 
   
   Also added test cases to cover the new changes 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: update publishService/statusAdress logic in gateway and ingress controller files (#2730) [apisix-ingress-controller]

Posted by "Hemanth1205 (via GitHub)" <gi...@apache.org>.
Hemanth1205 commented on code in PR #2732:
URL: https://github.com/apache/apisix-ingress-controller/pull/2732#discussion_r2945704088


##########
internal/controller/ingress_controller.go:
##########
@@ -694,9 +695,13 @@ func (r *IngressReconciler) updateStatus(ctx context.Context, tctx *provider.Tra
 			if addr == "" {
 				continue
 			}
-			loadBalancerStatus.Ingress = append(loadBalancerStatus.Ingress, networkingv1.IngressLoadBalancerIngress{
-				IP: addr,
-			})
+			ingress := networkingv1.IngressLoadBalancerIngress{}
+			if net.ParseIP(addr) != nil {
+				ingress.IP = addr
+			} else {
+				ingress.Hostname = addr
+			}
+			loadBalancerStatus.Ingress = append(loadBalancerStatus.Ingress, ingress)

Review Comment:
   updated code



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: update publishService/statusAdress logic in gateway and ingress controller files (#2730) [apisix-ingress-controller]

Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop commented on code in PR #2732:
URL: https://github.com/apache/apisix-ingress-controller/pull/2732#discussion_r2939660265


##########
internal/controller/ingress_controller.go:
##########
@@ -731,6 +736,26 @@ func (r *IngressReconciler) updateStatus(ctx context.Context, tctx *provider.Tra
 						})
 					}
 				}
+			} else if svc.Spec.Type == corev1.ServiceTypeClusterIP {

Review Comment:
   Current Ingress Reconcile Process: Reconcile() → updateStatus() → Write to Ingress status.
   The logic of the ClusterIP code path is:
   1. Query all Ingresses that reference the ClusterIP service
   2. Extract the hostname from their .Status.LoadBalancer.Ingress
   3. Write the hostname to the status of the Ingress currently being reconciled
   
   Issue: If the Ingress being reconciled also references that ClusterIP service (via a service pointed to by `publishService`), then:
   - Ingress A is reconciled → Query the list of Ingresses referencing that service → The list may include Ingress A itself
   - Ingress A’s status is updated → triggers a new reconcile → queries again → potentially causing an infinite loop
   
   Although the reflect.DeepEqual check (L770) short-circuits when the status remains unchanged, this relies on a prerequisite: that the hostnames collected from other Ingresses remain unchanged between two reconciles. If multiple Ingresses are being reconciled concurrently, a race condition may lead to repeated updates.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: update publishService/statusAdress logic in gateway and ingress controller files (#2730) [apisix-ingress-controller]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2732:
URL: https://github.com/apache/apisix-ingress-controller/pull/2732#discussion_r2944195597


##########
internal/controller/ingress_controller.go:
##########
@@ -694,9 +695,13 @@ func (r *IngressReconciler) updateStatus(ctx context.Context, tctx *provider.Tra
 			if addr == "" {
 				continue
 			}
-			loadBalancerStatus.Ingress = append(loadBalancerStatus.Ingress, networkingv1.IngressLoadBalancerIngress{
-				IP: addr,
-			})
+			ingress := networkingv1.IngressLoadBalancerIngress{}
+			if net.ParseIP(addr) != nil {
+				ingress.IP = addr
+			} else {
+				ingress.Hostname = addr
+			}
+			loadBalancerStatus.Ingress = append(loadBalancerStatus.Ingress, ingress)

Review Comment:
   The local variable `ingress` shadows the function parameter `ingress *networkingv1.Ingress` (line 680). While it doesn't cause a bug in the current code because the parameter isn't accessed within this `for` loop body, it's fragile — a future edit inside this loop that intends to reference the outer `*networkingv1.Ingress` would silently use the wrong variable. Consider renaming this to something like `lbIngress` or `lbEntry` to avoid the shadowing.
   



##########
test/e2e/ingress/ingress.go:
##########
@@ -1294,4 +1296,226 @@ spec:
 				Should(Equal(http.StatusOK))
 		})
 	})
+
+	Context("Ingress Status Address", func() {
+		var gatewayProxyWithStatusAddressYaml = `
+apiVersion: apisix.apache.org/v1alpha1
+kind: GatewayProxy
+metadata:
+  name: apisix-proxy-config
+  namespace: %s
+spec:
+  statusAddress:
+  - %s
+  provider:
+    type: ControlPlane
+    controlPlane:
+      endpoints:
+      - %s
+      auth:
+        type: AdminKey
+        adminKey:
+          value: "%s"
+`
+		var gatewayProxyWithPublishServiceYaml = `
+apiVersion: apisix.apache.org/v1alpha1
+kind: GatewayProxy
+metadata:
+  name: apisix-proxy-config
+  namespace: %s
+spec:
+  publishService: %s/%s
+  provider:
+    type: ControlPlane
+    controlPlane:
+      endpoints:
+      - %s
+      auth:
+        type: AdminKey
+        adminKey:
+          value: "%s"
+`
+		var ingressClassYaml = `
+apiVersion: networking.k8s.io/v1
+kind: IngressClass
+metadata:
+  name: %s
+spec:
+  controller: "%s"
+  parameters:
+    apiGroup: "apisix.apache.org"
+    kind: "GatewayProxy"
+    name: "apisix-proxy-config"
+    namespace: "%s"
+    scope: "Namespace"
+`
+		var ingressYaml = `
+apiVersion: networking.k8s.io/v1
+kind: Ingress
+metadata:
+  name: %s
+spec:
+  ingressClassName: %s
+  rules:
+  - host: status.example.com
+    http:
+      paths:
+      - path: /
+        pathType: Prefix
+        backend:
+          service:
+            name: %s
+            port:
+              number: 80
+`
+		getIngressLBStatus := func(ingressName string) ([]networkingv1.IngressLoadBalancerIngress, error) {
+			ing := &networkingv1.Ingress{}
+			if err := s.K8sClient.Get(context.Background(), types.NamespacedName{
+				Name:      ingressName,
+				Namespace: s.Namespace(),
+			}, ing); err != nil {
+				return nil, err
+			}
+			return ing.Status.LoadBalancer.Ingress, nil
+		}
+
+		checkIngressStatusAddress := func(addrValue, ingressSuffix, expectedIP, expectedHostname string) {
+			ingressClassName := s.Namespace()
+
+			By("create GatewayProxy with statusAddress")
+			gatewayProxy := fmt.Sprintf(gatewayProxyWithStatusAddressYaml,
+				s.Namespace(), addrValue, s.Deployer.GetAdminEndpoint(), s.AdminKey())
+			Expect(s.CreateResourceFromStringWithNamespace(gatewayProxy, s.Namespace())).NotTo(HaveOccurred(), "creating GatewayProxy")
+
+			By("create IngressClass")
+			Expect(s.CreateResourceFromStringWithNamespace(
+				fmt.Sprintf(ingressClassYaml, ingressClassName, s.GetControllerName(), s.Namespace()), ""),
+			).NotTo(HaveOccurred(), "creating IngressClass")

Review Comment:
   The `checkIngressStatusAddress` helper creates a cluster-scoped `IngressClass` (line 1391-1393) but does not clean it up with `DeferCleanup`. In contrast, the ClusterIP test below (line 1514-1518) properly cleans up its IngressClass. This could leak cluster-scoped resources across test runs. Consider adding `DeferCleanup` inside `checkIngressStatusAddress` to delete the IngressClass after each test, consistent with the pattern used in the ClusterIP test case.
   



##########
internal/controller/ingress_controller.go:
##########
@@ -731,6 +737,33 @@ func (r *IngressReconciler) updateStatus(ctx context.Context, tctx *provider.Tra
 						})
 					}
 				}
+			case corev1.ServiceTypeClusterIP:
+				// for ClusterIP services, find Ingresses that reference this service
+				// and collect hostnames from their load balancer status
+				// this is when we run the apisix in ClusterIP mode and enable Ingress
+				// when deploying in Cloud environments.
+				ingressList := &networkingv1.IngressList{}
+				if err := r.List(ctx, ingressList, client.MatchingFields{
+					indexer.ServiceIndexRef: indexer.GenIndexKey(namespace, name),
+				}); err != nil {
+					return fmt.Errorf("failed to list ingresses for ClusterIP service %s/%s: %w", namespace, name, err)
+				}
+				for _, ing := range ingressList.Items {
+					// Skip the current Ingress being reconciled to avoid a
+					// self-referential loop: updating its own status would trigger
+					// a new reconcile, which would collect its own (just-written)
+					// hostname again and potentially repeat indefinitely.
+					if ing.Namespace == ingress.Namespace && ing.Name == ingress.Name {
+						continue
+					}
+					for _, lb := range ing.Status.LoadBalancer.Ingress {
+						if lb.Hostname != "" {
+							loadBalancerStatus.Ingress = append(loadBalancerStatus.Ingress, networkingv1.IngressLoadBalancerIngress{
+								Hostname: lb.Hostname,
+							})
+						}
+					}

Review Comment:
   In the ClusterIP case, only `Hostname` values are propagated from referenced Ingresses, but `IP` values from their load balancer status are silently ignored. If an Ingress fronting the ClusterIP service has an IP-based load balancer status (e.g., `lb.IP` is set but `lb.Hostname` is empty), that address won't be propagated. Consider also propagating `lb.IP` for completeness, similar to the LoadBalancer case above which handles both IP and Hostname.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: update publishService/statusAdress logic in gateway and ingress controller files (#2730) [apisix-ingress-controller]

Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop merged PR #2732:
URL: https://github.com/apache/apisix-ingress-controller/pull/2732


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: update publishService/statusAdress logic in gateway and ingress controller files (#2730) [apisix-ingress-controller]

Posted by "hemanthjk-c (via GitHub)" <gi...@apache.org>.
hemanthjk-c commented on PR #2732:
URL: https://github.com/apache/apisix-ingress-controller/pull/2732#issuecomment-4197267279

   @Baoyuantop , since all the checks are complete and tests are passing can you merge the changes ?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: update publishService/statusAdress logic in gateway and ingress controller files (#2730) [apisix-ingress-controller]

Posted by "Hemanth1205 (via GitHub)" <gi...@apache.org>.
Hemanth1205 commented on code in PR #2732:
URL: https://github.com/apache/apisix-ingress-controller/pull/2732#discussion_r2941173154


##########
internal/controller/ingress_controller.go:
##########
@@ -731,6 +736,26 @@ func (r *IngressReconciler) updateStatus(ctx context.Context, tctx *provider.Tra
 						})
 					}
 				}
+			} else if svc.Spec.Type == corev1.ServiceTypeClusterIP {

Review Comment:
   updated the condition to skip the current ingress and pushed changes



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: update publishService/statusAdress logic in gateway and ingress controller files (#2730) [apisix-ingress-controller]

Posted by "Hemanth1205 (via GitHub)" <gi...@apache.org>.
Hemanth1205 commented on code in PR #2732:
URL: https://github.com/apache/apisix-ingress-controller/pull/2732#discussion_r2945705428


##########
internal/controller/ingress_controller.go:
##########
@@ -731,6 +737,33 @@ func (r *IngressReconciler) updateStatus(ctx context.Context, tctx *provider.Tra
 						})
 					}
 				}
+			case corev1.ServiceTypeClusterIP:
+				// for ClusterIP services, find Ingresses that reference this service
+				// and collect hostnames from their load balancer status
+				// this is when we run the apisix in ClusterIP mode and enable Ingress
+				// when deploying in Cloud environments.
+				ingressList := &networkingv1.IngressList{}
+				if err := r.List(ctx, ingressList, client.MatchingFields{
+					indexer.ServiceIndexRef: indexer.GenIndexKey(namespace, name),
+				}); err != nil {
+					return fmt.Errorf("failed to list ingresses for ClusterIP service %s/%s: %w", namespace, name, err)
+				}
+				for _, ing := range ingressList.Items {
+					// Skip the current Ingress being reconciled to avoid a
+					// self-referential loop: updating its own status would trigger
+					// a new reconcile, which would collect its own (just-written)
+					// hostname again and potentially repeat indefinitely.
+					if ing.Namespace == ingress.Namespace && ing.Name == ingress.Name {
+						continue
+					}
+					for _, lb := range ing.Status.LoadBalancer.Ingress {
+						if lb.Hostname != "" {
+							loadBalancerStatus.Ingress = append(loadBalancerStatus.Ingress, networkingv1.IngressLoadBalancerIngress{
+								Hostname: lb.Hostname,
+							})
+						}
+					}

Review Comment:
   updated code



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: update publishService/statusAdress logic in gateway and ingress controller files (#2730) [apisix-ingress-controller]

Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop commented on PR #2732:
URL: https://github.com/apache/apisix-ingress-controller/pull/2732#issuecomment-4089288373

   ## Review Comments
   
   ### 1. `ServiceIndexRef` semantic mismatch in ClusterIP branch
   
   In `ingress_controller.go`, the new ClusterIP branch uses:
   
   ```go
   ingressList := &networkingv1.IngressList{}
   if err := r.List(ctx, ingressList, client.MatchingFields{
       indexer.ServiceIndexRef: indexer.GenIndexKey(namespace, name),
   }); err != nil {
   ```
   
   Here `namespace` and `name` come from the `publishService` config (i.e., the APISIX gateway's ClusterIP Service). However, `ServiceIndexRef` is built by `IngressServiceIndexFunc`, which indexes Ingress objects by their **backend services** in `spec.rules[].http.paths[].backend.service.name` — not by any arbitrary service reference.
   
   This works **only** when some Ingress in the cluster has the APISIX ClusterIP Service as a backend in its routing rules (which is the scenario in #2730 — a cloud ALB Ingress fronting the APISIX ClusterIP Service). But this is an **implicit assumption** that isn't documented anywhere.
   
   **Suggestions:**
   - Add a code comment explaining this assumption explicitly (e.g., "This relies on another Ingress existing in the cluster whose `spec.rules` backends point at this ClusterIP service").
   - Log at info/debug level when `ingressList.Items` is empty after the query, so users can diagnose why ClusterIP `publishService` isn't propagating status.
   
   ### 2. `len()` short-circuit condition prevents status updates when values change
   
   This is a **pre-existing issue** in `gateway_controller.go`, but the PR builds on top of it and now makes it worse by adding the `Type` field:
   
   ```go
   if len(gateway.Status.Addresses) != len(gatewayProxy.Spec.StatusAddress) {
       for _, addr := range gatewayProxy.Spec.StatusAddress {
   ```
   
   This condition only triggers an update when the **count** of addresses changes. If a user modifies a `statusAddress` value without changing the count (e.g., `1.2.3.4` → `example.com`), the status won't be updated — the Gateway will keep the old address and the old (or missing) `Type`.
   
   Since this PR introduces the `Type` field on `GatewayStatusAddress`, this short-circuit becomes more impactful: even if the existing addresses lack a `Type` (pre-upgrade state), they won't be backfilled as long as the count matches.
   
   **Suggestion:** Replace the `len()` comparison with a proper equality check, e.g.:
   
   ```go
   if !reflect.DeepEqual(gateway.Status.Addresses, addrs) {
   ```
   
   or build `addrs` unconditionally and let the comparison guard the actual update call (similar to how `ingress_controller.go` already does it with `reflect.DeepEqual` at line 739).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org