Skip to content

Commit

Permalink
support pass in kubeconfig and master to client-go by flag
Browse files Browse the repository at this point in the history
  • Loading branch information
gaorong committed Nov 5, 2019
1 parent 9cc2a27 commit 3ad83d7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package main

import (
"flag"
"io"
"os"

Expand All @@ -31,20 +32,28 @@ const (
)

var (
version string
version string
kubeconfig string
master string
)

func init() {
flag.StringVar(&master, "master", "", "The address of the Kubernetes API server (overrides any value in kubeconfig).")
flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to kubeconfig file with authorization and master location information.")
}

func main() {
os.Exit(_main())
}

func _main() int {
defer log.Flush()
flag.Parse()
logger.SetupLogger(logger.GetLogFileLocation(defaultLogFilePath))

log.Infof("Starting L-IPAMD %s ...", version)

kubeClient, err := k8sapi.CreateKubeClient()
kubeClient, err := k8sapi.CreateKubeClient(kubeconfig, master)
if err != nil {
log.Errorf("Failed to create client: %v", err)
return 1
Expand Down
20 changes: 17 additions & 3 deletions pkg/k8sapi/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import (
"sync"
"time"

"github.com/operator-framework/operator-sdk/pkg/k8sclient"
"github.com/pkg/errors"

log "github.com/cihub/seelog"

clientset "k8s.io/client-go/kubernetes"

"github.com/operator-framework/operator-sdk/pkg/k8sclient"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/workqueue"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -80,8 +81,21 @@ func NewController(clientset kubernetes.Interface) *Controller {
}

// CreateKubeClient creates a k8s client
func CreateKubeClient() (clientset.Interface, error) {
kubeClient := k8sclient.GetKubeClient()
func CreateKubeClient(kubeconfig, master string) (clientset.Interface, error) {
var kubeClient clientset.Interface
if master == "" && kubeconfig == "" {
kubeClient = k8sclient.GetKubeClient()
} else {
config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
if err != nil {
panic(err)
}
kubeClient, err = clientset.NewForConfig(config)
if err != nil {
panic(err)
}
}

// Informers don't seem to do a good job logging error messages when it
// can't reach the server, making debugging hard. This makes it easier to
// figure out if apiserver is configured incorrectly.
Expand Down

0 comments on commit 3ad83d7

Please sign in to comment.