@@ -26,10 +26,10 @@ import (
26
26
v1 "k8s.io/api/core/v1"
27
27
apierrors "k8s.io/apimachinery/pkg/api/errors"
28
28
"k8s.io/apimachinery/pkg/api/resource"
29
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30
29
"k8s.io/client-go/tools/cache"
31
30
32
31
ctrl "sigs.k8s.io/controller-runtime"
32
+ "sigs.k8s.io/controller-runtime/pkg/client"
33
33
)
34
34
35
35
// +kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch;update;patch
@@ -77,7 +77,6 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
77
77
}
78
78
79
79
if ((! r .EnableAnnotation ) || (r .EnableAnnotation && annotation )) && ! ignoreAnnotation {
80
-
81
80
data , err := r .ClientSet .RESTClient ().Get ().AbsPath (fmt .Sprintf ("apis/metrics.k8s.io/v1beta1/namespaces/%v/pods/%v" , pod .Namespace , pod .Name )).DoRaw (ctx )
82
81
83
82
if err != nil {
@@ -93,20 +92,12 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
93
92
LatestPodRequest , err := fetchFromCache (cacheStore , pod .Name )
94
93
if err != nil {
95
94
SumPodRequest .Sample = 0
96
- SumPodRequest .TimeSinceFirstSample = 0
97
- SumPodRequest .Timestamp = time .Now ()
98
95
log .Info (fmt .Sprint ("Adding cache sample " , SumPodRequest .Sample ))
99
96
addToCache (cacheStore , SumPodRequest )
100
97
log .Info (fmt .Sprint ("Items in Cache: " , len (cacheStore .List ())))
101
98
} else {
102
-
99
+ log . Info ( fmt . Sprint ( "Items in Cache: " , len ( cacheStore . List ())))
103
100
SumPodRequest .Sample = LatestPodRequest .Sample + 1
104
- if LatestPodRequest .Sample == 1 {
105
- SumPodRequest .Timestamp = time .Now ()
106
- LatestPodRequest .Timestamp = SumPodRequest .Timestamp
107
- }
108
- log .Info (fmt .Sprint (time .Now (), LatestPodRequest .Timestamp ))
109
- SumPodRequest .TimeSinceFirstSample = time .Since (LatestPodRequest .Timestamp ).Seconds ()
110
101
111
102
log .Info (fmt .Sprint ("Updating cache sample " , SumPodRequest .Sample ))
112
103
@@ -143,7 +134,7 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
143
134
}
144
135
}
145
136
log .Info (fmt .Sprint (SumPodRequest ))
146
- if (SumPodRequest .Sample >= r .SampleSize ) && ( SumPodRequest . TimeSinceFirstSample >= r . MinSecondsBetweenPodRestart ) {
137
+ if (SumPodRequest .Sample >= r .SampleSize ) && r . MinimumUptimeOfPodInParent ( pod , ctx ) {
147
138
log .Info ("Sample Size and Minimum Time have been reached" )
148
139
PodChange := false
149
140
Requests := []NewContainerRequests {}
@@ -155,49 +146,52 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
155
146
if currentC .Name == c .Name {
156
147
for i , v := range pod .Spec .Containers {
157
148
if v .Name == c .Name {
158
- log .Info (c .Name )
159
- log .Info (fmt .Sprint ("Comparing CPU: " , fmt .Sprintf ("%dm" , AverageUsageCPU ), " <> " , fmt .Sprintf ("%dm" , currentC .CPU )))
160
- log .Info (fmt .Sprint ("Comparing Memory: " , fmt .Sprintf ("%dMi" , AverageUsageMemory ), " <> " , fmt .Sprintf ("%dMi" , currentC .Memory )))
149
+ if AverageUsageCPU < r .MinCPU && (r .MinCPU > 0 ) {
150
+ AverageUsageCPU = r .MinCPU
151
+ }
152
+ if AverageUsageCPU > r .MaxCPU && (r .MaxCPU > 0 ) {
153
+ AverageUsageCPU = r .MaxCPU
154
+ }
155
+ if AverageUsageMemory < r .MinMemory && (r .MinMemory > 0 ) {
156
+ AverageUsageMemory = r .MinMemory
157
+ }
158
+ if AverageUsageMemory > r .MaxMemory && (r .MaxMemory > 0 ) {
159
+ AverageUsageMemory = r .MaxMemory
160
+ }
161
+ log .Info (fmt .Sprint (c .Name , " Comparing CPU: " , fmt .Sprintf ("%dm" , AverageUsageCPU ), " <> " , fmt .Sprintf ("%dm" , currentC .CPU )))
162
+ log .Info (fmt .Sprint (c .Name , " Comparing Memory: " , fmt .Sprintf ("%dMi" , AverageUsageMemory ), " <> " , fmt .Sprintf ("%dMi" , currentC .Memory )))
161
163
if pod .Spec .Containers [i ].Resources .Requests != nil {
162
164
switch r .GetPodMode (pod , ctx ) {
163
165
case "average" :
164
166
if r .ValidateCPU (currentC .CPU , AverageUsageCPU ) {
165
167
pod .Spec .Containers [i ].Resources .Requests [v1 .ResourceCPU ] = resource .MustParse (fmt .Sprintf ("%dm" , int (float64 (AverageUsageCPU )* r .CPUFactor )))
166
168
PodChange = true
167
169
}
170
+ if r .ValidateMemory (currentC .Memory , AverageUsageMemory ) {
171
+ pod .Spec .Containers [i ].Resources .Requests [v1 .ResourceMemory ] = resource .MustParse (fmt .Sprintf ("%dMi" , int (float64 (AverageUsageMemory )* r .MemoryFactor )))
172
+ PodChange = true
173
+ }
168
174
case "min" :
169
175
if r .ValidateCPU (currentC .CPU , c .MinCPU ) {
170
176
pod .Spec .Containers [i ].Resources .Requests [v1 .ResourceCPU ] = resource .MustParse (fmt .Sprintf ("%dm" , int (float64 (c .MinCPU )* r .CPUFactor )))
171
177
PodChange = true
172
178
}
179
+ if r .ValidateMemory (currentC .Memory , c .MinMemory ) {
180
+ pod .Spec .Containers [i ].Resources .Requests [v1 .ResourceMemory ] = resource .MustParse (fmt .Sprintf ("%dMi" , int (float64 (c .MinMemory )* r .MemoryFactor )))
181
+ PodChange = true
182
+ }
173
183
case "max" :
174
184
if r .ValidateCPU (currentC .CPU , c .MaxCPU ) {
175
185
pod .Spec .Containers [i ].Resources .Requests [v1 .ResourceCPU ] = resource .MustParse (fmt .Sprintf ("%dm" , int (float64 (c .MaxCPU )* r .CPUFactor )))
176
186
PodChange = true
177
187
}
178
- }
179
- }
180
- if AverageUsageMemory > 0 {
181
- if pod .Spec .Containers [i ].Resources .Requests != nil {
182
- switch r .GetPodMode (pod , ctx ) {
183
- case "average" :
184
- if r .ValidateMemory (currentC .Memory , AverageUsageMemory ) {
185
- pod .Spec .Containers [i ].Resources .Requests [v1 .ResourceCPU ] = resource .MustParse (fmt .Sprintf ("%dm" , int (float64 (AverageUsageMemory )* r .MemoryFactor )))
186
- PodChange = true
187
- }
188
- case "min" :
189
- if r .ValidateMemory (currentC .Memory , c .MinMemory ) {
190
- pod .Spec .Containers [i ].Resources .Requests [v1 .ResourceCPU ] = resource .MustParse (fmt .Sprintf ("%dm" , int (float64 (c .MinMemory )* r .MemoryFactor )))
191
- PodChange = true
192
- }
193
- case "max" :
194
- if r .ValidateMemory (currentC .Memory , c .MaxMemory ) {
195
- pod .Spec .Containers [i ].Resources .Requests [v1 .ResourceCPU ] = resource .MustParse (fmt .Sprintf ("%dm" , int (float64 (c .MaxMemory )* r .MemoryFactor )))
196
- PodChange = true
197
- }
188
+ if r .ValidateMemory (currentC .Memory , c .MaxMemory ) {
189
+ pod .Spec .Containers [i ].Resources .Requests [v1 .ResourceMemory ] = resource .MustParse (fmt .Sprintf ("%dMi" , int (float64 (c .MaxMemory )* r .MemoryFactor )))
190
+ PodChange = true
198
191
}
199
192
}
200
193
}
194
+
201
195
Requests = append (Requests , NewContainerRequests {Name : c .Name , Requests : pod .Spec .Containers [i ].Resources })
202
196
}
203
197
}
@@ -206,68 +200,25 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
206
200
}
207
201
if PodChange {
208
202
pod .Annotations ["reqsizer.jatalocks.github.io/changed" ] = "true"
203
+
209
204
log .Info ("Pod Requests Will Change" )
210
205
211
206
if len (pod .OwnerReferences ) == 0 {
212
207
log .Info ("Pod has no owner" )
213
208
return r .UpdateKubeObject (& pod , ctx )
214
209
}
215
210
216
- var ownerName string
217
- switch pod .OwnerReferences [0 ].Kind {
218
- case "ReplicaSet" :
219
- replica , err := r .ClientSet .AppsV1 ().ReplicaSets (pod .Namespace ).Get (ctx , pod .OwnerReferences [0 ].Name , metav1.GetOptions {})
220
- if err != nil {
221
- log .Error (err , err .Error ())
222
- return ctrl.Result {}, err
223
- }
224
-
225
- ownerName = replica .OwnerReferences [0 ].Name
226
- if replica .OwnerReferences [0 ].Kind == "Deployment" {
227
- log .Info ("Is Owned by Deployment" )
228
- deployment , err := r .ClientSet .AppsV1 ().Deployments (pod .Namespace ).Get (ctx , ownerName , metav1.GetOptions {})
229
- if err != nil {
230
- log .Error (err , err .Error ())
231
- return ctrl.Result {}, err
232
- }
233
- UpdatePodController (& deployment .Spec .Template .Spec , Requests , ctx )
234
- deployment .Annotations ["reqsizer.jatalocks.github.io/changed" ] = "true"
211
+ err , podSpec , deployment , _ := r .GetPodParentKind (pod , ctx )
212
+ if err != nil {
213
+ return ctrl.Result {}, nil
214
+ }
235
215
236
- return r .UpdateKubeObject (deployment , ctx )
237
- } else {
238
- log .Info ("Is Owned by Unknown CRD" )
239
- return ctrl.Result {}, nil
240
- }
241
- case "DaemonSet" :
242
- log .Info ("Is Owned by DaemonSet" )
243
- ownerName = pod .OwnerReferences [0 ].Name
244
-
245
- deployment , err := r .ClientSet .AppsV1 ().DaemonSets (pod .Namespace ).Get (ctx , ownerName , metav1.GetOptions {})
246
- if err != nil {
247
- log .Error (err , err .Error ())
248
- return ctrl.Result {}, err
249
- }
250
- UpdatePodController (& deployment .Spec .Template .Spec , Requests , ctx )
251
- deployment .Annotations ["reqsizer.jatalocks.github.io/changed" ] = "true"
252
- return r .UpdateKubeObject (deployment , ctx )
253
- case "StatefulSet" :
254
- log .Info ("Is Owned by StatefulSet" )
255
- ownerName = pod .OwnerReferences [0 ].Name
256
-
257
- deployment , err := r .ClientSet .AppsV1 ().StatefulSets (pod .Namespace ).Get (ctx , ownerName , metav1.GetOptions {})
258
- if err != nil {
259
- log .Error (err , err .Error ())
260
- return ctrl.Result {}, err
261
- }
216
+ UpdatePodController (podSpec , Requests , ctx )
262
217
263
- UpdatePodController (& deployment .Spec .Template .Spec , Requests , ctx )
264
- deployment .Annotations ["reqsizer.jatalocks.github.io/changed" ] = "true"
265
- return r .UpdateKubeObject (deployment , ctx )
266
- default :
267
- fmt .Printf ("Could not find resource manager for type %s\n " , pod .OwnerReferences [0 ].Kind )
268
- }
218
+ return r .UpdateKubeObject (deployment .(client.Object ), ctx )
269
219
270
220
}
221
+
271
222
err := deleteFromCache (cacheStore , SumPodRequest )
272
223
if err != nil {
273
224
log .Error (err , err .Error ())
0 commit comments