github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/package-server/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/rest"
     9  	"k8s.io/client-go/tools/clientcmd"
    10  )
    11  
    12  // GetConfig returns a kubernetes config for configuring a client from a kubeconfig string
    13  func GetConfig(kubeconfig string) (*rest.Config, error) {
    14  	if len(kubeconfig) == 0 {
    15  		// Work around https://github.com/kubernetes/kubernetes/issues/40973
    16  		// See https://github.com/coreos/etcd-operator/issues/731#issuecomment-283804819
    17  		if len(os.Getenv("KUBERNETES_SERVICE_HOST")) == 0 {
    18  			addrs, err := net.LookupHost("kubernetes.default.svc")
    19  			if err != nil {
    20  				return nil, err
    21  			}
    22  
    23  			os.Setenv("KUBERNETES_SERVICE_HOST", addrs[0])
    24  		}
    25  
    26  		if len(os.Getenv("KUBERNETES_SERVICE_PORT")) == 0 {
    27  			os.Setenv("KUBERNETES_SERVICE_PORT", "443")
    28  		}
    29  
    30  		log.Infof("Using in-cluster kube client config")
    31  		return rest.InClusterConfig()
    32  	}
    33  	log.Infof("Loading kube client config from path %q", kubeconfig)
    34  	return clientcmd.BuildConfigFromFlags("", kubeconfig)
    35  }