Skip to content
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

Fix: adding new ips with inventory builder (#7577) #7583

Merged
merged 4 commits into from
Sep 10, 2021

Conversation

VladMasarik
Copy link
Contributor

@VladMasarik VladMasarik commented May 2, 2021

What type of PR is this?
/kind bug

What this PR does / why we need it:

  1. The documentation and help suggested that the dynamic inventory script was able to add new hosts if specified. However, this did not work, and instead the original config file was overwritten by the newly specified hosts. This PR fixes it by changing load_all() function call to just load().
  2. After fixing this issue there were problems with naming scheme of the nodes. This was previously unreachable code. The original script looked at the hostname of a node, and tried to look for a numeric ID. This however failed when the hostnames were manually created such as "master" / "main" / "worker". An exception was thrown, but ignored/swallowed. I was not able to find an important usage for the IDs, therefore I have adjusted it so that the IDs are checked only when a hostname of a node starts with the HOST_PREFIX string(ie. we can assume that the hostname was automatically created). Additionally, I have at least added a print out of the exceptions, and the script exits with 1 when they are raised.
  3. I have adjusted the documentation in the script's help command, and also added a few comments to help with understanding. It is up for a discussion whether they are helpful, or whether we should remove them. As for the other documentation, I searched for the word "inventory", but couldn't find any text that would need a change. I managed to find only one instance in Getting started.
  4. I have added a new add command for the script. The previously expected behavior was that if you had an inventory config with 4 nodes, the:
$ inventory.py 10.0.0.3

would add a new node with the 10.0.0.3 IP address, thus you would have 5 nodes, or 4 if it was a duplicate IP address. However, this would actually overwrite the original inventory configs, so you would end up with just 1 node. I am not sure if people use this script, but I assume that not changing the original behavior (whether intended or not) would be better, as this issue was introduced a year ago #5373 . Although, they seem to be doing the exact opposite of what this PR does. They never show the structure of their config file, and are using .ini files, which I am assuming are different from YAML, so what they were doing, beats me. So in order to keep the old functionality, but also allow the already existing one that did not work, this PR adds an add command. Example, you have 4 nodes in your inventory and execute:

$ inventory.py add gpuvm1,10.0.0.3 gpuvm2,10.0.0.5 

This adds 5th and 6th node called "gpuvm1" and "gpuvm2" into the existing inventory YAML file.

  1. I have added new tests, and also fixed the old ones, as they had a few problems, eg. they succeed although they were not supposed to. I surely did not cover everything, but I felt this PR is an overkill already, so this is up for a discussion as well. How do you suggest I should deal with the linting errors? I couldn't think of any nice solution.

Which issue(s) this PR fixes:

Fixes #7577

Special notes for your reviewer:
Lots of text, sorry for that, but I wanted to mention things I have considered so that you can point out something if I have missed a thing.

Does this PR introduce a user-facing change?:

The dynamic inventory builder will by default overwrite the inventory config. This was previously unintended behavior. In order to add new hosts into the already existing inventory config use the "add" command e.g. "$ inventory.py add 10.0.1.8".  

@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels May 2, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @VladMasarik. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label May 2, 2021
@k8s-ci-robot k8s-ci-robot requested review from EppO and holmsten May 2, 2021 17:44
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label May 2, 2021
@floryut
Copy link
Member

floryut commented May 3, 2021

Please check the fmt issue in the CI job

@EppO
Copy link
Contributor

EppO commented May 4, 2021

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels May 4, 2021
try:
for host in self.yaml_config['all']['hosts']:
# Read configuration of an existing host
existing_hosts[host] = self.yaml_config['all']['hosts'][host]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pep8 detects invalid coding style like"

pep8 run-test: commands[0] | bash -c 'find /builds/kargo-ci/kubernetes-sigs-kubespray/contrib/inventory_builder/* -type f -name '"'"'*.py'"'"' -print0 | xargs -0 flake8'
/builds/kargo-ci/kubernetes-sigs-kubespray/contrib/inventory_builder/inventory.py:179:80: E501 line too long (81 > 79 characters)
                    existing_hosts[host] = self.yaml_config['all']['hosts'][host]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I wanted to know how to resolve it properly, as I couldn't find any good solution, ie. something that would look better than ignoring the warnings. I fixed it though.

except OSError as e:
# I am assuming we are catching "cannot open file" exceptions
print(e)
sys.exit(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this mandatory for adding add option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the add work without it? Yes, in that case it is not mandatory.

However, not having it, means that a user can specify incorrect config path, and reading the file might fail. In that case, the exception is just ignored, and yaml_config is not read. Meaning, a user expected the script to use a config file, but it silently failed, and user doesn't even know something went wrong.

to after checking whether the config
should be loaded, and added check for
whether the config should be loaded
if the user wants to remove a node, we
need to load the config
@VladMasarik
Copy link
Contributor Author

While using the script, I noticed and fixed a few mistakes I made.

  1. python inventory.py -10.0.1.1 did not work as intended, and rather deleted the config. Basically the same mistake that python inventory.py 10.0.1.1 had, and why I created this PR.
  2. The config file was loaded regardless of the loadPreviousConfig, and some functions actually used it even though they were not meant to.

@VladMasarik
Copy link
Contributor Author

ping pong @floryut @oomichi

@VladMasarik
Copy link
Contributor Author

ping @floryut @oomichi

@floryut
Copy link
Member

floryut commented Aug 19, 2021

ping @floryut @oomichi

wow sorry, don't know how this PR was missed and forgotten, fine by me on the fix; I indeed saw that this wasn't adding anything and just reloading all the file, but hadn't time to take a look.

Anyway, thanks man.

/approve
/ok-to-test
/cc @oomichi @EppO

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: floryut, VladMasarik

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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 19, 2021
Copy link
Contributor

@oomichi oomichi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my late response.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 10, 2021
@k8s-ci-robot k8s-ci-robot merged commit a5a88e4 into kubernetes-sigs:master Sep 10, 2021
@floryut floryut mentioned this pull request Sep 15, 2021
@floryut
Copy link
Member

floryut commented Sep 15, 2021

@VladMasarik Added this to the release note for 2.17, again sorry this took time to merge, great work man.

@VladMasarik
Copy link
Contributor Author

@oomichi @floryut AFAIK maintaining OSS projects isnt easy, and it takes a few months to merge the requests. I am very happy to help, and have no problems so as long as I get clear deny / approve / not priority. So happy to help, and contribute more later!

@floryut
Copy link
Member

floryut commented Oct 4, 2021

@oomichi @floryut AFAIK maintaining OSS projects isnt easy, and it takes a few months to merge the requests. I am very happy to help, and have no problems so as long as I get clear deny / approve / not priority. So happy to help, and contribute more later!

Indeed, not whining at all but the hard part is managing time between work and family 😄
Thanks again for your PR, that's what allow us to keep the project alive

LuckySB pushed a commit to southbridgeio/kubespray that referenced this pull request Oct 23, 2021
…bernetes-sigs#7583)

* Fix: adding new ips with inventory builder (kubernetes-sigs#7577)

* moved conflig loading logic
to after checking whether the config
should be loaded, and added check for
whether the config should be loaded

* added check for removing nodes from config
if the user wants to remove a node, we
need to load the config

* Fix tox errors
sakuraiyuta pushed a commit to sakuraiyuta/kubespray that referenced this pull request Apr 16, 2022
…bernetes-sigs#7583)

* Fix: adding new ips with inventory builder (kubernetes-sigs#7577)

* moved conflig loading logic
to after checking whether the config
should be loaded, and added check for
whether the config should be loaded

* added check for removing nodes from config
if the user wants to remove a node, we
need to load the config

* Fix tox errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix: inventory builder overwrites the already existing nodes specified in the host.yaml
5 participants