-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmain.go
118 lines (105 loc) · 3.34 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Copyright 2018 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"flag"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"time"
"github.com/golang/glog"
"github.com/knative/serving/pkg/activator"
clientset "github.com/knative/serving/pkg/client/clientset/versioned"
"github.com/knative/serving/pkg/controller"
"github.com/knative/serving/pkg/signals"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
const (
maxRetry = 60
retryInterval = 1 * time.Second
)
type activationHandler struct {
act activator.Activator
}
// retryRoundTripper retries on 503's for up to 60 seconds. The reason is there is
// a small delay for k8s to include the ready IP in service.
// https://github.com/knative/serving/issues/660#issuecomment-384062553
type retryRoundTripper struct{}
func (rrt retryRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
transport := http.DefaultTransport
resp, err := transport.RoundTrip(r)
// TODO: Activator should retry with backoff.
// https://github.com/knative/serving/issues/1229
i := 1
for ; i < maxRetry; i++ {
if err == nil && resp != nil && resp.StatusCode != 503 {
break
}
resp.Body.Close()
time.Sleep(retryInterval)
resp, err = transport.RoundTrip(r)
}
// TODO: add metrics for number of tries and the response code.
glog.Infof("It took %d tries to get response code %d", i, resp.StatusCode)
return resp, nil
}
func (a *activationHandler) handler(w http.ResponseWriter, r *http.Request) {
namespace := r.Header.Get(controller.GetRevisionHeaderNamespace())
name := r.Header.Get(controller.GetRevisionHeaderName())
endpoint, status, err := a.act.ActiveEndpoint(namespace, name)
if err != nil {
msg := fmt.Sprintf("Error getting active endpoint: %v", err)
glog.Errorf(msg)
http.Error(w, msg, int(status))
return
}
target := &url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%d", endpoint.FQDN, endpoint.Port),
}
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)
}
func main() {
flag.Parse()
glog.Info("Starting the knative activator...")
clusterConfig, err := rest.InClusterConfig()
if err != nil {
glog.Fatal(err)
}
kubeClient, err := kubernetes.NewForConfig(clusterConfig)
if err != nil {
glog.Fatal(err)
}
elaClient, err := clientset.NewForConfig(clusterConfig)
if err != nil {
glog.Fatalf("Error building ela clientset: %v", err)
}
a := activator.NewRevisionActivator(kubeClient, elaClient)
a = activator.NewDedupingActivator(a)
ah := &activationHandler{a}
// set up signals so we handle the first shutdown signal gracefully
stopCh := signals.SetupSignalHandler()
go func() {
<-stopCh
a.Shutdown()
}()
http.HandleFunc("/", ah.handler)
http.ListenAndServe(":8080", nil)
}