Skip to content

Commit

Permalink
lazyload: avoid exclusive ports 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 af472dc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
37 changes: 23 additions & 14 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,17 @@ 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
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 +60,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 +74,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.")
}
9 changes: 9 additions & 0 deletions staging/src/slime.io/slime/modules/lazyload/pkg/proxy/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ func (p *HealthzProxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {

type Proxy struct {
WormholePort int
IsProbe bool
}

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

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

0 comments on commit af472dc

Please sign in to comment.