-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[uiSettings] always use the latest config document to create the new one #159649
Changes from all commits
456c48d
ed5af5c
7a50468
b91a030
2b64f78
6369aea
8d08346
5a3346e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,11 @@ | |
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server'; | ||
import Semver from 'semver'; | ||
import type { | ||
SavedObjectsClientContract, | ||
SavedObjectsFindResult, | ||
} from '@kbn/core-saved-objects-api-server'; | ||
import type { ConfigAttributes } from '../saved_objects'; | ||
import { isConfigVersionUpgradeable } from './is_config_version_upgradeable'; | ||
|
||
|
@@ -47,11 +51,26 @@ export async function getUpgradeableConfig({ | |
}); | ||
|
||
// try to find a config that we can upgrade | ||
const findResult = savedConfigs.find((savedConfig) => | ||
const matchingResults = savedConfigs.filter((savedConfig) => | ||
isConfigVersionUpgradeable(savedConfig.id, version) | ||
); | ||
if (findResult) { | ||
return { id: findResult.id, attributes: findResult.attributes }; | ||
const mostRecentConfig = getMostRecentConfig(matchingResults); | ||
if (mostRecentConfig) { | ||
return { id: mostRecentConfig.id, attributes: mostRecentConfig.attributes }; | ||
} | ||
Comment on lines
+54
to
60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The root change. The code was making the assumption that sorting by E.g ["9517", "16602", "63240"].sort()
>> (3) ['16602', '63240', '9517'] So we're now manually comparing the version of each matching document to make sure that the most recent is used. |
||
return null; | ||
} | ||
|
||
const getMostRecentConfig = ( | ||
results: Array<SavedObjectsFindResult<UpgradeableConfigAttributes>> | ||
): SavedObjectsFindResult<UpgradeableConfigAttributes> | undefined => { | ||
return results.reduce<SavedObjectsFindResult<UpgradeableConfigAttributes> | undefined>( | ||
(mostRecent, current) => { | ||
if (!mostRecent) { | ||
return current; | ||
} | ||
return Semver.gt(mostRecent.id, current.id) ? mostRecent : current; | ||
}, | ||
undefined | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import { firstValueFrom, Observable } from 'rxjs'; | |
import { mapToObject } from '@kbn/std'; | ||
|
||
import type { Logger } from '@kbn/logging'; | ||
import { stripVersionQualifier } from '@kbn/std'; | ||
import type { CoreContext, CoreService } from '@kbn/core-base-server-internal'; | ||
import type { InternalHttpServiceSetup } from '@kbn/core-http-server-internal'; | ||
import type { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server'; | ||
|
@@ -32,6 +33,7 @@ export interface SetupDeps { | |
http: InternalHttpServiceSetup; | ||
savedObjects: InternalSavedObjectsServiceSetup; | ||
} | ||
|
||
type ClientType<T> = T extends 'global' | ||
? UiSettingsGlobalClient | ||
: T extends 'namespace' | ||
|
@@ -109,7 +111,7 @@ export class UiSettingsService | |
const isNamespaceScope = scope === 'namespace'; | ||
const options = { | ||
type: (isNamespaceScope ? 'config' : 'config-global') as 'config' | 'config-global', | ||
id: version, | ||
id: stripVersionQualifier(version), | ||
Comment on lines
113
to
+114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency with how we're treating qualified versions (e.g X.Y.Z-SNAPSHOT or X.Y.Z-RC1) in other parts of core, especially in SO code, we're now stripping qualifiers when providing the version to the uiSettings client. Note that this isn't directly related to the fix, but it was trivial enough to include it there. |
||
buildNum, | ||
savedObjectsClient, | ||
defaults: isNamespaceScope | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
/** | ||
* Coerce a semver-like string (x.y.z-SNAPSHOT) or prerelease version (x.y.z-alpha) | ||
* to regular semver (x.y.z). | ||
*/ | ||
export function stripVersionQualifier(version: string): string { | ||
return version.split('-')[0]; | ||
} | ||
Comment on lines
+13
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extracted from the SO service to be reused elsewhere |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
* 2.0. | ||
*/ | ||
|
||
import { stripVersionQualifier } from '@kbn/std'; | ||
import { FtrProviderContext } from '../../ftr_provider_context'; | ||
|
||
export default function enterSpaceFunctionalTests({ | ||
|
@@ -32,7 +33,7 @@ export default function enterSpaceFunctionalTests({ | |
{ space: 'another-space' } | ||
); | ||
const config = await kibanaServer.savedObjects.get({ | ||
id: await kibanaServer.version.get(), | ||
id: stripVersionQualifier(await kibanaServer.version.get()), | ||
type: 'config', | ||
Comment on lines
35
to
37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only FTR test that was doing direct |
||
}); | ||
await kibanaServer.savedObjects.update({ | ||
|
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.
Could there ever be a scenario where we have an invalid version value in the
id
field? If so, would be nice to have test coverage for it. If not, ignore this!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.
In theory, there shouldn't, but life alway finds a way, so I guess adding a test wouldn't hurt