Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
support context and namespace kubectl flags (#5)
Browse files Browse the repository at this point in the history
* support context and namespace kubectl flags
Signed-off-by: VJ Patel <[email protected]>
  • Loading branch information
VJftw authored Jan 17, 2020
1 parent 068eecc commit 38eb6bf
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 19 deletions.
9 changes: 8 additions & 1 deletion cmd/dracon/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ import (
"github.com/spf13/viper"
)

var cfgFile string
// flags
var (
cfgFile string
kubernetesContext string
kubernetesNamespace string
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand All @@ -47,6 +52,8 @@ func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dracon.yaml)")
rootCmd.PersistentFlags().StringVar(&kubernetesContext, "context", "", "Kubernetes Context")
rootCmd.PersistentFlags().StringVar(&kubernetesNamespace, "namespace", "", "Kubernetes Namespace")
}

// initConfig reads in config file and ENV variables if set.
Expand Down
10 changes: 8 additions & 2 deletions cmd/dracon/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,21 @@ var runCmd = &cobra.Command{
}

for _, doc := range resDocs["PipelineResource"] {
err = kubernetes.Apply(string(doc))
err = kubernetes.Apply(string(doc), &kubernetes.KubectlOpts{
Namespace: kubernetesNamespace,
Context: kubernetesContext,
})
if err != nil {
log.Fatalf("Failed to apply templates: %s\n", err)
os.Exit(2)
}
}

for _, doc := range resDocs["PipelineRun"] {
err = kubernetes.Apply(string(doc))
err = kubernetes.Apply(string(doc), &kubernetes.KubectlOpts{
Namespace: kubernetesNamespace,
Context: kubernetesContext,
})
if err != nil {
log.Fatalf("Failed to apply templates: %s\n", err)
os.Exit(2)
Expand Down
5 changes: 4 additions & 1 deletion cmd/dracon/cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ var setupCmd = &cobra.Command{
for k, docs := range resDocs {
if k != "PipelineRun" && k != "PipelineResource" {
for _, doc := range docs {
err = kubernetes.Apply(string(doc))
err = kubernetes.Apply(string(doc), &kubernetes.KubectlOpts{
Namespace: kubernetesNamespace,
Context: kubernetesContext,
})
if err != nil {
log.Fatalf("Failed to apply templates :%s\n", err)
}
Expand Down
13 changes: 2 additions & 11 deletions docs/getting-started/minikube.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,20 @@ A helper script that automates the below exists in `./scripts/minikube.sh`.
## Usage
### Configure Kubectl
Configure Kubectl to use the `minikube` context and `dracon` namespace by default:
```bash
$ kubectl config use-context minikube
$ kubectl config set-context minikube --namespace=dracon
```
### Setting up a Pipeline
To setup an pipeline, you can execute:
```bash
$ dracon setup --pipeline examples/pipelines/golang-project
$ dracon --context minikube --namespace=dracon setup --pipeline examples/pipelines/golang-project
```
### Running a Pipeline
To run that example pipeline you can execute:
```bash
$ dracon run --pipeline examples/pipelines/golang-project
$ dracon --context minikube --namespace=dracon run --pipeline examples/pipelines/golang-project
```
### Inspecting a Pipeline
Expand Down
12 changes: 12 additions & 0 deletions pkg/kubernetes/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ go_library(
"//third_party/go:pkg_errors",
],
)

go_test(
name = "kubernetes_test",
srcs = [
"apply_test.go",
],
deps = [
":kubernetes",
"//third_party/go:pkg_errors",
"//third_party/go:stretchr_testify",
],
)
30 changes: 26 additions & 4 deletions pkg/kubernetes/apply.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,57 @@
package kubernetes

import (
"fmt"
"io"
"log"
"os/exec"

"github.com/pkg/errors"
)

// KubectlOpts represents options/flags to pass to kubectl
type KubectlOpts struct {
Context string
Namespace string
}

// Apply config using kubectl
func Apply(config string) error {
cmd := exec.Command("kubectl", "apply", "-f", "-")
func Apply(resources string, opts *KubectlOpts) error {
shCmd := GetCmd(opts)
cmd := exec.Command(shCmd[0], shCmd[1:]...)
stdin, err := cmd.StdinPipe()
if err != nil {
return errors.Wrap(err, "could not create stdin pipe")
}
go func() {
defer stdin.Close()
_, err := io.WriteString(stdin, config)
_, err := io.WriteString(stdin, resources)
if err != nil {
log.Fatal(err)
}
}()

output, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrap(err, config)
return errors.Wrap(err, resources)
}
if !cmd.ProcessState.Success() {
return errors.Wrap(err, string(output))
}
log.Println(string(output))
return nil
}

// GetCmd returns the kubectl command
func GetCmd(opts *KubectlOpts) []string {
cmd := []string{"kubectl", "apply", "-f", "-"}

if opts.Context != "" {
cmd = append(cmd, fmt.Sprintf(`--context=%s`, opts.Context))
}
if opts.Namespace != "" {
cmd = append(cmd, fmt.Sprintf(`--namespace=%s`, opts.Namespace))
}

return cmd
}
27 changes: 27 additions & 0 deletions pkg/kubernetes/apply_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package kubernetes

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetCmd(t *testing.T) {
var tests = []struct {
desc string
inOpts KubectlOpts
outCmd string
}{
{"none", KubectlOpts{}, "kubectl apply -f -"},
{"namespace", KubectlOpts{Namespace: "default"}, `kubectl apply -f - --namespace=default`},
{"context", KubectlOpts{Context: "minikube"}, `kubectl apply -f - --context=minikube`},
{"namespace&context", KubectlOpts{Context: "minikube", Namespace: "default"}, `kubectl apply -f - --context=minikube --namespace=default`},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
outCmd := strings.Join(GetCmd(&tt.inOpts), " ")
assert.Equal(t, tt.outCmd, outCmd)
})
}
}

0 comments on commit 38eb6bf

Please sign in to comment.