Skip to content

Commit

Permalink
allow context override with -x
Browse files Browse the repository at this point in the history
  • Loading branch information
cjimti committed Nov 24, 2018
1 parent b2a27f6 commit 6e4bb36
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/kubefwd/kubefwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func newRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "kubefwd",
Short: "Expose Kubernetes services for local development.",
Example: " kubefwd services --help\n" +
Example: " kubefwd services --help\n" +
" kubefwd svc -n the-project\n" +
" kubefwd svc -n the-project -l app=wx,component=api\n" +
" kubefwd svc -n default -n the-project\n",
Expand Down
27 changes: 17 additions & 10 deletions cmd/kubefwd/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
)

var namespaces []string
var contexts []string

func init() {
cfgFilePath := ""
Expand All @@ -53,6 +54,7 @@ func init() {
}

Cmd.Flags().StringP("kubeconfig", "c", cfgFilePath, "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 @@ -91,30 +93,35 @@ Try:
}

// k8s rest config
config := utils.K8sConfig(cmd)
selector := cmd.Flag("selector").Value.String()
// TODO: Future support for multiple contexts
config := utils.K8sConfig(cmd, contexts)

if len(namespaces) < 1 {
namespaces = []string{"default"}
// create the client set
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}

// labels selector to filter services
// see: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
selector := cmd.Flag("selector").Value.String()
listOptions := metav1.ListOptions{}
if selector != "" {
listOptions.LabelSelector = selector
}

// create the client set
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
// if no namespaces were specified, explicitly set
// one to "default"
if len(namespaces) < 1 {
namespaces = []string{"default"}
}

wg := &sync.WaitGroup{}

// ipC is the class C for the local IP address
// increment this for each cluster
ipC := 27

wg := &sync.WaitGroup{}

for i, namespace := range namespaces {
err = fwdServices(FwdServiceOpts{
Wg: wg,
Expand Down
20 changes: 12 additions & 8 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,31 @@ func (p *Publisher) Write(b []byte) (int, error) {
return 0, nil
}

func K8sConfig(cmd *cobra.Command) *restclient.Config {
func K8sConfig(cmd *cobra.Command, contexts []string) *restclient.Config {

cfgFilePath := cmd.Flag("kubeconfig").Value.String()
namespace := cmd.Flag("namespace").Value.String()

if namespace == "" {
namespace = "default"
}

if cfgFilePath == "" {
fmt.Println("No config found. Use --kubeconfig to specify one")
os.Exit(1)
}

overrides := &clientcmd.ConfigOverrides{}

if len(contexts) > 0 {
overrides = &clientcmd.ConfigOverrides{CurrentContext: contexts[0]}
}

// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", cfgFilePath)
restConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: cfgFilePath},
overrides,
).ClientConfig()
if err != nil {
panic(err.Error())
}

return config
return restConfig
}

type PortForwardOpts struct {
Expand Down

0 comments on commit 6e4bb36

Please sign in to comment.