Skip to content

Commit

Permalink
review comment fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ShwethaKumbla committed Jun 13, 2021
1 parent 115c5e5 commit c59f51e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
18 changes: 13 additions & 5 deletions klient/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

// DefaultClusterContext default cluster context
var DefaultClusterContext = ""

// New returns Kubernetes configuration value of type *rest.Config.
// filename is kubeconfig file
func New(fileName string) (*rest.Config, error) {
// if filename is not provided assume in-cluster-config
if fileName == "" {
Expand All @@ -50,9 +52,15 @@ func NewWithContextName(fileName, context string) (*rest.Config, error) {
}).ClientConfig()
}

// NewInCluster for clients that expect to be
// running inside a pod on kubernetes
func NewInCluster() (*rest.Config, error) {
return rest.InClusterConfig()
}

// ResolveKubeConfigFile returns the kubeconfig file from
// either flag --kubeconfig or env KUBECONFIG.
// It will only call flag.Parse() if flag.Parsed() is false.
// If flag.Parsed() is true then lookup for --kubeconfig flag.
// If --kubeconfig, or KUBECONFIG, or $HOME/.kube/config not provided then
// assume in cluster.
func ResolveKubeConfigFile() string {
Expand Down Expand Up @@ -101,9 +109,9 @@ func ResolveKubeConfigFile() string {

// ResolveClusterContext returns cluster context name based on --context flag.
func ResolveClusterContext() string {
// If a flag --context is specified use that
// If a flag --kube-context is specified use that
if flag.Parsed() {
f := flag.Lookup("context")
f := flag.Lookup("kube-context")
if f != nil {
return f.Value.String()
}
Expand All @@ -113,8 +121,8 @@ func ResolveClusterContext() string {
}

// exists reports whether the named file or directory exists.
func exists(name string) bool {
if _, err := os.Stat(name); err != nil {
func exists(filePath string) bool {
if _, err := os.Stat(filePath); err != nil {
if os.IsNotExist(err) {
return false
}
Expand Down
6 changes: 3 additions & 3 deletions klient/conf/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ func setup() {
// generate kube config data
data := genKubeconfig("test-context")

err = createFile(path, data)
err = createFile(kubeconfigpath, data)
if err != nil {
fmt.Println("failed to create config file", err)
return
}
}

fmt.Println("file created successfully", path)
fmt.Println("file created successfully", kubeconfigpath)

flag.StringVar(&kubeconfig, "kubeconfig", "", "Paths to a kubeconfig. Only required if out-of-cluster.")

// set --kubeconfig flag
err = flag.Set("kubeconfig", path)
err = flag.Set("kubeconfig", kubeconfigpath)
if err != nil {
fmt.Println("unexpected error while setting flag value", err)
return
Expand Down

0 comments on commit c59f51e

Please sign in to comment.