# Optimization Policy customization

PerfectScale provides flexible customization options for configuring optimization policies to achieve greater control over optimization. With this feature, you can seamlessly align your optimization policy with your optimization goals to achieve the desired results efficiently.

{% hint style="warning" %}
To prevent the risk of CPU throttling, the resource `Request = Limit` setup is not available for the CPU resource.
{% endhint %}

You can seamlessly apply the optimization policy [via UI](#setting-optimization-policy-via-ui) or directly through Kubernetes manifests [with CRD](#setting-optimization-policy-with-crd).&#x20;

{% hint style="info" %}
While the UI allows you to configure an optimization policy at the **cluster** and **workload** levels, using a CRD also enables you to set an optimization policy for a **specific namespace**.
{% endhint %}

When an Optimization Policy is managed with a **Custom Resource Definition (CRD)**, the UI reflects the CRD as the source of truth, and the relevant `CRD` badge will appear:

<figure><img src="https://1573387604-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FABMqnYtsOO44JmQTVSnn%2Fuploads%2FWOndb1qbr1JvMV6MQjS6%2FFrame%201707478487.png?alt=media&#x26;token=75137d8e-a222-4be6-a638-f23e64a8f7ee" alt="" width="375"><figcaption><p>Optimization policy configured with a CRD</p></figcaption></figure>

**Cluster-level CRD**

* If a cluster-level optimization policy CRD is applied, the UI will not allow changes to the cluster optimization policy.

**Workload-level CRD**

* If a workload-level optimization policy CRD is applied, the UI will not allow saving changes to that workload’s optimization policy.
* Users can preview recommendations from other policies even when a CRD is applied.

Different approaches for setting an optimization policy may take precedence over others. **Review the priority order** to ensure your configuration leads to the desired outcome:

**Workload-level (Highest Priority)**

1. Workload CRD -> `Custom CPU/Custom Memory Policy`
2. Workload CRD -> `Global`
3. Workload UI -> `Custom CPU/Custom Memory Policy`
4. Workload UI -> `Global`

**Namespace-level4$**

5. Namespace CRD -> `Custom CPU/Custom Memory Policy`
6. Namespace CRD -> `Global`

**Cluster-level**

7. Cluster CRD -> `Custom CPU/Custom Memory Policy`
8. Cluster CRD -> `Global`
9. Cluster UI -> `Custom CPU/Custom Memory Policy`
10. Cluster UI-> `Global`

## Independent optimization policies for CPU and Memory

PerfectScale enables independent optimization policies for **CPU** and **Memory**, ensuring recommendations and automation are precisely aligned with your optimization goals while meeting reliability requirements.

## Memory request = memory limit policy

For memory-sensitive workloads, PerfectScale allows you to apply a **Memory Request = Memory Limit** policy on top of your optimization policy. This helps reduce the risk of OOM events and prevents noisy-neighbor effects by ensuring guaranteed memory allocation for your critical services.

{% hint style="info" %}
In some cases, higher-priority constraints may affect recommendations and take precedence over the **Memory Request = Limit** setting, including:

* ResourceQuota
* LimitRange
* Automation CRD restrictions
* Avoid setting limits for young workloads
  {% endhint %}

{% hint style="danger" %}
A conflict may occur between the **Request = Limit** configuration and the Automation CRD.&#x20;

For example, if **Request = Limit** is enabled while the CRD automation configuration sets `memoryManagement.request.increaseEnabled=false`, recommendations will be restricted accordingly.
{% endhint %}

## Custom optimization policy time window

PerfectScale uses the default data maturity time window for the recommendations, depending on the selected Optimization Policy. This algorithm allows PerfectScale to collect sufficient data to ensure reliable recommendations and accurately adjust resources through automated actions.

However, there may be situations when the default setup is not efficient enough due to known and predictable resource spikes within a known timeframe. PerfectScale allows you to set a **Custom time window** for the optimization policy to tailor recommendations and automation to your services' specific needs.

You can adjust the time window for the optimization policy for both the entire cluster and a specific workload.&#x20;

## Setting optimization policy via UI

#### Setting independent policies for the cluster via UI

You can seamlessly set the independent CPU and Memory optimization policies for the already running cluster.

From the Overview screen, navigate to the **Cluster Settings** by clicking the **gear icon** next to the cluster. Open the Optimization Policy dropdown, and select **`Custom CPU & Mem Policies`**. Then select the desired policies for both CPU and Memory, and click the `Save changes` button.

<figure><img src="https://1573387604-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FABMqnYtsOO44JmQTVSnn%2Fuploads%2FlBb3QfuITLzxr3O4htf0%2FIndependent_cluster.gif?alt=media&#x26;token=406b7c80-dc1f-428d-ac9d-2b925ed86e88" alt=""><figcaption><p>Setting independent policies for the cluster</p></figcaption></figure>

#### Setting independent policies for the workload via UI

Click the specific workload to open the Zoom-in window. In the top-right corner, open the Optimization Policy dropdown, select **`Custom CPU & Mem Policies`**, elect the desired policies for both CPU and Memory, and click the `Save` button.

<figure><img src="https://1573387604-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FABMqnYtsOO44JmQTVSnn%2Fuploads%2FrU9kgWiLR1ZDuuvsaNO0%2FPodfit%20-_%20Zoom-in%20Modal%20(1).png?alt=media&#x26;token=b25690c7-74be-4f5d-88d6-a6d2e6945367" alt=""><figcaption><p>Setting independent policies for the workload</p></figcaption></figure>

#### Setting memoryRequestEqualsLimit for the cluster via UI

You can seamlessly set the memoryRequestEqualsLimit policy for the already running cluster.

From the Overview screen, navigate to the **Cluster Settings** by clicking the **gear icon** next to the cluster. Open the Optimization Policy dropdown, and select **`Memory Request = Limit`**. Then click the `Save changes` button.

<figure><img src="https://1573387604-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FABMqnYtsOO44JmQTVSnn%2Fuploads%2FwMbNe8brIVFSppTZ2FGS%2FReqLimPolicy.gif?alt=media&#x26;token=efe6d07c-a9ac-45cd-8e85-ee0955d5e578" alt=""><figcaption><p>Setting Memory Request = Limit policy for a cluster</p></figcaption></figure>

#### Setting memoryRequestEqualsLimit for the workload via UI

Click the specific workload to open the Zoom-in window. In the top-right corner, open the Optimization Policy dropdown, select **`Memory Request = Limit`**, and click the `Save` button.

<figure><img src="https://1573387604-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FABMqnYtsOO44JmQTVSnn%2Fuploads%2FICM7NkM4H1eXNvqNtSWU%2FPodfit%20-_%20Zoom-in%20Modal.png?alt=media&#x26;token=179508c8-7b4b-4d83-9429-2aed3328df7f" alt=""><figcaption><p>Setting Memory Request = Limit policy for a workload</p></figcaption></figure>

{% hint style="info" %}
Once **`Memory Request = Limit`** is enabled, PerfectScale automatically applies i[ndependent optimization policies](#independent-optimization-policies-for-cpu-and-memory) for CPU and memory for this workload.
{% endhint %}

#### Setting a custom time window for the cluster via UI

You can easily specify the custom time window for the optimization policy when onboarding the cluster, or modify it for an already running cluster.

When onboarding a new cluster, enable the toggle `Custom time window` in the Optimization Policy drop-down and select the policy and the desired time window.&#x20;

{% hint style="info" %}
If the exporter was installed with a specified custom policy, the subsequent upgrades should also include it. PerfectScale recommends using the upgrade command in the UI, as it keeps your fields from the installation. Find out more about upgrading the agent [here](https://docs.perfectscale.io/administration/cluster-settings#upgrade-available).
{% endhint %}

<figure><img src="https://1573387604-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FABMqnYtsOO44JmQTVSnn%2Fuploads%2Frnq2rwIfIFrHPrTqLCe5%2Fimage.png?alt=media&#x26;token=208d89dc-401c-4b9e-8a7c-97cf14bc913e" alt="" width="563"><figcaption><p>Custom time window</p></figcaption></figure>

If you want to modify the custom time window for the already onboarded cluster, navigate to the **Cluster Settings** by clicking the **gear icon** next to the cluster. Open the Optimization Policy dropdown, and select the desired policy and custom time window.&#x20;

{% hint style="info" %}
If a custom policy is set through the exporter when installing the PerfectScale Agent, it cannot be modified in the UI afterward. You can still change the custom policy by upgrading the exporter with the new value, or you can return it to the default by upgrading the exporter without specifying any value (this will also enable the option to change the custom time window through the UI).
{% endhint %}

<figure><img src="https://1573387604-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FABMqnYtsOO44JmQTVSnn%2Fuploads%2FnWMGXinAjqTVm71wXjsV%2Fcustom%20time%20window.gif?alt=media&#x26;token=1d6ca511-3a96-444c-9914-99263f607907" alt=""><figcaption><p>Setting custom time window for the cluster</p></figcaption></figure>

Once the custom time window is applied, it will be displayed next to the optimization policy in UI.

#### Setting a custom time window for the workload via UI

To set the custom time window for the optimization policy of the particular workload, navigate to the Zoom-in window by clicking the workload, select the desired optimization policy and custom time window, and click `Save` button.

<figure><img src="https://1573387604-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FABMqnYtsOO44JmQTVSnn%2Fuploads%2FXsr2EL2IQ4kpSuuHgih0%2Fimage.png?alt=media&#x26;token=6a3c09e0-163e-4d4f-a160-3bb5b7b0f208" alt=""><figcaption><p>Setting custom time window for the workload</p></figcaption></figure>

## Setting optimization policy with CRD

Custom Resource Definitions (CRDs) provide a cloud-native way to define and manage optimization policies directly through Kubernetes manifests.

This advanced approach is specifically helpful if you want to manage policies as code (versioning, reviews, GitOps workflows) and apply consistent configurations across clusters and environments with full control.

{% hint style="info" %}
CRDs support optimization policies at the cluster, namespace, and workload levels.
{% endhint %}

#### Setting optimization policy for the cluster with CRD

To apply a cluster-wide optimization policy, create a cluster-level custom resource. Here is an example:

```yaml
apiVersion: perfectscale.io/v1
kind: ClusterRecommendationsConfig
metadata:
  name: cluster-recommendations-config
spec:
  policies:
    workloadPolicy: Balanced # General optimization policy applied to all workloads by default
    cpuPolicy: MaxSavings # Override the general policy for CPU specifically
    memoryPolicy: MaxHeadroom # Override the general policy for memory specifically
    customTimeWindowDays: 30 # Use 30 days of historical data for recommendations
    memoryRequestEqualsLimit: false # Ensure memory request equals limit for stability
```

When an optimization policy is set at the cluster level via a CRD, a corresponding badge appears next to the policy name. Clicking the badge opens a pop-up with the configured CRD.

<figure><img src="https://1573387604-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FABMqnYtsOO44JmQTVSnn%2Fuploads%2Fg0fqXOEDiUbiJpEh2FTD%2FFrame%201707478488.png?alt=media&#x26;token=93a11932-211b-42aa-ba45-0d430f83d8b9" alt=""><figcaption><p>Cluster optimization policy with CRD</p></figcaption></figure>

#### Setting optimization policy for the namespace with CRD

To apply an optimization policy to the specific namespace, create a namespace-level custom resource. Here is an example:

```yaml
apiVersion: perfectscale.io/v1
kind: NamespaceRecommendationsConfig
metadata:
  name: demo-recommendations-config
  namespace: demo
spec:
  policies:
    workloadPolicy: MaxSavings # General optimization policy applied to all workloads by default
    cpuPolicy: MaxSavings # Override the general policy for CPU specifically
    memoryPolicy: Balanced # Override the general policy for memory specifically
    customTimeWindowDays: 14 # Use 14 days of historical data for recommendations
    memoryRequestEqualsLimit: true # Ensure memory request equals limit for stability
```

#### Setting optimization policy for the workload with CRD

To apply an optimization policy to the specific workload/workload type, create a workload-level custom resource. Here is an example:

```yaml
apiVersion: perfectscale.io/v1
kind: WorkloadRecommendationsConfig
metadata:
  name: rabbitmq-recommendations-config
  namespace: apps
spec:
  targetRef:
    kind: StatefulSet
    name: rabbitmq
  policies:
    workloadPolicy: MaxHeadroom # General optimization policy
    customTimeWindowDays: 30 # Use 30 days of historical data for recommendations
    memoryRequestEqualsLimit: true # Ensure memory request equals limit for stability
```

When an optimization policy is set at the workload with a CRD, a corresponding badge appears next to the policy name. Clicking the `View CRD` button opens a pop-up with the configured CRD.

<figure><img src="https://1573387604-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FABMqnYtsOO44JmQTVSnn%2Fuploads%2FSVKXWuWQSFVlabuFCdkm%2FFrame%201707478489.png?alt=media&#x26;token=055f8759-d8b7-4ca8-8ddd-ee70b50c6e3f" alt=""><figcaption><p>Workload optimization policy with CRD</p></figcaption></figure>

The drop-down menu includes a **Default** policy option to reset the optimization policy to the inherited configuration. This means that if a namespace-level CRD is present, you can return to the namespace configuration, or if only a cluster-level policy exists (via CRD or UI), you can return to the cluster default.

{% hint style="info" %}
If a workload-level CRD exists, you cannot save changes, but you can preview how different policies affect recommendations.
{% endhint %}

{% hint style="warning" %}
When a CRD is applied, you can view its configuration. However, if the current state differs from the CRD, you must first return to the default to access it.
{% endhint %}

## Reverting to default optimization policy settings

To revert to the default optimization policy settings **configured via UI**, simply upgrade the exporter without specifying any value. This will also enable modifying the custom time window through the UI.

```yaml
helm upgrade --install -n some-release-name --create-namespace some-namespace \
  --set secret.create=true \
  --set secret.clientId=CLIENT_ID \
  --set secret.clientSecret=CLIENT_SECRET \
  --set settings.clusterName=CLUSTER_NAME \
  perfectscale/exporter
```

To revert to the default Optimization Policy settings **configured with CRD**, delete the corresponding Custom Resource. If a workload-level CR is deleted, the namespace-level configuration will be applied (if available). If the namespace-level CR is deleted, the cluster-level configuration will be used. If the cluster-level CR is also removed, the system defaults will apply.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.perfectscale.io/customize-workflow/optimization-policy-customization.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
