// BuildConfigFromFlags is a helper function that builds configs from a master // url or a kubeconfig filepath. These are passed in as command line flags for cluster // components. Warnings should reflect this usage. If neither masterUrl or kubeconfigPath // are passed in we fallback to inClusterConfig. If inClusterConfig fails, we fallback // to the default config. func BuildConfigFromFlags(masterUrl, kubeconfigPath string) (*rest.Config, error) { if kubeconfigPath == "" && masterUrl == "" { glog.Warningf("Neither --kubeconfig nor --master was specified. Using the inClusterConfig. This might not work.") kubeconfig, err := rest.InClusterConfig() if err == nil { return kubeconfig, nil } glog.Warning("error creating inClusterConfig, falling back to default config: ", err) } return NewNonInteractiveDeferredLoadingClientConfig( &ClientConfigLoadingRules{ExplicitPath: kubeconfigPath}, &ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: masterUrl}}).ClientConfig() }
// NewInClusterClient returns a new Kubernetes client that expect to run inside the cluster func NewInClusterClient() (Client, error) { config, err := rest.InClusterConfig() if err != nil { return nil, err } clientset, err := kubernetes.NewForConfig(config) if err != nil { return nil, err } return &clientImpl{ clientset: clientset, }, nil }
// New creates a new Kubernetes discovery for the given role. func New(l log.Logger, conf *config.KubernetesSDConfig) (*Kubernetes, error) { var ( kcfg *rest.Config err error ) if conf.APIServer.URL == nil { kcfg, err = rest.InClusterConfig() if err != nil { return nil, err } } else { token := conf.BearerToken if conf.BearerTokenFile != "" { bf, err := ioutil.ReadFile(conf.BearerTokenFile) if err != nil { return nil, err } token = string(bf) } kcfg = &rest.Config{ Host: conf.APIServer.String(), BearerToken: token, TLSClientConfig: rest.TLSClientConfig{ CAFile: conf.TLSConfig.CAFile, }, } } kcfg.UserAgent = "prometheus/discovery" if conf.BasicAuth != nil { kcfg.Username = conf.BasicAuth.Username kcfg.Password = conf.BasicAuth.Password } kcfg.TLSClientConfig.CertFile = conf.TLSConfig.CertFile kcfg.TLSClientConfig.KeyFile = conf.TLSConfig.KeyFile kcfg.Insecure = conf.TLSConfig.InsecureSkipVerify c, err := kubernetes.NewForConfig(kcfg) if err != nil { return nil, err } return &Kubernetes{ client: c, logger: l, role: conf.Role, }, nil }
// NewInClusterClientWithEndpoint is the same as NewInClusterClient but uses the provided endpoint URL func NewInClusterClientWithEndpoint(endpoint string) (Client, error) { config, err := rest.InClusterConfig() if err != nil { return nil, err } config.Host = endpoint clientset, err := kubernetes.NewForConfig(config) if err != nil { return nil, err } return &clientImpl{ clientset: clientset, }, nil }
func main() { // creates the in-cluster config config, err := rest.InClusterConfig() if err != nil { panic(err.Error()) } // creates the clientset clientset, err := kubernetes.NewForConfig(config) if err != nil { panic(err.Error()) } for { pods, err := clientset.Core().Pods("").List(api.ListOptions{}) if err != nil { panic(err.Error()) } fmt.Printf("There are %d pods in the cluster\n", len(pods.Items)) time.Sleep(10 * time.Second) } }
func (inClusterClientConfig) ClientConfig() (*rest.Config, error) { return rest.InClusterConfig() }