Skip to content

fix: add null-safety checks for labels and services in K8s RPC ingress parsers - #6896

Closed
wy471x wants to merge 11 commits into
apache:masterfrom
wy471x:fix_K8s-RPC-ingress-parsers-error
Closed

wy471x wants to merge 11 commits into
apache:masterfrom
wy471x:fix_K8s-RPC-ingress-parsers-error

Conversation

@wy471x

@wy471x wy471x commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Make sure that:

  • You have read the contribution guidelines.
  • You submit test cases (unit or integration tests) that back your changes.
  • Your local test passed ./mvnw clean install -Dmaven.javadoc.skip=true.

Summary

Bug Fix (3 files)

DubboIngressParser.java, GrpcParser.java, SofaParser.java — in each parseIngressRule method, added null-safety guards before iterating over labels:

  1. Null/empty labels check — if labels is null or empty, skip the label iteration with continue instead of throwing NullPointerException.
  2. Missing service check — before calling .getMetadata().getAnnotations(), check if serviceLister.namespace(namespace).get(...) returned null (service not yet in cache). Log a
    warning and continue, allowing a later reconcile to pick it up once the service cache is populated.

Unit Tests (3 new files, 9 tests)

  • DubboIngressParserTest.java — 3 tests
  • GrpcParserTest.java — 3 tests
  • SofaParserTest.java — 3 tests

close #6486

@Aias00 Aias00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @wy471x!

The null-safety guards are correct and well-placed:

  • ingress.getMetadata().getLabels() is passed straight into parseIngressRule and can be null for an ingress without labels, so the old labels.keySet() was a guaranteed NPE; the null/empty guard fixes that. Skipping the whole path entry (rather than registering an IngressConfiguration with empty rules/metadata) is the right outcome — a selector with no rules and no service metadata would be meaningless.
  • Guarding the serviceLister lookup is equally important: the service may legitimately not be in the informer cache yet during startup/reconcile races, and warn-and-continue lets a later reconcile pick it up instead of crashing the parse loop.
  • The extra service.getMetadata() null check closes the remaining dereference chain.

The three test files cover the null-labels, missing-service, and happy-path cases for each parser, and the mocked Lister/Indexer setup matches how the production code queries the cache. LGTM, approving.

@dengliming

Copy link
Copy Markdown
Member

@wy471x Thanks for the fix. This PR currently has merge conflicts and needs to be rebased/merged with the latest target branch before it can be reviewed and merged. Could you please resolve the conflicts and update the PR?

@Aias00 Aias00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

REQUEST_CHANGES (PMC Aias00): this PR has merge conflicts (CONFLICTING) and cannot be merged. Please rebase onto the latest master and resolve the conflicts, then request a re-review.

wy471x and others added 4 commits September 21, 2026 21:47
# Conflicts:
#	shenyu-kubernetes-controller/src/test/java/org/apache/shenyu/k8s/parser/DubboIngressParserTest.java
The Dubbo ingress parser only emits an IngressConfiguration (and thus a
selector) for a path when the ingress carries metadata labels, so the
protocol-parsing tests and the null-label regression tests ended up with
an empty route config list.

Give those tests a non-empty labels map via a shared metadataLabels()
helper. Assertions are unchanged.

@Aias00 Aias00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving — consistent null-safety fix across all three parsers.

The chained expression

serviceLister.namespace(namespace).get(labels.get(label)).getMetadata().getAnnotations();

has three unguarded links. Any of them being null — an Ingress with no selector labels, a label naming a Service the lister hasn't cached (or that was just deleted), or a Service whose getMetadata() is null — throws an NPE inside the reconcile loop. Since this runs on every Ingress event, a single unresolvable reference can wedge the controller's parsing path.

The fix is applied identically in DubboIngressParser, GrpcParser and SofaParser:

V1Service service = serviceLister.namespace(namespace).get(labels.get(label));
if (Objects.isNull(service) || Objects.isNull(service.getMetadata())) {
    LOG.warn("Service {} not found in namespace {}", labels.get(label), namespace);
    continue;
}

Three things make this the right shape:

  • The labels == null || labels.isEmpty() guard is hoisted above the loop, so an Ingress without selector labels is skipped cleanly rather than entering the iteration at all.
  • continue (not a bare if) means a missing Service skips just that label and the remaining labels still get parsed — a partial failure doesn't discard the whole rule.
  • The LOG.warn names both the Service and the namespace, which is exactly what an operator needs to diagnose a stale selector.

Consistency across the three parsers matters here because they duplicate this logic; fixing only one would leave the same bug latent in the others.

One note for coordination: SofaParser is touched here while #6308 proposes removing sofa support entirely. Whichever lands second will need a rebase — not a problem with this change, just flagging the overlap.

@wy471x wy471x closed this Oct 1, 2026
@wy471x

wy471x commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Closing this PR as the linked issue #6486 has been fixed by #7057. The RPC ingress parsers now guard against missing labels and services, with RpcIngressParserNullSafetyTest covering the null-safety cases.

@wy471x
wy471x deleted the fix_K8s-RPC-ingress-parsers-error branch October 1, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Kubernetes RPC ingress parsers can throw NPE when labels or services are missing

3 participants