@@ -6,15 +6,20 @@ import (
66
77 authenticationv1 "k8s.io/api/authentication/v1"
88 genericapiserver "k8s.io/apiserver/pkg/server"
9+ coreinformers "k8s.io/client-go/informers/core/v1"
910 patchfilters "k8s.io/kubernetes/openshift-kube-apiserver/filters"
1011 "k8s.io/kubernetes/openshift-kube-apiserver/filters/apirequestcount"
1112
1213 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"
1419)
1520
1621// 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 ) {
1823 // load the oauthmetadata when we can return an error
1924 oAuthMetadata := []byte {}
2025 if len (oauthMetadataFile ) > 0 {
@@ -39,7 +44,7 @@ func BuildHandlerChain(consolePublicURL string, oauthMetadataFile string, reques
3944 handler = translateLegacyScopeImpersonation (handler )
4045
4146 // redirects from / and /console to consolePublicURL if you're using a browser
42- handler = withConsoleRedirect (handler , consolePublicURL )
47+ handler = withConsoleRedirect (handler , cmInformer )
4348
4449 return handler
4550 },
@@ -69,19 +74,28 @@ func withOAuthInfo(handler http.Handler, oAuthMetadata []byte) http.Handler {
6974
7075// If we know the location of the asset server, redirect to it when / is requested
7176// 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 ()
7780 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 )
8184 return
8285 }
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 )
8599 })
86100}
87101
0 commit comments