Skip to content

Commit

Permalink
Add context flag whenever kubeconfig flag is added
Browse files Browse the repository at this point in the history
Some users may have multiple clusters in their config;
adding support for context is very minimal and helps
simplify their Sonobuoy integration.

Fixes #421

Signed-off-by: John Schnake <jschnake@vmware.com>
  • Loading branch information
johnSchnake committed Apr 13, 2019
1 parent 922fd03 commit 566e80a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cmd/sonobuoy/app/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ func AddKubeConformanceImageVersion(imageVersion *image.ConformanceImageVersion,
flags.Var(imageVersion, "kube-conformance-image-version", help)
}

// AddKubeconfigFlag adds a kubeconfig flag to the provided command.
// AddKubeconfigFlag adds a kubeconfig and context flags to the provided command.
func AddKubeconfigFlag(cfg *Kubeconfig, flags *pflag.FlagSet) {
// The default is the empty string (look in the environment)
flags.Var(cfg, "kubeconfig", "Path to explicit kubeconfig file.")
flags.StringVar(&cfg.Context, "context", "", "Context in the kubeconfig to use.")
}

// AddPluginFlag describes which plugin's images to interact with
Expand Down
7 changes: 6 additions & 1 deletion cmd/sonobuoy/app/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import (
"github.com/spf13/pflag"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

// Add auth providers
_ "k8s.io/client-go/plugin/pkg/client/auth"
)

// Kubeconfig represents an explict or implict kubeconfig
type Kubeconfig struct {
*clientcmd.ClientConfigLoadingRules
Context string
}

// Make sure Kubeconfig implements Value properly
Expand Down Expand Up @@ -55,12 +57,15 @@ func (c *Kubeconfig) Set(str string) error {

// Get returns a rest Config, possibly based on a provided config
func (c *Kubeconfig) Get() (*rest.Config, error) {

if c.ClientConfigLoadingRules == nil {
c.ClientConfigLoadingRules = clientcmd.NewDefaultClientConfigLoadingRules()
}

configOverrides := &clientcmd.ConfigOverrides{}
if len(c.Context) > 0 {
configOverrides.CurrentContext = c.Context
}

kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(c, configOverrides)
return kubeConfig.ClientConfig()
}

0 comments on commit 566e80a

Please sign in to comment.