Skip to content

Commit e1c5fd0

Browse files
committed
feat: direct resources setup using crd
1 parent 64db414 commit e1c5fd0

File tree

10 files changed

+715
-3
lines changed

10 files changed

+715
-3
lines changed

README.md

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,13 +487,26 @@ Below are the configuration options organized by category:
487487
| `min-diff-memory-limit-algo` | `minDiffMemoryLimitAlgo` | Algorithm for minimum memory limit difference. | `"ratio"`, `"margin"` | `"ratio"` |
488488
| `min-diff-memory-limit-value` | `minDiffMemoryLimitValue` | Value for minimum memory limit difference calculation. Accepts any numeric value. | Any numeric value | `"0"` |
489489

490-
#### 8. Container-Specific Configurations
490+
#### 8. Direct Resource Specifications
491491

492-
The ResourcesConfig CRD allows you to specify container-specific configurations using the `containerConfigs` field. This is a map where the keys are container names and the values are objects containing any of the resource configuration fields.
492+
| Annotation Key | ResourcesConfig Field | Description | Options | Default |
493+
| --- | --- | --- | --- | --- |
494+
| `request-cpu` | `requestCpu` | Direct CPU request value. Takes precedence over VPA recommendations. | Any valid CPU value | `""` |
495+
| `request-memory` | `requestMemory` | Direct memory request value. Takes precedence over VPA recommendations. | Any valid memory value | `""` |
496+
| `limit-cpu` | `limitCpu` | Direct CPU limit value. Takes precedence over VPA recommendations. | Any valid CPU value | `""` |
497+
| `limit-memory` | `limitMemory` | Direct memory limit value. Takes precedence over VPA recommendations. | Any valid memory value | `""` |
498+
| N/A | `request.cpu` | Kubernetes-native style CPU request. Takes precedence over VPA recommendations. | Any valid CPU value | `""` |
499+
| N/A | `request.memory` | Kubernetes-native style memory request. Takes precedence over VPA recommendations. | Any valid memory value | `""` |
500+
| N/A | `limit.cpu` | Kubernetes-native style CPU limit. Takes precedence over VPA recommendations. | Any valid CPU value | `""` |
501+
| N/A | `limit.memory` | Kubernetes-native style memory limit. Takes precedence over VPA recommendations. | Any valid memory value | `""` |
502+
503+
#### 9. Container-Specific Configurations
504+
505+
The ResourcesConfig CRD allows you to specify container-specific configurations using the `containerConfigs` field. This is a map where the keys are container names and the values are objects containing any of the resource configuration fields, including direct resource specifications.
493506

494507
### Example Usage
495508

496-
#### Complete ResourcesConfig Example:
509+
#### Complete ResourcesConfig Example (Using VPA Recommendations):
497510

498511
```yaml
499512
apiVersion: oblik.socialgouv.io/v1
@@ -541,6 +554,66 @@ spec:
541554
maxRequestMemory: "256Mi"
542555
```
543556

557+
#### Direct Resource Specifications (Flat Style):
558+
559+
```yaml
560+
apiVersion: oblik.socialgouv.io/v1
561+
kind: ResourcesConfig
562+
metadata:
563+
name: web-app-resources
564+
namespace: default
565+
spec:
566+
targetRef:
567+
kind: Deployment
568+
name: web-app
569+
570+
# Direct resource specifications (flat style)
571+
requestCpu: "100m"
572+
requestMemory: "128Mi"
573+
limitCpu: "200m"
574+
limitMemory: "256Mi"
575+
576+
# Container-specific settings
577+
containerConfigs:
578+
nginx:
579+
requestCpu: "50m"
580+
requestMemory: "64Mi"
581+
limitCpu: "100m"
582+
limitMemory: "128Mi"
583+
```
584+
585+
#### Direct Resource Specifications (Kubernetes-Native Style):
586+
587+
```yaml
588+
apiVersion: oblik.socialgouv.io/v1
589+
kind: ResourcesConfig
590+
metadata:
591+
name: web-app-resources
592+
namespace: default
593+
spec:
594+
targetRef:
595+
kind: Deployment
596+
name: web-app
597+
598+
# Kubernetes-native style resource specifications
599+
request:
600+
cpu: "100m"
601+
memory: "128Mi"
602+
limit:
603+
cpu: "200m"
604+
memory: "256Mi"
605+
606+
# Container-specific settings
607+
containerConfigs:
608+
nginx:
609+
request:
610+
cpu: "50m"
611+
memory: "64Mi"
612+
limit:
613+
cpu: "100m"
614+
memory: "128Mi"
615+
```
616+
544617
#### Comparison: Annotations vs. ResourcesConfig
545618

546619
The same configuration using annotations would look like:

charts/oblik/templates/resourcesconfig-crd.yaml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,81 @@ spec:
255255
description: 'Allowed scaling direction for memory limit: "both", "up", "down"'
256256
type: string
257257
enum: ["both", "up", "down"]
258+
# Direct resource specifications (flat style)
259+
requestCpu:
260+
description: Direct CPU request value
261+
type: string
262+
requestMemory:
263+
description: Direct memory request value
264+
type: string
265+
limitCpu:
266+
description: Direct CPU limit value
267+
type: string
268+
limitMemory:
269+
description: Direct memory limit value
270+
type: string
271+
# Kubernetes-native style resource specifications (nested)
272+
request:
273+
description: Kubernetes-native style CPU and memory request specifications
274+
type: object
275+
properties:
276+
cpu:
277+
description: CPU request value
278+
type: string
279+
memory:
280+
description: Memory request value
281+
type: string
282+
limit:
283+
description: Kubernetes-native style CPU and memory limit specifications
284+
type: object
285+
properties:
286+
cpu:
287+
description: CPU limit value
288+
type: string
289+
memory:
290+
description: Memory limit value
291+
type: string
258292
containerConfigs:
259293
description: Container specific configurations
260294
type: object
261295
additionalProperties:
262296
type: object
263297
properties:
298+
# Direct resource specifications (flat style)
299+
requestCpu:
300+
description: Direct CPU request value
301+
type: string
302+
requestMemory:
303+
description: Direct memory request value
304+
type: string
305+
limitCpu:
306+
description: Direct CPU limit value
307+
type: string
308+
limitMemory:
309+
description: Direct memory limit value
310+
type: string
311+
# Kubernetes-native style resource specifications (nested)
312+
request:
313+
description: Kubernetes-native style CPU and memory request specifications
314+
type: object
315+
properties:
316+
cpu:
317+
description: CPU request value
318+
type: string
319+
memory:
320+
description: Memory request value
321+
type: string
322+
limit:
323+
description: Kubernetes-native style CPU and memory limit specifications
324+
type: object
325+
properties:
326+
cpu:
327+
description: CPU limit value
328+
type: string
329+
memory:
330+
description: Memory limit value
331+
type: string
332+
# Original container-specific configurations
264333
minLimitCpu:
265334
description: Minimum CPU limit value
266335
type: string

pkg/apis/oblik/v1/types.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ type ResourcesConfig struct {
1616
Status ResourcesConfigStatus `json:"status,omitempty"`
1717
}
1818

19+
// ResourceList represents CPU and memory resource specifications
20+
type ResourceList struct {
21+
// CPU resource value
22+
CPU string `json:"cpu,omitempty"`
23+
24+
// Memory resource value
25+
Memory string `json:"memory,omitempty"`
26+
}
27+
1928
// ResourcesConfigSpec defines the desired state of ResourcesConfig
2029
type ResourcesConfigSpec struct {
2130
// TargetRef points to the controller managing the set of pods
@@ -188,6 +197,16 @@ type ResourcesConfigSpec struct {
188197
// Allowed scaling direction for memory limit: "both", "up", "down"
189198
LimitMemoryScaleDirection string `json:"limitMemoryScaleDirection,omitempty"`
190199

200+
// Direct resource specifications (flat style)
201+
RequestCpu string `json:"requestCpu,omitempty"`
202+
RequestMemory string `json:"requestMemory,omitempty"`
203+
LimitCpu string `json:"limitCpu,omitempty"`
204+
LimitMemory string `json:"limitMemory,omitempty"`
205+
206+
// Kubernetes-native style resource specifications (nested)
207+
Request *ResourceList `json:"request,omitempty"`
208+
Limit *ResourceList `json:"limit,omitempty"`
209+
191210
// Container specific configurations
192211
ContainerConfigs map[string]ContainerConfig `json:"containerConfigs,omitempty"`
193212
}
@@ -206,6 +225,15 @@ type TargetRef struct {
206225

207226
// ContainerConfig defines container-specific configurations
208227
type ContainerConfig struct {
228+
// Direct resource specifications (flat style)
229+
RequestCpu string `json:"requestCpu,omitempty"`
230+
RequestMemory string `json:"requestMemory,omitempty"`
231+
LimitCpu string `json:"limitCpu,omitempty"`
232+
LimitMemory string `json:"limitMemory,omitempty"`
233+
234+
// Kubernetes-native style resource specifications (nested)
235+
Request *ResourceList `json:"request,omitempty"`
236+
Limit *ResourceList `json:"limit,omitempty"`
209237
// Minimum CPU limit value
210238
MinLimitCpu string `json:"minLimitCpu,omitempty"`
211239

pkg/config/load-cfg.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import (
99
type LoadCfg struct {
1010
Key string
1111

12+
// Direct resource specifications
13+
RequestCpuValue *string
14+
RequestMemoryValue *string
15+
LimitCpuValue *string
16+
LimitMemoryValue *string
17+
1218
RequestCPUApplyMode *ApplyMode
1319
RequestMemoryApplyMode *ApplyMode
1420
LimitCPUApplyMode *ApplyMode
@@ -622,4 +628,25 @@ func loadAnnotableCommonCfg(cfg *LoadCfg, annotable Annotable, annotationSuffix
622628
klog.Warningf("Unknown scale-direction: %s", limitMemoryScaleDirection)
623629
}
624630
}
631+
632+
// Process direct resource specifications
633+
requestCpuValue := getAnnotation("request-cpu")
634+
if requestCpuValue != "" {
635+
cfg.RequestCpuValue = &requestCpuValue
636+
}
637+
638+
requestMemoryValue := getAnnotation("request-memory")
639+
if requestMemoryValue != "" {
640+
cfg.RequestMemoryValue = &requestMemoryValue
641+
}
642+
643+
limitCpuValue := getAnnotation("limit-cpu")
644+
if limitCpuValue != "" {
645+
cfg.LimitCpuValue = &limitCpuValue
646+
}
647+
648+
limitMemoryValue := getAnnotation("limit-memory")
649+
if limitMemoryValue != "" {
650+
cfg.LimitMemoryValue = &limitMemoryValue
651+
}
625652
}

pkg/config/strategy-config.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,3 +1071,47 @@ func (v *StrategyConfig) GetLimitMemoryScaleDirection(containerName string) Scal
10711071
}
10721072
return ScaleDirectionBoth
10731073
}
1074+
1075+
// GetRequestCpuValue returns the direct CPU request value for the container
1076+
func (v *StrategyConfig) GetRequestCpuValue(containerName string) *string {
1077+
if v.Containers[containerName] != nil && v.Containers[containerName].RequestCpuValue != nil {
1078+
return v.Containers[containerName].RequestCpuValue
1079+
}
1080+
if v.RequestCpuValue != nil {
1081+
return v.RequestCpuValue
1082+
}
1083+
return nil
1084+
}
1085+
1086+
// GetRequestMemoryValue returns the direct memory request value for the container
1087+
func (v *StrategyConfig) GetRequestMemoryValue(containerName string) *string {
1088+
if v.Containers[containerName] != nil && v.Containers[containerName].RequestMemoryValue != nil {
1089+
return v.Containers[containerName].RequestMemoryValue
1090+
}
1091+
if v.RequestMemoryValue != nil {
1092+
return v.RequestMemoryValue
1093+
}
1094+
return nil
1095+
}
1096+
1097+
// GetLimitCpuValue returns the direct CPU limit value for the container
1098+
func (v *StrategyConfig) GetLimitCpuValue(containerName string) *string {
1099+
if v.Containers[containerName] != nil && v.Containers[containerName].LimitCpuValue != nil {
1100+
return v.Containers[containerName].LimitCpuValue
1101+
}
1102+
if v.LimitCpuValue != nil {
1103+
return v.LimitCpuValue
1104+
}
1105+
return nil
1106+
}
1107+
1108+
// GetLimitMemoryValue returns the direct memory limit value for the container
1109+
func (v *StrategyConfig) GetLimitMemoryValue(containerName string) *string {
1110+
if v.Containers[containerName] != nil && v.Containers[containerName].LimitMemoryValue != nil {
1111+
return v.Containers[containerName].LimitMemoryValue
1112+
}
1113+
if v.LimitMemoryValue != nil {
1114+
return v.LimitMemoryValue
1115+
}
1116+
return nil
1117+
}

0 commit comments

Comments
 (0)