1- // SPDX-FileCopyrightText: 2025 2025 INDUSTRIA DE DISEÑO TEXTIL S.A. (INDITEX S.A.)
2- // SPDX-FileContributor: enriqueavi@inditex.com
3- //
4- // SPDX-License-Identifier: Apache-2.0
5-
61package overcommit
72
83import (
94 "context"
105 "os"
116
127 "github.com/InditexTech/k8s-overcommit-operator/internal/metrics"
13-
148 corev1 "k8s.io/api/core/v1"
159 "k8s.io/apimachinery/pkg/api/resource"
1610 "k8s.io/client-go/tools/record"
@@ -24,100 +18,76 @@ func mutateContainers(containers []corev1.Container, pod *corev1.Pod, cpuValue f
2418 for i , container := range containers {
2519 limits := container .Resources .Limits
2620 requests := container .Resources .Requests
27- // If the container doesn't have limits, don't mutate the container
21+ if requests == nil {
22+ requests = corev1.ResourceList {}
23+ }
24+
2825 if limits == nil {
29- podlog .Info (
30- "Limits is nil, don't mutate the container" ,
31- "containerName" , container .Name , "generateName" , pod .GenerateName ,
32- )
33- } else if cpuValue == 1 && memoryValue == 1 {
34- podlog .Info (
35- "Container didn't mutate the cpu and memory" ,
36- "generateName" , pod .GenerateName , "cpuValue" , cpuValue , "memoryValue" , memoryValue ,
37- )
38- } else {
39- if cpuLimit , ok := limits [corev1 .ResourceCPU ]; ok {
40- // If the cpu overcommit value is 1, don't mutate the container
41- if cpuValue == 1 {
42- podlog .Info (
43- "Container didn't mutate the cpu" ,
44- "generateName" , pod .GenerateName , "cpuValue" , cpuValue ,
45- )
46- }
47- newCPURequest := float64 (cpuLimit .MilliValue ()) * cpuValue
48- requests [corev1 .ResourceCPU ] = * resource .NewMilliQuantity (int64 (newCPURequest ), resource .DecimalSI )
49- }
50- if memoryLimit , ok := limits [corev1 .ResourceMemory ]; ok {
51- // If the memory overcommit value is 1, don't mutate the container
52- if memoryValue == 1 {
53- podlog .Info (
54- "Container didn't mutate the memory" ,
55- "generateName" , pod .GenerateName , "memoryValue" , memoryValue ,
56- )
57- }
58- newMemoryRequest := float64 (memoryLimit .Value ()) * memoryValue
59- requests [corev1 .ResourceMemory ] = * resource .NewQuantity (int64 (newMemoryRequest ), resource .BinarySI )
60- }
61- containers [i ].Resources .Requests = requests
26+ continue
6227 }
63- }
64- }
6528
66- func makeOvercommit (pod * corev1.Pod , cpuValue float64 , memoryValue float64 ) {
67- mutateContainers (pod .Spec .Containers , pod , cpuValue , memoryValue )
68- podlog .Info (
69- "Containers mutated" , "generateName" , pod .GenerateName , "cpuValue" , cpuValue , "memoryValue" , memoryValue ,
70- )
71- }
29+ if cpuLimit , ok := limits [corev1 .ResourceCPU ]; ok && cpuValue != 1 {
30+ newCPURequest := float64 (cpuLimit .MilliValue ()) * cpuValue
31+ requests [corev1 .ResourceCPU ] = * resource .NewMilliQuantity (int64 (newCPURequest ), resource .DecimalSI )
32+ }
7233
73- func makeOvercommitInitContainers (pod * corev1.Pod , cpuValue float64 , memoryValue float64 ) {
74- mutateContainers (pod .Spec .InitContainers , pod , cpuValue , memoryValue )
75- podlog .Info (
76- "InitContainers mutated" , "generateName" , pod .GenerateName , "cpuValue" , cpuValue , "memoryValue" , memoryValue ,
77- )
34+ if memoryLimit , ok := limits [corev1 .ResourceMemory ]; ok && memoryValue != 1 {
35+ newMemoryRequest := float64 (memoryLimit .Value ()) * memoryValue
36+ requests [corev1 .ResourceMemory ] = * resource .NewQuantity (int64 (newMemoryRequest ), resource .BinarySI )
37+ }
38+
39+ containers [i ].Resources .Requests = requests
40+ }
7841}
7942
8043func Overcommit (pod * corev1.Pod , recorder record.EventRecorder , client client.Client ) {
8144 ctx := context .Background ()
82- podlog . Info ( "Mutating Pod" , "generateGame" , pod . GenerateName )
45+
8346 metrics .K8sOvercommitOperatorPodsRequestedTotal .WithLabelValues (os .Getenv ("OVERCOMMIT_CLASS_NAME" )).Inc ()
8447
85- // Get the overcommit values from the labels
8648 cpuValue , memoryValue := checkOvercommitType (ctx , * pod , client )
87- // Check if the values are valid
8849
89- // Multiplicate the limits by the overcommit value and set the new value as request
90- makeOvercommit (pod , cpuValue , memoryValue )
91- podlog .Info (
92- "Pod mutated" , "generateName" , pod .GenerateName , "cpuValue" , cpuValue , "memoryValue" , memoryValue ,
93- )
50+ mutateContainers (pod .Spec .Containers , pod , cpuValue , memoryValue )
9451
95- // If it has initContainers, make the overcommit
96- podlog .Info ("cheking if pod has initContainer" )
52+ // comportamiento actual para CREATE/UPDATE normales
9753 if len (pod .Spec .InitContainers ) > 0 {
98- podlog .Info ("Pod has initContainers, mutating them" , "generateName" , pod .GenerateName )
99- makeOvercommitInitContainers (pod , cpuValue , memoryValue )
54+ mutateContainers (pod .Spec .InitContainers , pod , cpuValue , memoryValue )
10055 }
10156
102- // Increment the metric K8sOvercommitOperatorMutatedPodsTotal
10357 metrics .K8sOvercommitOperatorMutatedPodsTotal .WithLabelValues (os .Getenv ("OVERCOMMIT_CLASS_NAME" )).Inc ()
10458
105- // Add an event to the pod
10659 recorder .Eventf (
10760 pod ,
10861 corev1 .EventTypeNormal ,
10962 "OvercommitApplied" ,
110- "Applied overcommit to containers of Pod '%s': OvercommitClass = %s, CPU Overcommit = %.2f, Memory Overcommit = %.2f" ,
63+ "Applied overcommit to Pod '%s': OvercommitClass = %s, CPU Overcommit = %.2f, Memory Overcommit = %.2f" ,
11164 pod .Name ,
11265 os .Getenv ("OVERCOMMIT_CLASS_NAME" ),
11366 cpuValue ,
11467 memoryValue ,
11568 )
116- if cpuValue == 1 && memoryValue == 1 {
117- metrics .K8sOvercommitOperatorPodsNotMutatedTotal .WithLabelValues (
118- os .Getenv ("OVERCOMMIT_CLASS_NAME" ), pod .GenerateName , pod .Namespace , "overcommit values = 1" ,
119- ).Inc ()
69+ }
12070
121- }
122- podlog .Info ("Pod mutated" , "generateName" , pod .GenerateName , "cpuValue" , cpuValue , "memoryValue" , memoryValue )
71+ func OvercommitOnResize (pod * corev1.Pod , recorder record.EventRecorder , client client.Client ) {
72+ ctx := context .Background ()
73+
74+ metrics .K8sOvercommitOperatorPodsRequestedTotal .WithLabelValues (os .Getenv ("OVERCOMMIT_CLASS_NAME" )).Inc ()
75+
76+ cpuValue , memoryValue := checkOvercommitType (ctx , * pod , client )
77+
78+ // En resize: solo containers normales.
79+ mutateContainers (pod .Spec .Containers , pod , cpuValue , memoryValue )
80+
81+ metrics .K8sOvercommitOperatorMutatedPodsTotal .WithLabelValues (os .Getenv ("OVERCOMMIT_CLASS_NAME" )).Inc ()
82+
83+ recorder .Eventf (
84+ pod ,
85+ corev1 .EventTypeNormal ,
86+ "OvercommitAppliedOnResize" ,
87+ "Applied overcommit on resize to Pod '%s': OvercommitClass = %s, CPU Overcommit = %.2f, Memory Overcommit = %.2f" ,
88+ pod .Name ,
89+ os .Getenv ("OVERCOMMIT_CLASS_NAME" ),
90+ cpuValue ,
91+ memoryValue ,
92+ )
12393}
0 commit comments