-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmake_overcommit.go
More file actions
140 lines (115 loc) · 4.8 KB
/
make_overcommit.go
File metadata and controls
140 lines (115 loc) · 4.8 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// 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
import (
"context"
"fmt"
"os"
"github.com/InditexTech/k8s-overcommit-operator/internal/metrics"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)
const (
// AnnotationOvercommitApplied is set on pods after overcommit mutation to ensure idempotency.
AnnotationOvercommitApplied = "overcommit.inditex.dev/applied"
)
var podlog = logf.Log.WithName("overcommit")
func mutateContainers(containers []corev1.Container, cpuValue float64, memoryValue float64) {
for i, container := range containers {
limits := container.Resources.Limits
requests := container.Resources.Requests
if requests == nil {
requests = corev1.ResourceList{}
}
if limits == nil {
continue
}
if cpuLimit, ok := limits[corev1.ResourceCPU]; ok && cpuValue != 1 {
newCPURequest := float64(cpuLimit.MilliValue()) * cpuValue
requests[corev1.ResourceCPU] = *resource.NewMilliQuantity(int64(newCPURequest), resource.DecimalSI)
}
if memoryLimit, ok := limits[corev1.ResourceMemory]; ok && memoryValue != 1 {
newMemoryRequest := float64(memoryLimit.Value()) * memoryValue
requests[corev1.ResourceMemory] = *resource.NewQuantity(int64(newMemoryRequest), resource.BinarySI)
}
containers[i].Resources.Requests = requests
}
}
func Overcommit(ctx context.Context, pod *corev1.Pod, recorder record.EventRecorder, client client.Client) {
webhookClassName := os.Getenv("OVERCOMMIT_CLASS_NAME")
resolution := checkOvercommitType(ctx, *pod, client)
className := resolution.className
if className == "" {
className = webhookClassName
}
metrics.K8sOvercommitOperatorPodsRequestedTotal.WithLabelValues(className).Inc()
// Idempotency: skip if this pod was already mutated by this class
if pod.Annotations != nil {
if applied, ok := pod.Annotations[AnnotationOvercommitApplied]; ok && applied == className {
podlog.Info("Pod already mutated by this overcommit class, skipping", "pod", pod.Name, "class", className)
return
}
}
mutateContainers(pod.Spec.Containers, resolution.cpuValue, resolution.memoryValue)
// Also mutate init containers on regular CREATE/UPDATE
if len(pod.Spec.InitContainers) > 0 {
mutateContainers(pod.Spec.InitContainers, resolution.cpuValue, resolution.memoryValue)
}
// Mark the pod as mutated to prevent double-application on reinvocation
setOvercommitAnnotation(pod, className, resolution.cpuValue, resolution.memoryValue)
metrics.K8sOvercommitOperatorMutatedPodsTotal.WithLabelValues(className).Inc()
if resolution.resolved {
metrics.K8sOvercommitPodMutated.WithLabelValues(className, resolution.ownerKind, resolution.ownerName, pod.Namespace).Inc()
}
recorder.Eventf(
pod,
corev1.EventTypeNormal,
"OvercommitApplied",
"Applied overcommit to Pod '%s': OvercommitClass = %s, CPU Overcommit = %.2f, Memory Overcommit = %.2f",
pod.Name,
className,
resolution.cpuValue,
resolution.memoryValue,
)
}
func OvercommitOnResize(ctx context.Context, pod *corev1.Pod, recorder record.EventRecorder, client client.Client) {
webhookClassName := os.Getenv("OVERCOMMIT_CLASS_NAME")
resolution := checkOvercommitType(ctx, *pod, client)
className := resolution.className
if className == "" {
className = webhookClassName
}
metrics.K8sOvercommitOperatorPodsRequestedTotal.WithLabelValues(className).Inc()
// On resize: only mutate regular containers, skip init containers.
mutateContainers(pod.Spec.Containers, resolution.cpuValue, resolution.memoryValue)
// Update annotation with new values after resize
setOvercommitAnnotation(pod, className, resolution.cpuValue, resolution.memoryValue)
metrics.K8sOvercommitOperatorMutatedPodsTotal.WithLabelValues(className).Inc()
if resolution.resolved {
metrics.K8sOvercommitPodMutated.WithLabelValues(className, resolution.ownerKind, resolution.ownerName, pod.Namespace).Inc()
}
recorder.Eventf(
pod,
corev1.EventTypeNormal,
"OvercommitAppliedOnResize",
"Applied overcommit on resize to Pod '%s': OvercommitClass = %s, CPU Overcommit = %.2f, Memory Overcommit = %.2f",
pod.Name,
className,
resolution.cpuValue,
resolution.memoryValue,
)
}
// setOvercommitAnnotation marks the pod as having been mutated by the overcommit webhook.
func setOvercommitAnnotation(pod *corev1.Pod, className string, cpuValue, memoryValue float64) {
if pod.Annotations == nil {
pod.Annotations = make(map[string]string)
}
pod.Annotations[AnnotationOvercommitApplied] = className
pod.Annotations["overcommit.inditex.dev/cpu"] = fmt.Sprintf("%.4f", cpuValue)
pod.Annotations["overcommit.inditex.dev/memory"] = fmt.Sprintf("%.4f", memoryValue)
}