[SO service] fix getAllIndices (#165224)

## Summary

Fix https://github.com/elastic/kibana/issues/165166

- remove `getAllIndices` from the SO service's setup contract
- adapt `getAllIndices` from the start contract to only return indices
that are effectively present in the current environment
This commit is contained in:
Pierre Gayvallet 2023-09-01 10:01:11 +02:00 committed by GitHub
parent 150a883f5c
commit eac09391a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 88 additions and 18 deletions

View file

@ -246,7 +246,6 @@ export function createPluginSetupContext<TPlugin, TPluginDependencies>(
setSpacesExtension: deps.savedObjects.setSpacesExtension,
registerType: deps.savedObjects.registerType,
getDefaultIndex: deps.savedObjects.getDefaultIndex,
getAllIndices: deps.savedObjects.getAllIndices,
},
status: {
core$: deps.status.core$,

View file

@ -57,12 +57,13 @@ import {
import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal';
import type { DeprecationRegistryProvider } from '@kbn/core-deprecations-server';
import type { NodeInfo } from '@kbn/core-node-server';
import { MAIN_SAVED_OBJECT_INDEX, ALL_SAVED_OBJECT_INDICES } from '@kbn/core-saved-objects-server';
import { MAIN_SAVED_OBJECT_INDEX } from '@kbn/core-saved-objects-server';
import { registerRoutes } from './routes';
import { calculateStatus$ } from './status';
import { registerCoreObjectTypes } from './object_types';
import { getSavedObjectsDeprecationsProvider } from './deprecations';
import { applyTypeDefaults } from './apply_type_defaults';
import { getAllIndices } from './utils';
/**
* @internal
@ -202,7 +203,6 @@ export class SavedObjectsService
},
getTypeRegistry: () => this.typeRegistry,
getDefaultIndex: () => MAIN_SAVED_OBJECT_INDEX,
getAllIndices: () => [...ALL_SAVED_OBJECT_INDICES],
};
}
@ -326,6 +326,8 @@ export class SavedObjectsService
clientProvider.setClientFactory(clientFactory);
}
const allIndices = getAllIndices({ registry: this.typeRegistry });
this.started = true;
return {
@ -361,7 +363,7 @@ export class SavedObjectsService
});
return [...indices];
},
getAllIndices: () => [...ALL_SAVED_OBJECT_INDICES],
getAllIndices: () => [...allIndices],
};
}

View file

@ -0,0 +1,48 @@
/*
* 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 SavedObjectsType, MAIN_SAVED_OBJECT_INDEX } from '@kbn/core-saved-objects-server';
import { SavedObjectTypeRegistry } from '@kbn/core-saved-objects-base-server-internal';
import { getAllIndices } from './get_all_indices';
describe('getAllIndices', () => {
const createType = (parts: Partial<SavedObjectsType>): SavedObjectsType => ({
name: 'test',
hidden: false,
namespaceType: 'single',
mappings: { properties: {} },
...parts,
});
const createRegistry = (...types: Array<Partial<SavedObjectsType>>): SavedObjectTypeRegistry => {
const registry = new SavedObjectTypeRegistry();
types.forEach((type) => {
registry.registerType(createType(type));
});
return registry;
};
it('returns the indices that are used by registered types', () => {
const registry = createRegistry(
{ name: 'type_1' },
{ name: 'type_2', indexPattern: '.kibana_ingest' }
);
expect(getAllIndices({ registry })).toEqual([MAIN_SAVED_OBJECT_INDEX, '.kibana_ingest']);
});
it('returns each index only once', () => {
const registry = createRegistry(
{ name: 'type_1' },
{ name: 'type_2', indexPattern: '.kibana_foo' },
{ name: 'type_3' },
{ name: 'type_4', indexPattern: '.kibana_foo' }
);
expect(getAllIndices({ registry })).toEqual([MAIN_SAVED_OBJECT_INDEX, '.kibana_foo']);
});
});

View file

@ -0,0 +1,16 @@
/*
* 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 { MAIN_SAVED_OBJECT_INDEX } from '@kbn/core-saved-objects-server';
import type { SavedObjectTypeRegistry } from '@kbn/core-saved-objects-base-server-internal';
export const getAllIndices = ({ registry }: { registry: SavedObjectTypeRegistry }): string[] => {
return [
...new Set(registry.getAllTypes().map((type) => type.indexPattern ?? MAIN_SAVED_OBJECT_INDEX)),
];
};

View file

@ -0,0 +1,9 @@
/*
* 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.
*/
export { getAllIndices } from './get_all_indices';

View file

@ -77,11 +77,9 @@ const createSetupContractMock = () => {
setSpacesExtension: jest.fn(),
registerType: jest.fn(),
getDefaultIndex: jest.fn(),
getAllIndices: jest.fn(),
};
setupContract.getDefaultIndex.mockReturnValue(MAIN_SAVED_OBJECT_INDEX);
setupContract.getAllIndices.mockReturnValue([MAIN_SAVED_OBJECT_INDEX]);
return setupContract;
};

View file

@ -137,13 +137,6 @@ export interface SavedObjectsServiceSetup {
* Returns the default index used for saved objects.
*/
getDefaultIndex: () => string;
/**
* Returns all (aliases to) kibana system indices used for saved object storage.
*
* @deprecated use the `start` contract counterpart.
*/
getAllIndices: () => string[];
}
/**
@ -235,6 +228,8 @@ export interface SavedObjectsServiceStart {
getDefaultIndex: () => string;
/**
* Returns all (aliases to) kibana system indices used for saved object storage.
*
* @remarks Only the indices effectively present in the current running environment will be returned.
*/
getAllIndices: () => string[];
}

View file

@ -54,11 +54,14 @@ export class SavedObjectTaggingPlugin
features.registerKibanaFeature(savedObjectsTaggingFeature);
if (usageCollection) {
const kibanaIndices = savedObjects.getAllIndices();
const getKibanaIndices = () =>
getStartServices()
.then(([core]) => core.savedObjects.getAllIndices())
.catch(() => []);
usageCollection.registerCollector(
createTagUsageCollector({
usageCollection,
kibanaIndices,
getKibanaIndices,
})
);
}

View file

@ -12,17 +12,17 @@ import { tagUsageCollectorSchema } from './schema';
export const createTagUsageCollector = ({
usageCollection,
kibanaIndices,
getKibanaIndices,
}: {
usageCollection: UsageCollectionSetup;
kibanaIndices: string[];
getKibanaIndices: () => Promise<string[]>;
}) => {
return usageCollection.makeUsageCollector<TaggingUsageData>({
type: 'saved_objects_tagging',
isReady: () => true,
schema: tagUsageCollectorSchema,
fetch: async ({ esClient }) => {
return fetchTagUsageData({ esClient, kibanaIndices });
return fetchTagUsageData({ esClient, kibanaIndices: await getKibanaIndices() });
},
});
};