# Configuring Automation for a namespace with CRD

## Including a namespace in the Automation

The following example of a namespace-level automation configuration allows you to customize automation settings for specific namespaces, catering to the needs of different applications. This example shows how to automate optimization for **all Deployment-type workloads in the namespace**.

**Create** a YAML file with the namespace-level automation configuration using the following template:

```yaml
apiVersion: perfectscale.io/v1
kind: NamespaceAutomationConfig
metadata:
  name: namespace-automation-config
  namespace: your-namespace # The namespace where the configuration should be applied
spec:
  automation:
    operational:
        automationMode: "Enabled" # Enables automation for a specific namespace
        duringRolloutInProgress: pause # A default behavior that prevents Automation from applying new recommendations when concurrent ReplicaSets are identified
        timeConstraints:
          wasteMaxAutomationFrequency: "30m" # Applies recommendations to decrease resources every 30 minutes
          fixResiliencyMaxAutomationFrequency: "30m" # Applies recommendations to increase resources every 30 minutes
          maintenanceWindow:
            monday:
              - "00:00-23:59"
            tuesday:
              - "00:00-23:59"
            wednesday:
              - "00:00-23:59"
            thursday:
              - "00:00-23:59"
            friday:
              - "00:00-23:59"
            saturday:
              - "00:00-23:59"
            sunday:
              - "00:00-23:59"
          maintenanceWindowIgnoredForResiliency: false # deprecated, use "maintenanceWindowIgnoredMinResiliencyLevel" instead
          maintenanceWindowIgnoredMinResiliencyLevel: None # Indicates the minimum resiliency risk level required to bypass maintenance window constraints and resolve the issue
        restrictions:
          workloadMinWasteUSDPerMonth: 5 # Activates automation only if monthly waste exceeds a set threshold (for example, $5)
          cpuManagement:
            request:
              increaseEnabled: false # Doesn't allow PerfectScale Automation to increase CPU requests if the performance risks are observed
              decreaseEnabled: true # Allows PerfectScale Automation to decrease CPU requests to minimize waste
              minimumCores: -1 # If a recommendation falls below the value, the automation will implement the minimum instead
              maximumCores: -1 # If a recommendation falls above the value, the automation will implement the maximum instead
            limit:
              keepLimit: true # Automation will maintain a non-zero CPU limit and will not set the value to zero
          memoryManagement:
            request:
              increaseEnabled: false # Doesn't allow PerfectScale Automation to increase Memory requests if the performance risks are observed
              decreaseEnabled: true # Allows PerfectScale Automation to decrease Memory requests to minimize waste
              minimumGiB: -1 # If a recommendation falls below the value, the automation will implement the minimum instead
              maximumGiB: -1 # If a recommendation falls above the value, the automation will implement the maximum instead
            limit:
              increaseEnabled: false # Doesn't allow PerfectScale Automation to increase Memory limit if the performance risks are observed
              decreaseEnabled: false # Doesn't allow PerfectScale Automation to decrease Memory limit
              minimumGiB: -1 # If a recommendation falls below the value, the automation will implement the minimum inst
              maximumGiB: -1 # If a recommendation falls above the value, the automation will implement the maximum instead
              memoryLeakDetection:
                maxMemoryIncreaseIterations:
                  daily: 3 # Allows automation to increase memory limit up to 3 times per day
                  weekly: 6 # Allows automation to increase memory limit up to 6 times per week
    workloadTypes:
      Deployment:
        operational:
          automationMode: "Enabled" # Enables automation for the workloads with a specific type in the cluster/namespace
          timeConstraints:
            wasteMaxAutomationFrequency: "30m" # Applies recommendations to decrease resources every 30 minutes
            fixResiliencyMaxAutomationFrequency: "30m" # Applies recommendations to increase resources every 30 minutes
            maintenanceWindow:
              monday:
                - "00:00-23:59"
              tuesday:
                - "00:00-23:59"
              wednesday:
                - "00:00-23:59"
              thursday:
                - "00:00-23:59"
              friday:
                - "00:00-23:59"
              saturday:
                - "00:00-23:59"
              sunday:
                - "00:00-23:59"
            maintenanceWindowIgnoredForResiliency: false # deprecated, use "maintenanceWindowIgnoredMinResiliencyLevel" instead
            maintenanceWindowIgnoredMinResiliencyLevel: None # Indicates the minimum resiliency risk level required to bypass maintenance window constraints and resolve the issue
          restrictions:
            workloadMinWasteUSDPerMonth: 5 # Activates automation only if monthly waste exceeds a set threshold (for example, $5)
            cpuManagement:
              request:
                increaseEnabled: false # Doesn't allow PerfectScale Automation to increase CPU requests if the performance risks are observed
                decreaseEnabled: true # Allows PerfectScale Automation to decrease CPU requests to minimize waste
                minimumCores: -1 # If a recommendation falls below the value, the automation will implement the minimum instead
                maximumCores: -1 # If a recommendation falls above the value, the automation will implement the maximum instead
              limit:
                keepLimit: true # Automation will maintain a non-zero CPU limit and will not set the value to zero
            memoryManagement:
              request:
                increaseEnabled: false # Doesn't allow PerfectScale Automation to increase Memory requests if the performance risks are observed
                decreaseEnabled: true # Allows PerfectScale Automation to decrease Memory requests to minimize waste
                minimumGiB: -1 # If a recommendation falls below the value, the automation will implement the minimum instead
                maximumGiB: -1 # If a recommendation falls above the value, the automation will implement the maximum instead
              limit:
                increaseEnabled: false # Doesn't allow PerfectScale Automation to increase Memory limit if the performance risks are observed
                decreaseEnabled: false # Doesn't allow PerfectScale Automation to decrease Memory limit
                minimumGiB: -1 # If a recommendation falls below the value, the automation will implement the minimum instead
                maximumGiB: -1 # If a recommendation falls above the value, the automation will implement the maximum instead
                memoryLeakDetection:
                  maxMemoryIncreaseIterations:
                    daily: 3 # Allows automation to increase memory limit up to 3 times per day
                    weekly: 6 # Allows automation to increase memory limit up to 6 times per week
      Rollout:
        operational:
          automationMode: "Enabled" # Enables rollout support               
```

**Apply** the created configuration by running the following command (where `namespace-automation-config.yaml` - the name of your file created in the previous step)

```
kubectl apply -f namespace-automation-config.yaml
```

### Automating multiple namespaces with a single configuration

If you want to apply the same configuration to multiple namespaces, remove **`namespace: your-namespace`** from the configuration and apply the configuration with the following command for each namespace **`kubectl apply -f namespace-automation-config.yaml  -n namespace`**, where **`namespace`** - the namespace where it will be applied.\
If a **`namespace`** is specified neither in the configuration nor in the command, the configuration will be applied to the default namespace.

### Automating kube-system namespace

{% hint style="warning" %}
If you need to automate the **`kube-system` namespace** in an **AKS cluster**, additional configuration is required [due to known limitations](https://learn.microsoft.com/en-us/azure/aks/faq#can-admission-controller-webhooks-impact-kube-system-and-internal-aks-namespaces-). \
\
Learn more about the configuration [here](#automating-the-kube-system-namespace-in-aks-clusters).
{% endhint %}

By default, PerfectScale does not automate the `kube-system` namespace. To enable automation for this namespace, follow these simple steps.

**Remove** the `kube-system` namespace from the `excludedNamespaces` (is only needed if you are using **Autoscaler v1.0.6 and older**).

```yaml
helm upgrade --install -n perfectscale --reset-then-reuse-values psc-autoscaler --set settings.excludedNamespaces=[] ./helm
```

**Create** a YAML file with the namespace-level automation configuration using the following template:

```yaml
apiVersion: perfectscale.io/v1
kind: NamespaceAutomationConfig
metadata:
  name: kube-system-ns-config
  namespace: kube-system # The namespace where the configuration should be applied
spec:
  automation:
    operational:
      automationMode: "Enabled" # Enables automation for a specific namespace
      duringRolloutInProgress: pause # A default behavior that prevents Automation from applying new recommendations when concurrent ReplicaSets are identified
      restrictions:
        workloadMinWasteUSDPerMonth: 5 # Activates automation only if monthly waste exceeds a set threshold (for example, $5)
        cpuManagement:
          request:
            increaseEnabled: true # Allows PerfectScale Automation to increase CPU requests if the performance risks are observed
            decreaseEnabled: true # Allows PerfectScale Automation to decrease CPU requests to minimize waste
          limit:
            keepLimit: false # Automation can set the CPU limit to zero
        memoryManagement:
          request:
            increaseEnabled: true # Allows PerfectScale Automation to increase Memory requests if the performance risks are observed
            decreaseEnabled: true # Allows PerfectScale Automation to decrease Memory requests to minimize waste
          limit:
            increaseEnabled: true # Allows PerfectScale Automation to increase Memory limit if the performance risks are observed
            decreaseEnabled: true # Allows PerfectScale Automation to decrease Memory limit
```

**Apply** the created configuration by running the following command (where `kube-system-ns-config.yaml` - the name of your file created in the previous step)

```yaml
kubectl apply -f kube-system-ns-config.yaml
```

Explore more about automation customization options [here](broken://pages/AoWNruTdYbpYb7aSotCr).

#### :exclamation: Automating kube-system namespace in AKS clusters

Azure Kubernetes Service has a [known limitation](https://learn.microsoft.com/en-us/azure/aks/faq#can-admission-controller-webhooks-impact-kube-system-and-internal-aks-namespaces-) that prevents tools from managing resources within the `kube-system` namespace by default. To enable automation in this namespace, it is necessary to add the following annotation to allow Admissions Enforcer to ignore it:

```
helm upgrade --install -n perfectscale psc-autoscaler \
      --set-string admission.annotations."admissions\.enforcer/disabled"=true \
      perfectscale/psc-autoscaler
```


---

# 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/enable-automation/configuring-automation-with-crd/including-a-cluster-namespace-or-workload-to-the-automation/configuring-automation-for-a-namespace-with-crd.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.
