-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcalculate_values_from_labels.go
More file actions
112 lines (102 loc) · 4.07 KB
/
Copy pathcalculate_values_from_labels.go
File metadata and controls
112 lines (102 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// SPDX-FileCopyrightText: 2025 2025 INDUSTRIA DE DISEÑO TEXTIL S.A. (INDITEX S.A.)
// SPDX-FileContributor: enriqueavi@inditex.com
//
// SPDX-License-Identifier: Apache-2.0
// Package overcommit implements the core overcommit mutation logic.
package overcommit
import (
"context"
"github.com/InditexTech/k8s-overcommit-operator/internal/utils"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type overcommitResolution struct {
className string
cpuValue float64
memoryValue float64
ownerName string
ownerKind string
resolved bool
}
// getNamespaceOvercommit gets the overcommit values from the namespace label or falls back to the default class.
// Returns safe no-op values when any error occurs to avoid mutating pods incorrectly.
func getNamespaceOvercommit(ctx context.Context, pod *corev1.Pod, k8sClient client.Client, label, ownerName, ownerKind string) overcommitResolution {
// Get the namespace of the pod
namespaceName := pod.Namespace
var ns corev1.Namespace
err := k8sClient.Get(ctx, client.ObjectKey{Name: namespaceName}, &ns)
if err != nil {
podlog.Error(err, "Error getting the namespace", "namespace", namespaceName)
return overcommitResolution{cpuValue: 1.0, memoryValue: 1.0, ownerName: ownerName, ownerKind: ownerKind}
}
// Check if the overcommit class label is in the namespace
if val, ok := ns.Labels[label]; ok {
podlog.Info("Namespace class found", "class", val)
overcommitClass, err := utils.GetOvercommitClassSpec(ctx, val, k8sClient)
if err != nil {
podlog.Error(err, "Error getting the overcommit class", "overcommitClassLabel", val)
return overcommitResolution{cpuValue: 1.0, memoryValue: 1.0, ownerName: ownerName, ownerKind: ownerKind}
}
return overcommitResolution{
className: val,
cpuValue: overcommitClass.CpuOvercommit,
memoryValue: overcommitClass.MemoryOvercommit,
ownerName: ownerName,
ownerKind: ownerKind,
resolved: true,
}
}
podlog.Info("Overcommit class not found in the namespace, using the default", "namespace", ns.Name)
defaultClass, err := utils.GetDefaultClass(ctx, k8sClient)
if err != nil {
podlog.Error(err, "Error getting the default overcommit class")
return overcommitResolution{cpuValue: 1.0, memoryValue: 1.0, ownerName: ownerName, ownerKind: ownerKind}
}
return overcommitResolution{
className: defaultClass.Name,
cpuValue: defaultClass.Spec.CpuOvercommit,
memoryValue: defaultClass.Spec.MemoryOvercommit,
ownerName: ownerName,
ownerKind: ownerKind,
resolved: true,
}
}
func checkOvercommitType(ctx context.Context, pod corev1.Pod, client client.Client) overcommitResolution {
ownerName, ownerKind, err := utils.GetPodOwner(ctx, client, &pod)
if err != nil {
podlog.Error(err, "Error getting the pod owner")
// Non-fatal: continue with empty owner info
}
label, err := utils.GetOvercommitLabel(ctx, client)
if err != nil {
podlog.Error(err, "Error getting the overcommit label")
return overcommitResolution{cpuValue: 1.0, memoryValue: 1.0, ownerName: ownerName, ownerKind: ownerKind}
}
// Check if the pod has the overcommit class label
value, exists := pod.Labels[label]
podlog.Info(
"Checking if pod has overcommit class label",
"overcommitClassLabel", value,
"exists", exists,
)
if exists {
// Overcommit class found in pod
overcommitClass, err := utils.GetOvercommitClassSpec(ctx, value, client)
if err != nil {
podlog.Error(err, "Error getting the overcommit class", "overcommitClassLabel", value)
// Overcommit class not found or some error, fall back to namespace/default
return getNamespaceOvercommit(ctx, &pod, client, label, ownerName, ownerKind)
}
return overcommitResolution{
className: value,
cpuValue: overcommitClass.CpuOvercommit,
memoryValue: overcommitClass.MemoryOvercommit,
ownerName: ownerName,
ownerKind: ownerKind,
resolved: true,
}
}
// Overcommit class not found, checking the namespace
podlog.Info("Overcommit class label not found in pod, checking the namespace")
return getNamespaceOvercommit(ctx, &pod, client, label, ownerName, ownerKind)
}