Skip to content

Commit

Permalink
Add support for Kubectl plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Jan 10, 2020
1 parent d8cf892 commit 8c352bd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ Try out an example

Read the [docs](https://github.com/brendandburns/configula/tree/master/docs)

### Integrating with Kubernetes
Configula can be used as a `kubectl` plugin.

To install the plugin run this command:

```
# assumes that configula is in /usr/local/bin/ change paths as needed.
ln -s /usr/local/bin/kubectl-configula /usr/local/bin/configula
```

Once that's done you can run:

```sh
kubectl configula create -f examples/namespaces.py
```

### FAQ
*What about other Javascript? Java? Go? .NET? Other Language?*

Expand Down
35 changes: 33 additions & 2 deletions cmd/configula/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package main

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

"github.com/brendandburns/configula/pkg/configula"
Expand All @@ -11,15 +14,43 @@ import (
var (
pythonCommand = flag.String("python", "python3", "The executable to run for Python, overridden by $CONFIGULA_PYTHON")
dryRun = flag.Bool("debug", false, "If true, only output the interim program, don't execute")
file = flag.StringP("filename", "f", "", "The file name to process")
)

func pluginMain() {
if len(*file) == 0 || len(flag.Args()) != 1 {
fmt.Fprintf(os.Stderr, "Usage: kubectl configula create|apply|delete -f <some-file>\n")
return
}
cmd := exec.Command("kubectl", flag.Args()[0], "-f", "-")
output, err := cmd.StdinPipe()
if err != nil {
panic(err)
}
go func() {
process(*file, output)
if err := output.Close(); err != nil {
panic(err)
}
}()
cmd.Run()
}

func main() {
flag.Parse()
if strings.HasSuffix(os.Args[0], "kubectl-configula") {
pluginMain()
return
}
if len(flag.Args()) == 0 {
fmt.Fprintf(os.Stderr, "Usage: configula [--debug] [--python=/some/path] <path/to/config/file>\n")
os.Exit(-1)
}
file, err := os.Open(flag.Args()[0])
process(flag.Args()[0], os.Stdout)
}

func process(filename string, output io.Writer) {
file, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read %s: %s", flag.Args()[0], err.Error())
os.Exit(1)
Expand All @@ -38,7 +69,7 @@ func main() {
configula.NewPythonExecutor(pythonExec),
}

if err := rn.Run(file, os.Stdout, *dryRun); err != nil {
if err := rn.Run(file, output, *dryRun); err != nil {
fmt.Fprintf(os.Stderr, "Failed to execute: %s", err)
os.Exit(-1)
}
Expand Down

0 comments on commit 8c352bd

Please sign in to comment.