function getScalingRuleSet()

in src/scaler/scaler-core/index.js [67:115]


function getScalingRuleSet(cluster) {
  const SCALING_PROFILES_FOLDER = './scaling-profiles/profiles/';
  const DEFAULT_PROFILE_NAME = 'CPU_AND_MEMORY';
  const CUSTOM_PROFILE_NAME = 'CUSTOM';

  /*
   * Sanitize the profile name before using
   * to prevent risk of directory traversal.
   */
  const profileName = sanitize(cluster.scalingProfile);
  let /** @type {RuleSet} **/ scalingRuleSet;
  if (profileName === CUSTOM_PROFILE_NAME && cluster.scalingRules) {
    scalingRuleSet = cluster.scalingRules.reduce((acc, current) => {
      logger.info({
        message: `	Custom scaling rule: ${current.name}`,
        projectId: cluster.projectId,
        regionId: cluster.regionId,
        clusterId: cluster.clusterId,
      });
      // @ts-ignore
      acc[current.name] = current;
      return acc;
    }, {});
  } else {
    try {
      scalingRuleSet = require(
        SCALING_PROFILES_FOLDER + profileName.toLowerCase(),
      ).ruleSet;
    } catch (err) {
      logger.warn({
        message: `Unknown scaling profile '${profileName}'`,
        projectId: cluster.projectId,
        regionId: cluster.regionId,
        clusterId: cluster.clusterId,
      });
      scalingRuleSet = require(
        SCALING_PROFILES_FOLDER + DEFAULT_PROFILE_NAME.toLowerCase(),
      ).ruleSet;
      cluster.scalingProfile = DEFAULT_PROFILE_NAME;
    }
  }
  logger.info({
    message: `Using scaling profile: ${cluster.scalingProfile}`,
    projectId: cluster.projectId,
    regionId: cluster.regionId,
    clusterId: cluster.clusterId,
  });
  return scalingRuleSet;
}