Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Feb 17, 2022
1 parent f40c881 commit 7b75a22
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
15 changes: 6 additions & 9 deletions x-pack/plugins/fleet/server/services/managed_package_policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import type { ElasticsearchClient, SavedObjectsClientContract } from 'src/core/server';
import semverGte from 'semver/functions/gte';
import semverLt from 'semver/functions/lt';

import type { UpgradePackagePolicyDryRunResponseItem } from '../../common';

Expand Down Expand Up @@ -47,14 +47,12 @@ export const upgradeManagedPackagePolicies = async (
);

for (const packagePolicy of packagePolicies) {
if (isPolicyVersionGteInstalledVersion(packagePolicy, installedPackage)) {
continue;
if (isPolicyVersionLtInstalledVersion(packagePolicy, installedPackage)) {
await upgradePackagePolicy(soClient, esClient, packagePolicy.id, results);
}
await upgradePackagePolicy(soClient, esClient, packagePolicy.id, results);
}
}
}

return results;
};

Expand All @@ -72,14 +70,13 @@ async function getPackagePoliciesNotMatchingVersion(
).items;
}

function isPolicyVersionGteInstalledVersion(
function isPolicyVersionLtInstalledVersion(
packagePolicy: PackagePolicy,
installedPackage: Installation
): boolean {
return (
!!packagePolicy.package &&
!!installedPackage.version &&
semverGte(packagePolicy.package.version, installedPackage.version)
packagePolicy.package !== undefined &&
semverLt(packagePolicy.package.version, installedPackage.version)
);
}

Expand Down
10 changes: 0 additions & 10 deletions x-pack/plugins/fleet/server/services/preconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import type { InputsOverride } from './package_policy';
import { preconfigurePackageInputs } from './package_policy';
import { appContextService } from './app_context';
import type { UpgradeManagedPackagePoliciesResult } from './managed_package_policies';
import { upgradeManagedPackagePolicies } from './managed_package_policies';
import { outputService } from './output';

interface PreconfigurationResult {
Expand Down Expand Up @@ -377,15 +376,6 @@ export async function ensurePreconfiguredPackagesAndPolicies(
};
}

export async function ensureManagedPackagePoliciesUpgraded(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient
): Promise<UpgradeManagedPackagePoliciesResult[]> {
// Handle automatic package policy upgrades for managed packages and package with
// the `keep_policies_up_to_date` setting enabled
return upgradeManagedPackagePolicies(soClient, esClient);
}

export function comparePreconfiguredPolicyToCurrent(
policyFromConfig: PreconfiguredAgentPolicy,
currentPolicy: AgentPolicy
Expand Down
11 changes: 5 additions & 6 deletions x-pack/plugins/fleet/server/services/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { appContextService } from './app_context';
import { agentPolicyService } from './agent_policy';
import {
cleanPreconfiguredOutputs,
ensureManagedPackagePoliciesUpgraded,
ensurePreconfiguredOutputs,
ensurePreconfiguredPackagesAndPolicies,
} from './preconfiguration';
Expand All @@ -34,6 +33,7 @@ import { getInstallations, installPackage } from './epm/packages';
import { isPackageInstalled } from './epm/packages/install';
import { pkgToPkgKey } from './epm/registry';
import type { UpgradeManagedPackagePoliciesResult } from './managed_package_policies';
import { upgradeManagedPackagePolicies } from './managed_package_policies';

export interface SetupStatus {
isInitialized: boolean;
Expand Down Expand Up @@ -110,12 +110,11 @@ async function createSetupSideEffects(
);

logger.debug('Ensure managed package policies are upgraded');
const packagePolicyUpgradeResults = await ensureManagedPackagePoliciesUpgraded(
soClient,
esClient
);
const packagePolicyUpgradeErrors = (
await upgradeManagedPackagePolicies(soClient, esClient)
).filter((result) => (result.errors ?? []).length > 0);

const nonFatalErrors = [...preconfiguredPackagesNonFatalErrors, ...packagePolicyUpgradeResults];
const nonFatalErrors = [...preconfiguredPackagesNonFatalErrors, ...packagePolicyUpgradeErrors];

logger.debug('Cleaning up Fleet outputs');
await cleanPreconfiguredOutputs(soClient, outputsOrUndefined ?? []);
Expand Down

0 comments on commit 7b75a22

Please sign in to comment.