Skip to content

Commit

Permalink
[Fleet] Allow to specify an upgrade window for rolling upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed May 10, 2022
1 parent 21092e2 commit 6b88b3a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/fleet/common/types/models/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export interface NewAgentAction {
agents: string[];
created_at?: string;
id?: string;
expiration?: string;
start_time?: string;
minimum_execution_duration?: number;
}

export interface AgentAction extends NewAgentAction {
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/fleet/server/routes/agent/upgrade_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const postBulkAgentsUpgradeHandler: RequestHandler<
const coreContext = await context.core;
const soClient = coreContext.savedObjects.client;
const esClient = coreContext.elasticsearch.client.asInternalUser;
const { version, source_uri: sourceUri, agents, force } = request.body;
const { version, source_uri: sourceUri, agents, force, upgradeDurationSeconds } = request.body;
const kibanaVersion = appContextService.getKibanaVersion();
try {
checkVersionIsSame(version, kibanaVersion);
Expand All @@ -102,6 +102,7 @@ export const postBulkAgentsUpgradeHandler: RequestHandler<
sourceUri,
version,
force,
upgradeDurationSeconds,
};
const results = await AgentService.sendUpgradeAgentsActions(soClient, esClient, upgradeOptions);
const body = results.items.reduce<PostBulkAgentUpgradeResponse>((acc, so) => {
Expand Down
16 changes: 10 additions & 6 deletions x-pack/plugins/fleet/server/services/agents/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ export async function createAgentAction(
const timestamp = new Date().toISOString();
const body: FleetServerAgentAction = {
'@timestamp': timestamp,
expiration: new Date(Date.now() + ONE_MONTH_IN_MS).toISOString(),
expiration: newAgentAction.expiration ?? new Date(Date.now() + ONE_MONTH_IN_MS).toISOString(),
agents: newAgentAction.agents,
action_id: id,
data: newAgentAction.data,
type: newAgentAction.type,
start_time: newAgentAction.start_time,
minimum_execution_duration: newAgentAction.minimum_execution_duration,
};

await esClient.create({
Expand All @@ -49,26 +51,28 @@ export async function createAgentAction(

export async function bulkCreateAgentActions(
esClient: ElasticsearchClient,
newAgentActions: Array<Omit<AgentAction, 'id'>>
newAgentActions: NewAgentAction[]
): Promise<AgentAction[]> {
const actions = newAgentActions.map((newAgentAction) => {
const id = uuid.v4();
const id = newAgentAction.id ?? uuid.v4();
return {
id,
...newAgentAction,
};
} as AgentAction;
});

if (actions.length === 0) {
return actions;
return [];
}

await esClient.bulk({
index: AGENT_ACTIONS_INDEX,
body: actions.flatMap((action) => {
const body: FleetServerAgentAction = {
'@timestamp': new Date().toISOString(),
expiration: new Date(Date.now() + ONE_MONTH_IN_MS).toISOString(),
expiration: action.expiration ?? new Date(Date.now() + ONE_MONTH_IN_MS).toISOString(),
start_time: action.start_time,
minimum_execution_duration: action.minimum_execution_duration,
agents: action.agents,
action_id: action.id,
data: action.data,
Expand Down
13 changes: 13 additions & 0 deletions x-pack/plugins/fleet/server/services/agents/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type { ElasticsearchClient, SavedObjectsClientContract } from '@kbn/core/server';
import moment from 'moment';

import type { Agent, BulkActionResult } from '../../types';
import { agentPolicyService } from '..';
Expand All @@ -28,6 +29,8 @@ import {
} from './crud';
import { searchHitToAgent } from './helpers';

const MINIMUM_EXECUTION_DURATION_SECONDS = 600; // 10m

function isMgetDoc(doc?: estypes.MgetResponseItem<unknown>): doc is estypes.GetGetResult {
return Boolean(doc && 'found' in doc);
}
Expand Down Expand Up @@ -78,6 +81,7 @@ export async function sendUpgradeAgentsActions(
version: string;
sourceUri?: string | undefined;
force?: boolean;
upgradeDurationSeconds?: number;
}
) {
// Full set of agents
Expand Down Expand Up @@ -158,12 +162,21 @@ export async function sendUpgradeAgentsActions(
source_uri: options.sourceUri,
};

const rollingUpgradeOptions = options?.upgradeDurationSeconds
? {
start_time: now,
minimum_execution_duration: MINIMUM_EXECUTION_DURATION_SECONDS,
expiration: moment().add(options?.upgradeDurationSeconds, 'seconds').toISOString(),
}
: {};

await createAgentAction(esClient, {
created_at: now,
data,
ack_data: data,
type: 'UPGRADE',
agents: agentsToUpdate.map((agent) => agent.id),
...rollingUpgradeOptions,
});

await bulkUpdateAgents(
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/server/types/rest_spec/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const PostBulkAgentUpgradeRequestSchema = {
source_uri: schema.maybe(schema.string()),
version: schema.string(),
force: schema.maybe(schema.boolean()),
upgradeDurationSeconds: schema.maybe(schema.number({ min: 600 })),
}),
};

Expand Down

0 comments on commit 6b88b3a

Please sign in to comment.