Skip to content

Validate global V2 templates don't set index.hidden#55010

Merged
andreidan merged 6 commits intoelastic:masterfrom
andreidan:global-v2template-dont-set-indexhidden
Apr 21, 2020
Merged

Validate global V2 templates don't set index.hidden#55010
andreidan merged 6 commits intoelastic:masterfrom
andreidan:global-v2template-dont-set-indexhidden

Conversation

@andreidan
Copy link
Copy Markdown
Contributor

No description provided.

@andreidan andreidan added :Data Management/Indices APIs DO NOT USE. Use ":Distributed/Indices APIs" or ":StorageEngine/Templates" instead. v8.0.0 v7.8.0 labels Apr 9, 2020
@elasticmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-core-features (:Core/Features/Indices APIs)

@andreidan andreidan requested review from dakrone and probakowski April 9, 2020 13:39
@probakowski
Copy link
Copy Markdown
Contributor

Thanks @andreidan for working on this, I've added 2 request around null handling and 1 small nit that can be skipped

@dakrone
Copy link
Copy Markdown
Member

dakrone commented Apr 9, 2020

We could probably also add validation to MetadataIndexTemplateService.validateTemplate that can check the current composed_of settings for this also, what do you think @andreidan?

@andreidan
Copy link
Copy Markdown
Contributor Author

@dakrone sure, I'll add that validation in this PR

Copy link
Copy Markdown
Contributor

@probakowski probakowski left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @andreidan !

@andreidan
Copy link
Copy Markdown
Contributor Author

@elasticmachine update branch

elasticmachine and others added 2 commits April 15, 2020 07:13
This also prevents updating the component template to add the index.hidden
setting if that ct is referenced by a global index template.
@andreidan
Copy link
Copy Markdown
Contributor Author

@dakrone made the changes to prevent the index.hidden setting from being part of a global template (note that we're preventing it being part of the global template full stop, regardless of the true/false value it might be set to).

I didn't use the validateTemplate method as you initially suggested for this validation as that's more generic, looking at the settings and mappings only, irrespective of what kind of construct it's providing them (index templates v1, v2 or component template). It's also run after the cluster update request was issued but for the purpose of this global indices validation we can do it before we issue the cluster state update (similar to the validate(PutRequest) method used by the V1 templates).

@andreidan andreidan requested a review from probakowski April 17, 2020 09:54
@andreidan
Copy link
Copy Markdown
Contributor Author

@dakrone @probakowski this probably slipped off your radar. do you have any more thoughts/suggestions on this?

Copy link
Copy Markdown
Member

@dakrone dakrone left a comment

Choose a reason for hiding this comment

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

LGTM, thanks Andrei!

Copy link
Copy Markdown
Contributor

@probakowski probakowski left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @andreidan! I left few comments but they are purely subjective and can be skipped

// if we're updating a component template, let's check if it's part of any V2 template that will yield the CT update invalid
if (create == false && finalSettings != null) {
// if the CT is specifying the `index.hidden` setting it cannot be part of any global template
if (IndexMetadata.INDEX_HIDDEN_SETTING.exists(finalSettings)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ifs can be merged

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think that'd be necessarily more readable (especially in the context of maybe us wanting to add more checks, this index.hidden one being just one condition we identified now). If it's ok, I'll leave it like this now

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You definitely can leave it as it is, as I said this totally my personal preference only

Comment on lines +204 to +212
List<String> globalTemplatesThatUseThisComponent = new ArrayList<>();
for (Map.Entry<String, IndexTemplateV2> entry : existingTemplates.entrySet()) {
IndexTemplateV2 templateV2 = entry.getValue();
if (templateV2.composedOf().contains(name) && templateV2.indexPatterns().stream().anyMatch(Regex::isMatchAllPattern)) {
// global templates don't support configuring the `index.hidden` setting so we don't need to resolve the settings as
// no other component template can remove this setting from the resolved settings, so just invalidate this update
globalTemplatesThatUseThisComponent.add(entry.getKey());
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This can be replaced with stream version, which for me is more readable:

List<String> globalTemplatesThatUseThisComponent = currentState.metadata().templatesV2().entrySet().stream()
                .filter(e -> e.getValue().composedOf().contains(name))
                .filter(e -> e.getValue().indexPatterns().stream().anyMatch(Regex::isMatchAllPattern))
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure about this, to me they're equally readable, but your option is a bit more concise.
In the interest of moving this rather stale PR forward I'm merging it as it is and we can refactor it later if that's ok?

@andreidan andreidan merged commit 2e76898 into elastic:master Apr 21, 2020
andreidan added a commit to andreidan/elasticsearch that referenced this pull request Apr 21, 2020
Validate adding global V2 templates don't configure the index.hidden setting.
This also prevents updating the component template to add the index.hidden
setting if that component template is referenced by a global index template.

(cherry picked from commit 2e76898)
Signed-off-by: Andrei Dan <andrei.dan@elastic.co>
andreidan added a commit that referenced this pull request Apr 21, 2020
…55519)

Validate adding global V2 templates don't configure the index.hidden setting.
This also prevents updating the component template to add the index.hidden
setting if that component template is referenced by a global index template.

(cherry picked from commit 2e76898)
Signed-off-by: Andrei Dan <andrei.dan@elastic.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Data Management/Indices APIs DO NOT USE. Use ":Distributed/Indices APIs" or ":StorageEngine/Templates" instead. >non-issue v7.8.0 v8.0.0-alpha1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants