-
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
utils
and utils.create_from_yaml
improvement suggestions
#722
Comments
The YAML loaders should parse (most) JSON correctly especially those created by kubectl so I don’t think the JSON part is needed. I don’t think you use the -f flag if you’re parsing from stdin though. |
It's not usual to do this, but I have seen people do this in scripts, for example:
In any case, would you accept a PR with my suggestions? |
I take 50% of what I said back - I’m conflicted on how useful this would be. It would be simple enough to implement though on top of the |
Currently, the function If so, my PR will include only these changes:
|
I'd rather not do it since your suggestion will break existing API while not necessary. If the addition of feature is deemed necessary, I'd rather make the change inside |
@micw523
|
The function should also support file handles instead of path strings, so if you pass it a StringIO object, stdin or an already open file with valid yaml it will also work. something like the suggested |
I think the Python interface, using JSON is very common, I create applications are the transfer of JSON format data for application creation, easy to use, integration into their own platform is also very convenient. |
That would be a very convenient feature, has anyone started working on it? Also wouldn't it be useful to expand the API with providing custom https://pyyaml.org/wiki/PyYAMLDocumentation#constructors-representers-resolvers |
@dee-me-tree-or-love I have a branch where this is already partially implemented. I need to clean it and make a PR. Will do so this week. |
@dee-me-tree-or-love I pushed my branch. I agree that custom resovlers and constructor could be useful, but in order to enhance the chance that this PR is accepted I think the scope should be minimum. |
@dee-me-tree-or-love I'm not sure if we need a custom class for loading YAML since
|
/assign |
The current master branch has |
Can you reopen this, @oz123? We could still get something done here. |
@micw523 I appreciate you not giving up on this. I will need more guidance as for what can be still added. |
As a start, the e2e_test folder now contains a few extra yaml files that weren’t there before. There are a few files that are List kind or contain multiple resources in them. Those cannot be handled by the What we can do is approximately the same thing you did it before and still change the |
Another reason the |
hello @oz123 @micw523, sorry I have been not responsive, great to see the progress, thanks! # nginx-deployment.yaml (silly example file)
apiVersion: v1
kind: Service
metadata:
name: ${SERVICE_NAME}
spec:
ports:
- port: ${SERVICE_PORT}
targetPort: ${TARGET_PORT}
name: web-app
- port: 80
targetPort: 80
name: nginx
type: ${SERVICE_PORT_TYPE} and then # Define the resolvers and the constructors
# see https://pyyaml.org/wiki/PyYAMLDocumentation#constructors-representers-resolvers
path_matcher = re.compile(r'\$\{([^}^{]+)\}')
def path_constructor(loader, node):
''' Extract the matched value, expand env variable, and replace the match '''
value = node.value
match = path_matcher.match(value)
env_var = match.group()[2:-1]
return os.environ.get(env_var) + value[match.end():]
def main():
# Configure the utils to use the resolver and constructor
utils.add_implicit_yaml_resolver('!path', path_matcher)
utils.add_yaml_constructor('!path', path_constructor)
# Now all the environment variables will be substituted on the flight
utils.create_from_yaml(k8s_client, "nginx-deployment.yaml")
... Thought of this from this discussion https://stackoverflow.com/questions/26712003/pyyaml-parsing-of-the-environment-variable-in-the-yaml-configuration-file Maybe there is a better way to do this already, which I am not aware of already, would very much appreciate a suggestion if so, sorry for a long message and if I misunderstood something |
@dee-me-tree-or-love adding such functionality to the kubernetes python client is a bad idea, you are mixing features of a specific yaml deserializer implementation with a generic api client and these two things should have no connection besides that the output of one (which is an generic python object) can be the input of the other. But your feature will be more easily possible with the proposed improvements as as it decouples the data format from the input to the function so you can use these features more freely, e.g. your code could then look like this:
(you can already do that right now by writing the parsed yaml to a NamedTempfile, but that is very ugly which is why this issue exists) |
@juliantaylor thanks a lot for the suggestion, indeed, I definitely didn't take the big picture into the consideration and made quite a mistake in mixing the boundaries... thanks a lot for pointing out this fact, it is a very good learning! |
This is a fix for kubernetes-client#722
@micw523 please see my PR #795 . This is my suggestion on how to allow creating of k8s objects A later version could deprecate usage of create_from_yaml with a file, and require direct usage Also, I still have not exposed |
This is a fix for kubernetes-client#722
Friendly bump, is this still on the review? :) |
@dee-me-tree-or-love @micw523 hasn't had any public activity since April, and I would appreciate it if we could merge this PR. It's been rotting here, and since I can't merge this one, I put many other contributions on hold. Also, there is another PR with a similar intent #770, which has been rotting. This functionality is really wanted (and needed), so please merge this. It would be a good community service. |
This issue is finally fixed. |
create_from_yaml
should accept a string like object, likeyaml.load
andsafe_safe
do.Then, it's the user's task to read the file.
To help users we can add a function
create_from_file
which accept a file path.create_from_file
will than callcreate_from_yaml
.The new signatures should be:
Additionally, we should add:
This approach brings the python client a little bit closer to
kubectl create -f <filename>
which accepts either files and streams from stdin, file, formatted as json and yaml).
I will happily prepare patch for this.
The text was updated successfully, but these errors were encountered: