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

add pacemaker_resource plugin #9531

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

munchtoast
Copy link

SUMMARY

Adds in pacemaker_resource plugin for managing pacemaker cluster resources.

ISSUE TYPE
  • New Module/Plugin Pull Request
COMPONENT NAME

pacemaker_resource

ADDITIONAL INFORMATION

@ansibullbot

This comment was marked as outdated.

@ansibullbot ansibullbot added WIP Work in progress ci_verified Push fixes to PR branch to re-run CI module module new_contributor Help guide this first time contributor plugins plugin (any type) labels Jan 6, 2025
@felixfontein felixfontein added check-before-release PR will be looked at again shortly before release and merged if possible. backport-10 Automatically create a backport for the stable-10 branch labels Jan 6, 2025
@munchtoast munchtoast force-pushed the main branch 2 times, most recently from 3ced6e7 to 1c0286e Compare January 9, 2025 15:51
Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

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

Hi @munchtoast thanks for your contribution!

I have many comments on this PR, hope not to crush your motivation. We have all made these mistakes in the past, it is just a matter of learning. :-)

Also, please note that all new modules must have tests created with them (unit tests or integration tests).

In your case, since this is a module based on run_command(), you can easily write unit tests using the test helper I wrote. Take a look at the directory
https://github.com/ansible-collections/community.general/tree/main/tests/unit/plugins/modules take a look at the tests modules that have an accompanying YAML file. I am yet to write proper documentation for that helper, but its use should be quite straightforward. If you decide to follow this path, I will be happy to assist you with those tests.

Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

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

Hi @munchtoast , still a draft, I know, but leaving a couple of comments nonetheless.

@munchtoast
Copy link
Author

Hi @munchtoast , still a draft, I know, but leaving a couple of comments nonetheless.

Hi @russoz, thank you so much for your reviews! I am currently writing unit tests but running into issues. I'll push what I have but I'm sorry for slow delay in applying feedback. Just wanted to give you an update that my wip commits should come in by end of this weekend.

@ansibullbot ansibullbot added the stale_ci CI is older than 7 days, rerun before merging label Jan 24, 2025
@russoz
Copy link
Collaborator

russoz commented Jan 24, 2025

No worries, take your time and feel free to ask for help. We have all been there once 😉

@ansibullbot ansibullbot added new_plugin New plugin tests tests unit tests/unit and removed ci_verified Push fixes to PR branch to re-run CI stale_ci CI is older than 7 days, rerun before merging labels Jan 27, 2025
@munchtoast
Copy link
Author

Noticing that there may be an issue with the current implementation, where the create should at least include resource_type.resource_name, but is not required for delete as the name is sufficient. Not sure if there's a way to make this required if state: create...

Also I noticed in other .yaml unit tests that environ is set to &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true}. When I tried utilizing this in my unit test I am getting assertion errors on args list. Setting the environ to {} allows the unit test to pass but I'm not sure how I could use this anchor?

@ansibullbot

This comment was marked as resolved.

@ansibullbot ansibullbot added the module_utils module_utils label Jan 27, 2025
@russoz
Copy link
Collaborator

russoz commented Jan 28, 2025

Also I noticed in other .yaml unit tests that environ is set to &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true}. When I tried utilizing this in my unit test I am getting assertion errors on args list. Setting the environ to {} allows the unit test to pass but I'm not sure how I could use this anchor?

Without seeing the specific error it's going to be hard to guess what the problem is. Could you please push the code generating that error so I can see the output here? Or if one of these runs above had that, please point it to me.

Couple of housekeeping comments:

  • I would suggest you rebase your branch, it is 140+ commits behind from main. There are no conflicts so it should be fairly simple, and it makes things slightly faster for git (and github).
  • There is no need to force push everytime, you can just do a normal push (if you change something through github's webUI you will need to git pull first, though).

Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

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

Additionally, some comments on the code - there are some improvements that could be made.

@ansibullbot
Copy link
Collaborator

@munchtoast this PR contains the following merge commits:

Please rebase your branch to remove these commits.

click here for bot help

@ansibullbot ansibullbot added merge_commit This PR contains at least one merge commit. Please resolve! needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html labels Jan 28, 2025
@munchtoast
Copy link
Author

Also I noticed in other .yaml unit tests that environ is set to &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true}. When I tried utilizing this in my unit test I am getting assertion errors on args list. Setting the environ to {} allows the unit test to pass but I'm not sure how I could use this anchor?

Without seeing the specific error it's going to be hard to guess what the problem is. Could you please push the code generating that error so I can see the output here? Or if one of these runs above had that, please point it to me.

I don't think this is an error, but seems to be misunderstanding on my part as I am still learning. Other test .yaml utilize the CmdRunner's init function. My latest commit is an attempt to use that format, which should help in adding future pacemaker modules.

Couple of housekeeping comments:

  • I would suggest you rebase your branch, it is 140+ commits behind from main. There are no conflicts so it should be fairly simple, and it makes things slightly faster for git (and github).
  • There is no need to force push everytime, you can just do a normal push (if you change something through github's webUI you will need to git pull first, though).

Should be up to date now!


def __init_module__(self):
self.runner = pacemaker_runner(self.module, cli_action='resource')
self.does_not = "Error: resource or tag id '{0}' not found".format(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this attribute being used? If not, then remove.

Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

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

Hi @munchtoast

I took abother look and spotted a couple of things more. I know this can be tiring and even frustrating and I want to thank you for your resilience.

See comments below. Thx

anchors:
environ: &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
test_cases:
- id: test_missing_input
Copy link
Collaborator

Choose a reason for hiding this comment

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

please increase the indentation by 2 spaces for this list - not only as it is kinda becoming our convention in the collection, but also for consistency (see line 22 for example)

value: " * virtual-ip\t(ocf:heartbeat:IPAddr2):\t Started"
mocks:
run_command:
- command: ["/testbin/pcs", resource, status, virtual-ip]
Copy link
Collaborator

Choose a reason for hiding this comment

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

minor nit, not blocking anything but just for the record, those quotes are not needed:

Suggested change
- command: ["/testbin/pcs", resource, status, virtual-ip]
- command: [/testbin/pcs, resource, status, virtual-ip]

Comment on lines 142 to 143
change_params = ('name', )
diff_params = ('name', )
Copy link
Collaborator

Choose a reason for hiding this comment

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

I missed this before, sorry, but I don't think the value of name will change, will it?

Comment on lines 176 to 177
self.vars.set('_value', self.vars.previous_value, output=False, change=True)
self.vars.set_meta('value', initial_value=self.vars.previous_value)
Copy link
Collaborator

Choose a reason for hiding this comment

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

while on the other hand, value of value is likely going to change, so maybe you add change=True, diff=True to it...

... and then you can ditch the variable _value, as you would be tracking change directly in the variable value.

Comment on lines 184 to 185
self.vars.value = None if out == "" else out
return self.vars.value
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is something else that I missed - within this tiny little function (defined within another function), you're making a change to the state of an attribute of another attribute of the class. It is not a huge problem in this context, so I will not press on you changing this, but just for the record, one thing that is usually important in software engineering is separation of concerns. This function should not be concerned about the object, its attribute, and the attribute of its attribute. There are probably better ways to explain that - I am not much of an academic - but making that reference here kinda breaks that separation. What you usually want to do is to have smaller pieces specialized in doing one thing and one thing only. In this case, I would write it as:

Suggested change
self.vars.value = None if out == "" else out
return self.vars.value
return None if out == "" else out

and later, when calling ctx.run() (line 193 for example), you would do:

            self.vars.value = ctx.run()

you might argue that you are saving for attributing the variable once instead of four times, but that pays off in readability, maintainability. Five years from now, someone (maybe even yourself, if you stay away from the module for a while), will read the state_absent() method and will have no idea that self.vars.value is altered at all. It is not obvious. Only if you read the entire thing, paying attention to the details, that will become evident. Sometimes it is better to write more and read/understand quicker than the other way around. ;-) my $0.02

Again, it is not a major issue in the context of a single Ansible module, but I try to make things better even in small situations. Feel free to reject the suggestion. :-)

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the explanation! I pushed in a new commit which should remove unintended modification on self.vars.value, and uses the proposed self.vars.value = ctx.run(). To pass with the current unit tests, I pushed this as replacement:

self.vars.value = ctx.run()
self.vars.set('value', self._get())

...Which should pass the unit tests but I'm not sure if this is too redundant? I could maybe remove the self.vars.set('value', self._get()), though it would be more definitive in terms of resource status.

Thoughts on this current approach?

self.vars.stdout = ctx.results_out
self.vars.stderr = ctx.results_err
self.vars.cmd = ctx.cmd
self.vars.set('new_value', self._get(), fact=True)
Copy link
Collaborator

Choose a reason for hiding this comment

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

whoa, and I missed that one before as well: I don't think we should be generating ansible_facts from this. Any particular reason to be using that?

Cc: @felixfontein

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree, modules should never return ansible_facts except in very special situations. I don't think this module is one of these exceptions. Can you explain why this module should return facts?

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the catch! The ansible_facts should not be returned here, and was a mistake on my end. This should be removed in a new commit.

short_description: Manage pacemaker resources
author:
- Dexter Le (@munchtoast)
version_added: 10.4.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

10.4.0 has been released, please adjust this to:

Suggested change
version_added: 10.4.0
version_added: 10.5.0

@ansibullbot ansibullbot added needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-10 Automatically create a backport for the stable-10 branch check-before-release PR will be looked at again shortly before release and merged if possible. module_utils module_utils module module needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR new_contributor Help guide this first time contributor new_plugin New plugin plugins plugin (any type) tests tests unit tests/unit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants