OptimizationPolicy CRD Specification
OptimizationPolicy CRD Specification
Section titled “OptimizationPolicy CRD Specification”Complete field reference for the OptimizationPolicy Custom Resource Definition.
Overview
Section titled “Overview”The OptimizationPolicy CRD defines how OptiPod optimizes Kubernetes workloads. Each policy specifies:
- Which workloads to target (selector)
- How to collect metrics (metricsConfig)
- Resource constraints (resourceBounds)
- How to apply changes (updateStrategy)
- When to apply changes (mode)
API Version and Kind
Section titled “API Version and Kind”apiVersion: optipod.io/v1alpha1kind: OptimizationPolicyMetadata
Section titled “Metadata”Standard Kubernetes metadata fields:
metadata: name: my-policy # Policy name (required) namespace: default # Policy namespace (required) labels: # Optional labels environment: production annotations: # Optional annotations description: "Production workload optimization"Spec Fields
Section titled “Spec Fields”mode (required)
Section titled “mode (required)”Defines the operational behavior of the policy.
Type: string Required: Yes Enum: Auto, Recommend, Disabled Default: None (must be specified)
Values:
Auto- Automatically applies resource recommendations to workloadsRecommend- Computes recommendations but does not apply them (safe mode)Disabled- Stops processing workloads under this policy
Example:
spec: mode: Recommend # Start in safe modeweight (optional)
Section titled “weight (optional)”Defines the priority of this policy when multiple policies match the same workload. Higher weight policies take precedence.
Type: integer Required: No Default: 100 Range: 1 to 1000
Example:
spec: weight: 200 # Higher priority than defaultselector (required)
Section titled “selector (required)”Defines which workloads this policy applies to. At least one selector type must be specified.
Type: object Required: Yes
selector.namespaceSelector (optional)
Section titled “selector.namespaceSelector (optional)”Selects namespaces by labels using Kubernetes label selector syntax.
Type: LabelSelector Required: No
Example:
selector: namespaceSelector: matchLabels: environment: production team: platformWith match expressions:
selector: namespaceSelector: matchExpressions: - key: environment operator: In values: - production - staging - key: team operator: NotIn values: - experimentalValid operators: In, NotIn, Exists, DoesNotExist
selector.workloadSelector (optional)
Section titled “selector.workloadSelector (optional)”Selects workloads by labels using Kubernetes label selector syntax.
Type: LabelSelector Required: No
Example:
selector: workloadSelector: matchLabels: optimize: "true" tier: backendselector.namespaces (optional)
Section titled “selector.namespaces (optional)”Defines allow and deny lists for namespace filtering.
Type: object Required: No
Fields:
allow([]string) - List of namespaces to includedeny([]string) - List of namespaces to exclude (takes precedence over allow)
Example:
selector: namespaces: allow: - production - staging deny: - kube-system - kube-publicselector.workloadTypes (optional)
Section titled “selector.workloadTypes (optional)”Defines include/exclude filters for workload types.
Type: object Required: No
Fields:
include([]WorkloadType) - Workload types to include (if empty, includes all)exclude([]WorkloadType) - Workload types to exclude (takes precedence over include)
Valid WorkloadType values: Deployment, StatefulSet, DaemonSet
Example:
selector: workloadTypes: include: - Deployment - StatefulSetOr exclude specific types:
selector: workloadTypes: exclude: - DaemonSetmetricsConfig (required)
Section titled “metricsConfig (required)”Defines metrics collection and processing configuration.
Type: object Required: Yes
metricsConfig.provider (required)
Section titled “metricsConfig.provider (required)”Specifies the metrics backend.
Type: string Required: Yes Enum: prometheus, metrics-server, custom
Example:
metricsConfig: provider: prometheusmetricsConfig.metricsServer (optional)
Section titled “metricsConfig.metricsServer (optional)”Configures metrics-server specific behavior. Only applicable when provider: metrics-server.
Type: object Required: No
Fields:
minSamplesRequired(integer) - Minimum number of cached samples required before computing recommendations- Range:
1to100000 - Default: Operator-wide default
- Range:
Example:
metricsConfig: provider: metrics-server metricsServer: minSamplesRequired: 20metricsConfig.rollingWindow (optional)
Section titled “metricsConfig.rollingWindow (optional)”Defines the time period over which metrics are aggregated.
Type: duration Required: No Default: 24h Format: Kubernetes duration (e.g., 12h, 24h, 48h)
Example:
metricsConfig: rollingWindow: 48h # 48 hours of datametricsConfig.percentile (optional)
Section titled “metricsConfig.percentile (optional)”Defines which percentile to use for recommendations.
Type: string Required: No Default: P90 Enum: P50, P90, P99
Example:
metricsConfig: percentile: P90 # Use 90th percentilemetricsConfig.safetyFactor (optional)
Section titled “metricsConfig.safetyFactor (optional)”Multiplier applied to the selected percentile value. Provides a buffer above observed usage.
Type: float64 Required: No Default: 1.2 Minimum: 1.0
Example:
metricsConfig: safetyFactor: 1.5 # 50% buffer above P90Calculation:
Recommendation = Percentile(Usage) × SafetyFactorresourceBounds (required)
Section titled “resourceBounds (required)”Defines min/max constraints for CPU and memory recommendations.
Type: object Required: Yes
resourceBounds.cpu (required)
Section titled “resourceBounds.cpu (required)”Defines CPU resource bounds.
Type: object Required: Yes
Fields:
min(Quantity) - Minimum allowed CPU value (required)max(Quantity) - Maximum allowed CPU value (required)
Example:
resourceBounds: cpu: min: "100m" # 100 millicores max: "4000m" # 4 coresValidation: min must be less than or equal to max
resourceBounds.memory (required)
Section titled “resourceBounds.memory (required)”Defines memory resource bounds.
Type: object Required: Yes
Fields:
min(Quantity) - Minimum allowed memory value (required)max(Quantity) - Maximum allowed memory value (required)
Example:
resourceBounds: memory: min: "128Mi" # 128 mebibytes max: "8Gi" # 8 gibibytesValidation: min must be less than or equal to max
updateStrategy (required)
Section titled “updateStrategy (required)”Defines how resource updates are applied to workloads.
Type: object Required: Yes
updateStrategy.strategy (optional)
Section titled “updateStrategy.strategy (optional)”Defines how resource updates are applied.
Type: string Required: No Default: webhook Enum: ssa, webhook
Values:
ssa- Uses Server-Side Apply for resource updateswebhook- Uses mutating webhooks for resource updates (GitOps-safe)
Example:
updateStrategy: strategy: webhookupdateStrategy.rolloutStrategy (optional)
Section titled “updateStrategy.rolloutStrategy (optional)”Defines when resource changes take effect. Only applicable for webhook strategy.
Type: string Required: No Default: onNextRestart Enum: immediate, onNextRestart
Values:
immediate- Triggers rolling restart after applying annotationsonNextRestart- Only applies annotations without triggering restarts
Example:
updateStrategy: rolloutStrategy: onNextRestartupdateStrategy.allowInPlaceResize (optional)
Section titled “updateStrategy.allowInPlaceResize (optional)”Enables in-place pod resize when supported by Kubernetes (1.27+).
Type: boolean Required: No Default: true
Example:
updateStrategy: allowInPlaceResize: trueupdateStrategy.allowRecreate (optional)
Section titled “updateStrategy.allowRecreate (optional)”Enables pod recreation when in-place resize is not available.
Type: boolean Required: No Default: false
Example:
updateStrategy: allowRecreate: false # Safer defaultupdateStrategy.updateRequestsOnly (optional)
Section titled “updateStrategy.updateRequestsOnly (optional)”Controls whether to update only requests or both requests and limits.
Type: boolean Required: No Default: true
Example:
updateStrategy: updateRequestsOnly: true # Only update requestsupdateStrategy.useServerSideApply (optional)
Section titled “updateStrategy.useServerSideApply (optional)”Enables Server-Side Apply for field-level ownership. Only applicable for SSA strategy.
Type: boolean Required: No Default: true
Example:
updateStrategy: strategy: ssa useServerSideApply: trueupdateStrategy.limitConfig (optional)
Section titled “updateStrategy.limitConfig (optional)”Defines how resource limits are calculated from recommendations. Only applicable when updateRequestsOnly: false.
Type: object Required: No
Fields:
cpuLimitMultiplier(float64) - Multiplier applied to CPU recommendation to calculate limit- Default:
1.0(limit equals recommendation) - Range:
1.0to10.0 - Example:
1.5means limit = recommendation × 1.5
- Default:
memoryLimitMultiplier(float64) - Multiplier applied to memory recommendation to calculate limit- Default:
1.1(limit is 10% higher than recommendation) - Range:
1.0to10.0 - Example:
1.2means limit = recommendation × 1.2
- Default:
Example:
updateStrategy: updateRequestsOnly: false limitConfig: cpuLimitMultiplier: 1.5 # Limit = Request × 1.5 memoryLimitMultiplier: 1.1 # Limit = Request × 1.1updateStrategy.allowUnsafeMemoryDecrease (optional)
Section titled “updateStrategy.allowUnsafeMemoryDecrease (optional)”Disables memory safety checks when true. Use with caution.
Type: boolean Required: No Default: false
Behavior:
- When
false(default): OptiPod blocks memory decreases that could cause pod eviction or OOM - When
true: OptiPod applies all memory recommendations regardless of safety concerns
Example:
updateStrategy: allowUnsafeMemoryDecrease: false # Safe defaultWarning: Setting this to true can cause pod instability. Only use in controlled environments.
updateStrategy.gradualDecreaseConfig (optional)
Section titled “updateStrategy.gradualDecreaseConfig (optional)”⚠️ Implementation Status: This field is defined in the CRD schema and validated, but not currently implemented in the controller. Setting this configuration has no effect on actual behavior.
Configures gradual memory reduction for safer optimization. When implemented, large memory decreases would be applied incrementally over multiple reconciliations.
Type: object Required: No
Fields:
enabled(boolean) - Activates gradual decrease functionality- Default:
false - Note: Currently has no effect
- Default:
memoryDecreasePercentage(integer) - Maximum percentage to decrease memory per reconciliation- Default:
10(10% per reconciliation) - Range:
1to50 - Note: Currently has no effect
- Default:
minimumDecreaseThreshold(Quantity) - Minimum decrease amount to trigger gradual reduction- Default:
100Mi - Decreases smaller than this threshold are applied immediately
- Note: Currently has no effect
- Default:
maximumTotalDecrease(integer) - Maximum total percentage decrease from original value- Default:
70(70% maximum total decrease) - Range:
1to90 - Prevents excessive optimization that could destabilize workloads
- Note: Currently has no effect
- Default:
Example:
updateStrategy: gradualDecreaseConfig: enabled: true memoryDecreasePercentage: 10 # Max 10% per cycle minimumDecreaseThreshold: 100Mi # Threshold to trigger maximumTotalDecrease: 70 # Max 70% total decreaseCurrent Behavior: Memory decreases are applied immediately in full, subject to other safety checks.
reconciliationInterval (optional)
Section titled “reconciliationInterval (optional)”Defines how often the policy is evaluated.
Type: duration Required: No Default: 5m Format: Kubernetes duration (e.g., 5m, 10m, 1h)
Example:
spec: reconciliationInterval: 5m # Evaluate every 5 minutesStatus Fields
Section titled “Status Fields”The status subresource provides information about the policy’s current state.
conditions
Section titled “conditions”Represents the current state of the OptimizationPolicy resource using standard Kubernetes conditions.
Type: []Condition List Type: map (keyed by type)
Example:
status: conditions: - type: Ready status: "True" lastTransitionTime: "2025-01-28T10:00:00Z" reason: PolicyActive message: "Policy is active and processing workloads"workloadsDiscovered
Section titled “workloadsDiscovered”Count of workloads matching this policy’s selector.
Type: integer
Example:
status: workloadsDiscovered: 10workloadsProcessed
Section titled “workloadsProcessed”Count of workloads successfully processed by this policy.
Type: integer
Example:
status: workloadsProcessed: 8lastReconciliation
Section titled “lastReconciliation”Timestamp of the last reconciliation.
Type: Time
Example:
status: lastReconciliation: "2025-01-28T10:30:00Z"workloadsByType
Section titled “workloadsByType”Provides breakdown of workloads by type.
Type: object
Fields:
deployments(integer) - Count of Deployment workloadsstatefulSets(integer) - Count of StatefulSet workloadsdaemonSets(integer) - Count of DaemonSet workloads
Example:
status: workloadsByType: deployments: 6 statefulSets: 2 daemonSets: 0Complete Example
Section titled “Complete Example”apiVersion: optipod.io/v1alpha1kind: OptimizationPolicymetadata: name: production-policy namespace: default labels: environment: productionspec: # Operational mode mode: Auto weight: 200
# Workload selection selector: namespaceSelector: matchLabels: environment: production workloadSelector: matchLabels: optimize: "true" namespaces: deny: - kube-system workloadTypes: include: - Deployment - StatefulSet
# Metrics configuration metricsConfig: provider: prometheus rollingWindow: 24h percentile: P90 safetyFactor: 1.2
# Resource constraints resourceBounds: cpu: min: "100m" max: "4000m" memory: min: "256Mi" max: "8Gi"
# Update strategy updateStrategy: strategy: webhook rolloutStrategy: onNextRestart allowInPlaceResize: true allowRecreate: false updateRequestsOnly: true # Note: gradualDecreaseConfig not yet implemented
# Reconciliation reconciliationInterval: 5m
status: conditions: - type: Ready status: "True" lastTransitionTime: "2025-01-28T10:00:00Z" reason: PolicyActive message: "Policy is active and processing workloads" workloadsDiscovered: 10 workloadsProcessed: 8 lastReconciliation: "2025-01-28T10:30:00Z" workloadsByType: deployments: 6 statefulSets: 2 daemonSets: 0Validation Rules
Section titled “Validation Rules”The OptimizationPolicy CRD enforces the following validation rules:
Required Fields
Section titled “Required Fields”spec.mode- Must be one of:Auto,Recommend,Disabledspec.selector- At least one selector type must be specifiedspec.metricsConfig.provider- Must be specifiedspec.resourceBounds.cpu.min- Must be greater than zerospec.resourceBounds.cpu.max- Must be greater than zerospec.resourceBounds.memory.min- Must be greater than zerospec.resourceBounds.memory.max- Must be greater than zero
Value Constraints
Section titled “Value Constraints”spec.weight- Must be between 1 and 1000spec.metricsConfig.safetyFactor- Must be >= 1.0spec.metricsConfig.metricsServer.minSamplesRequired- Must be >= 1spec.resourceBounds.cpu.min- Must be <=spec.resourceBounds.cpu.maxspec.resourceBounds.memory.min- Must be <=spec.resourceBounds.memory.maxspec.updateStrategy.limitConfig.cpuLimitMultiplier- Must be between 1.0 and 10.0spec.updateStrategy.limitConfig.memoryLimitMultiplier- Must be between 1.0 and 10.0spec.updateStrategy.gradualDecreaseConfig.memoryDecreasePercentage- Must be between 1 and 50spec.updateStrategy.gradualDecreaseConfig.maximumTotalDecrease- Must be between 1 and 90
Label Selector Validation
Section titled “Label Selector Validation”matchExpressions[].key- Required for each expressionmatchExpressions[].operator- Must be one of:In,NotIn,Exists,DoesNotExistmatchExpressions[].values- Required forInandNotInoperatorsmatchExpressions[].values- Must not be specified forExistsandDoesNotExistoperators
Workload Type Validation
Section titled “Workload Type Validation”- All workload types must be one of:
Deployment,StatefulSet,DaemonSet
kubectl Commands
Section titled “kubectl Commands”Create a policy
Section titled “Create a policy”kubectl apply -f policy.yamlList policies
Section titled “List policies”kubectl get optimizationpolicies# or use short namekubectl get optpolDescribe a policy
Section titled “Describe a policy”kubectl describe optimizationpolicy production-policyView policy status
Section titled “View policy status”kubectl get optimizationpolicy production-policy -o yamlDelete a policy
Section titled “Delete a policy”kubectl delete optimizationpolicy production-policyWatch policy changes
Section titled “Watch policy changes”kubectl get optimizationpolicies --watchRelated Documentation
Section titled “Related Documentation”- Creating Policies - Policy configuration guide
- Annotations Reference - Workload annotation format
- Operational Modes - Understanding Auto, Recommend, and Disabled modes
- Update Strategies - SSA vs Webhook strategies
- Safety Model - Understanding safety guarantees