2017-02-08 11:50:39 +01:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
2017-02-08 12:58:53 +01:00
|
|
|
|
|
|
|
"github.com/polydawn/meep"
|
2017-02-14 19:00:06 +01:00
|
|
|
"github.com/tazjin/kontemplate/util"
|
2017-02-08 11:50:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ResourceSet struct {
|
2017-07-30 00:48:58 +02:00
|
|
|
// Name of the resource set. This can be used in include/exclude statements during kontemplate runs.
|
2017-08-04 23:04:43 +02:00
|
|
|
Name string `json:"name"`
|
2017-07-30 00:48:58 +02:00
|
|
|
|
|
|
|
// Path to the folder containing the files for this resource set. This defaults to the value of the 'name' field
|
|
|
|
// if unset.
|
2017-08-04 23:04:43 +02:00
|
|
|
Path string `json:"path"`
|
2017-07-30 00:48:58 +02:00
|
|
|
|
|
|
|
// Values to include when interpolating resources from this resource set.
|
2017-02-08 11:50:39 +01:00
|
|
|
Values map[string]interface{} `json:"values"`
|
2017-02-08 17:14:40 +01:00
|
|
|
|
2017-07-30 00:48:58 +02:00
|
|
|
// Nested resource sets to include
|
2017-02-08 17:14:40 +01:00
|
|
|
Include []ResourceSet `json:"include"`
|
2017-07-30 00:48:58 +02:00
|
|
|
|
|
|
|
// Parent resource set for flattened resource sets. Should not be manually specified.
|
2017-08-04 23:04:43 +02:00
|
|
|
Parent string
|
2017-02-08 11:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Context struct {
|
2017-07-30 00:48:58 +02:00
|
|
|
// The name of the kubectl context
|
2017-08-04 23:04:43 +02:00
|
|
|
Name string `json:"context"`
|
2017-07-30 00:48:58 +02:00
|
|
|
|
|
|
|
// Global variables that should be accessible by all resource sets
|
2017-08-04 23:04:43 +02:00
|
|
|
Global map[string]interface{} `json:"global"`
|
2017-07-30 00:48:58 +02:00
|
|
|
|
|
|
|
// File names of YAML or JSON files including extra variables that should be globally accessible
|
2017-08-04 23:04:43 +02:00
|
|
|
VariableImports []string `json:"import"`
|
2017-07-30 00:48:58 +02:00
|
|
|
|
|
|
|
// The resource sets to include in this context
|
2017-08-04 23:04:43 +02:00
|
|
|
ResourceSets []ResourceSet `json:"include"`
|
2017-07-30 00:48:58 +02:00
|
|
|
|
|
|
|
// This field represents the absolute path to the context base directory and should not be manually specified.
|
2017-08-04 23:04:43 +02:00
|
|
|
BaseDir string
|
2017-02-08 11:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ContextLoadingError struct {
|
|
|
|
meep.AllTraits
|
|
|
|
Filename string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to load and deserialise a Context from the specified file.
|
|
|
|
func LoadContextFromFile(filename string) (*Context, error) {
|
|
|
|
var c Context
|
2017-06-22 15:52:43 +02:00
|
|
|
err := util.LoadJsonOrYaml(filename, &c)
|
2017-02-08 13:11:10 +01:00
|
|
|
|
2017-02-08 11:50:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, meep.New(
|
|
|
|
&ContextLoadingError{Filename: filename},
|
|
|
|
meep.Cause(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-07-13 15:57:18 +02:00
|
|
|
c.ResourceSets = flattenPrepareResourceSetPaths(&c.ResourceSets)
|
2017-02-08 11:50:39 +01:00
|
|
|
c.BaseDir = path.Dir(filename)
|
2017-04-04 14:28:22 +02:00
|
|
|
c.ResourceSets = loadAllDefaultValues(&c)
|
2017-02-08 11:50:39 +01:00
|
|
|
|
2017-06-22 17:01:36 +02:00
|
|
|
err = c.loadImportedVariables()
|
|
|
|
if err != nil {
|
|
|
|
return nil, meep.New(
|
|
|
|
&ContextLoadingError{Filename: filename},
|
|
|
|
meep.Cause(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-02-08 11:50:39 +01:00
|
|
|
return &c, nil
|
|
|
|
}
|
2017-02-08 17:14:40 +01:00
|
|
|
|
2017-06-22 17:01:36 +02:00
|
|
|
// Kontemplate supports specifying additional variable files with the `import` keyword. This function loads those
|
|
|
|
// variable files and merges them together with the context's other global variables.
|
|
|
|
func (ctx *Context) loadImportedVariables() error {
|
|
|
|
for _, file := range ctx.VariableImports {
|
|
|
|
var importedVars map[string]interface{}
|
|
|
|
err := util.LoadJsonOrYaml(path.Join(ctx.BaseDir, file), &importedVars)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Global = *util.Merge(&ctx.Global, &importedVars)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-13 15:57:18 +02:00
|
|
|
// Correctly prepares the file paths for resource sets by inferring implicit paths and flattening resource set
|
|
|
|
// collections, i.e. resource sets that themselves have an additional 'include' field set.
|
2017-02-08 17:14:40 +01:00
|
|
|
// Those will be regarded as a short-hand for including multiple resource sets from a subfolder.
|
|
|
|
// See https://github.com/tazjin/kontemplate/issues/9 for more information.
|
2017-07-13 15:57:18 +02:00
|
|
|
func flattenPrepareResourceSetPaths(rs *[]ResourceSet) []ResourceSet {
|
2017-02-08 17:14:40 +01:00
|
|
|
flattened := make([]ResourceSet, 0)
|
|
|
|
|
|
|
|
for _, r := range *rs {
|
2017-07-13 15:57:18 +02:00
|
|
|
// If a path is not explicitly specified it should default to the resource set name.
|
|
|
|
// This is also the classic behaviour prior to kontemplate 1.2
|
|
|
|
if r.Path == "" {
|
|
|
|
r.Path = r.Name
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:14:40 +01:00
|
|
|
if len(r.Include) == 0 {
|
|
|
|
flattened = append(flattened, r)
|
|
|
|
} else {
|
|
|
|
for _, subResourceSet := range r.Include {
|
2017-07-13 15:57:18 +02:00
|
|
|
if subResourceSet.Path == "" {
|
|
|
|
subResourceSet.Path = subResourceSet.Name
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:32:19 +01:00
|
|
|
subResourceSet.Parent = r.Name
|
2017-02-08 17:21:55 +01:00
|
|
|
subResourceSet.Name = path.Join(r.Name, subResourceSet.Name)
|
2017-07-13 15:57:18 +02:00
|
|
|
subResourceSet.Path = path.Join(r.Path, subResourceSet.Path)
|
2017-02-14 19:00:06 +01:00
|
|
|
subResourceSet.Values = *util.Merge(&r.Values, &subResourceSet.Values)
|
2017-02-08 17:14:40 +01:00
|
|
|
flattened = append(flattened, subResourceSet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 14:28:22 +02:00
|
|
|
return flattened
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadAllDefaultValues(c *Context) []ResourceSet {
|
|
|
|
updated := make([]ResourceSet, len(c.ResourceSets))
|
|
|
|
|
|
|
|
for i, rs := range c.ResourceSets {
|
|
|
|
merged := loadDefaultValues(&rs, c)
|
|
|
|
rs.Values = *merged
|
|
|
|
updated[i] = rs
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loads and merges default values for a resource set collection from path/to/set/default.{json|yaml}.
|
|
|
|
// YAML takes precedence over JSON.
|
|
|
|
// Default values in resource set collections have the lowest priority possible.
|
|
|
|
func loadDefaultValues(rs *ResourceSet, c *Context) *map[string]interface{} {
|
|
|
|
var defaultVars map[string]interface{}
|
|
|
|
|
|
|
|
// Attempt to load YAML values
|
2017-07-13 15:57:18 +02:00
|
|
|
err := util.LoadJsonOrYaml(path.Join(c.BaseDir, rs.Path, "default.yaml"), &defaultVars)
|
2017-04-04 14:28:22 +02:00
|
|
|
if err == nil {
|
|
|
|
return util.Merge(&defaultVars, &rs.Values)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to load JSON values
|
2017-07-13 15:57:18 +02:00
|
|
|
err = util.LoadJsonOrYaml(path.Join(c.BaseDir, rs.Path, "default.json"), &defaultVars)
|
2017-04-04 14:28:22 +02:00
|
|
|
if err == nil {
|
|
|
|
return util.Merge(&defaultVars, &rs.Values)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The actual error is not inspected here. The reasoning for this is that in case of serious problems (e.g.
|
|
|
|
// permission issues with the folder / folder not existing) failure will occur a bit later anyways.
|
|
|
|
// Otherwise we'd have to differentiate between file-not-found-errors (no default values specified) and other
|
|
|
|
// errors here.
|
|
|
|
return &rs.Values
|
2017-02-08 17:14:40 +01:00
|
|
|
}
|