@@ -6,15 +6,20 @@ import (
6
6
7
7
authenticationv1 "k8s.io/api/authentication/v1"
8
8
genericapiserver "k8s.io/apiserver/pkg/server"
9
+ coreinformers "k8s.io/client-go/informers/core/v1"
9
10
patchfilters "k8s.io/kubernetes/openshift-kube-apiserver/filters"
10
11
"k8s.io/kubernetes/openshift-kube-apiserver/filters/apirequestcount"
11
12
12
13
authorizationv1 "github.com/openshift/api/authorization/v1"
13
- "github.com/openshift/library-go/pkg/apiserver/httprequest"
14
+ )
15
+
16
+ const (
17
+ openShiftConfigManagedNamespaceName = "openshift-config-managed"
18
+ consolePublicConfigMapName = "console-public"
14
19
)
15
20
16
21
// TODO switch back to taking a kubeapiserver config. For now make it obviously safe for 3.11
17
- func BuildHandlerChain (consolePublicURL string , oauthMetadataFile string , requestLogger apirequestcount.APIRequestLogger ) (func (apiHandler http.Handler , kc * genericapiserver.Config ) http.Handler , error ) {
22
+ func BuildHandlerChain (oauthMetadataFile string , cmInformer coreinformers. ConfigMapInformer , requestLogger apirequestcount.APIRequestLogger ) (func (apiHandler http.Handler , kc * genericapiserver.Config ) http.Handler , error ) {
18
23
// load the oauthmetadata when we can return an error
19
24
oAuthMetadata := []byte {}
20
25
if len (oauthMetadataFile ) > 0 {
@@ -39,7 +44,7 @@ func BuildHandlerChain(consolePublicURL string, oauthMetadataFile string, reques
39
44
handler = translateLegacyScopeImpersonation (handler )
40
45
41
46
// redirects from / and /console to consolePublicURL if you're using a browser
42
- handler = withConsoleRedirect (handler , consolePublicURL )
47
+ handler = withConsoleRedirect (handler , cmInformer )
43
48
44
49
return handler
45
50
},
@@ -69,19 +74,28 @@ func withOAuthInfo(handler http.Handler, oAuthMetadata []byte) http.Handler {
69
74
70
75
// If we know the location of the asset server, redirect to it when / is requested
71
76
// and the Accept header supports text/html
72
- func withConsoleRedirect (handler http.Handler , consolePublicURL string ) http.Handler {
73
- if len (consolePublicURL ) == 0 {
74
- return handler
75
- }
76
-
77
+ func withConsoleRedirect (handler http.Handler , cmInformer coreinformers.ConfigMapInformer ) http.Handler {
78
+ cmLister := cmInformer .Lister ()
79
+ informer := cmInformer .Informer ()
77
80
return http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
78
- if strings .HasPrefix (req .URL .Path , "/console" ) ||
79
- ( req . URL . Path == "/" && httprequest . PrefersHTML ( req )) {
80
- http . Redirect (w , req , consolePublicURL , http . StatusFound )
81
+ if ! strings .HasPrefix (req .URL .Path , "/console" ) {
82
+ // Dispatch to the next handler
83
+ handler . ServeHTTP (w , req )
81
84
return
82
85
}
83
- // Dispatch to the next handler
84
- handler .ServeHTTP (w , req )
86
+
87
+ consoleUrl := ""
88
+ if informer .HasSynced () {
89
+ consolePublicConfig , err := cmLister .ConfigMaps (openShiftConfigManagedNamespaceName ).Get (consolePublicConfigMapName )
90
+ if err == nil {
91
+ consoleUrl = consolePublicConfig .Data ["consoleURL" ]
92
+ }
93
+ }
94
+ if len (consoleUrl ) > 0 {
95
+ http .Redirect (w , req , consoleUrl , http .StatusFound )
96
+ return
97
+ }
98
+ http .Error (w , "redirection failed: console URL not found" , http .StatusInternalServerError )
85
99
})
86
100
}
87
101
0 commit comments