forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webconsole_proxy.go
164 lines (142 loc) · 4.6 KB
/
webconsole_proxy.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package openshiftkubeapiserver
import (
"fmt"
"net/http"
"net/url"
"strings"
"sync/atomic"
"time"
"github.com/golang/glog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
coreclientv1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
webconsoleconfigv1 "github.com/openshift/api/webconsole/v1"
"github.com/openshift/origin/pkg/util/httprequest"
)
// If we know the location of the asset server, redirect to it when / is requested
// and the Accept header supports text/html
func withAssetServerRedirect(handler http.Handler, accessor *webConsolePublicURLAccessor) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/" && httprequest.PrefersHTML(req) {
webconsolePublicURL := accessor.getPublicConsoleURL()
if len(webconsolePublicURL) > 0 {
http.Redirect(w, req, webconsolePublicURL, http.StatusFound)
return
}
w.Header().Set("Retry-After", "3")
w.WriteHeader(http.StatusServiceUnavailable)
return
}
// Dispatch to the next handler
handler.ServeHTTP(w, req)
})
}
func withConsoleRedirection(handler, assetServerHandler http.Handler, accessor *webConsolePublicURLAccessor) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// blacklist well known paths so we do not risk recursion deadlocks
for _, prefix := range []string{"/apis", "/api", "/oapi", "/healthz", "/version"} {
if req.URL.Path == prefix || strings.HasPrefix(req.URL.Path, prefix+"/") {
// Dispatch to the next handler
handler.ServeHTTP(w, req)
return
}
}
webconsolePublicURL := accessor.getPublicConsoleURL()
if len(webconsolePublicURL) > 0 {
publicURL, err := url.Parse(webconsolePublicURL)
if err != nil {
// fails validation before here
glog.Fatal(err)
// Dispatch to the next handler
handler.ServeHTTP(w, req)
return
}
prefix := publicURL.Path
// prefix must not include a trailing '/'
lastIndex := len(publicURL.Path) - 1
if publicURL.Path[lastIndex] == '/' {
prefix = publicURL.Path[0:lastIndex]
}
if req.URL.Path == prefix || strings.HasPrefix(req.URL.Path, prefix+"/") {
assetServerHandler.ServeHTTP(w, req)
return
}
}
// Dispatch to the next handler
handler.ServeHTTP(w, req)
})
}
type webConsolePublicURLAccessor struct {
publicURL atomic.Value
configMapGetter coreclientv1.ConfigMapsGetter
polling time.Duration
}
func newWebConsolePublicURLAccessor(clientConfig *rest.Config) *webConsolePublicURLAccessor {
accessor := &webConsolePublicURLAccessor{
configMapGetter: coreclientv1.NewForConfigOrDie(clientConfig),
}
return accessor
}
func (a *webConsolePublicURLAccessor) getPublicConsoleURL() string {
currValue, ok := a.publicURL.Load().(string)
if ok && len(currValue) > 0 {
return currValue
}
// if we aren't already set, try to update
return a.updatePublicConsoleURL()
}
func (a *webConsolePublicURLAccessor) updatePublicConsoleURL() string {
// TODO: best effort ratelimit maybe
configMap, err := a.configMapGetter.ConfigMaps("openshift-web-console").Get("webconsole-config", metav1.GetOptions{})
if err != nil {
return ""
}
config, ok := configMap.Data["webconsole-config.yaml"]
if !ok {
return ""
}
webConsoleConfig, err := readWebConsoleConfiguration(config)
if err != nil {
return ""
}
newValue := webConsoleConfig.ClusterInfo.ConsolePublicURL
a.publicURL.Store(newValue)
// Once we have a value we relax polling to once per minute
a.polling = 1 * time.Minute
return newValue
}
func (a *webConsolePublicURLAccessor) Run(stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
a.polling = 1 * time.Second
for {
select {
case <-stopCh:
return
case <-time.After(a.polling):
}
a.updatePublicConsoleURL()
}
}
var (
webconsoleConfigScheme = runtime.NewScheme()
webconsoleConfigCodecs = serializer.NewCodecFactory(webconsoleConfigScheme)
)
func init() {
if err := webconsoleconfigv1.AddToScheme(webconsoleConfigScheme); err != nil {
panic(err)
}
}
func readWebConsoleConfiguration(objBytes string) (*webconsoleconfigv1.WebConsoleConfiguration, error) {
defaultConfigObj, err := runtime.Decode(webconsoleConfigCodecs.UniversalDecoder(webconsoleconfigv1.SchemeGroupVersion), []byte(objBytes))
if err != nil {
return nil, err
}
ret, ok := defaultConfigObj.(*webconsoleconfigv1.WebConsoleConfiguration)
if !ok {
return nil, fmt.Errorf("expected *webconsoleconfigv1.WebConsoleConfiguration, got %T", defaultConfigObj)
}
return ret, nil
}