From b345759de1d0f4ce325412ccea48c6e5463e547c Mon Sep 17 00:00:00 2001 From: Lior Noy Date: Tue, 30 Apr 2024 13:48:45 +0300 Subject: [PATCH] Modify the ss filtering functions This commit joins both filtering function for the ss entries, and rename it with a better name. Signed-off-by: Lior Noy --- ss/ss.go | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/ss/ss.go b/ss/ss.go index cb2e9d0c..acc9eb27 100644 --- a/ss/ss.go +++ b/ss/ss.go @@ -24,18 +24,9 @@ const ( duration = time.Second * 5 ) -var ( - // TcpSSFilterFn is a function variable in Go that filters entries from the 'ss' command output. - // It takes an entry from the 'ss' command output and returns true if the entry represents a TCP port in the listening state. - tcpSSFilterFn = func(s string) bool { - return strings.Contains(s, "127.0.0") || !strings.Contains(s, "LISTEN") - } - // UdpSSFilterFn is a function variable in Go that filters entries from the 'ss' command output. - // It takes an entry from the 'ss' command output and returns true if the entry represents a UDP port in the listening state. - udpSSFilterFn = func(s string) bool { - return strings.Contains(s, "127.0.0") || !strings.Contains(s, "ESTAB") - } -) +var filterOutFn = func(s string) bool { + return strings.Contains(s, "127.0.0") || strings.Contains(s, "[::1]") || (!strings.Contains(s, "LISTEN") && !strings.Contains(s, "ESTAB")) +} func CreateComDetailsFromNode(cs *client.ClientSet, node *corev1.Node, tcpFile, udpFile *os.File) ([]types.ComDetails, error) { debugPod, err := debug.New(cs, node.Name, consts.DefaultDebugNamespace, consts.DefaultDebugPodImage) @@ -58,8 +49,8 @@ func CreateComDetailsFromNode(cs *client.ClientSet, node *corev1.Node, tcpFile, return nil, err } - ssOutFilteredTCP := filterStrings(tcpSSFilterFn, splitByLines(ssOutTCP)) - ssOutFilteredUDP := filterStrings(udpSSFilterFn, splitByLines(ssOutUDP)) + ssOutFilteredTCP := filterStrings(filterOutFn, splitByLines(ssOutTCP)) + ssOutFilteredUDP := filterStrings(filterOutFn, splitByLines(ssOutUDP)) _, err = tcpFile.Write([]byte(fmt.Sprintf("node: %s\n%s", node.Name, strings.Join(ssOutFilteredTCP, "\n")))) if err != nil {