Skip to content

Commit f783240

Browse files
backend: cmd: Extract setupInClusterContext from createHeadlampHandler
Co-authored-by: René Dudfield <[email protected]>
1 parent c43ffc4 commit f783240

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

backend/cmd/headlamp.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -411,28 +411,7 @@ func createHeadlampHandler(config *HeadlampConfig) http.Handler {
411411
logStartupInfo(config)
412412
skipFunc := kubeconfig.SkipKubeContextInCommaSeparatedString(config.SkippedKubeContexts)
413413
setupPluginHandlers(config, skipFunc)
414-
415-
// In-cluster
416-
if config.UseInCluster {
417-
context, err := kubeconfig.GetInClusterContext(config.oidcIdpIssuerURL,
418-
config.oidcClientID, config.oidcClientSecret,
419-
strings.Join(config.oidcScopes, ","))
420-
if err != nil {
421-
logger.Log(logger.LevelError, nil, err, "Failed to get in-cluster context")
422-
}
423-
424-
context.Source = kubeconfig.InCluster
425-
426-
err = context.SetupProxy()
427-
if err != nil {
428-
logger.Log(logger.LevelError, nil, err, "Failed to setup proxy for in-cluster context")
429-
}
430-
431-
err = config.KubeConfigStore.AddContext(context)
432-
if err != nil {
433-
logger.Log(logger.LevelError, nil, err, "Failed to add in-cluster context")
434-
}
435-
}
414+
setupInClusterContext(config)
436415

437416
if config.StaticDir != "" {
438417
baseURLReplace(config.StaticDir, config.BaseURL)
@@ -819,6 +798,31 @@ func logStartupInfo(config *HeadlampConfig) {
819798
logger.Log(logger.LevelInfo, nil, nil, "Proxy URLs: "+fmt.Sprint(config.ProxyURLs))
820799
}
821800

801+
// setupInClusterContext prepares in-cluster context with OIDC authentication,
802+
// sets up a proxy and adds it to the kubeconfig store.
803+
func setupInClusterContext(config *HeadlampConfig) {
804+
if config.UseInCluster {
805+
context, err := kubeconfig.GetInClusterContext(config.oidcIdpIssuerURL,
806+
config.oidcClientID, config.oidcClientSecret,
807+
strings.Join(config.oidcScopes, ","))
808+
if err != nil {
809+
logger.Log(logger.LevelError, nil, err, "Failed to get in-cluster context")
810+
}
811+
812+
context.Source = kubeconfig.InCluster
813+
814+
err = context.SetupProxy()
815+
if err != nil {
816+
logger.Log(logger.LevelError, nil, err, "Failed to setup proxy for in-cluster context")
817+
}
818+
819+
err = config.KubeConfigStore.AddContext(context)
820+
if err != nil {
821+
logger.Log(logger.LevelError, nil, err, "Failed to add in-cluster context")
822+
}
823+
}
824+
}
825+
822826
func parseClusterAndToken(r *http.Request) (string, string) {
823827
cluster := ""
824828
re := regexp.MustCompile(`^/clusters/([^/]+)/.*`)

0 commit comments

Comments
 (0)