Skip to content

Commit

Permalink
rename InClusterConfig() to GetClusterConfig() (#137)
Browse files Browse the repository at this point in the history
use env var KUBECONFIG to retrieve kubeconfig (same as kubectl)
  • Loading branch information
moon03432 authored and jlewi committed Nov 15, 2017
1 parent a998436 commit 24412ea
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/tf_operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func init() {
flag.Parse()

// Workaround for watching TPR resource.
restCfg, err := k8sutil.InClusterConfig()
restCfg, err := k8sutil.GetClusterConfig()
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion developer_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ a K8s cluster.

Set your environment
```sh
export USE_KUBE_CONFIG=$(echo ~/.kube/config)
export KUBECONFIG=$(echo ~/.kube/config)
export MY_POD_NAMESPACE=default
export MY_POD_NAME=my-pod
```
Expand Down
15 changes: 8 additions & 7 deletions pkg/util/k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,36 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

const RecommendedConfigPathEnvVar = "KUBECONFIG"

// TODO(jlewi): I think this function is used to add an owner to a resource. I think we we should use this
// method to ensure all resources created for the TfJob are owned by the TfJob.
func addOwnerRefToObject(o metav1.Object, r metav1.OwnerReference) {
o.SetOwnerReferences(append(o.GetOwnerReferences(), r))
}

func MustNewKubeClient() kubernetes.Interface {
cfg, err := InClusterConfig()
cfg, err := GetClusterConfig()
if err != nil {
log.Fatal(err)
}
return kubernetes.NewForConfigOrDie(cfg)
}

func MustNewApiExtensionsClient() apiextensionsclient.Interface {
cfg, err := InClusterConfig()
cfg, err := GetClusterConfig()
if err != nil {
log.Fatal(err)
}
return apiextensionsclient.NewForConfigOrDie(cfg)
}

// TODO(jlewi): We should rename InClusterConfig to reflect the fact that we can obtain the config from the Kube
// configuration used by kubeconfig.
func InClusterConfig() (*rest.Config, error) {
if len(os.Getenv("USE_KUBE_CONFIG")) > 0 {
// Obtain the config from the Kube configuration used by kubeconfig, or from k8s cluster.
func GetClusterConfig() (*rest.Config, error) {
if len(os.Getenv(RecommendedConfigPathEnvVar)) > 0 {
// use the current context in kubeconfig
// This is very useful for running locally.
return clientcmd.BuildConfigFromFlags("", os.Getenv("USE_KUBE_CONFIG"))
return clientcmd.BuildConfigFromFlags("", os.Getenv(RecommendedConfigPathEnvVar))
}

// Work around https://github.com/kubernetes/kubernetes/issues/40973
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/k8sutil/tpr_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type TfJobRestClient struct {
}

func NewTfJobClient() (*TfJobRestClient, error) {
config, err := InClusterConfig()
config, err := GetClusterConfig()
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 24412ea

Please sign in to comment.