[Fleet] Disable ILM policy in serverless (#154460)

This commit is contained in:
Nicolas Chaulet 2023-04-10 12:38:45 -04:00 committed by GitHub
parent a0d754c595
commit 394174eedf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 156 additions and 18 deletions

View file

@ -1 +1,2 @@
xpack.fleet.enableExperimental: ['fleetServerStandalone']
xpack.fleet.internal.ILMPoliciesDisabled: true

View file

@ -44,6 +44,9 @@ export interface FleetConfigType {
disableRegistryVersionCheck?: boolean;
bundledPackageLocation?: string;
};
internal?: {
disableILMPolicies: boolean;
};
}
// Calling Object.entries(PackagesGroupedByStatus) gave `status: string`

View file

@ -158,6 +158,14 @@ export const config: PluginConfigDescriptor = {
}
},
}),
internal: schema.maybe(
schema.object({
disableILMPolicies: schema.boolean({
defaultValue: false,
}),
})
),
}),
};

View file

@ -61,12 +61,18 @@ export function buildDefaultSettings({
: defaultFields
).map((field) => field.name);
const isILMPolicyDisabled = appContextService.getConfig()?.internal?.disableILMPolicies ?? false;
return {
index: {
// ILM Policy must be added here, for now point to the default global ILM policy name
lifecycle: {
name: ilmPolicy ? ilmPolicy : type,
},
...(isILMPolicyDisabled
? {}
: {
// ILM Policy must be added here, for now point to the default global ILM policy name
lifecycle: {
name: ilmPolicy ? ilmPolicy : type,
},
}),
// What should be our default for the compression?
codec: 'best_compression',
// All the default fields which should be queried have to be added here.

View file

@ -8,11 +8,13 @@
import type { SavedObjectsClientContract, ElasticsearchClient } from '@kbn/core/server';
import { savedObjectsClientMock, elasticsearchServiceMock } from '@kbn/core/server/mocks';
import { loggerMock } from '@kbn/logging-mocks';
import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common/constants';
import { appContextService } from '../../app_context';
import { createAppContextStartContractMock } from '../../../mocks';
import { saveArchiveEntries } from '../archive/storage';
import { installILMPolicy } from '../elasticsearch/ilm/install';
import { installIlmForDataStream } from '../elasticsearch/datastream_ilm/install';
jest.mock('../elasticsearch/template/template');
jest.mock('../kibana/assets/install');
@ -20,6 +22,10 @@ jest.mock('../kibana/index_pattern/install');
jest.mock('./install');
jest.mock('./get');
jest.mock('../archive/storage');
jest.mock('../elasticsearch/ilm/install');
jest.mock('../elasticsearch/datastream_ilm/install');
import { updateCurrentWriteIndices } from '../elasticsearch/template/template';
import { installKibanaAssetsAndReferences } from '../kibana/assets/install';
@ -47,8 +53,22 @@ describe('_installPackage', () => {
beforeEach(async () => {
soClient = savedObjectsClientMock.create();
soClient.update.mockImplementation(async (type, id, attributes) => {
return { id, attributes } as any;
});
esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
appContextService.start(createAppContextStartContractMock());
jest.mocked(installILMPolicy).mockReset();
jest.mocked(installIlmForDataStream).mockReset();
jest.mocked(installIlmForDataStream).mockResolvedValue({
esReferences: [],
installedIlms: [],
});
jest.mocked(saveArchiveEntries).mockResolvedValue({
saved_objects: [],
});
});
afterEach(async () => {
appContextService.stop();
@ -96,4 +116,100 @@ describe('_installPackage', () => {
await expect(installationPromise).rejects.toThrow('mocked');
await expect(installationPromise).rejects.toThrow('should be caught');
});
it('do not install ILM policies if disabled in config', async () => {
appContextService.start(
createAppContextStartContractMock({
internal: {
disableILMPolicies: true,
},
})
);
// force errors from this function
mockedInstallKibanaAssetsAndReferences.mockResolvedValue([]);
// pick any function between when those are called and when await Promise.all is defined later
// and force it to take long enough for the errors to occur
// @ts-expect-error about call signature
mockedUpdateCurrentWriteIndices.mockImplementation(async () => await sleep(1000));
mockedInstallIndexTemplatesAndPipelines.mockResolvedValue({
installedTemplates: [],
esReferences: [],
});
await _installPackage({
savedObjectsClient: soClient,
// @ts-ignore
savedObjectsImporter: jest.fn(),
esClient,
logger: loggerMock.create(),
paths: [],
packageInfo: {
title: 'title',
name: 'xyz',
version: '4.5.6',
description: 'test',
type: 'integration',
categories: ['cloud', 'custom'],
format_version: 'string',
release: 'experimental',
conditions: { kibana: { version: 'x.y.z' } },
owner: { github: 'elastic/fleet' },
},
installType: 'install',
installSource: 'registry',
spaceId: DEFAULT_SPACE_ID,
});
expect(installILMPolicy).not.toBeCalled();
expect(installIlmForDataStream).not.toBeCalled();
// if we have a .catch this will fail nicely (test pass)
// otherwise the test will fail with either of the mocked errors
// await expect(installationPromise).rejects.toThrow('mocked');
// await expect(installationPromise).rejects.toThrow('should be caught');
});
it('install ILM policies if not disabled in config', async () => {
appContextService.start(
createAppContextStartContractMock({
internal: {
disableILMPolicies: false,
},
})
);
// force errors from this function
mockedInstallKibanaAssetsAndReferences.mockResolvedValue([]);
// pick any function between when those are called and when await Promise.all is defined later
// and force it to take long enough for the errors to occur
// @ts-expect-error about call signature
mockedUpdateCurrentWriteIndices.mockImplementation(async () => await sleep(1000));
mockedInstallIndexTemplatesAndPipelines.mockResolvedValue({
installedTemplates: [],
esReferences: [],
});
await _installPackage({
savedObjectsClient: soClient,
// @ts-ignore
savedObjectsImporter: jest.fn(),
esClient,
logger: loggerMock.create(),
paths: [],
packageInfo: {
title: 'title',
name: 'xyz',
version: '4.5.6',
description: 'test',
type: 'integration',
categories: ['cloud', 'custom'],
format_version: 'string',
release: 'experimental',
conditions: { kibana: { version: 'x.y.z' } },
owner: { github: 'elastic/fleet' },
},
installType: 'install',
installSource: 'registry',
spaceId: DEFAULT_SPACE_ID,
});
expect(installILMPolicy).toBeCalled();
expect(installIlmForDataStream).toBeCalled();
});
});

View file

@ -153,20 +153,24 @@ export async function _installPackage({
// currently only the base package has an ILM policy
// at some point ILM policies can be installed/modified
// per data stream and we should then save them
esReferences = await withPackageSpan('Install ILM policies', () =>
installILMPolicy(packageInfo, paths, esClient, savedObjectsClient, logger, esReferences)
);
const isILMPoliciesDisabled =
appContextService.getConfig()?.internal?.disableILMPolicies ?? false;
if (!isILMPoliciesDisabled) {
esReferences = await withPackageSpan('Install ILM policies', () =>
installILMPolicy(packageInfo, paths, esClient, savedObjectsClient, logger, esReferences)
);
({ esReferences } = await withPackageSpan('Install Data Stream ILM policies', () =>
installIlmForDataStream(
packageInfo,
paths,
esClient,
savedObjectsClient,
logger,
esReferences
)
));
({ esReferences } = await withPackageSpan('Install Data Stream ILM policies', () =>
installIlmForDataStream(
packageInfo,
paths,
esClient,
savedObjectsClient,
logger,
esReferences
)
));
}
// installs ml models
esReferences = await withPackageSpan('Install ML models', () =>