-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add method to dynamically set namespace in yaml files #782
Add method to dynamically set namespace in yaml files #782
Conversation
I will add a test case for the new method. |
I think this could be done easily outside of |
@micw523 yes with the MR it's possible since you can pass complete objects to the method. I still think that the possibility to set the namespace dynamically adds value to the end user, it's easier to set the namespace directly while iterating over the objects in: https://github.com/kubernetes-client/python/pull/783/files#diff-561089da9975de9999b6638341c50914R75 Otherwise I need to read all objects by myself patch them with the desired namespace and then pass them to |
I think without this function you still have to have a list of the yaml files you're going to load anyway and load one by one... I don't think patching it will be too much. On a side note, I don't think overriding namespace should be encouraged. I just checked the behavior of the typed client. If you attempt to override the namespace in the yaml file, you get this error:
I think it's important that the |
Currently one benefit would be that the You actually can overwrite the namespace you just need to modify the namespace of the loaded object see: https://github.com/kubernetes-client/python/pull/782/files#diff-561089da9975de9999b6638341c50914R67 but I think your concern is correct and we shouldn't overwrite the namespace of an API object (if defined). I still think that there is a good use case for
Without a If you like I would adjust my PR to match the behaviour of For the following manifest the behavior would be: apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: web
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- image: nginx
name: nginx If there is no namespace defined in the yaml file I can pass $ kubectl -n test apply -f test.yaml
deployment.apps/web created
$ kubectl -n test get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
web 1/1 1 1 6s If I add a namespace to the yaml file I get the error message you mentioned above: apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: web
name: web
namespace: test # defined a namespace
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- image: nginx
name: nginx See: kubectl -n default apply -f test.yaml
error: the namespace from the provided object "test" does not match the namespace "default". You must pass '--namespace=test' to perform this operation. TLDR: My goal would it be to mimic the behavior of |
To be honest, we’re not getting into apply -f behavior. We’re doing create only. See discussion in #504 |
In another thought, if the yaml file didn’t set a namespace, providing an option for setting it should be fine and is consistent with the offering by the current client. I think we should be consistent with what the client does - instead of changing the namespace of the yaml file, we could pass the namespace as a parameter to subsequent calls of |
That's actually the main use case I would like to support. I will start working on this at the end of the week. Thanks for your great feedback! |
/assign |
4c846b0
to
06776ce
Compare
@micw523 PTAL :) |
In my opinion we do not need a second function for this. Why not add namespace as an optional parameter to the original function since our typed client already does this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good otherwise.
Actually I just didn't thought on using an optional parameter. Makes everything much easier/clearer. I adjusted the code. |
The CI is not working due to a new release of kubernetes. I'll retest the PR after our CI is back up again. |
@micw523 I added some more docs to the |
ping @micw523 |
Hi @johscheuer, this lgtm to me and I already sent it to Roy to review. He might be a little too busy... |
/lgtm |
I'd expect the namespace field in yaml blobs to be generated, but this won't hurt /approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: johscheuer, roycaihw The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Fixes: #781
This PR allows a user to set a custom namespace if creating API objects from a yaml file. This is useful if you generate your namespaces dynamically in the Python code (e.g. e2e tests).