Skip to content

Commit e3874f8

Browse files
authored
Merge pull request #55 from kubescape/bugfix/mem-leak
Adding access by index
2 parents 5e60256 + 0018d33 commit e3874f8

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

pkg/controller/controller.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -234,32 +234,32 @@ func (c *Controller) handleApplicationProfile(applicationProfileUnstructured *un
234234
containersMap := make(map[string]collector.ContainerProfile)
235235

236236
// Merge all the container information of all the pods
237-
for _, pod := range pods.Items {
238-
appProfileNameForPod := fmt.Sprintf("pod-%s", pod.GetName())
239-
typedObj, err := c.dynamicClient.Resource(collector.AppProfileGvr).Namespace(pod.GetNamespace()).Get(context.TODO(), appProfileNameForPod, metav1.GetOptions{})
237+
for i := 0; i < len(pods.Items); i++ {
238+
appProfileNameForPod := fmt.Sprintf("pod-%s", pods.Items[i].GetName())
239+
typedObj, err := c.dynamicClient.Resource(collector.AppProfileGvr).Namespace(pods.Items[i].GetNamespace()).Get(context.TODO(), appProfileNameForPod, metav1.GetOptions{})
240240
if err != nil {
241-
log.Printf("ApplicationProfile for pod %v doesn't exist", pod.GetName())
241+
log.Printf("ApplicationProfile for pod %v doesn't exist", pods.Items[i].GetName())
242242
return
243243
}
244244
podApplicationProfileObj, err := getApplicationProfileFromUnstructured(typedObj)
245245
if err != nil {
246-
log.Printf("ApplicationProfile for pod %v doesn't exist", pod.GetName())
246+
log.Printf("ApplicationProfile for pod %v doesn't exist", pods.Items[i].GetName())
247247
return
248248
}
249249

250250
// TODO: Make this code more efficient and less repetitive.
251-
for _, container := range podApplicationProfileObj.Spec.Containers {
251+
for containerIndex := 0; containerIndex < len(podApplicationProfileObj.Spec.Containers); containerIndex++ {
252252
// Merge containers
253-
if mapContainer, exists := containersMap[container.Name]; exists {
253+
if mapContainer, exists := containersMap[podApplicationProfileObj.Spec.Containers[containerIndex].Name]; exists {
254254
// Merge SysCalls
255-
for _, sysCall := range container.SysCalls {
255+
for _, sysCall := range podApplicationProfileObj.Spec.Containers[containerIndex].SysCalls {
256256
if !slices.Contains(mapContainer.SysCalls, sysCall) {
257257
mapContainer.SysCalls = append(mapContainer.SysCalls, sysCall)
258258
}
259259
}
260260

261261
// Merge Execs
262-
for _, exec := range container.Execs {
262+
for _, exec := range podApplicationProfileObj.Spec.Containers[containerIndex].Execs {
263263
contains := false
264264
for _, mapExec := range mapContainer.Execs {
265265
if mapExec.Equals(exec) {
@@ -273,7 +273,7 @@ func (c *Controller) handleApplicationProfile(applicationProfileUnstructured *un
273273
}
274274

275275
// Merge Capabilities
276-
for _, capability := range container.Capabilities {
276+
for _, capability := range podApplicationProfileObj.Spec.Containers[containerIndex].Capabilities {
277277
contains := false
278278
for _, mapCapability := range mapContainer.Capabilities {
279279
if mapCapability.Equals(capability) {
@@ -287,7 +287,7 @@ func (c *Controller) handleApplicationProfile(applicationProfileUnstructured *un
287287
}
288288

289289
// Merge Opens
290-
for _, open := range container.Opens {
290+
for _, open := range podApplicationProfileObj.Spec.Containers[containerIndex].Opens {
291291
contains := false
292292
for _, mapOpen := range mapContainer.Opens {
293293
if mapOpen.Equals(open) {
@@ -301,7 +301,7 @@ func (c *Controller) handleApplicationProfile(applicationProfileUnstructured *un
301301
}
302302

303303
// Merge Dns
304-
for _, dns := range container.Dns {
304+
for _, dns := range podApplicationProfileObj.Spec.Containers[containerIndex].Dns {
305305
contains := false
306306
for _, mapDns := range mapContainer.Dns {
307307
if mapDns.Equals(dns) {
@@ -315,7 +315,7 @@ func (c *Controller) handleApplicationProfile(applicationProfileUnstructured *un
315315
}
316316

317317
// Merge Network
318-
for _, network := range container.NetworkActivity.Incoming {
318+
for _, network := range podApplicationProfileObj.Spec.Containers[containerIndex].NetworkActivity.Incoming {
319319
contains := false
320320
for _, mapNetwork := range mapContainer.NetworkActivity.Incoming {
321321
if mapNetwork.Equals(network) {
@@ -328,7 +328,7 @@ func (c *Controller) handleApplicationProfile(applicationProfileUnstructured *un
328328
}
329329
}
330330

331-
for _, network := range container.NetworkActivity.Outgoing {
331+
for _, network := range podApplicationProfileObj.Spec.Containers[containerIndex].NetworkActivity.Outgoing {
332332
contains := false
333333
for _, mapNetwork := range mapContainer.NetworkActivity.Outgoing {
334334
if mapNetwork.Equals(network) {
@@ -341,9 +341,9 @@ func (c *Controller) handleApplicationProfile(applicationProfileUnstructured *un
341341
}
342342
}
343343

344-
containersMap[container.Name] = mapContainer
344+
containersMap[podApplicationProfileObj.Spec.Containers[containerIndex].Name] = mapContainer
345345
} else {
346-
containersMap[container.Name] = container
346+
containersMap[podApplicationProfileObj.Spec.Containers[containerIndex].Name] = podApplicationProfileObj.Spec.Containers[containerIndex]
347347
}
348348
}
349349
}

0 commit comments

Comments
 (0)