Skip to content

Commit f339132

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

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

backend/cmd/headlamp.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -421,30 +421,7 @@ func createHeadlampHandler(config *HeadlampConfig) http.Handler {
421421
logStartupInfo(config)
422422
skipFunc := kubeconfig.SkipKubeContextInCommaSeparatedString(config.SkippedKubeContexts)
423423
setupPluginHandlers(config, skipFunc)
424-
425-
// In-cluster
426-
if config.UseInCluster {
427-
context, err := kubeconfig.GetInClusterContext(config.oidcIdpIssuerURL,
428-
config.oidcClientID, config.oidcClientSecret,
429-
strings.Join(config.oidcScopes, ","),
430-
config.oidcSkipTLSVerify,
431-
config.oidcCACert)
432-
if err != nil {
433-
logger.Log(logger.LevelError, nil, err, "Failed to get in-cluster context")
434-
}
435-
436-
context.Source = kubeconfig.InCluster
437-
438-
err = context.SetupProxy()
439-
if err != nil {
440-
logger.Log(logger.LevelError, nil, err, "Failed to setup proxy for in-cluster context")
441-
}
442-
443-
err = config.KubeConfigStore.AddContext(context)
444-
if err != nil {
445-
logger.Log(logger.LevelError, nil, err, "Failed to add in-cluster context")
446-
}
447-
}
424+
setupInClusterContext(config)
448425

449426
if config.StaticDir != "" {
450427
baseURLReplace(config.StaticDir, config.BaseURL)
@@ -844,6 +821,33 @@ func logStartupInfo(config *HeadlampConfig) {
844821
logger.Log(logger.LevelInfo, nil, nil, "Proxy URLs: "+fmt.Sprint(config.ProxyURLs))
845822
}
846823

824+
// setupInClusterContext prepares in-cluster context with OIDC authentication,
825+
// sets up a proxy and adds it to the kubeconfig store.
826+
func setupInClusterContext(config *HeadlampConfig) {
827+
if config.UseInCluster {
828+
context, err := kubeconfig.GetInClusterContext(config.oidcIdpIssuerURL,
829+
config.oidcClientID, config.oidcClientSecret,
830+
strings.Join(config.oidcScopes, ","),
831+
config.oidcSkipTLSVerify,
832+
config.oidcCACert)
833+
if err != nil {
834+
logger.Log(logger.LevelError, nil, err, "Failed to get in-cluster context")
835+
}
836+
837+
context.Source = kubeconfig.InCluster
838+
839+
err = context.SetupProxy()
840+
if err != nil {
841+
logger.Log(logger.LevelError, nil, err, "Failed to setup proxy for in-cluster context")
842+
}
843+
844+
err = config.KubeConfigStore.AddContext(context)
845+
if err != nil {
846+
logger.Log(logger.LevelError, nil, err, "Failed to add in-cluster context")
847+
}
848+
}
849+
}
850+
847851
func parseClusterAndToken(r *http.Request) (string, string) {
848852
cluster := ""
849853
re := regexp.MustCompile(`^/clusters/([^/]+)/.*`)

0 commit comments

Comments
 (0)