Skip to content

Commit 8c352bd

Browse files
committed
Add support for Kubectl plugins.
1 parent d8cf892 commit 8c352bd

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ Try out an example
131131

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

134+
### Integrating with Kubernetes
135+
Configula can be used as a `kubectl` plugin.
136+
137+
To install the plugin run this command:
138+
139+
```
140+
# assumes that configula is in /usr/local/bin/ change paths as needed.
141+
ln -s /usr/local/bin/kubectl-configula /usr/local/bin/configula
142+
```
143+
144+
Once that's done you can run:
145+
146+
```sh
147+
kubectl configula create -f examples/namespaces.py
148+
```
149+
134150
### FAQ
135151
*What about other Javascript? Java? Go? .NET? Other Language?*
136152

cmd/configula/main.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package main
22

33
import (
44
"fmt"
5+
"os/exec"
6+
"io"
7+
"strings"
58
"os"
69

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

20+
func pluginMain() {
21+
if len(*file) == 0 || len(flag.Args()) != 1 {
22+
fmt.Fprintf(os.Stderr, "Usage: kubectl configula create|apply|delete -f <some-file>\n")
23+
return
24+
}
25+
cmd := exec.Command("kubectl", flag.Args()[0], "-f", "-")
26+
output, err := cmd.StdinPipe()
27+
if err != nil {
28+
panic(err)
29+
}
30+
go func() {
31+
process(*file, output)
32+
if err := output.Close(); err != nil {
33+
panic(err)
34+
}
35+
}()
36+
cmd.Run()
37+
}
38+
1639
func main() {
1740
flag.Parse()
41+
if strings.HasSuffix(os.Args[0], "kubectl-configula") {
42+
pluginMain()
43+
return
44+
}
1845
if len(flag.Args()) == 0 {
1946
fmt.Fprintf(os.Stderr, "Usage: configula [--debug] [--python=/some/path] <path/to/config/file>\n")
2047
os.Exit(-1)
2148
}
22-
file, err := os.Open(flag.Args()[0])
49+
process(flag.Args()[0], os.Stdout)
50+
}
51+
52+
func process(filename string, output io.Writer) {
53+
file, err := os.Open(filename)
2354
if err != nil {
2455
fmt.Fprintf(os.Stderr, "Failed to read %s: %s", flag.Args()[0], err.Error())
2556
os.Exit(1)
@@ -38,7 +69,7 @@ func main() {
3869
configula.NewPythonExecutor(pythonExec),
3970
}
4071

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

0 commit comments

Comments
 (0)