Skip to content

Commit

Permalink
lazyload: avoid exclusive port for global-sidecar health check
Browse files Browse the repository at this point in the history
  • Loading branch information
cywang1905 committed Aug 23, 2022
1 parent 121e0af commit ed5bd92
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
40 changes: 25 additions & 15 deletions staging/src/slime.io/slime/modules/lazyload/cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const (
)

func main() {
// indicate whether probe port is same with one of wormhole ports
dupPort := false

// set log config
logLevel := os.Getenv(EnvLogLevel)
if logLevel == "" {
Expand All @@ -34,26 +37,18 @@ func main() {
TimestampFormat: time.RFC3339,
})

// start health check server
probePort := os.Getenv(EnvProbePort)
go func() {
handler := &proxy.HealthzProxy{}
log.Println("Starting health check on", ":" + probePort)
if err := http.ListenAndServe(":" + probePort, handler); err != nil {
log.Fatal("ListenAndServe:", err)
}
}()
probePort, err := strconv.Atoi(os.Getenv(EnvProbePort))
if err != nil {
log.Errorf("wrong probe port value %s", os.Getenv(EnvProbePort))
os.Exit(1)
}

// start multi ports defined in WormholePorts
// TODO start multi ports defined in WormholePorts in disaster backup scenario
// TODO if not, just start 80
wormholePorts := os.Getenv(EnvWormholePorts)
wormholePortsArr := strings.Split(wormholePorts, ",")
var whPorts []int
for _, whp := range wormholePortsArr {
if whp == probePort {
log.Errorf("wormholePort can not be %s, which is reserved for health check", probePort)
os.Exit(1)
}

p, err := strconv.Atoi(whp)
if err != nil {
log.Errorf("wrong wormholePort value %s", whp)
Expand All @@ -66,6 +61,11 @@ func main() {
for _, whPort := range whPorts {
wg.Add(1)
handler := &proxy.Proxy{WormholePort: whPort}
if whPort == probePort {
log.Println("Starting health check on", ":"+os.Getenv(EnvProbePort))
handler.IsProbe = true
dupPort = true
}
go func(whPort int) {
log.Println("Starting proxy on", "0.0.0.0"+":"+strconv.Itoa(whPort))
if err := http.ListenAndServe("0.0.0.0"+":"+strconv.Itoa(whPort), handler); err != nil {
Expand All @@ -75,6 +75,16 @@ func main() {
}(whPort)
}

if !dupPort {
go func() {
handler := &proxy.HealthzProxy{}
log.Println("Starting health check on", ":"+os.Getenv(EnvProbePort))
if err := http.ListenAndServe(":"+os.Getenv(EnvProbePort), handler); err != nil {
log.Fatal("ListenAndServe:", err)
}
}()
}

wg.Wait()
log.Infof("All servers exited.")
}
19 changes: 14 additions & 5 deletions staging/src/slime.io/slime/modules/lazyload/pkg/proxy/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,30 @@ const (
HeaderOrigDest = "Slime-Orig-Dest"
)

type HealthzProxy struct{}

func (p *HealthzProxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// health check
func healthCheck(w http.ResponseWriter, req *http.Request) {
if req.RequestURI == "/healthz/live" || req.RequestURI == "/healthz/ready" {
w.Write([]byte("Healthy!"))
w.WriteHeader(200)
return
}
w.WriteHeader(500)
}

type HealthzProxy struct{}

func (p *HealthzProxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
healthCheck(w, req)
}

type Proxy struct {
WormholePort int
IsProbe bool
}

func (p *Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if p.IsProbe {
healthCheck(w, req)
}

var (
reqCtx = req.Context()
reqHost = req.Host
Expand Down

0 comments on commit ed5bd92

Please sign in to comment.