// makeUserIdentificationFieldsConfig returns a client.Config capable of being merged using mergo for only server identification information func makeServerIdentificationConfig(info clientauth.Info) client.Config { config := client.Config{} config.CAFile = info.CAFile if info.Insecure != nil { config.Insecure = *info.Insecure } return config }
// MergeWithConfig returns a copy of a client.Config with values from the Info. // The fields of client.Config with a corresponding field in the Info are set // with the value from the Info. func (info Info) MergeWithConfig(c client.Config) (client.Config, error) { var config client.Config = c config.Username = info.User config.Password = info.Password config.CAFile = info.CAFile config.CertFile = info.CertFile config.KeyFile = info.KeyFile config.BearerToken = info.BearerToken if info.Insecure != nil { config.Insecure = *info.Insecure } return config, nil }
// newAPIClient creates a new client to speak to the kubernetes api service func (r *kubeAPIImpl) newAPIClient() (*unversioned.Client, error) { // step: create the configuration cfg := unversioned.Config{ Host: getURL(), Insecure: config.HTTPInsecure, Version: config.APIVersion, } // check: ensure the token file exists if config.TokenFile != "" { if _, err := os.Stat(config.TokenFile); os.IsNotExist(err) { return nil, fmt.Errorf("the token file: %s does not exist", config.TokenFile) } content, err := ioutil.ReadFile(config.TokenFile) if err != nil { return nil, fmt.Errorf("unable to read the token file: %s, error: %s", config.TokenFile, err) } config.Token = string(content) } // check: are we using a user token to authenticate? if config.Token != "" { cfg.BearerToken = config.Token } // check: are we using a cert to authenticate if config.CaCertFile != "" { cfg.Insecure = false cfg.TLSClientConfig = unversioned.TLSClientConfig{ CAFile: config.CaCertFile, } } // step: initialize the client kube, err := unversioned.New(&cfg) if err != nil { return nil, fmt.Errorf("unable to create a kubernetes api client, reason: %s", err) } return kube, nil }
func makeTransport(config *schedulerapi.ExtenderConfig) (http.RoundTripper, error) { var cfg client.Config if config.TLSConfig != nil { cfg.TLSClientConfig = *config.TLSConfig } if config.EnableHttps { hasCA := len(cfg.CAFile) > 0 || len(cfg.CAData) > 0 if !hasCA { cfg.Insecure = true } } tlsConfig, err := client.TLSConfigFor(&cfg) if err != nil { return nil, err } if tlsConfig != nil { return &http.Transport{ TLSClientConfig: tlsConfig, }, nil } return http.DefaultTransport, nil }
func CreateKubeSources(uri *url.URL, c cache.Cache) ([]api.Source, error) { var ( kubeConfig *kube_client.Config err error ) opts := uri.Query() configOverrides, err := getConfigOverrides(uri) if err != nil { return nil, err } inClusterConfig := defaultInClusterConfig if len(opts["inClusterConfig"]) > 0 { inClusterConfig, err = strconv.ParseBool(opts["inClusterConfig"][0]) if err != nil { return nil, err } } if inClusterConfig { kubeConfig, err = kube_client.InClusterConfig() if err != nil { return nil, err } if configOverrides.ClusterInfo.Server != "" { kubeConfig.Host = configOverrides.ClusterInfo.Server } kubeConfig.Version = configOverrides.ClusterInfo.APIVersion kubeConfig.Insecure = configOverrides.ClusterInfo.InsecureSkipTLSVerify if configOverrides.ClusterInfo.InsecureSkipTLSVerify { kubeConfig.TLSClientConfig.CAFile = "" } } else { authFile := "" if len(opts["auth"]) > 0 { authFile = opts["auth"][0] } if authFile != "" { if kubeConfig, err = kubeClientCmd.NewNonInteractiveDeferredLoadingClientConfig( &kubeClientCmd.ClientConfigLoadingRules{ExplicitPath: authFile}, configOverrides).ClientConfig(); err != nil { return nil, err } } else { kubeConfig = &kube_client.Config{ Host: configOverrides.ClusterInfo.Server, Version: configOverrides.ClusterInfo.APIVersion, Insecure: configOverrides.ClusterInfo.InsecureSkipTLSVerify, } } } if len(kubeConfig.Host) == 0 { return nil, fmt.Errorf("invalid kubernetes master url specified") } if len(kubeConfig.Version) == 0 { return nil, fmt.Errorf("invalid kubernetes API version specified") } useServiceAccount := defaultUseServiceAccount if len(opts["useServiceAccount"]) >= 1 { useServiceAccount, err = strconv.ParseBool(opts["useServiceAccount"][0]) if err != nil { return nil, err } } if useServiceAccount { // If a readable service account token exists, then use it if contents, err := ioutil.ReadFile(defaultServiceAccountFile); err == nil { kubeConfig.BearerToken = string(contents) } } kubeClient := kube_client.NewOrDie(kubeConfig) nodesApi, err := nodes.NewKubeNodes(kubeClient) if err != nil { return nil, err } kubeletPort := defaultKubeletPort if len(opts["kubeletPort"]) >= 1 { kubeletPort, err = strconv.Atoi(opts["kubeletPort"][0]) if err != nil { return nil, err } } kubeletHttps := defaultKubeletHttps if len(opts["kubeletHttps"]) >= 1 { kubeletHttps, err = strconv.ParseBool(opts["kubeletHttps"][0]) if err != nil { return nil, err } } glog.Infof("Using Kubernetes client with master %q and version %q\n", kubeConfig.Host, kubeConfig.Version) glog.Infof("Using kubelet port %d", kubeletPort) kubeletConfig := &kube_client.KubeletConfig{ Port: uint(kubeletPort), EnableHttps: kubeletHttps, TLSClientConfig: kubeConfig.TLSClientConfig, } kubeletApi, err := datasource.NewKubelet(kubeletConfig) if err != nil { return nil, err } kubePodsSource := NewKubePodMetrics(kubeletPort, kubeletApi, nodesApi, newPodsApi(kubeClient)) kubeNodeSource := NewKubeNodeMetrics(kubeletPort, kubeletApi, nodesApi) kubeEventsSource := NewKubeEvents(kubeClient, c) return []api.Source{kubePodsSource, kubeNodeSource, kubeEventsSource}, nil }