You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[feature]: Add support for running operators in namespaced scope #1213
Is your feature request related to a problem? Please describe.
In many clusters, cluster-wide operators require permissions that are too broad.
Namespaced operation would improve security (least privilege), fit multi-tenant setups, and make adoption easier in restricted environments.
Describe the solution you would like
Add an option to configure the operator to watch only one namespace (and optionally multiple namespaces), for example via runtime/builder config and/or WATCH_NAMESPACE env var.
When enabled, operator behavior and manifests should support namespaced RBAC where possible (Role/RoleBinding instead of ClusterRole/ClusterRoleBinding), while keeping current cluster-wide behavior as default.
@nur-of there is already the ability to set the scope of the resource watcher to just a namespace. Please see the docs for more details and if that fits your needs already.
Hey @kimpenhaus thank you for the response. When running the generate command using the kubeops cli it only generates cluster-wide RBAC, and there is no option to generate namespace-scoped RBAC, so if I want to use the generated manifests, I have to patch their kind. It's not a deal breaker, but it would be nice to have.
Is there any update on this? I've not tried it yet but my understanding of the linked docs is that while they will apply the desired filtering, they don't change the fact that you require cluster-scoped Role and RoleBinding.
I have a similar use case where I want to configure an operator to only watch a particular namespace, but I'm having to give it permission to technically be able to do things outside of that namespace due to the ClusterRole and ClusterRoleBinding
not yet - I was on vacation with the family until weekend and hadn't had a chance to look into this until now. I will keep you updated - maybe I'll find a slot by this week 🤞🏼
I actually tried this locally and it seems to be working. I created a new Kubernetes context that uses a custom service account. I then created a namespace-scoped Role and RoleBinding and associated them with the ServiceAccount.
When I ran my operator, I got a 403 forbidden. Which is what I expected at that stage.
Running it again, it successfully started watching the CRD and the roles associated with the user seemed to now be sufficient!
I originally thought it's an issue with what Kubernetes API is being used to watch CRDs, but it seems to be based on how you filter as well. There might be more to it but that would be beyond my current Kubernetes understanding.
@nur-of@adrianhr91 I have extended the generator to discover namespaced scope and generate only role/role binding instead of cluster role/cluster role binding.
@nur-of@adrianhr91 I have extended the generator to discover namespaced scope and generate only role/role binding instead of cluster role/cluster role binding.
do you have any chance to test this out and provide some feedback if that matches your needs?
thanks, m.
I have a hand-crafted version of these resources because my operator gets deployed as part of a wider Helm Chart. But conceptually it looks good and it matches what I had generated manually
@kimpenhaus I got to test 13.2.0. The generated Role/RoleBinding shape is exactly what I was after, but the static discovery never triggers for our operator, so we still get a ClusterRole. Two separate reasons, both visible in our real Program.cs.
Here is the relevant part of it, trimmed:
Logger.InitializeLogging();
try
{
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog();
// WatchNamespace is read from the WATCH_NAMESPACE env var
var orchestratorOptions = OrchestratorOptions.FromEnvironment();
builder.Services.AddSingleton(orchestratorOptions);
builder.Services
.AddKubernetesOperator(settings =>
{
settings
.WithName(operatorName)
.WithNamespace(orchestratorOptions.WatchNamespace)
.WithLeaderElection(LeaderElectionType.Single)
.WithAutoAttachFinalizers();
})
.RegisterComponents();
// ... rest of the host
await app.RunAsync();
}
finally
{
await Log.CloseAndFlushAsync();
}
kubeops generate operator reports:
Warning: .../Source/Orchestrator/Program.cs(25,13): The watch namespace is assigned
conditionally and cannot be evaluated statically.
The try/finally wrapper defeats discovery
NamespaceConfigurationWalker.AddValue calls IsConditional(syntax), which walks syntax.Ancestors() and matches TryStatementSyntax among others. Ancestors() doesn't stop at the enclosing lambda, so the top-level try { … } finally { await Log.CloseAndFlushAsync(); } — the canonical Serilog host shape — is enough to mark the namespace as unresolvable.
I verified this in isolation: I hardcoded .WithNamespace("abc") and kept everything else the same, and still got the same "assigned conditionally" warning plus a ClusterRole. Removing only the try/finally made the warning disappear. So any operator using that very common Program.cs shape silently falls back to cluster-wide, even with a literal namespace.
Two small things here: a try block isn't a conditional, so the diagnostic text is misleading as well; and I'd expect the ancestor walk to stop at the lambda body it is analysing.
Our namespace comes from WATCH_NAMESPACE, so it can never constant-fold
This is the bigger one, and it's the half of the original request that didn't make it in. We deliberately don't hardcode the namespace:
public static OrchestratorOptions FromEnvironment() => new(
WatchNamespace: StartupArgumentReader.PrintAndGetStartupArgument(
"WATCH_NAMESPACE", string.Empty, "The namespace the operator is watching", required: true),
// ...
);
The same image is deployed to several namespaces, and our acceptance suite runs the operator against a namespace it cace(orchestratorOptions.WatchNamespace) will never be a compile-time constant, ConstantValue.HasValue is false, andDiscover returns Unknown → cluster-wide. Hardcoding a literal purely to satisfy the generator would break the per-run namespace, so the feature is out of reach for us as implemented.
settings.Namespace = orchestratorOptions.WatchNamespace behaves identically — VisitSimpleAssignment routes into the same AddValue.
What would work for us:
An explicit opt-in rather than inferred intent, e.g. --rbac-scope namespaced on generate operator, or simply having the existing --namespace option imply namespaced RBAC. That covers the env-var case, is deterministic, and can't be silently defeated by the shape of Program.cs. Static discovery could stay as the default when no flag is given.
Unrelated blocker while testing
Worth its own issue if you agree, but flagging it here since it's what initially made it look like nothing was happening: on 13.2.0, kubeops generate operator throws and writes no files at all, while still exiting 0:
Generate CRDs.
Unhandled exception: System.IO.FileNotFoundException: Could not find assembly
'Microsoft.CodeAnalysis, Version=5.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
at KubeOps.Cli.Transpilation.AssemblyLoader.<GetConvertedEntities>b__11_0(TypeInfo t)
in /_/src/KubeOps.Cli/Transpilation/AssemblyLoader.cs:line 225
at KubeOps.Cli.Generators.CrdGenerator.Generate(ResultOutput output)
in /_/src/KubeOps.Cli/Generators/CrdGenerator.cs:line 20
at KubeOps.Cli.Commands.Generator.OperatorGenerator.Handler(IAnsiConsole console, ParseResult parseResult)
in /_/src/KubeOps.Cli/Commands/Generator/OperatorGenerator.cs:line 147
The exit code being 0 means an MSBuild Exec wrapping the CLI reports success, so the generation looks like a no-op rather than a failure. The 13.1.0 CLI generates all files correctly against the same project (net10.0, SDK 10.0.109), so it looks like a regression.
Happy to open a separate issue with a minimal repro if that's easier for you.
@nur-of thanks for the detailed analysis - I wanted to keep it simple and easy to use - that's why I added sort of auto-detection. Reading all this I think there is no way around having an additional flag. I will try to add this in the next couple of days.
the second issue you've mentioned should hopefully be fixed with #1237 - that also introduces a proper return value 😅
Activity
kimpenhaus commentedon Jul 30, 2026
@nur-of there is already the ability to set the scope of the resource watcher to just a namespace. Please see the docs for more details and if that fits your needs already.
Best, Marcus 👋
nur-of commentedon Jul 30, 2026
Hey @kimpenhaus thank you for the response. When running the generate command using the kubeops cli it only generates cluster-wide RBAC, and there is no option to generate namespace-scoped RBAC, so if I want to use the generated manifests, I have to patch their kind. It's not a deal breaker, but it would be nice to have.
kimpenhaus commentedon Jul 30, 2026
Got you - I can have a look once I am back at home (currently on a trip with the family)
nur-of commentedon Jul 31, 2026
Thank you! Enjoy your trip!
adrianhr91 commentedon Aug 24, 2026
Is there any update on this? I've not tried it yet but my understanding of the linked docs is that while they will apply the desired filtering, they don't change the fact that you require cluster-scoped Role and RoleBinding.
I have a similar use case where I want to configure an operator to only watch a particular namespace, but I'm having to give it permission to technically be able to do things outside of that namespace due to the ClusterRole and ClusterRoleBinding
kimpenhaus commentedon Aug 24, 2026
not yet - I was on vacation with the family until weekend and hadn't had a chance to look into this until now. I will keep you updated - maybe I'll find a slot by this week 🤞🏼
adrianhr91 commentedon Aug 24, 2026
I actually tried this locally and it seems to be working. I created a new Kubernetes context that uses a custom service account. I then created a namespace-scoped Role and RoleBinding and associated them with the ServiceAccount.
When I ran my operator, I got a 403 forbidden. Which is what I expected at that stage.
I then changed my configuration
Running it again, it successfully started watching the CRD and the roles associated with the user seemed to now be sufficient!
I originally thought it's an issue with what Kubernetes API is being used to watch CRDs, but it seems to be based on how you filter as well. There might be more to it but that would be beyond my current Kubernetes understanding.
feat(cli/rbac): introduce namespaced RBAC generation support (#1230)
kimpenhaus commentedon Aug 26, 2026
@nur-of @adrianhr91 I have extended the generator to discover namespaced scope and generate only role/role binding instead of cluster role/cluster role binding.
see v13.2.0-prerelease.1
do you have any chance to test this out and provide some feedback if that matches your needs?
thanks, m.
adrianhr91 commentedon Aug 27, 2026
I have a hand-crafted version of these resources because my operator gets deployed as part of a wider Helm Chart. But conceptually it looks good and it matches what I had generated manually
nur-of commentedon Sep 3, 2026
@kimpenhaus I'll test it, thank you :)
nur-of commentedon Sep 4, 2026
@kimpenhaus I got to test 13.2.0. The generated Role/RoleBinding shape is exactly what I was after, but the static discovery never triggers for our operator, so we still get a ClusterRole. Two separate reasons, both visible in our real Program.cs.
Here is the relevant part of it, trimmed:
kubeops generate operator reports:
NamespaceConfigurationWalker.AddValue calls IsConditional(syntax), which walks syntax.Ancestors() and matches TryStatementSyntax among others. Ancestors() doesn't stop at the enclosing lambda, so the top-level try { … } finally { await Log.CloseAndFlushAsync(); } — the canonical Serilog host shape — is enough to mark the namespace as unresolvable.
I verified this in isolation: I hardcoded .WithNamespace("abc") and kept everything else the same, and still got the same "assigned conditionally" warning plus a ClusterRole. Removing only the try/finally made the warning disappear. So any operator using that very common Program.cs shape silently falls back to cluster-wide, even with a literal namespace.
Two small things here: a try block isn't a conditional, so the diagnostic text is misleading as well; and I'd expect the ancestor walk to stop at the lambda body it is analysing.
This is the bigger one, and it's the half of the original request that didn't make it in. We deliberately don't hardcode the namespace:
The same image is deployed to several namespaces, and our acceptance suite runs the operator against a namespace it cace(orchestratorOptions.WatchNamespace) will never be a compile-time constant, ConstantValue.HasValue is false, andDiscover returns Unknown → cluster-wide. Hardcoding a literal purely to satisfy the generator would break the per-run namespace, so the feature is out of reach for us as implemented.
settings.Namespace = orchestratorOptions.WatchNamespace behaves identically — VisitSimpleAssignment routes into the same AddValue.
What would work for us:
An explicit opt-in rather than inferred intent, e.g. --rbac-scope namespaced on generate operator, or simply having the existing --namespace option imply namespaced RBAC. That covers the env-var case, is deterministic, and can't be silently defeated by the shape of Program.cs. Static discovery could stay as the default when no flag is given.
Unrelated blocker while testing
Worth its own issue if you agree, but flagging it here since it's what initially made it look like nothing was happening: on 13.2.0, kubeops generate operator throws and writes no files at all, while still exiting 0:
The exit code being 0 means an MSBuild Exec wrapping the CLI reports success, so the generation looks like a no-op rather than a failure. The 13.1.0 CLI generates all files correctly against the same project (net10.0, SDK 10.0.109), so it looks like a regression.
Happy to open a separate issue with a minimal repro if that's easier for you.
Thanks again for the quick turnaround on this.
kimpenhaus commentedon Sep 9, 2026
@nur-of thanks for the detailed analysis - I wanted to keep it simple and easy to use - that's why I added sort of auto-detection. Reading all this I think there is no way around having an additional flag. I will try to add this in the next couple of days.
the second issue you've mentioned should hopefully be fixed with #1237 - that also introduces a proper return value 😅
thanks again
nur-of commentedon Sep 11, 2026
@kimpenhaus Thanks. I'll keep an eye out for the next release :)
feat(cli/rbac): add explicit --rbac-scope option (#1251)
kimpenhaus commentedon Sep 17, 2026
@nur-of with the next version there should be an additional
--rbac-scopecli flag which could have either of the values:auto|cluster|namespacedthat should hopefully help you with your use-case.
happy to hear yout feedback 😃
nur-of commentedon Sep 22, 2026
Hey @kimpenhaus, yes this should do it, thank you!