add function to get object namespaced name#24
add function to get object namespaced name#24Vicente-Cheng wants to merge 2 commits intoharvester:mainfrom
Conversation
4945a87 to
6a42808
Compare
- we could use this function to generate the <namespace>/<name> Signed-off-by: Vicente Cheng <vicente.cheng@suse.com>
6a42808 to
03c0193
Compare
| func GetNamespacedName[T any](t T) (string, error) { | ||
| switch obj := any(t).(type) { | ||
| case metav1.Object: | ||
| if obj.GetNamespace() == "" { |
There was a problem hiding this comment.
Doesn't an empty string mean the default namespace, per the k8s api doc?
There was a problem hiding this comment.
An empty namespace is equivalent to the "default" namespace, but "default" is the canonical representation. Not all objects are required to be scoped to a namespace - the value of this field for those objects will be empty.
In our case, however, we should assume that we have a namespace, because otherwise the use of this helper function would make no sense. This is also implied by the function name. Therefore I think it is correct to throw an error.
There was a problem hiding this comment.
My only hesitation is that how do we guarantee default is always specified, considering Harvester is interacting with other components like kubevirt and LH. Will those components follow the same convention? Or will this function be used strictly for Harvester resources only?
03c0193 to
3feb537
Compare
Signed-off-by: Vicente Cheng <vicente.cheng@suse.com>
3feb537 to
caf82de
Compare
|
Any progress here? I want to make use of this function in one of my PR. We can somehow reach a consensus? |
|
If we want to keep this function, we should follow how K8s does it. Here is an example of the equivalence in the k8s src: https://github.com/kubernetes/kubernetes/blob/1ab54ffa6429fb0a3ff0789d4b98bf76bbfddc94/pkg/controller/devicetainteviction/device_taint_eviction.go#L916-L921 These are examples where the empty namespace is specifically used to indicate cluster-scoped resources:
FWIW, is this function truly necessary? Most examples in k8s src just create the struct as-is, and let server-side do the error handling, and decide whether the inputs are acceptable or not. |
|
Sorry, I missed this. After @ihcsim mentioned, I did some research for the namespace generation mechanism. So, I prefer @ihcsim's last comment, we do nothing with non-namespace objects and leave the generic function here (if we prefer to have the common function for most cases) (e.g. https://github.com/kubernetes/kubernetes/blob/1ab54ffa6429fb0a3ff0789d4b98bf76bbfddc94/pkg/controller/devicetainteviction/device_taint_eviction.go#L916-L921) Or do we just leave each component to implement what they want? |
|
I am fine with having this generic function, but following the K8s src. Thanks. |
Add the general function to return the namespaced name.