Skip to content

[MeshSync] Return ErrNilClient when initializing with nil Kubernetes client - #645

Open
chill-czar wants to merge 1 commit into
meshery:masterfrom
chill-czar:bug/sarthak/nil-client-constructor-check
Open

chill-czar wants to merge 1 commit into
meshery:masterfrom
chill-czar:bug/sarthak/nil-client-constructor-check

Conversation

@chill-czar

@chill-czar chill-czar commented Nov 30, 2026 •

Copy link
Copy Markdown

Description

When meshsync.New is invoked with a nil kubeClient, it unconditionally dereferences kubeClient.KubeClient while resolving the cluster ID and dynamic shared informer, causing an unhandled nil pointer dereference panic.

meshsync/error.go defines and registers ErrNilClient (ErrNilClientCode = "1016") with the remediation "Supply a valid Kubernetes client to meshsync.New", but the check was only present in UpdateInformer() and missing from the constructor itself.

This PR checks kubeClient == nil at the start of meshsync.New and returns ErrNilClient(), preventing panics when constructing a Handler or when initializing the Kubernetes Discoverer adapter (internal/discovery/kubernetes.New).

Notes for Reviewers

  • Added TestNewWithNilKubeClientReturnsErrNilClient in meshsync/meshsync_test.go.
  • Added TestNewWithNilKubeClientReturnsErrNilClient in internal/discovery/kubernetes/kubernetes_test.go.
  • Verified locally with go test -failfast --short ./... -race (all 7 packages pass) and make build.

Signed commits

  • Yes, I signed my commits.

Summary by CodeRabbit

  • Bug Fixes
    • Creating a discovery or mesh sync component without a Kubernetes client now returns a clear error instead of proceeding with invalid initialization.

…client

When meshsync.New was invoked with a nil kubeClient, it unconditionally
dereferenced kubeClient.KubeClient while resolving the cluster ID and
dynamic informer, resulting in an unhandled nil pointer panic.

Check kubeClient at the beginning of meshsync.New and return the
allocated ErrNilClient error (code 1016), preventing panics when
constructing Handler or initializing the Kubernetes Discoverer adapter
without a valid client.

Signed-off-by: Sarthak Chaudhari <aisarthak2@gmail.com>
@github-actions github-actions Bot added the language/go Golang related label Nov 30, 2026
@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

meshsync.New now returns ErrNilClient() when it receives a nil Kubernetes client. Tests verify the nil result and error code for both the handler and Kubernetes discoverer initialization paths.

Changes: Nil client guard

Layer / File(s) Summary
Guard and validation
meshsync/meshsync.go, meshsync/meshsync_test.go, internal/discovery/kubernetes/kubernetes_test.go
New returns a nil handler and ErrNilClient() before initialization when the Kubernetes client is nil. Tests check that the handler or discoverer is nil and that the error code is 1016.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

  • meshery/meshsync#627: Introduces the Kubernetes Discoverer adapter whose initialization path is covered by this change.
  • meshery/meshsync#631: Assigns error code 1016 to ErrNilClient, which this change returns.

Suggested labels: language/go

Suggested reviewers: rameshwaris, pontusringblom

Merge Risk: Minimal · up to de913

Passing a nil Kubernetes client now returns a clear error instead of causing a panic. Non-nil clients behave as before. No merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: meshsync.New now returns ErrNilClient when given a nil Kubernetes client, instead of risking a nil-pointer panic. The raw summary and objectives confirm that the chang…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: meshsync.New now returns ErrNilClient when given a nil Kubernetes client, instead of risking a nil-pointer panic. The raw summary and objectives confirm that the change adds this guard in meshsync.New, with tests for both the Handler and the Kubernetes Discoverer paths.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f37bbad5-86f1-475d-a39f-a14f572a3799
Commits

Reviewing files that changed from the base of the PR and between 69cc3c6 and de91333.

Files selected for processing: 3

  • internal/discovery/kubernetes/kubernetes_test.go
  • meshsync/meshsync.go
  • meshsync/meshsync_test.go

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@pontusringblom pontusringblom left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Clean guard: the nil check lands before the first dereference of kubeClient, reuses the existing ErrNilClient, and the discovery wrapper test confirms the error propagates through kubernetes.New. Two small test asks below.

Please consider and reply to each comment in this review. For every suggestion, state how you've incorporated it or explain your reasoning for not doing so.

})
require.Nil(t, d)
require.Error(t, err)
assert.Equal(t, "1016", meshkiterrors.GetCode(err))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use meshsync.ErrNilClientCode here instead of the literal "1016" (one added import of github.com/meshery/meshsync/meshsync) so this test tracks the constant if the code ever changes, matching how meshsync_test.go already asserts it.

Comment thread meshsync/meshsync_test.go
nil,
internalconfig.OutputFiltrationContainer{},
)
if h != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Minor consistency point: this file hand-rolls its assertions while the companion test in internal/discovery/kubernetes uses testify require/assert. Matching that style keeps the two new tests reading the same way.

@leecalcote
leecalcote removed the request for review from rahmanaisha November 30, 2026 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

language/go Golang related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants