Add support for UDP in kuryr/demo container
Update kuryr/demo container to support UDP server. After this change, the kuryr/demo image runs both HTTP and UDP servers. The listeners ports values could be configured using pod's environment variables. And in addition, a simple udp client binary tool was added to image, to use it, you should run : udp_client <server's_ip> <server's port> Change-Id: I9f2f2ad3273f947fecb776c0f56f6829df81aa67
This commit is contained in:
parent
16062f90bf
commit
3ae7eef13e
@ -156,6 +156,8 @@ RUN mkdir -p rootfs/usr/bin; \
|
||||
cp /usr/local/bin/curl rootfs/usr/bin/curl
|
||||
|
||||
ADD ./server.go .
|
||||
ADD ./udp_client.go .
|
||||
RUN go build -ldflags "-linkmode external -extldflags -static" -o rootfs/usr/bin/helloserver server.go
|
||||
RUN go build -ldflags "-linkmode external -extldflags -static" -o rootfs/usr/bin/udp_client udp_client.go
|
||||
RUN mkdir -p rootfs/etc/ssl/certs \
|
||||
&& cp /etc/ssl/certs/ca-certificates.crt rootfs/etc/ssl/certs/ca-certificates.crt
|
||||
|
@ -3,9 +3,13 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
@ -16,22 +20,63 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", handler)
|
||||
func send_udp_response(conn *net.UDPConn, addr *net.UDPAddr) {
|
||||
hostname, err := os.Hostname()
|
||||
if err == nil {
|
||||
resp_str := fmt.Sprintf("%s: HELLO! I AM ALIVE!!!\n", hostname)
|
||||
_, err := conn.WriteToUDP([]byte(resp_str), addr)
|
||||
if err != nil {
|
||||
log.Println("Failed to reply to client")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func run_udp_server(port int) {
|
||||
|
||||
p := make([]byte, 2048)
|
||||
|
||||
log.Println("Running UDP server")
|
||||
ser, _ := net.ListenUDP("udp", &net.UDPAddr{IP: []byte{0, 0, 0, 0}, Port: port, Zone: ""})
|
||||
defer ser.Close()
|
||||
|
||||
for {
|
||||
_, remoteaddr, err := ser.ReadFromUDP(p)
|
||||
if err != nil {
|
||||
log.Println("We got an Error on reading")
|
||||
continue
|
||||
}
|
||||
log.Println("Received UDP request")
|
||||
send_udp_response(ser, remoteaddr)
|
||||
}
|
||||
}
|
||||
|
||||
func udp_handling(wg sync.WaitGroup) {
|
||||
udpPort, udpPortPresent := os.LookupEnv("UDP_PORT")
|
||||
|
||||
var port_num int = 9090
|
||||
if udpPortPresent {
|
||||
port_num, _ = strconv.Atoi(strings.TrimSpace(udpPort))
|
||||
}
|
||||
run_udp_server(port_num)
|
||||
}
|
||||
|
||||
func http_handling(wg sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
|
||||
http.HandleFunc("/", handler)
|
||||
httpsPort, httpsPortPresent := os.LookupEnv("HTTPS_PORT")
|
||||
|
||||
var port string
|
||||
|
||||
if httpsPortPresent {
|
||||
port = ":" + strings.TrimSpace(httpsPort)
|
||||
|
||||
cert, certPresent := os.LookupEnv("HTTPS_CERT_PATH")
|
||||
key, keyPresent := os.LookupEnv("HTTPS_KEY_PATH")
|
||||
|
||||
if !certPresent || !keyPresent {
|
||||
log.Fatal("HTTPS_PORT configured but missing HTTPS_CERT_PATH and/or HTTPS_KEY_PATH")
|
||||
}
|
||||
|
||||
log.Println("Running HTTPS server")
|
||||
log.Fatal(http.ListenAndServeTLS(port, cert, key, nil))
|
||||
} else {
|
||||
httpPort, confPresent := os.LookupEnv("HTTP_PORT")
|
||||
@ -40,7 +85,20 @@ func main() {
|
||||
} else {
|
||||
port = ":8080"
|
||||
}
|
||||
|
||||
log.Println("Running HTTP server")
|
||||
log.Fatal(http.ListenAndServe(port, nil))
|
||||
}
|
||||
log.Println("Exit HTTP server...")
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
runtime.GOMAXPROCS(2)
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
|
||||
go http_handling(wg)
|
||||
go udp_handling(wg)
|
||||
wg.Wait()
|
||||
}
|
||||
|
30
test_container/udp_client.go
Normal file
30
test_container/udp_client.go
Normal file
@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
)
|
||||
|
||||
// udp_client.go syntax : udp_client <server_IP> <server_port>
|
||||
|
||||
func main() {
|
||||
|
||||
server_ip_port := os.Args[1] + ":" + os.Args[2]
|
||||
|
||||
p := make([]byte, 2048)
|
||||
conn, err := net.Dial("udp", server_ip_port)
|
||||
if err != nil {
|
||||
fmt.Printf("Some error %v", err)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(conn, "Hi UDP Server, How are you?")
|
||||
_, err = bufio.NewReader(conn).Read(p)
|
||||
if err == nil {
|
||||
fmt.Printf("%s\n", p)
|
||||
} else {
|
||||
fmt.Printf("Some error %v\n", err)
|
||||
}
|
||||
conn.Close()
|
||||
}
|
Loading…
Reference in New Issue
Block a user