Skip to content

Commit

Permalink
Updates activator to support h2c (#1260)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsnchan authored and google-prow-robot committed Jun 18, 2018
1 parent 2ba8e0c commit 8cac18c
Show file tree
Hide file tree
Showing 17 changed files with 1,147 additions and 268 deletions.
7 changes: 4 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions cmd/activator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
"github.com/knative/serving/pkg/activator"
clientset "github.com/knative/serving/pkg/client/clientset/versioned"
"github.com/knative/serving/pkg/controller"
h2cutil "github.com/knative/serving/pkg/h2c"
"github.com/knative/serving/pkg/signals"
"github.com/knative/serving/third_party/h2c"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
Expand All @@ -45,7 +47,13 @@ type activationHandler struct {
type retryRoundTripper struct{}

func (rrt retryRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
transport := http.DefaultTransport
var transport http.RoundTripper

transport = http.DefaultTransport
if r.ProtoMajor == 2 {
transport = h2cutil.NewTransport()
}

resp, err := transport.RoundTrip(r)
// TODO: Activator should retry with backoff.
// https://github.com/knative/serving/issues/1229
Expand Down Expand Up @@ -79,9 +87,11 @@ func (a *activationHandler) handler(w http.ResponseWriter, r *http.Request) {
}
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.Transport = retryRoundTripper{}

// TODO: Clear the host to avoid 404's.
// https://github.com/elafros/elafros/issues/964
r.Host = ""

proxy.ServeHTTP(w, r)
}

Expand Down Expand Up @@ -114,5 +124,5 @@ func main() {
}()

http.HandleFunc("/", ah.handler)
http.ListenAndServe(":8080", nil)
}
h2c.ListenAndServe(":8080", nil)
}
21 changes: 21 additions & 0 deletions pkg/h2c/transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package h2c

import (
"crypto/tls"
"net"
"net/http"

"golang.org/x/net/http2"
)

// NewTransport will reroute all https traffic to http. This is
// to explicitly allow h2c (http2 without TLS) transport.
// See https://github.com/golang/go/issues/14141 for more details.
func NewTransport() http.RoundTripper {
return &http2.Transport{
AllowHTTP: true,
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
return net.Dial(netw, addr)
},
}
}
Loading

0 comments on commit 8cac18c

Please sign in to comment.