Skip to content

fix: correct CRD type mismatches and add omitempty to status fields#4571

Open
sharanrajt wants to merge 2 commits intostacklok:mainfrom
sharanrajt:main
Open

fix: correct CRD type mismatches and add omitempty to status fields#4571
sharanrajt wants to merge 2 commits intostacklok:mainfrom
sharanrajt:main

Conversation

@sharanrajt
Copy link
Copy Markdown

@sharanrajt sharanrajt commented Apr 6, 2026

Summary

  • Go's int is platform-dependent (32- or 64-bit), but Kubernetes CRD integer fields should use explicitly sized types (int32/int64). Several CRD structs (VirtualMCPServerStatus.BackendCount, MCPRegistryDatabaseConfig.Port/MaxOpenConns/MaxIdleConns, SyncStatus.AttemptCount/ServerCount) used bare int, producing an imprecise OpenAPI schema and causing testify type-mismatch failures.
  • MCPGroupStatus.Servers and MCPGroupStatus.ServerCount lacked omitempty on their JSON tags, causing zero-value fields to always appear in serialised status payloads.

What changed:

  • Changed BackendCount, Port, MaxOpenConns, MaxIdleConns, AttemptCount, and ServerCount from int to int32 in CRD types
  • Updated GetDatabasePort() return type to int32
  • Added inline bounds check (capped at math.MaxInt32) in k8s_reporter.go before casting to int32
  • Added explicit int() / int32() casts at all call sites to satisfy the new types
  • Regenerated CRD manifests (format: int32 added to affected fields)
  • Updated unit, integration, and E2E tests to use int32 literals and BeEquivalentTo matcher
  • Added omitempty to Servers and ServerCount JSON tags in MCPGroupStatus

Fixes #4544
Fixes #4537

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

Changes

File Change
cmd/thv-operator/api/v1alpha1/mcpgroup_types.go Add omitempty to Servers and ServerCount JSON tags
cmd/thv-operator/api/v1alpha1/mcpregistry_types.go Change Port, MaxOpenConns, MaxIdleConns, AttemptCount, ServerCount from int to int32; update GetDatabasePort() return type
cmd/thv-operator/api/v1alpha1/virtualmcpserver_types.go Change BackendCount from int to int32
cmd/thv-operator/pkg/registryapi/config/config.go Add int() casts when assigning int32 CRD fields to internal config
cmd/thv-operator/pkg/registryapi/pgpass_test.go Cast port to int32 in test helper
cmd/thv-operator/pkg/virtualmcpserverstatus/collector.go Cast readyCount to int32 when assigning BackendCount
cmd/thv-operator/test-integration/mcp-registry/status_helpers.go Cast SyncStatus.ServerCount to int for Gomega comparison
deploy/charts/operator-crds/files/crds/toolhive.stacklok.dev_mcpregistries.yaml Add format: int32 to port, maxOpenConns, maxIdleConns, attemptCount, serverCount
deploy/charts/operator-crds/files/crds/toolhive.stacklok.dev_virtualmcpservers.yaml Add format: int32 to backendCount
deploy/charts/operator-crds/templates/toolhive.stacklok.dev_mcpregistries.yaml Same CRD template regeneration
deploy/charts/operator-crds/templates/toolhive.stacklok.dev_virtualmcpservers.yaml Same CRD template regeneration
pkg/vmcp/status/k8s_reporter.go Add math import; inline bounds check capping at MaxInt32 before int32 cast
pkg/vmcp/status/k8s_reporter_test.go Change backendCount field to int32; update loop variable, assertions, and Len calls
test/e2e/thv-operator/virtualmcp/virtualmcp_yardstick_base_test.go Use BeEquivalentTo(2) instead of Equal(2) for BackendCount

Does this introduce a user-facing change?

No. The serialised status output becomes cleaner (zero-value MCPGroupStatus fields now omitted), and the CRD OpenAPI schema is more precise (format: int32), but API semantics are unchanged.

Special notes for reviewers

  • nolint:gosec comments on int32() casts are guarded by an explicit math.MaxInt32 bounds check in k8s_reporter.go, or are inherently safe because the values are small counts (backend count, connection pool sizes).
  • The E2E test switches from Equal(2) to BeEquivalentTo(2) to avoid Gomega's strict type matching between int and int32.

jerm-dro
jerm-dro previously approved these changes Apr 6, 2026
Copy link
Copy Markdown
Contributor

@jerm-dro jerm-dro left a comment

Choose a reason for hiding this comment

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

nitpick: The PR description lists several changes that aren't in the diff — switching PgpassSecretRef to SecretKeyReference, adding omitempty to User/Password, and a safeIntToInt64 helper. The actual code does an inline bounds check instead of a helper, and the other two changes don't appear at all. Could you trim the description to match what's actually here? Makes it easier for reviewers (and future git-log readers) to understand the scope.

The actual changes fully address both #4544 and #4537 — looks good.

@sharanrajt
Copy link
Copy Markdown
Author

Thanks for the review, I have updated the PR description to match the actual diff. Let me know if anything else needs adjusting!

Add omitempty to servers and serverCount JSON tags in
MCPGroupStatus so optional fields are omitted from serialized
output when empty, consistent with other optional fields in
the struct.

Fixes stacklok#4537
The VirtualMCPServerStatus.BackendCount CRD field is int32,
but test assertions compared it against int values, causing
testify's assert.Equal to fail on type mismatch.

Update the test struct's backendCount field to int32 and use
int32 literals in direct assertions to match the CRD type.

Fixes stacklok#4544
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.

Add omitempty to MCPGroupStatus Servers and ServerCount JSON tags Use int32 instead of int for CRD integer fields

2 participants