@@ -9,22 +9,30 @@ package overcommit
99import (
1010 "context"
1111
12- "github.com/InditexTech/k8s-overcommit-operator/internal/metrics"
1312 "github.com/InditexTech/k8s-overcommit-operator/internal/utils"
1413 corev1 "k8s.io/api/core/v1"
1514 "sigs.k8s.io/controller-runtime/pkg/client"
1615)
1716
17+ type overcommitResolution struct {
18+ className string
19+ cpuValue float64
20+ memoryValue float64
21+ ownerName string
22+ ownerKind string
23+ resolved bool
24+ }
25+
1826// getNamespaceOvercommit gets the overcommit values from the namespace label or falls back to the default class.
19- // Returns (1.0, 1.0) as safe no-op values when any error occurs to avoid mutating pods incorrectly.
20- func getNamespaceOvercommit (ctx context.Context , pod * corev1.Pod , k8sClient client.Client , label , ownerName , ownerKind string ) ( float64 , float64 ) {
27+ // Returns safe no-op values when any error occurs to avoid mutating pods incorrectly.
28+ func getNamespaceOvercommit (ctx context.Context , pod * corev1.Pod , k8sClient client.Client , label , ownerName , ownerKind string ) overcommitResolution {
2129 // Get the namespace of the pod
2230 namespaceName := pod .Namespace
2331 var ns corev1.Namespace
2432 err := k8sClient .Get (ctx , client.ObjectKey {Name : namespaceName }, & ns )
2533 if err != nil {
2634 podlog .Error (err , "Error getting the namespace" , "namespace" , namespaceName )
27- return 1.0 , 1.0
35+ return overcommitResolution { cpuValue : 1.0 , memoryValue : 1.0 , ownerName : ownerName , ownerKind : ownerKind }
2836 }
2937
3038 // Check if the overcommit class label is in the namespace
@@ -33,23 +41,35 @@ func getNamespaceOvercommit(ctx context.Context, pod *corev1.Pod, k8sClient clie
3341 overcommitClass , err := utils .GetOvercommitClassSpec (ctx , val , k8sClient )
3442 if err != nil {
3543 podlog .Error (err , "Error getting the overcommit class" , "overcommitClassLabel" , val )
36- return 1.0 , 1.0
44+ return overcommitResolution {cpuValue : 1.0 , memoryValue : 1.0 , ownerName : ownerName , ownerKind : ownerKind }
45+ }
46+ return overcommitResolution {
47+ className : val ,
48+ cpuValue : overcommitClass .CpuOvercommit ,
49+ memoryValue : overcommitClass .MemoryOvercommit ,
50+ ownerName : ownerName ,
51+ ownerKind : ownerKind ,
52+ resolved : true ,
3753 }
38- metrics .K8sOvercommitPodMutated .WithLabelValues (val , ownerKind , ownerName , pod .Namespace ).Inc ()
39- return overcommitClass .CpuOvercommit , overcommitClass .MemoryOvercommit
4054 }
4155
4256 podlog .Info ("Overcommit class not found in the namespace, using the default" , "namespace" , ns .Name )
43- defaultClass , err := utils .GetDefaultSpec (ctx , k8sClient )
57+ defaultClass , err := utils .GetDefaultClass (ctx , k8sClient )
4458 if err != nil {
4559 podlog .Error (err , "Error getting the default overcommit class" )
46- return 1.0 , 1.0
60+ return overcommitResolution {cpuValue : 1.0 , memoryValue : 1.0 , ownerName : ownerName , ownerKind : ownerKind }
61+ }
62+ return overcommitResolution {
63+ className : defaultClass .Name ,
64+ cpuValue : defaultClass .Spec .CpuOvercommit ,
65+ memoryValue : defaultClass .Spec .MemoryOvercommit ,
66+ ownerName : ownerName ,
67+ ownerKind : ownerKind ,
68+ resolved : true ,
4769 }
48- metrics .K8sOvercommitPodMutated .WithLabelValues ("default" , ownerKind , ownerName , pod .Namespace ).Inc ()
49- return defaultClass .CpuOvercommit , defaultClass .MemoryOvercommit
5070}
5171
52- func checkOvercommitType (ctx context.Context , pod corev1.Pod , client client.Client ) ( float64 , float64 ) {
72+ func checkOvercommitType (ctx context.Context , pod corev1.Pod , client client.Client ) overcommitResolution {
5373 ownerName , ownerKind , err := utils .GetPodOwner (ctx , client , & pod )
5474 if err != nil {
5575 podlog .Error (err , "Error getting the pod owner" )
@@ -59,7 +79,7 @@ func checkOvercommitType(ctx context.Context, pod corev1.Pod, client client.Clie
5979 label , err := utils .GetOvercommitLabel (ctx , client )
6080 if err != nil {
6181 podlog .Error (err , "Error getting the overcommit label" )
62- return 1.0 , 1.0
82+ return overcommitResolution { cpuValue : 1.0 , memoryValue : 1.0 , ownerName : ownerName , ownerKind : ownerKind }
6383 }
6484 // Check if the pod has the overcommit class label
6585 value , exists := pod .Labels [label ]
@@ -76,8 +96,14 @@ func checkOvercommitType(ctx context.Context, pod corev1.Pod, client client.Clie
7696 // Overcommit class not found or some error, fall back to namespace/default
7797 return getNamespaceOvercommit (ctx , & pod , client , label , ownerName , ownerKind )
7898 }
79- metrics .K8sOvercommitPodMutated .WithLabelValues (value , ownerKind , ownerName , pod .Namespace ).Inc ()
80- return overcommitClass .CpuOvercommit , overcommitClass .MemoryOvercommit
99+ return overcommitResolution {
100+ className : value ,
101+ cpuValue : overcommitClass .CpuOvercommit ,
102+ memoryValue : overcommitClass .MemoryOvercommit ,
103+ ownerName : ownerName ,
104+ ownerKind : ownerKind ,
105+ resolved : true ,
106+ }
81107 }
82108
83109 // Overcommit class not found, checking the namespace
0 commit comments