Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Commit

Permalink
WIP: disaster recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
cywang1905 committed Apr 21, 2022
1 parent 78f3d09 commit dc580b1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 37 deletions.
11 changes: 7 additions & 4 deletions charts/templates/cluster-global-sidecar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ spec:
- name: http-{{ . }}
port: {{ int . }}
protocol: TCP
targetPort: 80
targetPort: {{ int . }}
{{- end }}
selector:
app: global-sidecar
Expand All @@ -53,7 +53,7 @@ metadata:
labels:
app: global-sidecar
spec:
replicas: 1
replicas: {{ $gs.replicas | default 1 }}
selector:
matchLabels:
app: global-sidecar
Expand Down Expand Up @@ -95,6 +95,9 @@ spec:
serviceAccountName: global-sidecar
containers:
- name: global-sidecar
env:
- name: wormholePorts
value: {{ join ";" $f.wormholePort | quote }}
{{- if $gs.image.tag }}
image: "{{ $gs.image.repository }}:{{ $gs.image.tag}}"
{{- else }}
Expand All @@ -107,7 +110,7 @@ spec:
failureThreshold: 3
httpGet:
path: /healthz/live
port: 8080
port: 18080
scheme: HTTP
initialDelaySeconds: 600
periodSeconds: 30
Expand All @@ -117,7 +120,7 @@ spec:
failureThreshold: 30
httpGet:
path: /healthz/ready
port: 8080
port: 18080
scheme: HTTP
initialDelaySeconds: 1
periodSeconds: 2
Expand Down
40 changes: 32 additions & 8 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,52 @@ package main
import (
"flag"
"net/http"
"os"
"strconv"
"strings"

log "github.com/sirupsen/logrus"

"slime.io/slime/modules/lazyload/pkg/proxy"
)

const WormholePorts = "wormholePorts"

func main() {
// start health check server
go func() {
handler := &proxy.HealthzProxy{}
log.Println("Starting health check on :8080")
if err := http.ListenAndServe(":8080", handler); err != nil {
log.Println("Starting health check on :18080")
if err := http.ListenAndServe(":18080", handler); err != nil {
log.Fatal("ListenAndServe:", err)
}
}()

addr := flag.String("addr", "0.0.0.0:80", "The addr of the application.")
flag.Parse()
wormholePorts := os.Getenv(WormholePorts)
whPorts := strings.Split(wormholePorts, ";")

// start multi ports defined in WormholePorts
for idx, whPort := range whPorts {

handler := &proxy.Proxy{}
if whPort == "18080" {
log.Errorf("wormholePort can not be 18080, which is reserved for health check")
os.Exit(1)
}

log.Println("Starting proxy server on", *addr)
if err := http.ListenAndServe(*addr, handler); err != nil {
log.Fatal("ListenAndServe:", err)
p, err := strconv.Atoi(whPort)
if err != nil {
log.Errorf("wrong wormholePort value %s", whPort)
os.Exit(1)
}
handler := &proxy.Proxy{WormholePort: p}

addr := flag.String("addr"+strconv.Itoa(idx), "0.0.0.0:"+whPort, "The addr of the application.")
flag.Parse()

if idx != len(whPorts)-1 {
go handler.StartServer(addr)
} else {
handler.StartServer(addr)
}
}
}
3 changes: 2 additions & 1 deletion controllers/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"regexp"

lazyloadv1alpha1 "slime.io/slime/modules/lazyload/api/v1alpha1"
)

Expand All @@ -25,7 +26,7 @@ func newDomainAliasRules(domainAlias []*lazyloadv1alpha1.DomainAlias) []*domainA
log.Errorf("domainAlias template is empty")
return nil
}
rule := &domainAliasRule{
rule := &domainAliasRule{
pattern: pattern,
templates: templates,
re: re,
Expand Down
62 changes: 38 additions & 24 deletions pkg/proxy/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,18 @@ func (p *HealthzProxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
}

type Proxy struct{}
type Proxy struct{
WormholePort int
}

func (p *Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var (
reqCtx = req.Context()
reqHost = req.Host
origDest, origDestIp string
origDestPort = 80
origDestPort = p.WormholePort
)

if values := req.Header[HeaderOrigDest]; len(values) > 0 {
origDest = values[0]
req.Header.Del(HeaderOrigDest)
}

if origDest == "" {
http.Error(w, "lack of header "+HeaderOrigDest, http.StatusBadRequest)
return
}
if idx := strings.LastIndex(origDest, ":"); idx >= 0 {
origDestIp = origDest[:idx]
if v, err := strconv.Atoi(origDest[idx+1:]); err != nil {
http.Error(w, fmt.Sprintf("invalid header %s value: %s", HeaderOrigDest, origDest), http.StatusBadRequest)
return
} else {
origDestPort = v
}
} else {
origDestIp = origDest
}

// try to complete short name
if values := req.Header[HeaderSourceNs]; len(values) > 0 && values[0] != "" {
req.Header.Del(HeaderSourceNs)
Expand All @@ -72,7 +53,33 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
}
}
log.Infof("received request, reqHost: %s", reqHost)
log.Infof("proxy received request, reqHost: %s", reqHost)

if values := req.Header[HeaderOrigDest]; len(values) > 0 {
origDest = values[0]
req.Header.Del(HeaderOrigDest)

if idx := strings.LastIndex(origDest, ":"); idx >= 0 {
origDestIp = origDest[:idx]
if v, err := strconv.Atoi(origDest[idx+1:]); err != nil {
http.Error(w, fmt.Sprintf("invalid header %s value: %s", HeaderOrigDest, origDest), http.StatusBadRequest)
return
} else {
origDestPort = v
}
} else {
origDestIp = origDest
}
}

if origDest == "" {
if idx := strings.LastIndex(reqHost, ":"); idx >= 0 {
origDestIp = reqHost[:idx]
} else {
origDestIp = reqHost
}
}
log.Infof("proxy forward request to: %s:%d", origDestIp, origDestPort)

if req.URL.Scheme == "" {
req.URL.Scheme = "http"
Expand Down Expand Up @@ -121,3 +128,10 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(resp.StatusCode)
_, _ = io.Copy(w, resp.Body)
}

func (p *Proxy) StartServer(addr *string) {
log.Println("Starting LazyLoad proxy on ", *addr)
if err := http.ListenAndServe(*addr, p); err != nil {
log.Fatal("LazyLoad proxy ListenAndServe error:", err)
}
}

0 comments on commit dc580b1

Please sign in to comment.