@@ -32,6 +32,17 @@ import (
3232 "github.com/dexidp/dex/storage/kubernetes/k8sapi"
3333)
3434
35+ const (
36+ serviceAccountPath = "/var/run/secrets/kubernetes.io/serviceaccount/"
37+ serviceAccountTokenPath = serviceAccountPath + "token"
38+ serviceAccountCAPath = serviceAccountPath + "ca.crt"
39+ serviceAccountNamespacePath = serviceAccountPath + "namespace"
40+
41+ kubernetesServiceHostENV = "KUBERNETES_SERVICE_HOST"
42+ kubernetesServicePortENV = "KUBERNETES_SERVICE_PORT"
43+ kubernetesPodNamespaceENV = "KUBERNETES_POD_NAMESPACE"
44+ )
45+
3546type client struct {
3647 client * http.Client
3748 baseURL string
@@ -508,33 +519,35 @@ func getInClusterConfigNamespace(token, namespaceENV, namespacePath string) (str
508519 return "" , fmt .Errorf ("%v: trying to get namespace from file: %v" , err , fileErr )
509520}
510521
511- func inClusterConfig () (k8sapi.Cluster , k8sapi.AuthInfo , string , error ) {
512- const (
513- serviceAccountPath = "/var/run/secrets/kubernetes.io/serviceaccount/"
514- serviceAccountTokenPath = serviceAccountPath + "token"
515- serviceAccountCAPath = serviceAccountPath + "ca.crt"
516- serviceAccountNamespacePath = serviceAccountPath + "namespace"
517-
518- kubernetesServiceHostENV = "KUBERNETES_SERVICE_HOST"
519- kubernetesServicePortENV = "KUBERNETES_SERVICE_PORT"
520- kubernetesPodNamespaceENV = "KUBERNETES_POD_NAMESPACE"
521- )
522-
523- host , port := os .Getenv (kubernetesServiceHostENV ), os .Getenv (kubernetesServicePortENV )
522+ func getInClusterConnectOptions (host , port string ) (k8sapi.Cluster , error ) {
524523 if len (host ) == 0 || len (port ) == 0 {
525- return k8sapi.Cluster {}, k8sapi. AuthInfo {}, "" , fmt .Errorf (
524+ return k8sapi.Cluster {}, fmt .Errorf (
526525 "unable to load in-cluster configuration, %s and %s must be defined" ,
527526 kubernetesServiceHostENV ,
528527 kubernetesServicePortENV ,
529528 )
530529 }
530+
531531 // we need to wrap IPv6 addresses in square brackets
532- // IPv4 also works with square brackets
533- host = "[" + host + "]"
532+ // IPv4 used to work with square brackets, but it was fixed in the latest Go versions
533+ // https://github.com/golang/go/issues/75712
534+ ipAddr := net .ParseIP (host )
535+ if ipAddr != nil && ipAddr .To4 () == nil {
536+ host = "[" + host + "]"
537+ }
538+
534539 cluster := k8sapi.Cluster {
535540 Server : "https://" + host + ":" + port ,
536541 CertificateAuthority : serviceAccountCAPath ,
537542 }
543+ return cluster , nil
544+ }
545+
546+ func inClusterConfig () (k8sapi.Cluster , k8sapi.AuthInfo , string , error ) {
547+ cluster , err := getInClusterConnectOptions (os .Getenv (kubernetesServiceHostENV ), os .Getenv (kubernetesServicePortENV ))
548+ if err != nil {
549+ return cluster , k8sapi.AuthInfo {}, "" , err
550+ }
538551
539552 token , err := os .ReadFile (serviceAccountTokenPath )
540553 if err != nil {
0 commit comments