Skip to content

Commit

Permalink
Fix logic for serverless and for index templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ElenaStoeva committed Jan 20, 2025
1 parent 184edbf commit bfafc18
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export function serializeTemplate(templateDeserialized: TemplateDeserialized): T

export function deserializeTemplate(
templateEs: TemplateSerialized & { name: string },
cloudManagedTemplatePrefix?: string
cloudManagedTemplatePrefix?: string,
isLogsdbEnabled: boolean
): TemplateDeserialized {
const {
name,
Expand Down Expand Up @@ -92,7 +93,7 @@ export function deserializeTemplate(
const ilmPolicyName = settings?.index?.lifecycle?.name;

const indexMode = (settings?.index?.mode ??
(indexPatterns.some((pattern) => pattern === 'logs-*-*')
(isLogsdbEnabled && indexPatterns.some((pattern) => pattern === 'logs-*-*')
? LOGSDB_INDEX_MODE
: STANDARD_INDEX_MODE)) as IndexMode;

Expand Down Expand Up @@ -127,11 +128,16 @@ export function deserializeTemplate(

export function deserializeTemplateList(
indexTemplates: Array<{ name: string; index_template: TemplateSerialized }>,
cloudManagedTemplatePrefix?: string
cloudManagedTemplatePrefix?: string,
isLogsdbEnabled: boolean
): TemplateListItem[] {
return indexTemplates.map(({ name, index_template: templateSerialized }) => {
const { template: { mappings, settings, aliases } = {}, ...deserializedTemplate } =
deserializeTemplate({ name, ...templateSerialized }, cloudManagedTemplatePrefix);
deserializeTemplate(
{ name, ...templateSerialized },
cloudManagedTemplatePrefix,
isLogsdbEnabled
);

return {
...deserializedTemplate,
Expand Down Expand Up @@ -168,13 +174,15 @@ export function serializeLegacyTemplate(template: TemplateDeserialized): LegacyT

export function deserializeLegacyTemplate(
templateEs: LegacyTemplateSerialized & { name: string },
cloudManagedTemplatePrefix?: string
cloudManagedTemplatePrefix?: string,
isLogsdbEnabled: boolean
): TemplateDeserialized {
const { settings, aliases, mappings, ...rest } = templateEs;

const deserializedTemplate = deserializeTemplate(
{ ...rest, template: { aliases, settings, mappings } },
cloudManagedTemplatePrefix
cloudManagedTemplatePrefix,
isLogsdbEnabled
);

return {
Expand All @@ -189,11 +197,16 @@ export function deserializeLegacyTemplate(

export function deserializeLegacyTemplateList(
indexTemplatesByName: { [key: string]: LegacyTemplateSerialized },
cloudManagedTemplatePrefix?: string
cloudManagedTemplatePrefix?: string,
isLogsdbEnabled
): TemplateListItem[] {
return Object.entries(indexTemplatesByName).map(([name, templateSerialized]) => {
const { template: { mappings, settings, aliases } = {}, ...deserializedTemplate } =
deserializeLegacyTemplate({ name, ...templateSerialized }, cloudManagedTemplatePrefix);
deserializeLegacyTemplate(
{ name, ...templateSerialized },
cloudManagedTemplatePrefix,
isLogsdbEnabled
);

return {
...deserializedTemplate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
*/

import { ByteSizeValue } from '@kbn/config-schema';
import { LOGSDB_INDEX_MODE, STANDARD_INDEX_MODE } from '../../common/constants';
import { STANDARD_INDEX_MODE } from '../../common/constants';
import { IndexMode } from '../../common/types/data_streams';
import type { DataStream, EnhancedDataStreamFromEs, Health } from '../../common';

export function deserializeDataStream(
dataStreamFromEs: EnhancedDataStreamFromEs,
isLogsdbEnabled: boolean
): DataStream {
export function deserializeDataStream(dataStreamFromEs: EnhancedDataStreamFromEs): DataStream {
const {
name,
timestamp_field: timeStampField,
Expand Down Expand Up @@ -79,16 +76,12 @@ export function deserializeDataStream(
globalMaxRetention,
},
nextGenerationManagedBy,
indexMode: (indexMode ??
(isLogsdbEnabled && /^logs-[^-]+-[^-]+$/.test(name)
? LOGSDB_INDEX_MODE
: STANDARD_INDEX_MODE)) as IndexMode,
indexMode: (indexMode ?? STANDARD_INDEX_MODE) as IndexMode,
};
}

export function deserializeDataStreamList(
dataStreamsFromEs: EnhancedDataStreamFromEs[],
isLogsdbEnabled: boolean
dataStreamsFromEs: EnhancedDataStreamFromEs[]
): DataStream[] {
return dataStreamsFromEs.map((dataStream) => deserializeDataStream(dataStream, isLogsdbEnabled));
return dataStreamsFromEs.map((dataStream) => deserializeDataStream(dataStream));
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ export function registerGetAllRoute({ router, lib: { handleEsError }, config }:
const { index_templates: indexTemplates } =
await client.asCurrentUser.indices.getIndexTemplate();

const { persistent } = await client.asCurrentUser.cluster.getSettings();

// Only take the lifecycle of the first data stream since all data streams have the same global retention period
const lifecycle = await getDataStreamLifecycle(client, dataStreams[0].name);
// @ts-ignore - TS doesn't know about the `global_retention` property yet
Expand All @@ -193,12 +191,7 @@ export function registerGetAllRoute({ router, lib: { handleEsError }, config }:
indexTemplates,
});

return response.ok({
body: deserializeDataStreamList(
enhancedDataStreams,
persistent?.cluster?.logsdb?.enabled
),
});
return response.ok({ body: deserializeDataStreamList(enhancedDataStreams) });
} catch (error) {
return handleEsError({ error, response });
}
Expand Down Expand Up @@ -269,13 +262,7 @@ export function registerGetOneRoute({ router, lib: { handleEsError }, config }:
globalMaxRetention,
indexTemplates,
});

const { persistent } = await client.asCurrentUser.cluster.getSettings();

const body = deserializeDataStream(
enhancedDataStreams[0],
persistent?.cluster?.logsdb?.enabled
);
const body = deserializeDataStream(enhancedDataStreams[0]);
return response.ok({ body });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,18 @@ export function registerGetAllRoute({ router, config, lib: { handleEsError } }:
const { index_templates: templatesEs } =
await client.asCurrentUser.indices.getIndexTemplate();

const { persistent, defaults } = await client.asInternalUser.cluster.getSettings({
include_defaults: true,
});
const isLogsdbEnabled =
persistent?.cluster?.logsdb?.enabled ?? defaults?.cluster?.logsdb?.enabled;

// @ts-expect-error TemplateSerialized.index_patterns not compatible with IndicesIndexTemplate.index_patterns
const templates = deserializeTemplateList(templatesEs, cloudManagedTemplatePrefix);
const templates = deserializeTemplateList(
templatesEs,
cloudManagedTemplatePrefix,
isLogsdbEnabled
);

if (config.isLegacyTemplatesEnabled === false) {
// If isLegacyTemplatesEnabled=false, we do not want to fetch legacy templates and return an empty array;
Expand All @@ -50,7 +60,8 @@ export function registerGetAllRoute({ router, config, lib: { handleEsError } }:

const legacyTemplates = deserializeLegacyTemplateList(
legacyTemplatesEs,
cloudManagedTemplatePrefix
cloudManagedTemplatePrefix,
isLogsdbEnabled
);

const body = {
Expand Down Expand Up @@ -98,6 +109,12 @@ export function registerGetOneRoute({ router, config, lib: { handleEsError } }:
try {
const cloudManagedTemplatePrefix = await getCloudManagedTemplatePrefix(client);

const { persistent, defaults } = await client.asInternalUser.cluster.getSettings({
include_defaults: true,
});
const isLogsdbEnabled =
persistent?.cluster?.logsdb?.enabled ?? defaults?.cluster?.logsdb?.enabled;

if (isLegacy) {
const indexTemplateByName = await client.asCurrentUser.indices.getTemplate({
name,
Expand All @@ -107,7 +124,8 @@ export function registerGetOneRoute({ router, config, lib: { handleEsError } }:
return response.ok({
body: deserializeLegacyTemplate(
{ ...indexTemplateByName[name], name },
cloudManagedTemplatePrefix
cloudManagedTemplatePrefix,
isLogsdbEnabled
),
});
}
Expand All @@ -120,7 +138,8 @@ export function registerGetOneRoute({ router, config, lib: { handleEsError } }:
body: deserializeTemplate(
// @ts-expect-error TemplateSerialized.index_patterns not compatible with IndicesIndexTemplate.index_patterns
{ ...indexTemplates[0].index_template, name },
cloudManagedTemplatePrefix
cloudManagedTemplatePrefix,
isLogsdbEnabled
),
});
}
Expand Down

0 comments on commit bfafc18

Please sign in to comment.