Skip to content

Commit

Permalink
Feature: Set current kubernetes context
Browse files Browse the repository at this point in the history
  • Loading branch information
waveywaves authored and tekton-robot committed Nov 28, 2019
1 parent 4c772bd commit c915e7d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
3 changes: 3 additions & 0 deletions pkg/cli/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type Params interface {
// SetKubeConfigPath uses the kubeconfig path to instantiate tekton
// returned by Clientset function
SetKubeConfigPath(string)
// SetKubeContext extends the specificity of the above SetKubeConfigPath
// by using a context other than the default context in the given kubeconfig
SetKubeContext(string)
Clients() (*Clients, error)
KubeClient() (k8s.Interface, error)

Expand Down
8 changes: 8 additions & 0 deletions pkg/cli/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
type TektonParams struct {
clients *Clients
kubeConfigPath string
kubeContext string
namespace string
}

Expand All @@ -38,6 +39,10 @@ func (p *TektonParams) SetKubeConfigPath(path string) {
p.kubeConfigPath = path
}

func (p *TektonParams) SetKubeContext(context string) {
p.kubeContext = context
}

func (p *TektonParams) tektonClient(config *rest.Config) (versioned.Interface, error) {
cs, err := versioned.NewForConfig(config)
if err != nil {
Expand Down Expand Up @@ -108,6 +113,9 @@ func (p *TektonParams) config() (*rest.Config, error) {
loadingRules.ExplicitPath = p.kubeConfigPath
}
configOverrides := &clientcmd.ConfigOverrides{}
if p.kubeContext != "" {
configOverrides.CurrentContext = p.kubeContext
}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
if p.namespace == "" {
namespace, _, err := kubeConfig.Namespace()
Expand Down
11 changes: 11 additions & 0 deletions pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

const (
kubeConfig = "kubeconfig"
context = "context"
namespace = "namespace"
nocolour = "nocolour"
)
Expand All @@ -33,6 +34,10 @@ func AddTektonOptions(cmd *cobra.Command) {
kubeConfig, "k", "",
"kubectl config file (default: $HOME/.kube/config)")

cmd.PersistentFlags().StringP(
context, "c", "",
"name of the kubeconfig context to use (default: kubectl config current-context)")

cmd.PersistentFlags().StringP(
namespace, "n", "",
"namespace to use (default: from $KUBECONFIG)")
Expand Down Expand Up @@ -61,6 +66,12 @@ func InitParams(p cli.Params, cmd *cobra.Command) error {
}
p.SetKubeConfigPath(kcPath)

kContext, err := cmd.Flags().GetString(context)
if err != nil {
return err
}
p.SetKubeContext(kContext)

// ensure that the config is valid by creating a client
if _, err := p.Clients(); err != nil {
return err
Expand Down
18 changes: 11 additions & 7 deletions pkg/test/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (
)

type Params struct {
ns, kc string
Tekton versioned.Interface
Kube k8s.Interface
Clock clockwork.Clock
Cls *cli.Clients
ns, kConfig, kContext string
Tekton versioned.Interface
Kube k8s.Interface
Clock clockwork.Clock
Cls *cli.Clients
}

var _ cli.Params = &Params{}
Expand All @@ -42,11 +42,15 @@ func (p *Params) SetNoColour(b bool) {
}

func (p *Params) SetKubeConfigPath(path string) {
p.kc = path
p.kConfig = path
}

func (p *Params) SetKubeContext(context string) {
p.kContext = context
}

func (p *Params) KubeConfigPath() string {
return p.kc
return p.kConfig
}

func (p *Params) tektonClient() (versioned.Interface, error) {
Expand Down

0 comments on commit c915e7d

Please sign in to comment.