Skip to content

Commit

Permalink
Modify the ss filtering functions
Browse files Browse the repository at this point in the history
This commit joins both filtering function for the ss entries, and
rename it with a better name.

Signed-off-by: Lior Noy <[email protected]>
  • Loading branch information
liornoy committed Apr 30, 2024
1 parent ae78a57 commit b345759
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions ss/ss.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit b345759

Please sign in to comment.