Skip to content

Commit f23f521

Browse files
author
Ricardo Lüders
authored
feat: limit number of containers per namespace (openshift#557)
* feat: limit number of containers per namespace * refactor: change gaher_log helper to return array of errors * style: lint lll * Revert "refactor: change gaher_log helper to return array of errors" This reverts commit cef3813. * refactor: avoiding error log flood to gather_log * style: making lint happy * refactor: hardcode the limit of containers per namespace * fix: breaking loop and better container couting * refactor: increasing container limit per namespace
1 parent 6158575 commit f23f521

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

pkg/gatherers/common/gather_logs.go

+44-21
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type LogContainersFilter struct {
2323
LabelSelector string
2424
FieldSelector string
2525
ContainerNameRegexFilter string
26+
MaxNamespaceContainers int
2627
}
2728

2829
// LogMessagesFilter allows you to filter messages
@@ -40,6 +41,7 @@ type LogMessagesFilter struct {
4041
// - namespace in which to search for pods
4142
// - labelSelector to filter pods by their labels (keep empty to not filter)
4243
// - containerNameRegexFilter to filter containers in the pod (keep empty to not filter)
44+
// - maxNamespaceContainers to limit the containers in the given namespace (keep empty to not limit)
4345
// - logMessagesFilter allows you to specify
4446
// - messagesToSearch to filter the logs by substrings (case-insensitive)
4547
// or regex (add `(?i)` in the beginning to make search case-insensitive). Leave nil to not filter.
@@ -73,6 +75,7 @@ func CollectLogsFromContainers( //nolint:gocyclo
7375
return nil, err
7476
}
7577

78+
var skippedContainers int
7679
var records []record.Record
7780

7881
for i := range pods.Items {
@@ -84,6 +87,12 @@ func CollectLogsFromContainers( //nolint:gocyclo
8487
containerNames = append(containerNames, pods.Items[i].Spec.InitContainers[j].Name)
8588
}
8689

90+
containersLimited := containersFilter.MaxNamespaceContainers > 0 && len(records) >= containersFilter.MaxNamespaceContainers
91+
if containersLimited {
92+
skippedContainers += len(containerNames)
93+
continue
94+
}
95+
8796
pod := &pods.Items[i]
8897

8998
for _, containerName := range containerNames {
@@ -97,29 +106,12 @@ func CollectLogsFromContainers( //nolint:gocyclo
97106
}
98107
}
99108

100-
sinceSeconds := &messagesFilter.SinceSeconds
101-
if messagesFilter.SinceSeconds == 0 {
102-
sinceSeconds = nil
109+
if containersLimited {
110+
skippedContainers = len(containerNames) - containersFilter.MaxNamespaceContainers
111+
break
103112
}
104113

105-
limitBytes := &messagesFilter.LimitBytes
106-
if messagesFilter.LimitBytes == 0 {
107-
limitBytes = nil
108-
}
109-
110-
tailLines := &messagesFilter.TailLines
111-
if messagesFilter.TailLines == 0 {
112-
tailLines = nil
113-
}
114-
115-
request := coreClient.Pods(containersFilter.Namespace).GetLogs(pod.Name, &corev1.PodLogOptions{
116-
Container: containerName,
117-
SinceSeconds: sinceSeconds,
118-
LimitBytes: limitBytes,
119-
TailLines: tailLines,
120-
Previous: messagesFilter.Previous,
121-
Timestamps: true,
122-
})
114+
request := coreClient.Pods(containersFilter.Namespace).GetLogs(pod.Name, podLogOptions(containerName, messagesFilter))
123115

124116
logs, err := filterLogs(ctx, request, messagesFilter.MessagesToSearch, messagesFilter.IsRegexSearch)
125117
if err != nil {
@@ -139,6 +131,11 @@ func CollectLogsFromContainers( //nolint:gocyclo
139131
klog.Infof("no pods in %v namespace were found", containersFilter.Namespace)
140132
}
141133

134+
if skippedContainers > 0 {
135+
return records, fmt.Errorf("skipping %d containers on namespace %s (max: %d)",
136+
skippedContainers, containersFilter.Namespace, containersFilter.MaxNamespaceContainers)
137+
}
138+
142139
return records, nil
143140
}
144141

@@ -198,3 +195,29 @@ func FilterLogFromScanner(scanner *bufio.Scanner, messagesToSearch []string, reg
198195

199196
return strings.Join(result, "\n"), nil
200197
}
198+
199+
func podLogOptions(containerName string, messagesFilter LogMessagesFilter) *corev1.PodLogOptions {
200+
sinceSeconds := &messagesFilter.SinceSeconds
201+
if messagesFilter.SinceSeconds == 0 {
202+
sinceSeconds = nil
203+
}
204+
205+
limitBytes := &messagesFilter.LimitBytes
206+
if messagesFilter.LimitBytes == 0 {
207+
limitBytes = nil
208+
}
209+
210+
tailLines := &messagesFilter.TailLines
211+
if messagesFilter.TailLines == 0 {
212+
tailLines = nil
213+
}
214+
215+
return &corev1.PodLogOptions{
216+
Container: containerName,
217+
SinceSeconds: sinceSeconds,
218+
LimitBytes: limitBytes,
219+
TailLines: tailLines,
220+
Previous: messagesFilter.Previous,
221+
Timestamps: true,
222+
}
223+
}

pkg/gatherers/conditional/gather_logs_of_namespace.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ func (g *Gatherer) gatherLogsOfNamespace(ctx context.Context, namespace string,
5959
ctx,
6060
coreClient,
6161
common.LogContainersFilter{
62-
Namespace: namespace,
62+
Namespace: namespace,
63+
MaxNamespaceContainers: 64, // arbitrary fixed value
6364
},
6465
common.LogMessagesFilter{
6566
TailLines: tailLines,

0 commit comments

Comments
 (0)