github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/api/client/util.go (about)

     1  package client
     2  
     3  import (
     4  	"net"
     5  	"os"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  	_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
     9  	"k8s.io/client-go/rest"
    10  	"k8s.io/client-go/tools/clientcmd"
    11  )
    12  
    13  // getConfig returns a kubernetes config for configuring a client from a kubeconfig string
    14  func getConfig(kubeconfig string) (*rest.Config, error) {
    15  	if len(kubeconfig) == 0 {
    16  		// Work around https://github.com/kubernetes/kubernetes/issues/40973
    17  		// See https://github.com/coreos/etcd-operator/issues/731#issuecomment-283804819
    18  		if len(os.Getenv("KUBERNETES_SERVICE_HOST")) == 0 {
    19  			addrs, err := net.LookupHost("kubernetes.default.svc")
    20  			if err != nil {
    21  				return nil, err
    22  			}
    23  
    24  			os.Setenv("KUBERNETES_SERVICE_HOST", addrs[0])
    25  		}
    26  
    27  		if len(os.Getenv("KUBERNETES_SERVICE_PORT")) == 0 {
    28  			os.Setenv("KUBERNETES_SERVICE_PORT", "443")
    29  		}
    30  
    31  		log.Infof("Using in-cluster kube client config")
    32  		return rest.InClusterConfig()
    33  	}
    34  	log.Infof("Loading kube client config from path %q", kubeconfig)
    35  	return clientcmd.BuildConfigFromFlags("", kubeconfig)
    36  }