# Configure optimization using CRDs

> Cluster- and namespace-level optimization configuration with CRDs

---

Site [llms.txt](/llms.txt)

---

StormForge's optimization configuration options ensure consistent right-sizing across workloads and give teams control over their work.

* **Platform teams** can set organization-wide default optimization values
    
* **Platform teams** can provide different default optimization values for all workloads matching a type or label selector
    
* **Development teams** can fine-tune settings further for workloads in their namespaces, or for those matching specific label selectors
    
Optimization configurations are Custom Resource Definitions (CRDs) consisting of [rules](#rules) evaluated and applied to workloads in definition order.

This page describes the `optimize.stormforge.io/v2` API. For a complete field reference, see the [ClusterOptimizationConfiguration reference][coc-ref] and the [OptimizationConfiguration reference][oc-ref]. If your manifests use `optimize.stormforge.io/v1`, see [Migrate from the v1 API](#migrate-from-the-v1-api).

## Types

Define optimization configurations using the following CRD types:

* **ClusterOptimizationConfiguration**
    * A cluster-scoped resource intended for use by platform teams to define organization-wide optimization policy.

    * **ClusterOptimizationConfiguration rules are always evaluated before any OptimizationConfiguration rules.**

* **OptimizationConfiguration**
    * A namespace-scoped resource intended for use by application or development teams that own namespace resources.

    * **OptimizationConfiguration rules apply only to workloads in the resource's namespace and are evaluated after ClusterOptimizationConfiguration rules.**

### ClusterOptimizationConfiguration

Defines a list of cluster-scoped rules. A `match` field on the configuration or on a rule selects the workloads the rules apply to, by workload labels, namespace labels, or a CEL expression. An `order` field sets the evaluation order of a ClusterOptimizationConfiguration relative to other ClusterOptimizationConfigurations.

```yaml
apiVersion: optimize.stormforge.io/v2
kind: ClusterOptimizationConfiguration
metadata:
  name: cluster-defaults
spec:
  order: 10
  match: {}
  rules:
  - name: NAME
    match: {}
    settings: []
```

### OptimizationConfiguration

Defines a list of namespace-scoped rules. A `match` field on the configuration or on a rule selects the workloads the rules apply to, by workload labels or a CEL expression. An `order` field sets the evaluation order of an OptimizationConfiguration relative to other OptimizationConfigurations.

```yaml
apiVersion: optimize.stormforge.io/v2
kind: OptimizationConfiguration
metadata:
  name: namespace-defaults
  namespace: default
spec:
  order: 10
  match: {}
  rules:
  - name: NAME
    match: {}
    settings: []
```

## Ordering

When multiple optimization configurations of the same type exist, `spec.order` controls their relative evaluation order. The order field follows the **Higher = Stronger** pattern, where higher numbers are evaluated later, and can override settings specified in lower-order configurations. Configurations with the same `spec.order` are ordered alphabetically by name.

```yaml
spec:
  order: 10
```

<div class="alert alert-info" role="alert"><div class="h4 alert-heading" role="heading">Order values</div>


Order values must be in the range `[-1000, 1000]`, inclusive. If not specified, order defaults to 0.
</div>


## Rules

Define cascading rules, or configuration steps, as `spec.rules`. Each rule applies a new configuration or modifies the previous configuration for all workloads, or for the workloads its `match` selects. Each rule needs a unique `name` and a list of [settings](#settings).

```yaml
apiVersion: optimize.stormforge.io/v2
kind: ClusterOptimizationConfiguration
metadata:
  name: cluster-defaults
spec:
  order: 10
  rules:
  - name: defaults
    settings:
    - name: schedule
      value: P2D
  - name: daemonsets
    match:
      expression: 'object.kind == "DaemonSet"'
    settings:
    - name: memory.optimization-goal
      value: Balanced
  - name: production-web
    match:
      objectSelector: app.kubernetes.io/name=web
      namespaceSelector: env=prod
    settings:
    - name: cpu.optimization-goal
      value: Reliability
```

## Match workloads

Use `match` to constrain which workloads a configuration or rule applies to. A `match` can set any of these criteria:

* **`objectSelector`:** A label selector applied to the workload's labels.

* **`namespaceSelector`:** A label selector applied to the labels of the workload's namespace. ClusterOptimizationConfiguration only; an OptimizationConfiguration already applies to a single namespace.

* **`expression`:** A Common Expression Language (CEL) expression for matching non-label workload characteristics such as annotations, workload kind, or name. See [CEL variables](#cel-variables).

* **`anyOf`:** A list of alternative terms. Each term can set the same criteria as `match`, except `anyOf`.

Selectors are label-selector strings, in the same syntax as `kubectl get --selector`. For example, `app=web`, `tier in (frontend, backend)`, or `!legacy`.

### How criteria combine

* A workload matches when it satisfies **every** criterion set directly on `match`.
* When `anyOf` is set, the workload must also match **at least one** of its terms. Each term's own criteria must all match.
* An omitted or empty criterion matches every workload, so `match: {}` matches everything. Within `anyOf`, an empty term matches no workloads.
* `spec.match` applies to every rule in the configuration. If a rule also has its own `match`, a workload must satisfy both.

To combine conditions in one expression, use `&&` and `||`.

### Match examples

This configuration applies only to workloads that meet all three criteria: labeled `app=web`, in a namespace labeled `env=prod`, and named `web-*`:

```yaml
apiVersion: optimize.stormforge.io/v2
kind: ClusterOptimizationConfiguration
metadata:
  name: prod-web
spec:
  match:
    objectSelector: app=web
    namespaceSelector: env=prod
    expression: 'object.metadata.name.matches("^web-.*")'
  rules:
  - name: defaults
    settings:
    - name: schedule
      value: P1D
```

This configuration uses `anyOf` to apply to web workloads in production namespaces, and to every workload labeled `app=cache`:

```yaml
apiVersion: optimize.stormforge.io/v2
kind: ClusterOptimizationConfiguration
metadata:
  name: web-or-cache
spec:
  match:
    anyOf:
    - objectSelector: app=web
      namespaceSelector: env=prod
    - objectSelector: app=cache
  rules:
  - name: defaults
    settings:
    - name: schedule
      value: P1D
```

This configuration sets `namespaceSelector` alongside `anyOf`, so it applies to workloads in the `stream`, `subsystem`, or `support` teams' namespaces that are also labeled `app-type=critical` or named `critical-*`:

```yaml
apiVersion: optimize.stormforge.io/v2
kind: ClusterOptimizationConfiguration
metadata:
  name: critical-team-workloads
spec:
  match:
    namespaceSelector: owner in (stream, subsystem, support)
    anyOf:
    - objectSelector: app-type=critical
    - expression: 'object.metadata.name.startsWith("critical-")'
  rules:
  - name: defaults
    settings:
    - name: cpu.optimization-goal
      value: Reliability
  - name: deployments
    match:
      expression: 'object.kind == "Deployment"'
    settings:
    - name: schedule
      value: P1D
```

The `deployments` rule applies only to Deployments that also satisfy `spec.match`.

### CEL variables

The following variables are available to `expression`:

* **`object`:** The workload object.

    * `apiVersion`: The workload's GroupVersion string (for example, `apps/v1`).

    * `kind`: The workload kind (for example, `Deployment`).

    * `metadata.name`: The workload name.

    * `metadata.namespace`: The workload namespace.

    * `metadata.labels`: A map of the workload's labels.

    * `metadata.annotations`: A map of the workload's annotations.

*   **`namespaceObject`:** The namespace object containing the workload; ClusterOptimizationConfiguration only.

    * `metadata.name`: The namespace name.

    * `metadata.labels`: A map of the namespace's labels.

    * `metadata.annotations`: A map of the namespace's annotations.

## Settings

Settings is a `listMap` keyed on `name`, consisting of `name` and either `value` or `values`, where `name` must be the name of a [StormForge configuration setting](/docs/configure/settings/), like `schedule` or `containers.cpu.requests.max`. A `value` is a string; `values` is a list of strings.

When a workload matches a rule, the rule's settings are applied. Settings defined in later rules can override those from earlier rules.

## Migrate from the v1 API

`optimize.stormforge.io/v1` is deprecated. Existing v1 resources keep working, and the cluster still accepts v1 manifests, but every v1 request returns this warning:

```text
optimize.stormforge.io/v1 is deprecated; use v2. The v1 representation is reconstructed and is not preserved if the object is modified at v2.
```

The API server converts between versions through the StormForge webhook, so the webhook must be enabled. See [Webhook configuration][webhook-config].

For the v1 fields, see the [ClusterOptimizationConfiguration v1 reference][coc-v1-ref] and the [OptimizationConfiguration v1 reference][oc-v1-ref].

To migrate a manifest:

1. Change `apiVersion` to `optimize.stormforge.io/v2`.
1. In each rule, replace `selector`, `namespaceSelector`, and `matchConditions` with a single `match`:

    | v1 rule field | v2 `match` field |
    |---------------|------------------|
    | `selector` | `objectSelector`, as a selector string |
    | `namespaceSelector` | `namespaceSelector`, as a selector string |
    | `matchConditions` | `expression`; join multiple conditions with `&&` |

    Write selector strings in `kubectl` syntax. `matchLabels` entries become `key=value`, and `matchExpressions` operators become `key in (a, b)`, `key notin (a, b)`, `key`, and `!key`.

    When you join match conditions into one expression, wrap any condition that contains `||` in parentheses. `&&` binds more tightly than `||`, so `a || b && c` means `a || (b && c)`.

1. Optionally, move criteria that every rule repeats into `spec.match`.
1. Apply the updated manifest. It updates the existing resource, so you don't need to delete the v1 version first.

For example, this v1 rule:

```yaml
apiVersion: optimize.stormforge.io/v1
kind: ClusterOptimizationConfiguration
metadata:
  name: prod-cluster-defaults
spec:
  rules:
  - name: prod-deployments
    selector:
      matchLabels:
        app.kubernetes.io/name: web
    namespaceSelector:
      matchExpressions:
      - key: env
        operator: In
        values: [prod, staging]
    matchConditions:
    - name: is-deployment
      expression: 'object.kind == "Deployment"'
    - name: not-canary
      expression: '!object.metadata.name.endsWith("-canary")'
    settings:
    - name: schedule
      value: P1D
```

becomes:

```yaml
apiVersion: optimize.stormforge.io/v2
kind: ClusterOptimizationConfiguration
metadata:
  name: prod-cluster-defaults
spec:
  rules:
  - name: prod-deployments
    match:
      objectSelector: app.kubernetes.io/name=web
      namespaceSelector: env in (prod, staging)
      expression: 'object.kind == "Deployment" && !object.metadata.name.endsWith("-canary")'
    settings:
    - name: schedule
      value: P1D
```

<div class="alert alert-warning" role="alert"><div class="h4 alert-heading" role="heading">v2-only fields</div>


`spec.match` and `anyOf` have no v1 equivalent, so a resource that uses either can't be read at v1. Before you adopt them, migrate any manifests and tooling that still use v1, such as GitOps repositories.
</div>


[coc-ref]: /docs/reference/crds/clusteroptimizationconfiguration/
[oc-ref]: /docs/reference/crds/optimizationconfiguration/
[coc-v1-ref]: /docs/reference/crds/clusteroptimizationconfiguration-v1/
[oc-v1-ref]: /docs/reference/crds/optimizationconfiguration-v1/
[webhook-config]: /docs/installation/webhook-config/
