chore(slo): Simplify resources installation (#166987)

This commit is contained in:
Kevin Delemme 2023-09-26 15:15:46 -04:00 committed by GitHub
parent c9a98a7846
commit 2739c9dba5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 167 deletions

View file

@ -12,7 +12,6 @@ import {
SLO_COMPONENT_TEMPLATE_SETTINGS_NAME,
SLO_INDEX_TEMPLATE_NAME,
SLO_INGEST_PIPELINE_NAME,
SLO_RESOURCES_VERSION,
SLO_SUMMARY_COMPONENT_TEMPLATE_MAPPINGS_NAME,
SLO_SUMMARY_COMPONENT_TEMPLATE_SETTINGS_NAME,
SLO_SUMMARY_INDEX_TEMPLATE_NAME,
@ -21,95 +20,48 @@ import {
import { DefaultResourceInstaller } from './resource_installer';
describe('resourceInstaller', () => {
describe('when the common resources are not installed yet', () => {
it('installs the common resources', async () => {
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient();
mockClusterClient.indices.getIndexTemplate.mockResponseOnce({ index_templates: [] });
const installer = new DefaultResourceInstaller(mockClusterClient, loggerMock.create());
it('installs the common resources', async () => {
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient();
mockClusterClient.indices.getIndexTemplate.mockResponseOnce({ index_templates: [] });
const installer = new DefaultResourceInstaller(mockClusterClient, loggerMock.create());
await installer.ensureCommonResourcesInstalled();
await installer.ensureCommonResourcesInstalled();
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ name: SLO_COMPONENT_TEMPLATE_MAPPINGS_NAME })
);
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ name: SLO_COMPONENT_TEMPLATE_SETTINGS_NAME })
);
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenNthCalledWith(
3,
expect.objectContaining({ name: SLO_SUMMARY_COMPONENT_TEMPLATE_MAPPINGS_NAME })
);
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenNthCalledWith(
4,
expect.objectContaining({ name: SLO_SUMMARY_COMPONENT_TEMPLATE_SETTINGS_NAME })
);
expect(mockClusterClient.indices.putIndexTemplate).toHaveBeenCalledTimes(2);
expect(mockClusterClient.indices.putIndexTemplate).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ name: SLO_INDEX_TEMPLATE_NAME })
);
expect(mockClusterClient.indices.putIndexTemplate).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ name: SLO_SUMMARY_INDEX_TEMPLATE_NAME })
);
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(4);
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ name: SLO_COMPONENT_TEMPLATE_MAPPINGS_NAME })
);
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ name: SLO_COMPONENT_TEMPLATE_SETTINGS_NAME })
);
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenNthCalledWith(
3,
expect.objectContaining({ name: SLO_SUMMARY_COMPONENT_TEMPLATE_MAPPINGS_NAME })
);
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenNthCalledWith(
4,
expect.objectContaining({ name: SLO_SUMMARY_COMPONENT_TEMPLATE_SETTINGS_NAME })
);
expect(mockClusterClient.indices.putIndexTemplate).toHaveBeenCalledTimes(2);
expect(mockClusterClient.indices.putIndexTemplate).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ name: SLO_INDEX_TEMPLATE_NAME })
);
expect(mockClusterClient.indices.putIndexTemplate).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ name: SLO_SUMMARY_INDEX_TEMPLATE_NAME })
);
expect(mockClusterClient.ingest.putPipeline).toHaveBeenCalledTimes(2);
expect(mockClusterClient.ingest.putPipeline).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ id: SLO_INGEST_PIPELINE_NAME })
);
expect(mockClusterClient.ingest.putPipeline).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ id: SLO_SUMMARY_INGEST_PIPELINE_NAME })
);
});
});
describe('when the common resources are already installed', () => {
it('skips the installation', async () => {
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient();
mockClusterClient.indices.getIndexTemplate.mockResponseOnce({
index_templates: [
{
name: SLO_INDEX_TEMPLATE_NAME,
index_template: {
index_patterns: [],
composed_of: [],
_meta: { version: SLO_RESOURCES_VERSION },
},
},
],
});
mockClusterClient.indices.getIndexTemplate.mockResponseOnce({
index_templates: [
{
name: SLO_SUMMARY_INDEX_TEMPLATE_NAME,
index_template: {
index_patterns: [],
composed_of: [],
_meta: { version: SLO_RESOURCES_VERSION },
},
},
],
});
mockClusterClient.ingest.getPipeline.mockResponseOnce({
// @ts-ignore _meta not typed properly
[SLO_INGEST_PIPELINE_NAME]: { _meta: { version: SLO_RESOURCES_VERSION } },
});
mockClusterClient.ingest.getPipeline.mockResponseOnce({
// @ts-ignore _meta not typed properly
[SLO_SUMMARY_INGEST_PIPELINE_NAME]: { _meta: { version: SLO_RESOURCES_VERSION } },
});
const installer = new DefaultResourceInstaller(mockClusterClient, loggerMock.create());
await installer.ensureCommonResourcesInstalled();
expect(mockClusterClient.cluster.putComponentTemplate).not.toHaveBeenCalled();
expect(mockClusterClient.indices.putIndexTemplate).not.toHaveBeenCalled();
expect(mockClusterClient.ingest.putPipeline).not.toHaveBeenCalled();
});
expect(mockClusterClient.ingest.putPipeline).toHaveBeenCalledTimes(2);
expect(mockClusterClient.ingest.putPipeline).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ id: SLO_INGEST_PIPELINE_NAME })
);
expect(mockClusterClient.ingest.putPipeline).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ id: SLO_SUMMARY_INGEST_PIPELINE_NAME })
);
});
});

View file

@ -23,7 +23,6 @@ import {
SLO_INDEX_TEMPLATE_PATTERN,
SLO_INGEST_PIPELINE_INDEX_NAME_PREFIX,
SLO_INGEST_PIPELINE_NAME,
SLO_RESOURCES_VERSION,
SLO_SUMMARY_COMPONENT_TEMPLATE_MAPPINGS_NAME,
SLO_SUMMARY_COMPONENT_TEMPLATE_SETTINGS_NAME,
SLO_SUMMARY_DESTINATION_INDEX_NAME,
@ -46,13 +45,6 @@ export class DefaultResourceInstaller implements ResourceInstaller {
constructor(private esClient: ElasticsearchClient, private logger: Logger) {}
public async ensureCommonResourcesInstalled(): Promise<void> {
const alreadyInstalled = await this.areResourcesAlreadyInstalled();
if (alreadyInstalled) {
this.logger.info('SLO resources already installed - skipping');
return;
}
try {
this.logger.info('Installing SLO shared resources');
await Promise.all([
@ -105,77 +97,6 @@ export class DefaultResourceInstaller implements ResourceInstaller {
}
}
private async areResourcesAlreadyInstalled(): Promise<boolean> {
let indexTemplateExists = false;
try {
const { index_templates: indexTemplates } = await this.execute(() =>
this.esClient.indices.getIndexTemplate({
name: SLO_INDEX_TEMPLATE_NAME,
})
);
const sloIndexTemplate = indexTemplates.find(
(template) => template.name === SLO_INDEX_TEMPLATE_NAME
);
indexTemplateExists =
!!sloIndexTemplate &&
sloIndexTemplate.index_template._meta?.version === SLO_RESOURCES_VERSION;
} catch (err) {
return false;
}
let summaryIndexTemplateExists = false;
try {
const { index_templates: indexTemplates } = await this.execute(() =>
this.esClient.indices.getIndexTemplate({
name: SLO_SUMMARY_INDEX_TEMPLATE_NAME,
})
);
const sloSummaryIndexTemplate = indexTemplates.find(
(template) => template.name === SLO_SUMMARY_INDEX_TEMPLATE_NAME
);
summaryIndexTemplateExists =
!!sloSummaryIndexTemplate &&
sloSummaryIndexTemplate.index_template._meta?.version === SLO_RESOURCES_VERSION;
} catch (err) {
return false;
}
let ingestPipelineExists = false;
try {
const pipeline = await this.execute(() =>
this.esClient.ingest.getPipeline({ id: SLO_INGEST_PIPELINE_NAME })
);
ingestPipelineExists =
// @ts-ignore _meta is not defined on the type
pipeline && pipeline[SLO_INGEST_PIPELINE_NAME]._meta.version === SLO_RESOURCES_VERSION;
} catch (err) {
return false;
}
let summaryIngestPipelineExists = false;
try {
const pipeline = await this.execute(() =>
this.esClient.ingest.getPipeline({ id: SLO_SUMMARY_INGEST_PIPELINE_NAME })
);
summaryIngestPipelineExists =
pipeline &&
// @ts-ignore _meta is not defined on the type
pipeline[SLO_SUMMARY_INGEST_PIPELINE_NAME]._meta.version === SLO_RESOURCES_VERSION;
} catch (err) {
return false;
}
return (
indexTemplateExists &&
summaryIndexTemplateExists &&
ingestPipelineExists &&
summaryIngestPipelineExists
);
}
private async createOrUpdateComponentTemplate(template: ClusterPutComponentTemplateRequest) {
this.logger.info(`Installing SLO component template [${template.name}]`);
return this.execute(() => this.esClient.cluster.putComponentTemplate(template));