diff options
author | Vincent Ambo <tazjin@gmail.com> | 2017-02-08T12·04+0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-08T12·04+0100 |
commit | c58ce7e2ab76a591774ef2537f46665fd0885b1d (patch) | |
tree | ddf42dd4de67f7bea83db0d3db2dd154b24a5f7a | |
parent | ad82ff3e7560edf132d04b8a4e08447af3821efb (diff) | |
parent | 1e80f19f7b3e5246b42e74862b32885fb621b03c (diff) |
Merge pull request #7 from tazjin/feat/travis
Enable Travis.CI builds
-rw-r--r-- | .travis.yml | 1 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | context/context.go | 3 | ||||
-rw-r--r-- | main.go | 72 | ||||
-rw-r--r-- | templater/templater.go | 70 |
5 files changed, 108 insertions, 40 deletions
diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000000..4f2ee4d97338 --- /dev/null +++ b/.travis.yml @@ -0,0 +1 @@ +language: go diff --git a/README.md b/README.md index f9172283297c..df74f2c334f3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ KonTemplate - A simple Kubernetes templater =========================================== +[![Build Status](https://travis-ci.org/tazjin/kontemplate.svg?branch=master)](https://travis-ci.org/tazjin/kontemplate) + I made this tool out of frustration with the available ways to template Kubernetes resource files. All I want out of such a tool is a way to specify lots of resources with placeholders that get filled in with specific values, based on which context (i.e. k8s cluster) is specified. diff --git a/context/context.go b/context/context.go index e842feae1dc5..612faa39acab 100644 --- a/context/context.go +++ b/context/context.go @@ -2,9 +2,10 @@ package context import ( "encoding/json" - "github.com/polydawn/meep" "io/ioutil" "path" + + "github.com/polydawn/meep" ) type ResourceSet struct { diff --git a/main.go b/main.go index 158c9a566a86..de734e8ac37d 100644 --- a/main.go +++ b/main.go @@ -4,36 +4,70 @@ import ( "fmt" "os" + "github.com/polydawn/meep" "github.com/tazjin/kontemplate/context" "github.com/tazjin/kontemplate/templater" + "github.com/urfave/cli" ) func main() { - args := os.Args[1:] - if len(args) == 0 { - fmt.Fprintln(os.Stderr, "Usage: kontemplate <cluster-config>") - os.Exit(1) - } + app := cli.NewApp() - c, err := context.LoadContextFromFile(os.Args[1]) + app.Name = "kontemplate" + app.Usage = "simple Kubernetes resource templating" + app.Version = "0.0.1" - if err != nil { - fmt.Fprintf(os.Stderr, "%v\n", err) - os.Exit(1) + app.Commands = []cli.Command{ + ApplyCommand(), } - fmt.Fprintf(os.Stderr, "Applying cluster %s\n", c.Name) + app.Run(os.Args) +} + +func ApplyCommand() cli.Command { + return cli.Command{ + Name: "run", + Usage: "Interpolate and print templates", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "file, f", + Usage: "Cluster configuration file to use", + }, + cli.StringSliceFlag{ + Name: "limit, l", + Usage: "Limit templating to certain resource sets", + }, + }, + Action: func(c *cli.Context) error { + limit := c.StringSlice("limit") + f := c.String("file") + + if f == "" { + return meep.New( + &meep.ErrInvalidParam{ + Param: "file", + Reason: "Cluster config file must be specified", + }, + ) + } + + ctx, err := context.LoadContextFromFile(f) + + if err != nil { + return err + } + + resources, err := templater.LoadAndPrepareTemplates(&limit, ctx) - for _, rs := range c.ResourceSets { - fmt.Fprintf(os.Stderr, "Applying resource %s with values %v\n", rs.Name, rs.Values) - resources, err := templater.LoadAndPrepareTemplates(c) + if err != nil { + return err + } - if err != nil { - fmt.Println(err) - } + for _, r := range resources { + fmt.Println(r) + } - for _, r := range resources { - fmt.Print(r) - } + return nil + }, } } diff --git a/templater/templater.go b/templater/templater.go index 27beff371e5c..a2c860c60d3d 100644 --- a/templater/templater.go +++ b/templater/templater.go @@ -24,37 +24,67 @@ type TemplatingError struct { meep.AllTraits } -func LoadAndPrepareTemplates(c *context.Context) ([]string, error) { - output := make([]string, 0) - +func LoadAndPrepareTemplates(limit *[]string, c *context.Context) (output []string, err error) { for _, rs := range c.ResourceSets { - fmt.Fprintf(os.Stderr, "Loading resources for %s\n", rs.Name) + if resourceSetIncluded(limit, &rs.Name) { + err = processResourceSet(c, &rs, &output) + + if err != nil { + return + } + } + } - rp := path.Join(c.BaseDir, rs.Name) - files, err := ioutil.ReadDir(rp) + return +} - if err != nil { - return nil, meep.New( - &TemplateNotFoundError{Name: rs.Name}, - meep.Cause(err), - ) +func resourceSetIncluded(limit *[]string, resourceSetName *string) bool { + if len(*limit) == 0 { + return true + } + + for _, name := range *limit { + if name == *resourceSetName { + return true } + } + + return false +} - for _, file := range files { - if !file.IsDir() && isResourceFile(file) { - p := path.Join(rp, file.Name()) - o, err := templateFile(c, &rs, p) +func processResourceSet(c *context.Context, rs *context.ResourceSet, output *[]string) error { + fmt.Fprintf(os.Stderr, "Loading resources for %s\n", rs.Name) - if err != nil { - return nil, err - } + rp := path.Join(c.BaseDir, rs.Name) + files, err := ioutil.ReadDir(rp) - output = append(output, o) + err = processFiles(c, rs, rp, files, output) + + if err != nil { + return meep.New( + &TemplateNotFoundError{Name: rs.Name}, + meep.Cause(err), + ) + } + + return nil +} + +func processFiles(c *context.Context, rs *context.ResourceSet, rp string, files []os.FileInfo, output *[]string) error { + for _, file := range files { + if !file.IsDir() && isResourceFile(file) { + p := path.Join(rp, file.Name()) + o, err := templateFile(c, rs, p) + + if err != nil { + return err } + + *output = append(*output, o) } } - return output, nil + return nil } func templateFile(c *context.Context, rs *context.ResourceSet, filename string) (string, error) { |