Skip to content

[platform] Validate computed tenant namespace length in cozystack-api#2376

Merged
kvaps merged 1 commit intomainfrom
fix/tenant-namespace-validation-length
Apr 12, 2026
Merged

[platform] Validate computed tenant namespace length in cozystack-api#2376
kvaps merged 1 commit intomainfrom
fix/tenant-namespace-validation-length

Conversation

@lexfrei
Copy link
Copy Markdown
Contributor

@lexfrei lexfrei commented Apr 11, 2026

What this PR does

The aggregated cozystack-api validates each individual Tenant name length against the Helm release limit (46 chars for the tenant- prefix), but it never validates the computed workload namespace that is built by dash-joining the parent namespace with the tenant name. A user can therefore kubectl apply a nested Tenant whose own name passes the per-name check, only to have Kubernetes later reject the generated Namespace for exceeding the 63-character DNS-1123 label limit. The failure surfaces as an opaque HelmRelease reconcile error, and the Tenant CR is left stranded in the cluster with a status.namespace that can never exist.

This PR closes that gap at the aggregation layer by rejecting such requests synchronously in Create() with a clear field.Invalid error that names the computed namespace and its length, so the failure is reported at kubectl apply time before any HelmRelease is created.

The change is scoped to the Tenant kind only. Update() is not touched because Kubernetes names and namespaces are immutable and the existing code already intentionally skips name validation there. No Helm chart, CRD, or codegen changes are required.

The PR is split into two commits that document the bug and its fix via TDD:

  1. Pin the gap — adds a test that demonstrates the pre-fix behavior: the pre-fix Create() path has no function that rejects a realistic nested-tenant combination whose computed namespace exceeds 63 chars. Reviewers can check out this commit and observe the gap.
  2. The fix — adds validateTenantNamespaceLength, the kindName == "Tenant" gate in Create(), and TestValidateTenantNamespaceLength covering boundary cases (pass at 63, fail at 64, fail at 68). The pinning test from commit 1 is removed in the same commit now that the gap is closed.

Release note

[platform] cozystack-api now rejects Tenant creation at admission time when the ancestor-chain namespace would exceed the 63-character Kubernetes namespace limit, instead of allowing the resource through and failing later at HelmRelease reconcile.

Summary by CodeRabbit

  • Bug Fixes

    • Tenant application creation now validates computed tenant namespace length against the Kubernetes DNS-1123 63-character limit; creations that would produce too-long namespace names now fail with a clear validation error.
  • Tests

    • Added tests covering tenant namespace length validation, including multiple parent/tenant-name scenarios, boundary conditions, and verification that error messages include the offending namespace and its length.

@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Apr 11, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6e4870ff-1047-4bc2-bc33-00746a6bd0ff

📥 Commits

Reviewing files that changed from the base of the PR and between 8d7e962 and 2b96be8.

📒 Files selected for processing (2)
  • pkg/registry/apps/application/rest.go
  • pkg/registry/apps/application/rest_validation_test.go
✅ Files skipped from review due to trivial changes (1)
  • pkg/registry/apps/application/rest.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/registry/apps/application/rest_validation_test.go

📝 Walkthrough

Walkthrough

Added tenant namespace length validation to REST.Create that computes the workload namespace from the provided namespace and tenant name, then verifies it does not exceed the Kubernetes DNS-1123 label limit of 63 characters. Includes a helper method and corresponding test coverage.

Changes

Cohort / File(s) Summary
Tenant Namespace Validation
pkg/registry/apps/application/rest.go
Introduced maxNamespaceName constant (63) and validateTenantNamespaceLength() helper used in REST.Create to compute the tenant workload namespace and return apierrors.NewInvalid (field metadata.name) when the computed namespace exceeds 63 chars.
Validation Tests
pkg/registry/apps/application/rest_validation_test.go
Added TestValidateTenantNamespaceLength covering boundary and failing cases; imports updated and assertions check that error messages include the computed namespace and the " characters" fragment.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

🐰 I hop through namespaces, nimble and spry,
Counting letters beneath the sky,
Sixty‑three is the cap I keep in sight,
No overflow now—everything’s right,
A carrot for tests, and code that’s light 🥕✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding validation for computed tenant namespace length in cozystack-api.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/tenant-namespace-validation-length

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@lexfrei lexfrei self-assigned this Apr 11, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces validation to ensure that the computed workload namespace for "Tenant" applications does not exceed the 63-character DNS-1123 label limit. It adds the validateTenantNamespaceLength method to the REST struct, which is called during the creation of Tenant applications, and includes comprehensive unit tests to verify the validation logic across various nesting depths and name lengths. I have no feedback to provide as there were no review comments to evaluate.

Reject Tenant creation when the computed workload namespace (parent
namespace + "-" + tenant name) would exceed the 63-character DNS-1123
label limit. Previously only the per-name length was checked against
the Helm release limit, so a deeply-nested tenant could pass Create()
and then fail later as an opaque HelmRelease reconcile error.

Assisted-By: Claude <[email protected]>
Signed-off-by: Aleksei Sviridkin <[email protected]>
@lexfrei lexfrei force-pushed the fix/tenant-namespace-validation-length branch from 8d7e962 to 2b96be8 Compare April 12, 2026 00:29
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Apr 12, 2026
@kvaps kvaps merged commit aa7d1a8 into main Apr 12, 2026
7 of 8 checks passed
@kvaps kvaps deleted the fix/tenant-namespace-validation-length branch April 12, 2026 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants