diff --git a/charts/templates/cluster-global-sidecar.yaml b/charts/templates/cluster-global-sidecar.yaml index 69a4ab3..ae30f73 100644 --- a/charts/templates/cluster-global-sidecar.yaml +++ b/charts/templates/cluster-global-sidecar.yaml @@ -30,7 +30,7 @@ spec: - name: http-{{ . }} port: {{ int . }} protocol: TCP - targetPort: 80 + targetPort: {{ int . }} {{- end }} selector: app: global-sidecar @@ -53,7 +53,7 @@ metadata: labels: app: global-sidecar spec: - replicas: 1 + replicas: {{ $gs.replicas | default 1 }} selector: matchLabels: app: global-sidecar @@ -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 }} @@ -107,7 +110,7 @@ spec: failureThreshold: 3 httpGet: path: /healthz/live - port: 8080 + port: 18080 scheme: HTTP initialDelaySeconds: 600 periodSeconds: 30 @@ -117,7 +120,7 @@ spec: failureThreshold: 30 httpGet: path: /healthz/ready - port: 8080 + port: 18080 scheme: HTTP initialDelaySeconds: 1 periodSeconds: 2 diff --git a/cmd/proxy/main.go b/cmd/proxy/main.go index 07d31bc..39a76eb 100644 --- a/cmd/proxy/main.go +++ b/cmd/proxy/main.go @@ -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) + } } } diff --git a/controllers/domain.go b/controllers/domain.go index 5e78d7b..55663af 100644 --- a/controllers/domain.go +++ b/controllers/domain.go @@ -2,6 +2,7 @@ package controllers import ( "regexp" + lazyloadv1alpha1 "slime.io/slime/modules/lazyload/api/v1alpha1" ) @@ -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, diff --git a/pkg/proxy/http.go b/pkg/proxy/http.go index 77f4377..45b41fe 100644 --- a/pkg/proxy/http.go +++ b/pkg/proxy/http.go @@ -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) @@ -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" @@ -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) + } +}