Skip to content

Commit

Permalink
Clean up glbc k8s client initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
bowei committed Dec 29, 2017
1 parent fbfd9d6 commit 3c5e408
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
35 changes: 14 additions & 21 deletions cmd/glbc/app/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package app

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
Expand All @@ -29,45 +28,39 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/ingress-gce/pkg/utils"
"k8s.io/kubernetes/pkg/cloudprovider"
"k8s.io/kubernetes/pkg/cloudprovider/providers/gce"

// Register the GCP authorization provider.
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)

const (
// Sleep interval to retry cloud client creation.
cloudClientRetryInterval = 10 * time.Second
)

// NewKubeClient returns a Kubernetes client given the command line settings.
func NewKubeClient() (kubernetes.Interface, error) {
var err error
var config *rest.Config

if Flags.InCluster {
if config, err = rest.InClusterConfig(); err != nil {
return nil, err
}
} else {
if Flags.APIServerHost == "" {
return nil, fmt.Errorf("please specify the api server address using the flag --apiserver-host")
}

config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: Flags.KubeConfigFile},
&clientcmd.ConfigOverrides{
ClusterInfo: clientcmdapi.Cluster{
Server: Flags.APIServerHost,
},
}).ClientConfig()
glog.V(0).Infof("Using in cluster configuration")
config, err := rest.InClusterConfig()
if err != nil {
glog.Fatalf("error creating client configuration: %v", err)
return nil, err
}
return kubernetes.NewForConfig(config)
}

glog.V(0).Infof("Using APIServerHost=%q, KubeConfig=%q", Flags.APIServerHost, Flags.KubeConfigFile)
config, err := clientcmd.BuildConfigFromFlags(Flags.APIServerHost, Flags.KubeConfigFile)
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(config)
}

// NewGCEClient returns a client to the GCE environment.
func NewGCEClient(config io.Reader) *gce.GCECloud {
getConfigReader := func() io.Reader { return nil }

Expand Down
4 changes: 2 additions & 2 deletions cmd/glbc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func main() {
flag.Set("v", "4")
}

glog.V(0).Infof("Starting GLBC image: %v, cluster name %v", imageVersion, app.Flags.ClusterName)
glog.V(0).Infof("Starting GLBC image: %q, cluster name %q", imageVersion, app.Flags.ClusterName)

kubeClient, err := app.NewKubeClient()
if err != nil {
glog.Fatalf("Failed to create client: %v.", err)
glog.Fatalf("Failed to create client: %v", err)
}

var namer *utils.Namer
Expand Down

0 comments on commit 3c5e408

Please sign in to comment.