{"record":{"id":"390929507e7fd497","repo":"microsoft/aspire","slug":"kubernetes-namespace-kubernetesnamespace-is-invalid-must","errorCode":null,"errorMessage":"Kubernetes namespace '{kubernetesNamespace}' is invalid. Must match RFC 1123: lowercase alphanumeric characters or hyphens, start and end with an alphanumeric character, and be at most {KubernetesNamespaceMaxLength} characters.","messagePattern":"Kubernetes namespace '(.+?)' is invalid\\. Must match RFC 1123: lowercase alphanumeric characters or hyphens, start and end with an alphanumeric character, and be at most (.+?) characters\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Radius/RadiusExtensions.cs","lineNumber":73,"sourceCode":"\n    /// <summary>\n    /// Sets the Kubernetes namespace for the Radius environment.\n    /// </summary>\n    /// <param name=\"builder\">The Radius environment resource builder.</param>\n    /// <param name=\"kubernetesNamespace\">A valid RFC 1123 namespace name.</param>\n    /// <returns>A reference to the <see cref=\"IResourceBuilder{RadiusEnvironmentResource}\"/>.</returns>\n    /// <exception cref=\"ArgumentException\">Thrown when the namespace is not a valid RFC 1123 label.</exception>\n    [AspireExport]\n    public static IResourceBuilder<RadiusEnvironmentResource> WithNamespace(\n        this IResourceBuilder<RadiusEnvironmentResource> builder,\n        string kubernetesNamespace)\n    {\n        ArgumentNullException.ThrowIfNull(builder);\n        ArgumentException.ThrowIfNullOrEmpty(kubernetesNamespace);\n\n        if (kubernetesNamespace.Length > KubernetesNamespaceMaxLength || !DnsLabelPattern().IsMatch(kubernetesNamespace))\n        {\n            throw new ArgumentException(\n                $\"Kubernetes namespace '{kubernetesNamespace}' is invalid. \" +\n                \"Must match RFC 1123: lowercase alphanumeric characters or hyphens, \" +\n                $\"start and end with an alphanumeric character, and be at most {KubernetesNamespaceMaxLength} characters.\",\n                nameof(kubernetesNamespace));\n        }\n\n        builder.Resource.Namespace = kubernetesNamespace;\n        return builder;\n    }\n\n    /// <summary>\n    /// Registers a callback that can customize the generated Radius infrastructure before Bicep is emitted.\n    /// </summary>\n    /// <param name=\"builder\">The Radius environment resource builder.</param>\n    /// <param name=\"configure\">The callback that mutates the generated infrastructure options.</param>\n    /// <returns>The same <see cref=\"IResourceBuilder{RadiusEnvironmentResource}\"/> for chaining.</returns>\n    [Experimental(\"ASPIRERADIUS004\", UrlFormat = \"https://aka.ms/aspire/diagnostics/{0}\")]\n    [AspireExportIgnore(Reason = \"RadiusInfrastructureOptions customization callbacks are not ATS-compatible.\")]","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Radius/RadiusExtensions.cs#L55-L91","documentation":"WithNamespace validates the Kubernetes namespace against RFC 1123 DNS label rules: lowercase alphanumeric and hyphens, starting/ending with an alphanumeric, and at most KubernetesNamespaceMaxLength (63) characters. Invalid namespaces would be rejected by Kubernetes at apply time, so the extension rejects them at configuration time with a clear ArgumentException.","triggerScenarios":"Calling radiusEnvironment.WithNamespace(...) with an uppercase name ('Default'), underscores ('my_ns'), a leading/trailing hyphen, a slash, or a string longer than 63 characters.","commonSituations":"Reusing an app/environment name with mixed case as a namespace; Windows-style identifiers with underscores; copy-pasting fully-qualified resource names into the namespace argument.","solutions":["Lowercase the value and replace underscores with hyphens","Trim leading/trailing hyphens and non-alphanumeric characters","Shorten the value to at most 63 characters","Pre-generate a compliant name, e.g. app name converted with a slugify helper"],"exampleFix":"// before\n.WithNamespace(\"MyApp_Prod\")\n// after\n.WithNamespace(\"myapp-prod\")","handlingStrategy":"validation","validationCode":"var valid = ns.Length <= 63 && System.Text.RegularExpressions.Regex.IsMatch(ns, \"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$\"); if (!valid) throw new ArgumentException($\"Namespace '{ns}' must be RFC 1123 compliant.\");","typeGuard":"bool IsValidK8sNamespace(string? ns) => ns is not null && ns.Length <= 63 && System.Text.RegularExpressions.Regex.IsMatch(ns, \"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$\");","tryCatchPattern":"try { env.WithNamespace(ns); } catch (ArgumentException ex) when (ex.ParamName == nameof(ns)) { logger.LogError(ex, \"Invalid Kubernetes namespace\"); throw; }","preventionTips":["Normalize names with .ToLowerInvariant().Replace('_','-') before calling","Keep environment/app names DNS-label friendly from the start","Unit-test namespace generation logic"],"tags":["radius","kubernetes","namespace","validation"],"backgroundTag":"invalid-identifier-format","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}