Operator-sdk: Namespace empty in controller

Created on 27 Jun 2019  路  7Comments  路  Source: operator-framework/operator-sdk

I'm having problems when i create kubernetes objects in my controler.

In my Reconcile() function I can not get the namespace name.

I want the namespace name, but the variables below are coming empty

request.Namespace

and

instance := &examplev1alpha1.TestOperator{}
instance.Namespace

What am I doing wrong?

triagsupport

Most helpful comment

@joelanford @yueyongyue @josericardomcastro You seen to be misunderstanding the concept of resource scope, and Operators SDK docs does not help a lot in this matter, indeed.

A CRD definition must have Scope: Namespaced if the resource of the Kind it defines is meant to exist isolated in different namespaces. In fact, most Kubernetes resources are Namespaced - services, deployments, pods, roles, etc. A cluster-scoped resource is a resource that does not belong to a namespace, like CustomResourceDefinition, ClusterRole, or MutatingWebhookConfiguration.

Think like this: "will you create/delete it with -n <namespace>? If so, then it'll not be cluster-scoped". It's only logical that a cluster-scoped resource won't have a namespace.

It doesn't mean the operator will only be able to work for a single namespace. This is about resource scope, not the operator's scope.

All 7 comments

@josericardomcastro If the CRD associated with examplev1alpha1.TestOperator is cluster-scoped, your Reconcile will get requests with the request.Namespace set to the empty string.

Your CRD's scope is defined in your CRD yaml file in ./deploy/crds/<yourcrd>_crd.yaml under spec.scope. Is that set to Namespaced or Cluster?

Another possibility is a misconfigured watch in your controller setup, so if you can share that code, that might help to debug your issue.

@joelanford my CRD is Cluster scope because i want to watch all namespaces, is this correct?

So how do I get the name of the namespace that is creating the crd?

@josericardomcastro Cluster-scoped CRDs will never have a namespace, since they're created at the cluster scope and don't belong to a specific namespace.

It sounds like what you're going for is an operator that watches all namespaces for namespace-scoped CRs. Is that correct?

If so, have you seen this section of our docs about operator scope? I think you should ignore the parts about CRD scope.

https://github.com/operator-framework/operator-sdk/blob/master/doc/operator-scope.md#operator-scope

Were you able to solve your problem? If not, let us know and we can re-open to continue the discussion!

I have the same problem,How to solve?

@joelanford @yueyongyue @josericardomcastro You seen to be misunderstanding the concept of resource scope, and Operators SDK docs does not help a lot in this matter, indeed.

A CRD definition must have Scope: Namespaced if the resource of the Kind it defines is meant to exist isolated in different namespaces. In fact, most Kubernetes resources are Namespaced - services, deployments, pods, roles, etc. A cluster-scoped resource is a resource that does not belong to a namespace, like CustomResourceDefinition, ClusterRole, or MutatingWebhookConfiguration.

Think like this: "will you create/delete it with -n <namespace>? If so, then it'll not be cluster-scoped". It's only logical that a cluster-scoped resource won't have a namespace.

It doesn't mean the operator will only be able to work for a single namespace. This is about resource scope, not the operator's scope.

@edsonmarquezani I agree. There is CRD scope, which dictates whether CRs are namespaced or not, AND there is operator scope, which is really just about which RBAC permissions an operator's service account has that enables it to watch and reconcile its CRs and dependent resources.

If the operator needs to reconcile or watch cluster-scoped CRDs, the operator needs RBAC permissions at the cluster level, hence why the docs discuss the various aspects and interplay between operator scope and CRD scope.

I agree that the docs are a bit confusing though. If you'd like, feel free to submit a PR if you have ideas on how to make them easier to understand.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joelanford picture joelanford  路  3Comments

ffoysal picture ffoysal  路  3Comments

gyliu513 picture gyliu513  路  3Comments

kristiandrucker picture kristiandrucker  路  5Comments

ricardozanini picture ricardozanini  路  4Comments