Skip to content

Commit

Permalink
Using pod ip env
Browse files Browse the repository at this point in the history
  • Loading branch information
soneillf5 committed May 7, 2021
1 parent 28c0d0b commit 7d2ce87
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ spec:
spec:
containers:
- name: udp-service
image: seanoneillf5/udp-server:1.0
image: seanoneillf5/udp-server:1.1
ports:
- containerPort: 3334
name: udp-server
protocol: UDP
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
---
apiVersion: v1
kind: Service
Expand Down
2 changes: 1 addition & 1 deletion tests/test-servers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ If you make changes to the TCP server:
local version ```-> imagePullPolicy: Never```
* Test the changes
* Include the change as part of the commit that requires the tcp-server change
* Build the docker image with an increased version number ```docker build --build-arg type=tcp -t tcp-server:v2```
* Build the docker image with an increased version number ```docker build --build-arg type=tcp -t tcp-server:v2 .```
* Push the docker image to the public repo
* Update the tag [service yaml](../data/transport-server-tcp-load-balance/standard/service_deployment.yaml) to match
the new tag
Expand Down
19 changes: 7 additions & 12 deletions tests/test-servers/udp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import (
"fmt"
"log"
"net"
"os"
)

func main() {
ip := os.Getenv("POD_IP")
log.Printf("ip: %v\n", ip)
if ip == "" {
log.Fatalf("missing required env var: POD_IP")
}
port := flag.String("port", "3334", "The port the server listens to")
flag.Parse()
listener, err := net.ListenPacket("udp", fmt.Sprintf(":%v", *port))
Expand All @@ -16,7 +22,6 @@ func main() {
}
defer listener.Close()
log.Printf("listening to udp connections at: :%v\n", *port)
address := fmt.Sprintf("%v", GetOutboundIP().String())
buffer := make([]byte, 1024)
for {
n, addr, err := listener.ReadFrom(buffer)
Expand All @@ -25,7 +30,7 @@ func main() {
}

fmt.Printf("packet-received: bytes=%d from=%s\n", n, addr.String())
address := fmt.Sprintf("%v:%v", address, *port)
address := fmt.Sprintf("%v:%v", ip, *port)
log.Printf("write data to connection: %v\n", address)
n, err = listener.WriteTo([]byte(address), addr)
if err != nil {
Expand All @@ -34,13 +39,3 @@ func main() {
fmt.Printf("packet-written: bytes=%d to=%s\n", n, addr.String())
}
}

func GetOutboundIP() net.IP {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
}

0 comments on commit 7d2ce87

Please sign in to comment.