Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] optimizing package policy upgrade and dry run #126088

Merged
merged 12 commits into from
Feb 23, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jest.mock(
} => {
return {
packagePolicyService: {
_compilePackagePolicyInputs: jest.fn((registryPkgInfo, packageInfo, vars, dataInputs) =>
_compilePackagePolicyInputs: jest.fn((packageInfo, vars, dataInputs) =>
Promise.resolve(dataInputs)
),
buildPackagePolicyFromPackage: jest.fn(),
Expand Down
22 changes: 14 additions & 8 deletions x-pack/plugins/fleet/server/services/managed_package_policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const upgradeManagedPackagePolicies = async (

for (const packagePolicy of packagePolicies) {
if (isPolicyVersionLtInstalledVersion(packagePolicy, installedPackage)) {
await upgradePackagePolicy(soClient, esClient, packagePolicy.id, results);
await upgradePackagePolicy(soClient, esClient, packagePolicy, installedPackage, results);
}
}
}
Expand Down Expand Up @@ -84,13 +84,19 @@ function isPolicyVersionLtInstalledVersion(
async function upgradePackagePolicy(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
packagePolicyId: string,
packagePolicy: PackagePolicy,
installedPackage: Installation,
results: UpgradeManagedPackagePoliciesResult[]
) {
// Since upgrades don't report diffs/errors, we need to perform a dry run first in order
// to notify the user of any granular policy upgrade errors that occur during Fleet's
// preconfiguration check
const dryRunResults = await packagePolicyService.getUpgradeDryRunDiff(soClient, packagePolicyId);
const dryRunResults = await packagePolicyService.getUpgradeDryRunDiff(
soClient,
packagePolicy.id,
packagePolicy,
installedPackage.version
);

if (dryRunResults.hasErrors) {
const errors = dryRunResults.diff
Expand All @@ -100,17 +106,17 @@ async function upgradePackagePolicy(
appContextService
.getLogger()
.error(
new Error(`Error upgrading package policy ${packagePolicyId}: ${JSON.stringify(errors)}`)
new Error(`Error upgrading package policy ${packagePolicy.id}: ${JSON.stringify(errors)}`)
);

results.push({ packagePolicyId, diff: dryRunResults.diff, errors });
results.push({ packagePolicyId: packagePolicy.id, diff: dryRunResults.diff, errors });
return;
}

try {
await packagePolicyService.upgrade(soClient, esClient, [packagePolicyId]);
results.push({ packagePolicyId, diff: dryRunResults.diff, errors: [] });
await packagePolicyService.upgrade(soClient, esClient, [packagePolicy.id]);
results.push({ packagePolicyId: packagePolicy.id, diff: dryRunResults.diff, errors: [] });
} catch (error) {
results.push({ packagePolicyId, diff: dryRunResults.diff, errors: [error] });
results.push({ packagePolicyId: packagePolicy.id, diff: dryRunResults.diff, errors: [error] });
}
}
Loading