pkg/generator: Work in my namespace, not default#227
pkg/generator: Work in my namespace, not default#227fanminshi merged 1 commit intooperator-framework:masterfrom
Conversation
|
@eparis In the issue #187, I'd suggest that we set the pod's namespace via a environment variable instead of via mounting a volume . e.g apiVersion: apps/v1
kind: Deployment
metadata:
name: <your-operator>
spec:
replicas: 1
template:
metadata:
labels:
name: <your-operator>
spec:
containers:
- name: <your-operator>
image: <your-operator-image>
env:
- name: MY_POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespaceIn main.go: namespace := os.Getenv("MY_POD_NAMESPACE")
...As you can see, this approach has less generated code than the mounting volume approach, and you don't have to retrieve the namespace value from a local file which is also susceptible to IO error; hence setting and getting namespace from env seems simpler. |
|
@eparis kindly ping? |
|
I'm at summit, but I hope to get this re-pushed today/tomorrow (the other 4 tee tiny PRs I created yesterday on the plane) |
|
@eparis sounds good! |
|
FYI, I pushed it with ENV |
|
After discussing this with rest of operator-sdk team, we decided to that it is probably the best to wrap the The reason for behind that is to provide an opinionated way of getting namespace so that the user doesn't have figure out what's the best way to get namespace. Hence, I think that it is best to have the following function // MustGetNamespace gets the namespace of the pod and panics if namespace is not found.
func MustGetNamespace() string {
pn := os.Getenv(PodNameSpaceEnvVar)
if len(pn) == 0 {
panic("namespace can not be empty")
}
return pn
}Note: We don't want the namespace to be empty because the and the // PodNameSpaceEnvVar is the constant for env variable POD_NAMESPACE
// which is the namespace that the pod is currently running in.
PodNameSpaceEnvVar = "POD_NAMESPACE"And in namespace := k8utils.MustGetNamespace()
...Also use apiVersion: apps/v1
kind: Deployment
metadata:
name: <your-operator>
spec:
replicas: 1
template:
metadata:
labels:
name: <your-operator>
spec:
containers:
- name: <your-operator>
image: <your-operator-image>
env:
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace |
|
pkg/generator/olm_catalog_tmpl.go uses Is there a reason to use Should the description for |
This will make it, by default, watch for CRs in the same namespace as the operator and it will create the busybox pods in that namespace as well. The watch namespace can be set by setting the WATCH_NAMESPACE env explicitly in the operator deployment. Resolves operator-framework#187
|
Yes, we should standardize env vars. I realized that
If
We want to force operator to only watch one ns and this ns is default to the namespace that the operator pod is running in. We probably won't explain the unrecommended modification to user for now until we have a use case. The goal of operator-sdk is to provide an opinionated way of building operator. Some references on above decision:
ref: #236 (comment) |
|
I think we can merge this as is, it is better than the current behavior. We can implement a MustGetNamespace in a follow up PR, both functions are useful. |
|
merging. let's add any improvements in future prs if deemed necessary. |
This will make it, by default, watch for CRs in the same namespace as the
operator and it will create the busybox pods in that namespace as well.
Resolves #187