Skip to content

Commit

Permalink
Deprecate migrations.enableV2 (elastic#96398)
Browse files Browse the repository at this point in the history
* deprecate migrations.enableV2

* provide deprecation test tool from core

* use deprecation test tool in tests

* add a test for SO depreacations
  • Loading branch information
mshustov authored and kibanamachine committed Apr 7, 2021
1 parent 41c9cc4 commit 56335ed
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 74 deletions.
21 changes: 3 additions & 18 deletions src/core/server/config/deprecation/core_deprecations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,12 @@
* Side Public License, v 1.
*/

import { configDeprecationFactory, applyDeprecations } from '@kbn/config';
import { getDeprecationsForGlobalSettings } from '../test_utils';
import { coreDeprecationProvider } from './core_deprecations';

const initialEnv = { ...process.env };

const applyCoreDeprecations = (settings: Record<string, any> = {}) => {
const deprecations = coreDeprecationProvider(configDeprecationFactory);
const deprecationMessages: string[] = [];
const migrated = applyDeprecations(
settings,
deprecations.map((deprecation) => ({
deprecation,
path: '',
})),
() => ({ message }) => deprecationMessages.push(message)
);
return {
messages: deprecationMessages,
migrated,
};
};
const applyCoreDeprecations = (settings?: Record<string, any>) =>
getDeprecationsForGlobalSettings({ provider: coreDeprecationProvider, settings });

describe('core deprecations', () => {
beforeEach(() => {
Expand Down
52 changes: 52 additions & 0 deletions src/core/server/config/test_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.
*/
import type { ConfigDeprecationProvider } from '@kbn/config';
import { configDeprecationFactory, applyDeprecations } from '@kbn/config';

function collectDeprecations(
provider: ConfigDeprecationProvider,
settings: Record<string, any>,
path: string
) {
const deprecations = provider(configDeprecationFactory);
const deprecationMessages: string[] = [];
const migrated = applyDeprecations(
settings,
deprecations.map((deprecation) => ({
deprecation,
path,
})),
() => ({ message }) => deprecationMessages.push(message)
);
return {
messages: deprecationMessages,
migrated,
};
}

export const getDeprecationsFor = ({
provider,
settings = {},
path,
}: {
provider: ConfigDeprecationProvider;
settings?: Record<string, any>;
path: string;
}) => {
return collectDeprecations(provider, { [path]: settings }, path);
};

export const getDeprecationsForGlobalSettings = ({
provider,
settings = {},
}: {
provider: ConfigDeprecationProvider;
settings?: Record<string, any>;
}) => {
return collectDeprecations(provider, settings, '');
};
26 changes: 7 additions & 19 deletions src/core/server/elasticsearch/elasticsearch_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,17 @@ import {
mockReadPkcs12Truststore,
} from './elasticsearch_config.test.mocks';

import { applyDeprecations, configDeprecationFactory } from '@kbn/config';
import { ElasticsearchConfig, config } from './elasticsearch_config';
import { getDeprecationsFor } from '../config/test_utils';

const CONFIG_PATH = 'elasticsearch';

const applyElasticsearchDeprecations = (settings: Record<string, any> = {}) => {
const deprecations = config.deprecations!(configDeprecationFactory);
const deprecationMessages: string[] = [];
const _config: any = {};
_config[CONFIG_PATH] = settings;
const migrated = applyDeprecations(
_config,
deprecations.map((deprecation) => ({
deprecation,
path: CONFIG_PATH,
})),
() => ({ message }) => deprecationMessages.push(message)
);
return {
messages: deprecationMessages,
migrated,
};
};
const applyElasticsearchDeprecations = (settings: Record<string, any> = {}) =>
getDeprecationsFor({
provider: config.deprecations!,
settings,
path: CONFIG_PATH,
});

test('set correct defaults', () => {
const configValue = new ElasticsearchConfig(config.schema.validate({}));
Expand Down
26 changes: 7 additions & 19 deletions src/core/server/kibana_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,16 @@
*/

import { config } from './kibana_config';
import { applyDeprecations, configDeprecationFactory } from '@kbn/config';
import { getDeprecationsFor } from './config/test_utils';

const CONFIG_PATH = 'kibana';

const applyKibanaDeprecations = (settings: Record<string, any> = {}) => {
const deprecations = config.deprecations!(configDeprecationFactory);
const deprecationMessages: string[] = [];
const _config: any = {};
_config[CONFIG_PATH] = settings;
const migrated = applyDeprecations(
_config,
deprecations.map((deprecation) => ({
deprecation,
path: CONFIG_PATH,
})),
() => ({ message }) => deprecationMessages.push(message)
);
return {
messages: deprecationMessages,
migrated,
};
};
const applyKibanaDeprecations = (settings: Record<string, any> = {}) =>
getDeprecationsFor({
provider: config.deprecations!,
settings,
path: CONFIG_PATH,
});

it('set correct defaults ', () => {
const configValue = config.schema.validate({});
Expand Down
44 changes: 44 additions & 0 deletions src/core/server/saved_objects/saved_objects_config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.
*/

import { savedObjectsMigrationConfig } from './saved_objects_config';
import { getDeprecationsFor } from '../config/test_utils';

const applyMigrationsDeprecations = (settings: Record<string, any> = {}) =>
getDeprecationsFor({
provider: savedObjectsMigrationConfig.deprecations!,
settings,
path: 'migrations',
});

describe('migrations config', function () {
describe('deprecations', () => {
it('logs a warning if migrations.enableV2 is set: true', () => {
const { messages } = applyMigrationsDeprecations({ enableV2: true });
expect(messages).toMatchInlineSnapshot(`
Array [
"\\"migrations.enableV2\\" is deprecated and will be removed in an upcoming release without any further notice.",
]
`);
});

it('logs a warning if migrations.enableV2 is set: false', () => {
const { messages } = applyMigrationsDeprecations({ enableV2: false });
expect(messages).toMatchInlineSnapshot(`
Array [
"\\"migrations.enableV2\\" is deprecated and will be removed in an upcoming release without any further notice.",
]
`);
});
});

it('does not log a warning if migrations.enableV2 is not set', () => {
const { messages } = applyMigrationsDeprecations({ batchSize: 1_000 });
expect(messages).toMatchInlineSnapshot(`Array []`);
});
});
55 changes: 37 additions & 18 deletions src/core/server/saved_objects/saved_objects_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,50 @@
*/

import { schema, TypeOf } from '@kbn/config-schema';
import type { ServiceConfigDescriptor } from '../internal_types';
import type { ConfigDeprecationProvider } from '../config';

export type SavedObjectsMigrationConfigType = TypeOf<typeof savedObjectsMigrationConfig.schema>;
const migrationSchema = schema.object({
batchSize: schema.number({ defaultValue: 1_000 }),
scrollDuration: schema.string({ defaultValue: '15m' }),
pollInterval: schema.number({ defaultValue: 1_500 }),
skip: schema.boolean({ defaultValue: false }),
enableV2: schema.boolean({ defaultValue: true }),
retryAttempts: schema.number({ defaultValue: 15 }),
});

export const savedObjectsMigrationConfig = {
export type SavedObjectsMigrationConfigType = TypeOf<typeof migrationSchema>;

const migrationDeprecations: ConfigDeprecationProvider = () => [
(settings, fromPath, addDeprecation) => {
const migrationsConfig = settings[fromPath];
if (migrationsConfig?.enableV2 !== undefined) {
addDeprecation({
message:
'"migrations.enableV2" is deprecated and will be removed in an upcoming release without any further notice.',
documentationUrl: 'https://ela.st/kbn-so-migration-v2',
});
}
return settings;
},
];

export const savedObjectsMigrationConfig: ServiceConfigDescriptor<SavedObjectsMigrationConfigType> = {
path: 'migrations',
schema: schema.object({
batchSize: schema.number({ defaultValue: 1000 }),
scrollDuration: schema.string({ defaultValue: '15m' }),
pollInterval: schema.number({ defaultValue: 1500 }),
skip: schema.boolean({ defaultValue: false }),
// TODO migrationsV2: remove/deprecate once we release migrations v2
enableV2: schema.boolean({ defaultValue: true }),
/** the number of times v2 migrations will retry temporary failures such as a timeout, 503 status code or snapshot_in_progress_exception */
retryAttempts: schema.number({ defaultValue: 15 }),
}),
schema: migrationSchema,
deprecations: migrationDeprecations,
};

export type SavedObjectsConfigType = TypeOf<typeof savedObjectsConfig.schema>;
const soSchema = schema.object({
maxImportPayloadBytes: schema.byteSize({ defaultValue: 26_214_400 }),
maxImportExportSize: schema.number({ defaultValue: 10_000 }),
});

export type SavedObjectsConfigType = TypeOf<typeof soSchema>;

export const savedObjectsConfig = {
export const savedObjectsConfig: ServiceConfigDescriptor<SavedObjectsConfigType> = {
path: 'savedObjects',
schema: schema.object({
maxImportPayloadBytes: schema.byteSize({ defaultValue: 26_214_400 }),
maxImportExportSize: schema.number({ defaultValue: 10_000 }),
}),
schema: soSchema,
};

export class SavedObjectConfig {
Expand Down
1 change: 1 addition & 0 deletions src/core/server/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
export { createHttpServer } from './http/test_utils';
export { ServiceStatusLevelSnapshotSerializer } from './status/test_utils';
export { setupServer } from './saved_objects/routes/test_utils';
export { getDeprecationsFor, getDeprecationsForGlobalSettings } from './config/test_utils';

0 comments on commit 56335ed

Please sign in to comment.