Description
Problems
In my case, my deployment manifests have both cluster scoped resources (e.g. CustomResourceDefinition
, ClusterRoleBinding
) and namespace scoped resources(e.g. Deployment
, Service
). I want to assign a namespace value to the namespace scoped resources. I also want the declarativeObject
to be the ownerRef
of both kinds, so that I can have the deployment manifests be easily cleaned up by just deleting the declarativeObject
.
Available features
Current k-d-p provides 3 handy features:
- Users do not need to assign namespaces to their deployment manifests, as long as the
declarativeObject
is namespace scoped, its value can be used to modify any namespace scoped deployment manifests which does not have the namespace assigned. - Users can choose to disable this namespace auto-assign feature by
WithPreserveNamespace
. - Users can request the
declarativeObject
to be the owner of the deployment manifests, which is very easy for resource clean up. This can be done viadeclarative.WithOwner(declarative.SourceAsOwner)
. If thedeclarativeObject
is namespace scoped, the cluster scoped deployment manifests will be skipped and won't have a ownerRef. thedeclarativeObject
is cluster scoped, all manifests will have it as theownerRef
.
My current solutions
Right now, I have two options but neither are handy enough:
Option 1:
Set the declarativeObject
as namespace scoped, this will auto-assign the namespace to deployment manifests. It has two problems:
- I have to create the namespace object before applying the
declarativeObject
object and cannot rely on my k-d-p operator to guarantee the existence of the namespace. - I have to manage the cluster scoped resources separately, because namespace scoped resource can not be the
ownerRef
if cluster resources.
Option 2:
The declarativeObject
is cluster scoped. It can be the ownerRef
of all manifests and I can add the namespace existence logic in my k-d-p operator. But I have to implement my own declarative methods to update the namespace.
Proposals
Add a new declarative method which does not rely on the declarativeObject
but can assign given namespace to the deployment manifests (it's different than skipping the namespace as what WithPreserveNamespace
does, but assigning default namespace). Even better, if the namespace object does not exist in the cluster, I want it to be created by request.
New declarative method
WithNamespace(namespace string, createIfNotExist bool)
This method assigns namespace namespace
to any namespace scoped deployment manifests, only if their current namespace is empty (basically the same behavior of current setNamespaces
but using a dedicated namespace value).
namespace
is the dedicated namespace value,
createIfNotExist
guarantees the existence of the namespace object before applying any other resources.
Please share your thoughts!
I think the use case I described can be very common for users who need k-d-p. And I'm just proposing a simple solution that is good enough for my use case. I think the behavior of WithNamespace
can also be extended to update all the namespace-scoped deployment manifests rather than merely the unassigned ones. But I don't have a strong opinion on this.
Besides updating the metadata.namespace
, we can also extend the setNamespace
to update spec/conversion/webhook/clientConfig/service/namespace
in CustomResourceDefinition
, spec/service/namespace
in APIService
, etc.