Skip to content

Commit

Permalink
feat(config): enable setting path to kubeconfig (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockopp authored Feb 24, 2020
1 parent 4dceb78 commit 625d1e2
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 56 deletions.
14 changes: 10 additions & 4 deletions cmd/vela-kubernetes/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ func (a *Apply) Command(c *Config, file string) *exec.Cmd {
// variable to store flags for command
var flags []string

// check if config namespace is provided
if len(c.Namespace) > 0 {
// add flag for namespace from provided config namespace
flags = append(flags, fmt.Sprintf("--namespace=%s", c.Namespace))
// check if config path is provided
if len(c.Path) > 0 {
// add flag for path from provided config path
flags = append(flags, fmt.Sprintf("--kubeconfig=%s", c.Path))
}

// check if config cluster is provided
Expand All @@ -49,6 +49,12 @@ func (a *Apply) Command(c *Config, file string) *exec.Cmd {
flags = append(flags, fmt.Sprintf("--context=%s", c.Context))
}

// check if config namespace is provided
if len(c.Namespace) > 0 {
// add flag for namespace from provided config namespace
flags = append(flags, fmt.Sprintf("--namespace=%s", c.Namespace))
}

// add flag for apply kubectl command
flags = append(flags, "apply")

Expand Down
6 changes: 4 additions & 2 deletions cmd/vela-kubernetes/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ func TestKubernetes_Apply_Command(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
File: "file",
Cluster: "cluster",
Context: "context",
File: "file",
Namespace: "namespace",
Path: "~/.kube/config",
}

a := &Apply{
Expand All @@ -30,9 +31,10 @@ func TestKubernetes_Apply_Command(t *testing.T) {
for _, file := range a.Files {
want := exec.Command(
kubectlBin,
fmt.Sprintf("--namespace=%s", c.Namespace),
fmt.Sprintf("--kubeconfig=%s", c.Path),
fmt.Sprintf("--cluster=%s", c.Cluster),
fmt.Sprintf("--context=%s", c.Context),
fmt.Sprintf("--namespace=%s", c.Namespace),
"apply",
fmt.Sprintf("--dry-run=%t", a.DryRun),
fmt.Sprintf("--filename=%s", file),
Expand Down
34 changes: 32 additions & 2 deletions cmd/vela-kubernetes/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,38 @@ func execCmd(e *exec.Cmd) error {

// versionCmd is a helper function to output
// the client and server version information.
func versionCmd() *exec.Cmd {
func versionCmd(c *Config) *exec.Cmd {
logrus.Trace("creating kubectl version command")

return exec.Command(kubectlBin, "version")
// variable to store flags for command
var flags []string

// check if config path is provided
if len(c.Path) > 0 {
// add flag for path from provided config path
flags = append(flags, fmt.Sprintf("--kubeconfig=%s", c.Path))
}

// check if config cluster is provided
if len(c.Cluster) > 0 {
// add flag for cluster from provided config cluster
flags = append(flags, fmt.Sprintf("--cluster=%s", c.Cluster))
}

// check if config context is provided
if len(c.Context) > 0 {
// add flag for context from provided config context
flags = append(flags, fmt.Sprintf("--context=%s", c.Context))
}

// check if config namespace is provided
if len(c.Namespace) > 0 {
// add flag for namespace from provided config namespace
flags = append(flags, fmt.Sprintf("--namespace=%s", c.Namespace))
}

// add flag for version kubectl command
flags = append(flags, "version")

return exec.Command(kubectlBin, flags...)
}
16 changes: 15 additions & 1 deletion cmd/vela-kubernetes/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package main

import (
"fmt"
"os/exec"
"reflect"
"testing"
Expand All @@ -22,12 +23,25 @@ func TestKubernetes_execCmd(t *testing.T) {

func TestKubernetes_versionCmd(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
Cluster: "cluster",
Context: "context",
File: "file",
Namespace: "namespace",
Path: "~/.kube/config",
}

want := exec.Command(
kubectlBin,
fmt.Sprintf("--kubeconfig=%s", c.Path),
fmt.Sprintf("--cluster=%s", c.Cluster),
fmt.Sprintf("--context=%s", c.Context),
fmt.Sprintf("--namespace=%s", c.Namespace),
"version",
)

got := versionCmd()
got := versionCmd(c)

if !reflect.DeepEqual(got, want) {
t.Errorf("versionCmd is %v, want %v", got, want)
Expand Down
19 changes: 13 additions & 6 deletions cmd/vela-kubernetes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ var appFS = afero.NewOsFs()
type Config struct {
// action to perform against Kubernetes
Action string
// configuration file for communication with Kubernetes
File string
// name of the configuration cluster from file
Cluster string
// name of the configuration context from file
Context string
// configuration file for communication with Kubernetes
File string
// name of the configuration namespace from file
Namespace string
// path to configuration file for communcation with Kubernetes
Path string
}

// Validate verifies the Config is properly configured.
Expand Down Expand Up @@ -71,14 +73,19 @@ func (c *Config) Write() error {
home = u.HomeDir
}

// create full path for .kube/config file
path := filepath.Join(home, ".kube/config")
// check if a custom config path was provided
if len(c.Path) == 0 {
// create full path for .kube/config file
c.Path = filepath.Join(home, ".kube/config")
}

logrus.Debugf("Creating kubectl configuration file %s", c.Path)

// send Filesystem call to create directory path for .kube/config file
err = a.Fs.MkdirAll(filepath.Dir(path), 0777)
err = a.Fs.MkdirAll(filepath.Dir(c.Path), 0777)
if err != nil {
return err
}

return a.WriteFile(path, []byte(c.File), 0600)
return a.WriteFile(c.Path, []byte(c.File), 0600)
}
8 changes: 4 additions & 4 deletions cmd/vela-kubernetes/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ func TestKubernetes_Config_Validate(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
File: "file",
Cluster: "cluster",
Context: "context",
File: "file",
Namespace: "namespace",
}

Expand All @@ -30,9 +30,9 @@ func TestKubernetes_Config_Validate_NoAction(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
File: "file",
Cluster: "cluster",
Context: "context",
File: "file",
Namespace: "namespace",
}

Expand Down Expand Up @@ -64,9 +64,9 @@ func TestKubernetes_Config_Write(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
File: "file",
Cluster: "cluster",
Context: "context",
File: "file",
Namespace: "namespace",
}

Expand All @@ -83,9 +83,9 @@ func TestKubernetes_Config_Write_Error(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
File: "file",
Cluster: "cluster",
Context: "context",
File: "file",
Namespace: "namespace",
}

Expand Down
18 changes: 12 additions & 6 deletions cmd/vela-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ func main() {
Name: "config.action",
Usage: "action to perform against Kubernetes",
},
cli.StringFlag{
EnvVar: "PARAMETER_CONFIG,CONFIG_FILE,KUBE_CONFIG",
Name: "config.file",
Usage: "kubectl configuration for interacting with Kubernetes",
},
cli.StringFlag{
EnvVar: "PARAMETER_CLUSTER,CONFIG_CLUSTER,KUBE_CLUSTER",
Name: "config.cluster",
Expand All @@ -87,11 +82,21 @@ func main() {
Name: "config.context",
Usage: "kubectl context for interacting with Kubernetes",
},
cli.StringFlag{
EnvVar: "PARAMETER_CONFIG,CONFIG_FILE,KUBE_CONFIG",
Name: "config.file",
Usage: "kubectl configuration for interacting with Kubernetes",
},
cli.StringFlag{
EnvVar: "PARAMETER_NAMESPACE,CONFIG_NAMESPACE,KUBE_NAMESPACE",
Name: "config.namespace",
Usage: "kubectl namespace for interacting with Kubernetes",
},
cli.StringFlag{
EnvVar: "PARAMETER_CONFIG_PATH,CONFIG_PATH,KUBE_CONFIG_PATH",
Name: "config.path",
Usage: "path to kubectl configuration file",
},

// Patch Flags

Expand Down Expand Up @@ -182,10 +187,11 @@ func run(c *cli.Context) error {
// config configuration
Config: &Config{
Action: c.String("config.action"),
File: c.String("config.file"),
Cluster: c.String("config.cluster"),
Context: c.String("config.context"),
File: c.String("config.file"),
Namespace: c.String("config.namespace"),
Path: c.String("config.path"),
},
// patch configuration
Patch: &Patch{
Expand Down
14 changes: 10 additions & 4 deletions cmd/vela-kubernetes/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func (p *Patch) Command(c *Config, file string, container *Container) *exec.Cmd
// variable to store flags for command
var flags []string

// check if config namespace is provided
if len(c.Namespace) > 0 {
// add flag for namespace from provided config namespace
flags = append(flags, fmt.Sprintf("--namespace=%s", c.Namespace))
// check if config path is provided
if len(c.Path) > 0 {
// add flag for path from provided config path
flags = append(flags, fmt.Sprintf("--kubeconfig=%s", c.Path))
}

// check if config cluster is provided
Expand All @@ -71,6 +71,12 @@ func (p *Patch) Command(c *Config, file string, container *Container) *exec.Cmd
flags = append(flags, fmt.Sprintf("--context=%s", c.Context))
}

// check if config namespace is provided
if len(c.Namespace) > 0 {
// add flag for namespace from provided config namespace
flags = append(flags, fmt.Sprintf("--namespace=%s", c.Namespace))
}

// add flag for patch kubectl command
flags = append(flags, "patch")

Expand Down
8 changes: 5 additions & 3 deletions cmd/vela-kubernetes/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import (
func TestKubernetes_Patch_Command(t *testing.T) {
// setup types
c := &Config{
Action: "patch",
File: "file",
Action: "apply",
Cluster: "cluster",
Context: "context",
File: "file",
Namespace: "namespace",
Path: "~/.kube/config",
}

p := &Patch{
Expand All @@ -40,9 +41,10 @@ func TestKubernetes_Patch_Command(t *testing.T) {

want := exec.Command(
kubectlBin,
fmt.Sprintf("--namespace=%s", c.Namespace),
fmt.Sprintf("--kubeconfig=%s", c.Path),
fmt.Sprintf("--cluster=%s", c.Cluster),
fmt.Sprintf("--context=%s", c.Context),
fmt.Sprintf("--namespace=%s", c.Namespace),
"patch",
fmt.Sprintf("--local=%t", p.DryRun),
fmt.Sprintf("--filename=%s", file),
Expand Down
2 changes: 1 addition & 1 deletion cmd/vela-kubernetes/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (p *Plugin) Exec() error {
}

// output kubectl version for troubleshooting
err = execCmd(versionCmd())
err = execCmd(versionCmd(p.Config))
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 625d1e2

Please sign in to comment.