Skip to content

Commit

Permalink
parse and use --kubeconfig flag, fix #9
Browse files Browse the repository at this point in the history
  • Loading branch information
kovetskiy committed Jun 7, 2019
1 parent 3760d63 commit c6efeff
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/tubectl/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func matchResources(resources []Resource, params *ParamsMatch) ([]Resource, erro

func completeParams(client string, params *Params) (*Params, error) {
if params.CompleteContext {
contexts, err := parseKubernetesContexts()
contexts, err := parseKubernetesContexts(params.Kubeconfig)
if err != nil {
return params, err
}
Expand Down
32 changes: 28 additions & 4 deletions cmd/tubectl/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ type Resource struct {
Namespace string
}

func parseKubernetesContexts() ([]string, error) {
config, err := clientcmdapi.NewDefaultClientConfigLoadingRules().Load()
func parseKubernetesContexts(kubeconfig string) ([]string, error) {
loader := clientcmdapi.NewDefaultClientConfigLoadingRules()
if kubeconfig != "" {
loader.Precedence = append(
[]string{kubeconfig},
loader.Precedence...,
)
}

config, err := loader.Load()
if err != nil {
return nil, karma.Format(
err,
Expand All @@ -38,7 +46,10 @@ func parseKubernetesContexts() ([]string, error) {
func requestNamespaces(client string, params *Params) ([]string, error) {
// omit namespace argument because requesting list of them
cmd, args := getCommand(
client, buildArgContext(params.Context), "", "",
client,
buildArgKubeconfig(params.Kubeconfig),
buildArgContext(params.Context),
"", "",
"get", "namespaces", "-o", "json",
)

Expand Down Expand Up @@ -76,6 +87,7 @@ func requestNamespaces(client string, params *Params) ([]string, error) {
func requestResources(client string, params *Params) ([]Resource, error) {
cmd, args := getCommand(
client,
buildArgKubeconfig(params.Kubeconfig),
buildArgContext(params.Context),
buildArgNamespace(params.Namespace),
buildArgAllNamespaces(params.AllNamespaces),
Expand Down Expand Up @@ -133,12 +145,16 @@ func unmarshalResources(contents []byte) ([]Resource, error) {

func getCommand(
client string,
argContext,
argKubeconfig string,
argContext string,
argNamespace string,
argAllNamespaces string,
value ...string,
) (*exec.Cmd, []string) {
args := []string{}
if argKubeconfig != "" {
args = append(args, argKubeconfig)
}
if argContext != "" {
args = append(args, argContext)
}
Expand All @@ -155,6 +171,14 @@ func getCommand(
return exec.Command(client, args...), append([]string{client}, args...)
}

func buildArgKubeconfig(value string) string {
if value != "" {
return "--kubeconfig=" + value
}

return ""
}

func buildArgContext(value string) string {
if value != "" {
return "--context=" + value
Expand Down
4 changes: 4 additions & 0 deletions cmd/tubectl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func main() {
func syscallExec(client string, params *Params) {
args := []string{client}

if arg := buildArgKubeconfig(params.Kubeconfig); arg != "" {
args = append(args, arg)
}

if arg := buildArgContext(params.Context); arg != "" {
args = append(args, arg)
}
Expand Down
38 changes: 37 additions & 1 deletion cmd/tubectl/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type Params struct {
Namespace string
AllNamespaces bool

Match *ParamsMatch
Match *ParamsMatch
Kubeconfig string
}

type ParamsMatch struct {
Expand Down Expand Up @@ -46,6 +47,8 @@ const (
FlagNamespaceShort = "-n"
FlagNamespaceShortValue = "-n="
FlagAllNamespaces = "--all-namespaces"
FlagKubeconfig = "--kubeconfig"
FlagKubeconfigValue = "--kubeconfig="
)

var (
Expand Down Expand Up @@ -74,6 +77,18 @@ func parseParams(raw []string) *Params {
continue
}

if params.Kubeconfig == "" {
var usedNext bool
params.Kubeconfig, usedNext = parseKubeconfig(value, raw, index)
if usedNext {
index++
}

if params.Kubeconfig != "" {
continue
}
}

if params.Context == "" {
var usedNext bool
params.Context, usedNext = parseContext(value, raw, index)
Expand Down Expand Up @@ -239,6 +254,27 @@ func parseContext(
return "", false
}

func parseKubeconfig(
value string,
args []string,
index int,
) (name string, usedNext bool) {
if value == FlagKubeconfig {
if index+1 <= len(args)-1 {
return args[index+1], true
}

return "", false
}

if len(value) > len(FlagKubeconfigValue) &&
strings.HasPrefix(value, FlagKubeconfigValue) {
return value[len(FlagKubeconfigValue):], false
}

return "", false
}

func parseNamespace(
value string,
args []string,
Expand Down
21 changes: 21 additions & 0 deletions cmd/tubectl/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,27 @@ func TestParams_Suites(t *testing.T) {
Args: []string{"blah%"},
},
},
{
[]string{"--kubeconfig=x", "a"},
&Params{
Args: []string{"a"},
Kubeconfig: "x",
},
},
{
[]string{"--kubeconfig", "x", "a"},
&Params{
Args: []string{"a"},
Kubeconfig: "x",
},
},
{
[]string{"--kubeconfig", "x", "a", "--kubeconfig=y", "z"},
&Params{
Args: []string{"a", "--kubeconfig=y", "z"},
Kubeconfig: "x",
},
},
//
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/tubectl/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func getTask(client string, params *Params, resource Resource) Task {
return func(writer io.Writer) error {
values := []string{}

if arg := buildArgKubeconfig(params.Kubeconfig); arg != "" {
values = append(values, arg)
}

if arg := buildArgContext(params.Context); arg != "" {
values = append(values, arg)
}
Expand Down

0 comments on commit c6efeff

Please sign in to comment.