mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 19:01:49 +00:00
agnhost/netexec: logging cleanup and print more verbose errors
There's currently no way to know whether an error is for SCTP or UDP, for example: Jul 24 09:55:54.469: INFO: netserver-0[e2e-nettest-3476].container[webserver].log 2020/07/24 09:53:52 Started UDP server 2020/07/24 09:53:52 Error occurred. error:protocol not supported In this case the "Error occurred. error:protocol not supported" is actually for the SCTP socket. Make that more apparent.
This commit is contained in:
parent
1ed2cf1895
commit
31c563a46f
@ -491,9 +491,9 @@ func hostNameHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
// udp server supports the hostName, echo and clientIP commands.
|
// udp server supports the hostName, echo and clientIP commands.
|
||||||
func startUDPServer(udpPort int) {
|
func startUDPServer(udpPort int) {
|
||||||
serverAddress, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", udpPort))
|
serverAddress, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", udpPort))
|
||||||
assertNoError(err)
|
assertNoError(err, fmt.Sprintf("failed to resolve UDP address for port %d", sctpPort))
|
||||||
serverConn, err := net.ListenUDP("udp", serverAddress)
|
serverConn, err := net.ListenUDP("udp", serverAddress)
|
||||||
assertNoError(err)
|
assertNoError(err, fmt.Sprintf("failed to create listener for UDP address %v", serverAddress))
|
||||||
defer serverConn.Close()
|
defer serverConn.Close()
|
||||||
buf := make([]byte, 2048)
|
buf := make([]byte, 2048)
|
||||||
|
|
||||||
@ -506,27 +506,27 @@ func startUDPServer(udpPort int) {
|
|||||||
}()
|
}()
|
||||||
for {
|
for {
|
||||||
n, clientAddress, err := serverConn.ReadFromUDP(buf)
|
n, clientAddress, err := serverConn.ReadFromUDP(buf)
|
||||||
assertNoError(err)
|
assertNoError(err, fmt.Sprintf("failed accepting UDP connections"))
|
||||||
receivedText := strings.ToLower(strings.TrimSpace(string(buf[0:n])))
|
receivedText := strings.ToLower(strings.TrimSpace(string(buf[0:n])))
|
||||||
if receivedText == "hostname" {
|
if receivedText == "hostname" {
|
||||||
log.Println("Sending udp hostName response")
|
log.Println("Sending udp hostName response")
|
||||||
_, err = serverConn.WriteToUDP([]byte(getHostName()), clientAddress)
|
_, err = serverConn.WriteToUDP([]byte(getHostName()), clientAddress)
|
||||||
assertNoError(err)
|
assertNoError(err, fmt.Sprintf("failed to write hostname to UDP client %s", clientAddress))
|
||||||
} else if strings.HasPrefix(receivedText, "echo ") {
|
} else if strings.HasPrefix(receivedText, "echo ") {
|
||||||
parts := strings.SplitN(receivedText, " ", 2)
|
parts := strings.SplitN(receivedText, " ", 2)
|
||||||
resp := ""
|
resp := ""
|
||||||
if len(parts) == 2 {
|
if len(parts) == 2 {
|
||||||
resp = parts[1]
|
resp = parts[1]
|
||||||
}
|
}
|
||||||
log.Printf("Echoing %v\n", resp)
|
log.Printf("Echoing %v to UDP client %s\n", resp, clientAddress)
|
||||||
_, err = serverConn.WriteToUDP([]byte(resp), clientAddress)
|
_, err = serverConn.WriteToUDP([]byte(resp), clientAddress)
|
||||||
assertNoError(err)
|
assertNoError(err, fmt.Sprintf("failed to echo to UDP client %s", clientAddress))
|
||||||
} else if receivedText == "clientip" {
|
} else if receivedText == "clientip" {
|
||||||
log.Printf("Sending back clientip to %s", clientAddress.String())
|
log.Printf("Sending clientip back to UDP client %s\n", clientAddress)
|
||||||
_, err = serverConn.WriteToUDP([]byte(clientAddress.String()), clientAddress)
|
_, err = serverConn.WriteToUDP([]byte(clientAddress.String()), clientAddress)
|
||||||
assertNoError(err)
|
assertNoError(err, fmt.Sprintf("failed to write clientip to UDP client %s", clientAddress))
|
||||||
} else if len(receivedText) > 0 {
|
} else if len(receivedText) > 0 {
|
||||||
log.Printf("Unknown udp command received: %v\n", receivedText)
|
log.Printf("Unknown UDP command received from %s: %v\n", clientAddress, receivedText)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -534,9 +534,9 @@ func startUDPServer(udpPort int) {
|
|||||||
// sctp server supports the hostName, echo and clientIP commands.
|
// sctp server supports the hostName, echo and clientIP commands.
|
||||||
func startSCTPServer(sctpPort int) {
|
func startSCTPServer(sctpPort int) {
|
||||||
serverAddress, err := sctp.ResolveSCTPAddr("sctp", fmt.Sprintf(":%d", sctpPort))
|
serverAddress, err := sctp.ResolveSCTPAddr("sctp", fmt.Sprintf(":%d", sctpPort))
|
||||||
assertNoError(err)
|
assertNoError(err, fmt.Sprintf("failed to resolve SCTP address for port %d", sctpPort))
|
||||||
listener, err := sctp.ListenSCTP("sctp", serverAddress)
|
listener, err := sctp.ListenSCTP("sctp", serverAddress)
|
||||||
assertNoError(err)
|
assertNoError(err, fmt.Sprintf("failed to create listener for SCTP address %v", serverAddress))
|
||||||
defer listener.Close()
|
defer listener.Close()
|
||||||
buf := make([]byte, 1024)
|
buf := make([]byte, 1024)
|
||||||
|
|
||||||
@ -549,30 +549,30 @@ func startSCTPServer(sctpPort int) {
|
|||||||
}()
|
}()
|
||||||
for {
|
for {
|
||||||
conn, err := listener.AcceptSCTP()
|
conn, err := listener.AcceptSCTP()
|
||||||
assertNoError(err)
|
assertNoError(err, fmt.Sprintf("failed accepting SCTP connections"))
|
||||||
|
clientAddress := conn.RemoteAddr().String()
|
||||||
n, err := conn.Read(buf)
|
n, err := conn.Read(buf)
|
||||||
assertNoError(err)
|
assertNoError(err, fmt.Sprintf("failed to read from SCTP client %s", clientAddress))
|
||||||
receivedText := strings.ToLower(strings.TrimSpace(string(buf[0:n])))
|
receivedText := strings.ToLower(strings.TrimSpace(string(buf[0:n])))
|
||||||
if receivedText == "hostname" {
|
if receivedText == "hostname" {
|
||||||
log.Println("Sending sctp hostName response")
|
log.Println("Sending SCTP hostName response")
|
||||||
_, err = conn.Write([]byte(getHostName()))
|
_, err = conn.Write([]byte(getHostName()))
|
||||||
assertNoError(err)
|
assertNoError(err, fmt.Sprintf("failed to write hostname to SCTP client %s", clientAddress))
|
||||||
} else if strings.HasPrefix(receivedText, "echo ") {
|
} else if strings.HasPrefix(receivedText, "echo ") {
|
||||||
parts := strings.SplitN(receivedText, " ", 2)
|
parts := strings.SplitN(receivedText, " ", 2)
|
||||||
resp := ""
|
resp := ""
|
||||||
if len(parts) == 2 {
|
if len(parts) == 2 {
|
||||||
resp = parts[1]
|
resp = parts[1]
|
||||||
}
|
}
|
||||||
log.Printf("Echoing %v\n", resp)
|
log.Printf("Echoing %v to SCTP client %s\n", resp, clientAddress)
|
||||||
_, err = conn.Write([]byte(resp))
|
_, err = conn.Write([]byte(resp))
|
||||||
assertNoError(err)
|
assertNoError(err, fmt.Sprintf("failed to echo to SCTP client %s", clientAddress))
|
||||||
} else if receivedText == "clientip" {
|
} else if receivedText == "clientip" {
|
||||||
clientAddress := conn.RemoteAddr()
|
log.Printf("Sending clientip back to SCTP client %s\n", clientAddress)
|
||||||
log.Printf("Sending back clientip to %s", clientAddress.String())
|
_, err = conn.Write([]byte(clientAddress))
|
||||||
_, err = conn.Write([]byte(clientAddress.String()))
|
assertNoError(err, fmt.Sprintf("failed to write clientip to SCTP client %s", clientAddress))
|
||||||
assertNoError(err)
|
|
||||||
} else if len(receivedText) > 0 {
|
} else if len(receivedText) > 0 {
|
||||||
log.Printf("Unknown sctp command received: %v\n", receivedText)
|
log.Printf("Unknown SCTP command received from %s: %v\n", clientAddress, receivedText)
|
||||||
}
|
}
|
||||||
conn.Close()
|
conn.Close()
|
||||||
}
|
}
|
||||||
@ -580,12 +580,12 @@ func startSCTPServer(sctpPort int) {
|
|||||||
|
|
||||||
func getHostName() string {
|
func getHostName() string {
|
||||||
hostName, err := os.Hostname()
|
hostName, err := os.Hostname()
|
||||||
assertNoError(err)
|
assertNoError(err, "failed to get hostname")
|
||||||
return hostName
|
return hostName
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertNoError(err error) {
|
func assertNoError(err error, detail string) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error occurred. error:", err)
|
log.Fatalf("Error occurred: %s:%v", detail, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user