Skip to content

Commit

Permalink
implement namespace specific cluster operations
Browse files Browse the repository at this point in the history
This commit allows running "create", "delete" and "apply" kedge
subcommands with an optional "-n/--namespace" flag, which
specifies the Kubernetes namespace to perform the given operation
on.

The ExecuteKubectl() function in pkg/cmd/kubernetes.go is
refactored to accept multiple arguments to pass to "kubectl"
instead of just one, as before.

Fixes kedgeproject#93
  • Loading branch information
concaf committed Jul 24, 2017
1 parent 83859a8 commit 2ffb34d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
6 changes: 4 additions & 2 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ import (

// Variables
var (
ApplyFiles []string
ApplyNamespace string
ApplyFiles []string
)

// Represents the "apply" command
var applyCmd = &cobra.Command{
Use: "apply",
Short: "Apply a configuration to a resource on the Kubernetes cluster. This resource will be created if it doesn't exist yet.",
Run: func(cmd *cobra.Command, args []string) {
if err := pkgcmd.ExecuteKubectl(ApplyFiles, "apply"); err != nil {
if err := pkgcmd.ExecuteKubectl(ApplyFiles, "apply", "--namespace", ApplyNamespace); err != nil {
fmt.Println(err)
os.Exit(-1)
}
Expand All @@ -42,5 +43,6 @@ var applyCmd = &cobra.Command{

func init() {
applyCmd.Flags().StringArrayVarP(&ApplyFiles, "files", "f", []string{}, "Specify files")
applyCmd.Flags().StringVarP(&ApplyNamespace, "namespace", "n", "default", "Kubernetes namespace to deploy your application to")
RootCmd.AddCommand(applyCmd)
}
7 changes: 5 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (

// Variables
var (
CreateFiles []string
CreateNamespace string
CreateFiles []string
)

// Represents the "create" command
Expand All @@ -38,7 +39,7 @@ var createCmd = &cobra.Command{
fmt.Println(err)
os.Exit(-1)
}
if err := pkgcmd.ExecuteKubectl(CreateFiles, "create"); err != nil {
if err := pkgcmd.ExecuteKubectl(CreateFiles, "create", "--namespace", CreateNamespace); err != nil {
fmt.Println(err)
os.Exit(-1)
}
Expand All @@ -48,5 +49,7 @@ var createCmd = &cobra.Command{
func init() {
createCmd.Flags().StringArrayVarP(&CreateFiles, "files", "f", []string{}, "Specify files")
createCmd.MarkFlagRequired("files")
createCmd.Flags().StringVarP(&CreateNamespace, "namespace", "n", "default", "Kubernetes namespace to deploy your application to")

RootCmd.AddCommand(createCmd)
}
6 changes: 4 additions & 2 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (

// Variables
var (
DeleteFiles []string
DeleteNamespace string
DeleteFiles []string
)

// Represents the "delete" command
Expand All @@ -38,7 +39,7 @@ var deleteCmd = &cobra.Command{
fmt.Println(err)
os.Exit(-1)
}
if err := pkgcmd.ExecuteKubectl(DeleteFiles, "delete"); err != nil {
if err := pkgcmd.ExecuteKubectl(DeleteFiles, "delete", "--namespace", DeleteNamespace); err != nil {
fmt.Println(err)
os.Exit(-1)
}
Expand All @@ -48,5 +49,6 @@ var deleteCmd = &cobra.Command{
func init() {
deleteCmd.Flags().StringArrayVarP(&DeleteFiles, "files", "f", []string{}, "Specify files")
deleteCmd.MarkFlagRequired("files")
deleteCmd.Flags().StringVarP(&DeleteNamespace, "namespace", "n", "default", "Kubernetes namespace to delete your application from")
RootCmd.AddCommand(deleteCmd)
}
8 changes: 6 additions & 2 deletions pkg/cmd/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/pkg/errors"
)

func ExecuteKubectl(files []string, command string) error {
func ExecuteKubectl(files []string, command ...string) error {

appData, err := getApplicationsFromFiles(files)
if err != nil {
Expand All @@ -53,7 +53,11 @@ func ExecuteKubectl(files []string, command string) error {
return errors.Wrap(err, "failed to marshal object")
}

cmd := exec.Command("kubectl", command, "-f", "-")
// We need to add "-f -" at the end of the command passed to us to
// pass the files.
// e.g. If the command is "apply --namespace staging", then the
// final command becomes "kubectl apply --namespace staging -f -"
cmd := exec.Command("kubectl", append(command, "-f", "-")...)

stdin, err := cmd.StdinPipe()
if err != nil {
Expand Down

0 comments on commit 2ffb34d

Please sign in to comment.