-
Notifications
You must be signed in to change notification settings - Fork 898
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
Map Amazon labels to tags #14436
Map Amazon labels to tags #14436
Conversation
return unless object.kind_of?(VmOrTemplate) | ||
|
||
tags = hash[:tags].collect do |tag_hash| | ||
ContainerLabelTagMapping.find_or_create_tag(tag_hash) |
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.
hm, this method looks super ineffective for being run multiple times for each vm_or_template (huge n+1)
@kbrock seems to be super ineffective to be call here find_or_create_tag
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.
@Ladas Keep in mind that it's not going to map every tag, only those that were matched in the refresh parser. In practice I wouldn't expect more than 1-2 per VM. Is my guess incorrect? Also, I don't see a way around it, so suggestions welcome.
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.
right, we should rewrite the find_or_create_tag, so we don't do the n+1 queries there
so we should first process all the tags to pickup the unique combinations. Then load all existing in one query and create the missing.
If this could lead to huge number of AR object in memory, it would be nice to store only id then.
is the find_or_create_tag used in some refresh already?
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.
Maybe we should just move this whole save_tags_inventory here?
@kbrock are you fine with using the ineffective way and then fixing the whole ContainerLabelTagMapping separately? Also we should extract it from ContainerLabelTagMapping, since it's a generic tagging problem, not just for Containers?
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.
@Ladas Unless the ems is a ContainerManager, that won't get invoked. I guess I'm not sure what or how you're looking to use that.
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.
n+1 — hehe, you found where I buried the bodies ;-\
Confusingly, all SaveInventoryFoo modules are mixed into one namespace, doesn't matter in which file they are. Please move save_tags_inventory here from save_inventory_container.rb, rather than creating 2nd identically named method.
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.
@cben It does have the additional restriction of limiting to VmOrTemplate, though. Or do you feel that's not necessary?
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.
Ah, didn't notice VmOrTemplate. But IIUC you can't really have 2 methods with same name, only one wins (probably the save_containers one without restriction, or tests would fail).
Why do you need this restriction? You control which models get tagged by choosing where to calls to save_tags_inventory
(typically via :tags in child keys lists). Also, in what situation would you expect to have computed map_labels
, only to throw it away here?
@cben thoughts? |
@djberg96 @cben so as I try to explain here ManageIQ/manageiq-providers-amazon#194 (comment) , we should not be probably doing tagging in a refresh at all. Since we will not be able to apply the policies later (correctly) So I would be rather if we would move tagging to post refresh, using ^ for creating non existent tags and then queuing separate jobs, that would take care of correct tag assignment. A correct interface for assigning a tag should be https://github.com/Ladas/manageiq/blob/cf9e566a86adafd9d9717499b13e6b4492d2c317/app/models/classification.rb#L107-L107 (and unclassify_by_tag) since it is being used in automate. @Fryguy is that correct interface for the Classification? Seems like https://github.com/Ladas/manageiq/blob/cf9e566a86adafd9d9717499b13e6b4492d2c317/app/models/classification.rb#L127 is also invoking the right policies. |
Remove the save_tags_inventory method, use the one in save_inventory.rb. Remove type check, add 3rd argument for backwards compatibility. Update taggings tests, add node tests.
@cben, as per your suggestion, I moved the Regarding the specs, I don't explicitly test the mappings since those have their own tests already. Instead the And please double check this to make sure I haven't broken anything on the container side. :) |
# | ||
def save_tags_inventory(object, collection, _target = nil) | ||
return if collection.blank? | ||
tags = collection.is_a?(Hash) ? collection[:tags] : collection |
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.
do we actually use the Hash somewhere? The saving code should also pass it as a child, so collection[:tags] should be always passed, right?
@cben - can you approve this PR so we can get it in today before the feature freeze? Thanks |
@djberg96 would be nice to fix the rubocop issues? @blomquisg looks good to merge, but we should create the issues for the Policies and the Performance at the same time :-) |
Checked commits https://github.com/djberg96/manageiq/compare/eaf2277d27b0b25188c9d4b839e345547e599929~...e7334400a6367083e65ff9bc9e4663809f9d19c6 with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0 |
@miq-bot remove_label wip |
@Fryguy @blomquisg - this is good to go and has to be merged today to meet the feature freeze deadline, thanks. |
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.
LGTM. Nice test 👍
|
||
@tag1 = FactoryGirl.create(:tag, :name => '/managed/amazon:vm:owner') | ||
@tag2 = FactoryGirl.create(:tag, :name => '/managed/kubernetes:container_node:stuff') | ||
@tag3 = FactoryGirl.create(:tag, :name => '/managed/kubernetes:foo') # All |
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.
[Completely doesn't matter in this test: An actual "All" mapping would use /managed/kubernetes::foo
with double colon.]
# {:category_tag_id=>139, :entry_name=>"foo", :entry_description=>"bar"} | ||
# | ||
# The +collection+ argument can either be a Hash, in which case the argument | ||
# should have a single :tags key, or a simple Array of hashes. |
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.
I'm a little surprised you need to deal with {:tags => ...} form, IIUC ManageIQ/manageiq-providers-amazon#183 creates :labels and :tags on same level, you have :labels and :tags in child keys on same level, and save_child_inventory
calls save_#{k}_inventory
on hashes[k]
so I'd expect save_tags_inventory
to not see the :tags, just the value.
But I don't mind.
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.
@cben This is something we plan to refactor in the future, i.e. generate the tags on the core side rather than the refresher side.
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.
LGTM
Thanks @agrare ... I've been swamped in meetings and customer issues :( |
This PR maps custom attributes on the AWS side to tags on the ManageIQ side based on mappings that have been setup by a user.
See also ManageIQ/manageiq-providers-amazon#183 which sets the :tags attribute on Vm's and Images, which you will need for this PR to have any noticeable effect.
Once you have your Gemfile pointing at that fork and the "aws_tags3" branch, you can perform the following steps to see it in action:
Within ManageIQ go to Configuration -> Region -> Map Tags. Add an entry, selecting "Amazon::Vm" as the entity, type in "owner" as the resource label, and whatever you want for the category, e.g. "amazon_vm_owner". Then refresh your Amazon provider (or add one if you haven't already). Any VM with a custom attribute of "owner" will now show up as a company tag on the instance view.
Use the "US East - Northern Virginia" region from our Dev environment if you want to make sure you get some VM's with an "owner" attribute.