Skip to content

Commit

Permalink
Merge pull request #86 from calmkart/issue81
Browse files Browse the repository at this point in the history
enhancement:issue81, custom GetConfig -> change to k8s ToRawKubeConfigLoader
  • Loading branch information
cjimti authored Nov 7, 2019
2 parents 6c8c96e + f7c6f5c commit 963be3e
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 137 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea
.DS_Store
.vscode
dist
vendor

57 changes: 23 additions & 34 deletions cmd/kubefwd/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package services

import (
"fmt"
"os"
"path/filepath"
"strconv"
"sync"
"time"
Expand All @@ -33,7 +31,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
Expand All @@ -54,22 +52,7 @@ func init() {
log.Errorf("Runtime error: %s", err.Error())
}

cfgFilePath := ""

if home := fwdhost.HomeDir(); home != "" {
cfgFilePath = filepath.Join(home, ".kube", "config")
}

// if sudo -E is used and the KUBECONFIG environment variable is set
// make it the default, override with command line.
envCfg, ok := os.LookupEnv("KUBECONFIG")
if ok {
if envCfg != "" {
cfgFilePath = envCfg
}
}

Cmd.Flags().StringP("kubeconfig", "c", cfgFilePath, "absolute path to a kubectl config file")
Cmd.Flags().StringP("kubeconfig", "c", "", "absolute path to a kubectl config file")
Cmd.Flags().StringSliceVarP(&contexts, "context", "x", []string{}, "specify a context to override the current context")
Cmd.Flags().StringSliceVarP(&namespaces, "namespace", "n", []string{}, "Specify a namespace. Specify multiple namespaces by duplicating this argument.")
Cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on; supports '=', '==', and '!=' (e.g. -l key1=value1,key2=value2).")
Expand Down Expand Up @@ -128,15 +111,23 @@ Try:

log.Printf("Hostfile management: %s", msg)

// NOTE: may be using the default set in init()
cfgFilePath := cmd.Flag("kubeconfig").Value.String()
if cfgFilePath == "" {
log.Fatalf("No config found. Use --kubeconfig to specify one")
// if sudo -E is used and the KUBECONFIG environment variable is set
// it's easy to merge with kubeconfig files in env automatic.
// if KUBECONFIG is blank, ToRawKubeConfigLoader() will use the
// default kubeconfig file in $HOME/.kube/config
cfgFilePath := ""

// if we set the option --kubeconfig, It will have a higher priority
// than KUBECONFIG environment. so it will override the KubeConfig options.
flagCfgFilePath := cmd.Flag("kubeconfig").Value.String()
if flagCfgFilePath != "" {
cfgFilePath = flagCfgFilePath
}

clientConfig, err := fwdcfg.GetConfig(cfgFilePath)
// build the ClientConfig
rawConfig, err := fwdcfg.GetClientConfig(cfgFilePath)
if err != nil {
log.Fatalf("Error reading configuration configuration: %s\n", err.Error())
log.Fatalf("Error in get rawConfig: %s\n", err.Error())
}

// labels selector to filter services
Expand All @@ -151,18 +142,17 @@ Try:
// explicitly set one to "default"
if len(namespaces) < 1 {
namespaces = []string{"default"}
x := clientConfig.CurrentContext

x := rawConfig.CurrentContext
// use the first context if specified
if len(contexts) > 0 {
x = contexts[0]
}

for _, ctx := range clientConfig.Contexts {
if ctx.Name == x {
if ctx.Context.Namespace != "" {
log.Printf("Using namespace %s from current context %s.", ctx.Context.Namespace, ctx.Name)
namespaces = []string{ctx.Context.Namespace}
for ctxName, ctxConfig := range rawConfig.Contexts {
if ctxName == x {
if ctxConfig.Namespace != "" {
log.Printf("Using namespace %s from current context %s.", ctxConfig.Namespace, ctxName)
namespaces = []string{ctxConfig.Namespace}
break
}
}
Expand All @@ -177,11 +167,10 @@ Try:

// if no context override
if len(contexts) < 1 {
contexts = append(contexts, clientConfig.CurrentContext)
contexts = append(contexts, rawConfig.CurrentContext)
}

for i, ctx := range contexts {

// k8s REST config
restConfig, err := fwdcfg.GetRestConfig(cfgFilePath, ctx)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ go 1.13

require (
github.com/elazarl/goproxy v0.0.0-20181111060418-2ce16c963a8a // indirect
github.com/gogo/protobuf v1.1.1 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5
github.com/txn2/txeh v1.2.1
gopkg.in/yaml.v2 v2.2.2
k8s.io/api v0.0.0-20190620084959-7cf5895f2711
k8s.io/apimachinery v0.0.0-20190612205821-1799e75a0719
k8s.io/client-go v0.0.0-20190620085101-78d2af792bab
gopkg.in/yaml.v2 v2.2.4
k8s.io/api v0.0.0-20191031065753-b19d8caf39be
k8s.io/apimachinery v0.0.0-20191102025618-50aa20a7b23f
k8s.io/cli-runtime v0.0.0-20191102031428-d1199d98239f
k8s.io/client-go v0.0.0-20191101230044-e9766ae82012
)
Loading

0 comments on commit 963be3e

Please sign in to comment.