{"record":{"id":"e03330c03f91b85c","repo":"microsoft/aspire","slug":"resource-r-name-is-configured-to-publish-as-a-kubernetes","errorCode":null,"errorMessage":"Resource '{r.Name}' is configured to publish as a Kubernetes service, but there are no 'KubernetesEnvironmentResource' resources or Kubernetes-backed compute environments. Ensure you have added one by calling 'AddKubernetesEnvironment'.","messagePattern":"Resource '(.+?)' is configured to publish as a Kubernetes service, but there are no 'KubernetesEnvironmentResource' resources or Kubernetes-backed compute environments\\. Ensure you have added one by calling 'AddKubernetesEnvironment'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Kubernetes/KubernetesEnvironmentExtensions.cs","lineNumber":58,"sourceCode":"                {\n                    ValidateAndFinalizePersistentVolumeBindings(ctx);\n\n                    if (!ctx.ExecutionContext.IsPublishMode)\n                    {\n                        return Task.CompletedTask;\n                    }\n\n                    var hasKubernetesEnvironment = ctx.Model.Resources.OfType<KubernetesEnvironmentResource>().Any() ||\n                        ctx.Model.Resources.OfType<IComputeEnvironmentResource>()\n                            .Any(r => r.HasAnnotationOfType<KubernetesEnvironmentAnnotation>());\n\n                    if (!hasKubernetesEnvironment)\n                    {\n                        foreach (var r in ctx.Model.GetComputeResources())\n                        {\n                            if (r.HasAnnotationOfType<KubernetesServiceCustomizationAnnotation>())\n                            {\n                                throw new InvalidOperationException($\"Resource '{r.Name}' is configured to publish as a Kubernetes service, but there are no '{nameof(KubernetesEnvironmentResource)}' resources or Kubernetes-backed compute environments. Ensure you have added one by calling '{nameof(AddKubernetesEnvironment)}'.\");\n                            }\n                        }\n                    }\n\n                    return Task.CompletedTask;\n                },\n                dependsOn: WellKnownPipelineSteps.ValidateComputeEnvironments,\n                requiredBy: WellKnownPipelineSteps.BeforeStart);\n        }\n\n        return builder;\n    }\n\n    private static void ValidateAndFinalizePersistentVolumeBindings(PipelineStepContext context)\n    {\n        var bindings = GetPersistentVolumeBindings(context);\n\n        if (context.ExecutionContext.IsRunMode)","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Kubernetes/KubernetesEnvironmentExtensions.cs#L40-L76","documentation":"A compute resource carries a KubernetesServiceCustomizationAnnotation but the application model contains no KubernetesEnvironmentResource (no AddKubernetesEnvironment call). Publishing cannot place the resource anywhere, so the model callback throws InvalidOperationException during publish-time validation.","triggerScenarios":"Calling WithComputeEnvironment/Kubernetes customization methods (e.g. WithReplicas, node affinity, etc.) on a resource while never calling builder.AddKubernetesEnvironment(...), then publishing.","commonSituations":"Migrating an AppHost from another environment (e.g. Docker) to Kubernetes; a team member added resource-level Kubernetes customizations but forgot to add the environment; deleting the AddKubernetesEnvironment line during refactoring.","solutions":["Add builder.AddKubernetesEnvironment(\"kubernetes\") to the AppHost before publishing.","Remove the Kubernetes-specific customization calls if Kubernetes publishing is not intended.","Verify the environment is added in Program.cs of the AppHost project."],"exampleFix":"// before\nvar k8s = builder.AddKubernetesEnvironment(\"env\"); // missing\nbuilder.AddProject<Projects.Api>(\"api\").WithReplicas(3); // Kubernetes customization\n// after\nbuilder.AddKubernetesEnvironment(\"env\");\nbuilder.AddProject<Projects.Api>(\"api\").WithReplicas(3);","handlingStrategy":"validation","validationCode":"if (!builder.Resources.OfType<KubernetesEnvironmentResource>().Any()) throw new InvalidOperationException(\"Add AddKubernetesEnvironment before Kubernetes customizations.\");","typeGuard":null,"tryCatchPattern":"try { appHostBuilder.Build(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"AddKubernetesEnvironment\")) { /* fix model and rebuild */ }","preventionTips":["Always call AddKubernetesEnvironment in the AppHost when using Kubernetes customizations","Keep environment registration at the top of Program.cs","Run a publish locally in CI to catch model errors early"],"tags":["kubernetes","publish","configuration","missing-environment"],"backgroundTag":"missing-dependency","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}