Skip to content

Commit 7660896

Browse files
✨ add factor feature (#13)
1 parent 1042650 commit 7660896

File tree

7 files changed

+36
-11
lines changed

7 files changed

+36
-11
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
**kube-reqsizer** is a kubernetes controller that will measure the usage of pods over time and optimize (reduce/increase) their requests based on the average usage.
77

8-
NOTE: This is an alternative to [Vertical-Pod-Autoscaler](https://github.com/kubernetes/autoscaler/tree/master/vertical-pod-autoscaler). The intended use of this project is to provide a simpler, more straightforward install and mechanism, without CRDs, **and that can work with [Horizontal-Pod-Autoscaler](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/).**
8+
When all required conditions meet, the controller calculates the result requirements based on all the samples taken so far a pod.
9+
It then goes "upstream" to the parent controller of that pod, for example *Deployment*, and updates the relevant containers for the pod inside the deployment as a reconciliation, as if its desired state is the new state with the new requirements.
10+
11+
Note: This is an alternative to [Vertical-Pod-Autoscaler](https://github.com/kubernetes/autoscaler/tree/master/vertical-pod-autoscaler). The intended use of this project is to provide a simpler, more straightforward install and mechanism, without CRDs, **and that can work with [Horizontal-Pod-Autoscaler](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/).**
912
## Deploy - Helm
1013

1114
```bash
@@ -17,6 +20,7 @@ helm install kube-reqsizer/kube-reqsizer
1720
**Core Values:**
1821

1922
```yaml
23+
enabledAnnotation: true
2024
sampleSize: 1
2125
minSeconds: 1
2226
enableIncrease: true
@@ -25,7 +29,8 @@ maxMemory: 0
2529
minMemory: 0
2630
maxCPU: 0
2731
minCPU: 0
28-
enabledAnnotation: true
32+
cpuFactor: 1
33+
memoryFactor: 1
2934
logLevel: info
3035
```
3136
## Prerequisites
@@ -51,7 +56,7 @@ logLevel: info
5156

5257
The sample size to create an average from when reconciling.
5358

54-
--min-seconds int (default 1)
59+
--min-seconds float (default 1)
5560

5661
Minimum seconds between pod restart.
5762
This ensures the controller will not restart a pod if the minimum time
@@ -74,6 +79,12 @@ logLevel: info
7479

7580
--min-memory int (default 0)
7681
Minimum memory in (Mi) that the controller can set a pod request to. 0 is infinite
82+
83+
--cpu-factor float (default 1)
84+
A factor to multiply CPU requests when reconciling.
85+
86+
--memory-factor float (default 1)
87+
A factor to multiply Memory requests when reconciling.
7788
```
7889

7990
### Annotations

charts/kube-reqsizer/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ type: application
1313
# This is the chart version. This version number should be incremented each time you make changes
1414
# to the chart and its templates, including the app version.
1515
# Versions are expected to follow Semantic Versioning (https://semver.org/)
16-
version: 0.6.4
16+
version: 0.6.5

charts/kube-reqsizer/templates/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ spec:
3232
- --max-memory={{.Values.maxMemory}}
3333
- --min-cpu={{.Values.minCPU}}
3434
- --min-memory={{.Values.minMemory}}
35+
- --cpu-factor={{.Values.cpuFactor}}
36+
- --memory-factor={{.Values.memoryFactor}}
3537
resources:
3638
{{- toYaml .Values.controllerManager.manager.resources | nindent 10 }}
3739
command:

charts/kube-reqsizer/values.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
enabledAnnotation: true
12
sampleSize: 1
23
minSeconds: 1
34
enableIncrease: true
@@ -6,7 +7,8 @@ maxMemory: 0
67
minMemory: 0
78
maxCPU: 0
89
minCPU: 0
9-
enabledAnnotation: true
10+
cpuFactor: 1
11+
memoryFactor: 1
1012
logLevel: info
1113

1214
controllerManager:

controllers/pod_controller.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,17 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
162162
switch r.GetPodMode(pod, ctx) {
163163
case "average":
164164
if r.ValidateCPU(currentC.CPU, AverageUsageCPU) {
165-
pod.Spec.Containers[i].Resources.Requests[v1.ResourceCPU] = resource.MustParse(fmt.Sprintf("%dm", AverageUsageCPU))
165+
pod.Spec.Containers[i].Resources.Requests[v1.ResourceCPU] = resource.MustParse(fmt.Sprintf("%dm", int(float64(AverageUsageCPU)*r.CPUFactor)))
166166
PodChange = true
167167
}
168168
case "min":
169169
if r.ValidateCPU(currentC.CPU, c.MinCPU) {
170-
pod.Spec.Containers[i].Resources.Requests[v1.ResourceCPU] = resource.MustParse(fmt.Sprintf("%dm", c.MinCPU))
170+
pod.Spec.Containers[i].Resources.Requests[v1.ResourceCPU] = resource.MustParse(fmt.Sprintf("%dm", int(float64(c.MinCPU)*r.CPUFactor)))
171171
PodChange = true
172172
}
173173
case "max":
174174
if r.ValidateCPU(currentC.CPU, c.MaxCPU) {
175-
pod.Spec.Containers[i].Resources.Requests[v1.ResourceCPU] = resource.MustParse(fmt.Sprintf("%dm", c.MaxCPU))
175+
pod.Spec.Containers[i].Resources.Requests[v1.ResourceCPU] = resource.MustParse(fmt.Sprintf("%dm", int(float64(c.MaxCPU)*r.CPUFactor)))
176176
PodChange = true
177177
}
178178
}
@@ -182,17 +182,17 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
182182
switch r.GetPodMode(pod, ctx) {
183183
case "average":
184184
if r.ValidateMemory(currentC.Memory, AverageUsageMemory) {
185-
pod.Spec.Containers[i].Resources.Requests[v1.ResourceCPU] = resource.MustParse(fmt.Sprintf("%dm", AverageUsageMemory))
185+
pod.Spec.Containers[i].Resources.Requests[v1.ResourceCPU] = resource.MustParse(fmt.Sprintf("%dm", int(float64(AverageUsageMemory)*r.MemoryFactor)))
186186
PodChange = true
187187
}
188188
case "min":
189189
if r.ValidateMemory(currentC.Memory, c.MinMemory) {
190-
pod.Spec.Containers[i].Resources.Requests[v1.ResourceCPU] = resource.MustParse(fmt.Sprintf("%dm", c.MinMemory))
190+
pod.Spec.Containers[i].Resources.Requests[v1.ResourceCPU] = resource.MustParse(fmt.Sprintf("%dm", int(float64(c.MinMemory)*r.MemoryFactor)))
191191
PodChange = true
192192
}
193193
case "max":
194194
if r.ValidateMemory(currentC.Memory, c.MaxMemory) {
195-
pod.Spec.Containers[i].Resources.Requests[v1.ResourceCPU] = resource.MustParse(fmt.Sprintf("%dm", c.MaxMemory))
195+
pod.Spec.Containers[i].Resources.Requests[v1.ResourceCPU] = resource.MustParse(fmt.Sprintf("%dm", int(float64(c.MaxMemory)*r.MemoryFactor)))
196196
PodChange = true
197197
}
198198
}

controllers/pod_controller_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type PodReconciler struct {
2626
MaxCPU int64
2727
MinMemory int64
2828
MinCPU int64
29+
CPUFactor float64
30+
MemoryFactor float64
2931
}
3032

3133
type PodRequests struct {

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,16 @@ func main() {
6363
var minMemory int64
6464
var minCPU int64
6565

66+
var cpuFactor float64
67+
var memoryFactor float64
68+
6669
flag.BoolVar(&enableIncrease, "enable-increase", true, "Enables the controller to increase pod requests")
6770
flag.BoolVar(&enableReduce, "enable-reduce", true, "Enables the controller to reduce pod requests")
6871
flag.Int64Var(&maxMemory, "max-memory", 0, "Maximum memory in (Mi) that the controller can set a pod request to. 0 is infinite")
6972
flag.Int64Var(&maxCPU, "max-cpu", 0, "Maximum CPU in (m) that the controller can set a pod request to. 0 is infinite")
73+
flag.Float64Var(&cpuFactor, "cpu-factor", 1, "A factor to multiply CPU requests when reconciling. 1 By default.")
74+
flag.Float64Var(&memoryFactor, "memory-factor", 1, "A factor to multiply Memory requests when reconciling. 1 By default.")
75+
7076
flag.Int64Var(&minMemory, "min-memory", 0, "Minimum memory in (Mi) that the controller can set a pod request to. 0 is infinite")
7177
flag.Int64Var(&minCPU, "min-cpu", 0, "Minimum CPU in (m) that the controller can set a pod request to. 0 is infinite")
7278

@@ -132,6 +138,8 @@ func main() {
132138
MaxCPU: maxCPU,
133139
MinMemory: minMemory,
134140
MinCPU: minCPU,
141+
CPUFactor: cpuFactor,
142+
MemoryFactor: memoryFactor,
135143
}).SetupWithManager(mgr); err != nil {
136144
setupLog.Error(err, "unable to create controller", "controller", "Pod")
137145
log.Error(err, err.Error())

0 commit comments

Comments
 (0)