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

Pull the correct collection plugin for the product #15658

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from

Conversation

jessicamack
Copy link
Member

@jessicamack jessicamack commented Nov 22, 2024

SUMMARY

There exist several cases of inventory plugins with the same name but different implementations. Historically, AWX/Controller pivoted on attributes on the inventory source to figure out which ansible inventory to call. Now, instead of a single implementation we've split them into two implementations completely. This PR is to implement the change to pick which one we want and when.

Requires: ansible/awx-plugins#61

ISSUE TYPE
  • New or Enhanced Feature
COMPONENT NAME
  • Collection
AWX VERSION

ADDITIONAL INFORMATION

Comment on lines +1408 to +1410
awx_entry_points = {ep.name: ep for ep in entry_points(group='awx_plugins.inventory')}
supported_entry_points = {ep.name: ep for ep in entry_points(group='awx_plugins.inventory.supported')}
entry_points = awx_entry_points if detect_server_product_name() == 'AWX' else {**awx_entry_points, **supported_entry_points}
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps, it's better to skip the lookup altogether when not needed?

Suggested change
awx_entry_points = {ep.name: ep for ep in entry_points(group='awx_plugins.inventory')}
supported_entry_points = {ep.name: ep for ep in entry_points(group='awx_plugins.inventory.supported')}
entry_points = awx_entry_points if detect_server_product_name() == 'AWX' else {**awx_entry_points, **supported_entry_points}
awx_entry_points = {ep.name: ep for ep in entry_points(group='awx_plugins.inventory')}
supported_entry_points = dict() if detect_server_product_name() == 'AWX' else {ep.name: ep for ep in entry_points(group='awx_plugins.inventory.supported')}
entry_points = {**awx_entry_points, **supported_entry_points}

Comment on lines +1412 to +1414
for entry_point_name, entry_point in entry_points.items():
cls = entry_point.load()
InventorySourceOptions.injectors[entry_point_name] = cls
Copy link
Member

Choose a reason for hiding this comment

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

It'd be best if this wasn't happening during the import time but called in some predictable hook point, early in django's lifecycle.

Copy link
Member

Choose a reason for hiding this comment

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

Yep, here is a better place

Copy link
Member

@webknjaz webknjaz left a comment

Choose a reason for hiding this comment

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

May I suggest a little refactoring?

Comment on lines +1408 to +1410
awx_entry_points = {ep.name: ep for ep in entry_points(group='awx_plugins.inventory')}
supported_entry_points = {ep.name: ep for ep in entry_points(group='awx_plugins.inventory.supported')}
entry_points = awx_entry_points if detect_server_product_name() == 'AWX' else {**awx_entry_points, **supported_entry_points}
Copy link
Member

@webknjaz webknjaz Nov 27, 2024

Choose a reason for hiding this comment

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

How about

Suggested change
awx_entry_points = {ep.name: ep for ep in entry_points(group='awx_plugins.inventory')}
supported_entry_points = {ep.name: ep for ep in entry_points(group='awx_plugins.inventory.supported')}
entry_points = awx_entry_points if detect_server_product_name() == 'AWX' else {**awx_entry_points, **supported_entry_points}
def _load_all_entry_points_for(entry_point_subsections: list[str], /) -> dict[str, EntryPoint]:
return {
ep.name: ep
for entry_point_category in entry_point_subsections
for ep in entry_points(group=f'awx_plugins.{entry_point_category}')
}
is_awx = detect_server_product_name() == 'AWX'
extra_entry_point_groups = () if is_awx else ('inventory.supported', )
entry_points = _load_all_entry_points_for(['inventory', *extra_entry_point_groups])

?

Copy link
Member

Choose a reason for hiding this comment

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

Although, it probably makes sense to put _load_all_entry_points_for() into awx_plugins.interfaces, making it reusable.

cc @chrismeyersfsu

Copy link
Member

Choose a reason for hiding this comment

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

I lean towards not putting it in awx_plugins.interfaces right now. AWX is the only thing we plan on consuming these entry points at this time.

@@ -9,6 +9,7 @@
import copy
import os.path
from urllib.parse import urljoin
from importlib.metadata import entry_points
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
from importlib.metadata import entry_points
from importlib.metadata import entry_points, EntryPoint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants