2019-09-04 12:15:40 +02:00
|
|
|
// Copyright (C) 2016-2019 Vincent Ambo <mail@tazj.in>
|
2017-11-21 11:24:04 +01:00
|
|
|
//
|
|
|
|
// This file is part of Kontemplate.
|
|
|
|
//
|
|
|
|
// Kontemplate is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2017-02-08 11:50:39 +01:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2018-03-09 15:17:54 +01:00
|
|
|
"fmt"
|
2017-02-08 11:50:39 +01:00
|
|
|
"path"
|
2018-05-30 21:43:31 +02:00
|
|
|
"strings"
|
2017-02-08 12:58:53 +01:00
|
|
|
|
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
|
|
|
|
2018-11-15 13:49:23 +01:00
|
|
|
// Args to pass on to kubectl for this resource set.
|
|
|
|
Args []string `json:"args"`
|
|
|
|
|
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
|
2018-06-09 16:23:09 +02:00
|
|
|
VariableImportFiles []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
|
|
|
|
2018-06-09 16:23:09 +02:00
|
|
|
// Variables imported from additional files
|
|
|
|
ImportedVars map[string]interface{}
|
|
|
|
|
|
|
|
// Explicitly set variables (via `--var`) that should override all others
|
|
|
|
ExplicitVars map[string]interface{}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-09 15:17:54 +01:00
|
|
|
func contextLoadingError(filename string, cause error) error {
|
|
|
|
return fmt.Errorf("Context loading failed on file %s due to: \n%v", filename, cause)
|
2017-02-08 11:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to load and deserialise a Context from the specified file.
|
2018-06-09 16:23:09 +02:00
|
|
|
func LoadContext(filename string, explicitVars *[]string) (*Context, error) {
|
|
|
|
var ctx Context
|
2018-06-09 20:14:38 +02:00
|
|
|
err := util.LoadData(filename, &ctx)
|
2017-02-08 13:11:10 +01:00
|
|
|
|
2017-02-08 11:50:39 +01:00
|
|
|
if err != nil {
|
2018-03-09 15:17:54 +01:00
|
|
|
return nil, contextLoadingError(filename, err)
|
2017-02-08 11:50:39 +01:00
|
|
|
}
|
|
|
|
|
2018-06-09 16:23:09 +02:00
|
|
|
ctx.BaseDir = path.Dir(filename)
|
2017-02-08 11:50:39 +01:00
|
|
|
|
2018-06-09 16:23:09 +02:00
|
|
|
// Prepare the resource sets by resolving parents etc.
|
2019-09-04 11:56:07 +02:00
|
|
|
ctx.ResourceSets = flattenPrepareResourceSetPaths(&ctx.BaseDir, &ctx.ResourceSets)
|
2018-06-09 16:23:09 +02:00
|
|
|
|
|
|
|
// Add variables explicitly specified on the command line
|
|
|
|
ctx.ExplicitVars, err = loadExplicitVars(explicitVars)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error setting explicit variables: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add variables loaded from import files
|
|
|
|
ctx.ImportedVars, err = ctx.loadImportedVariables()
|
2017-06-22 17:01:36 +02:00
|
|
|
if err != nil {
|
2018-03-09 15:17:54 +01:00
|
|
|
return nil, contextLoadingError(filename, err)
|
2017-06-22 17:01:36 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 12:23:03 +02:00
|
|
|
// Merge variables defined at different levels. The
|
|
|
|
// `mergeContextValues` function is documented with the merge
|
|
|
|
// hierarchy.
|
2018-06-09 16:23:09 +02:00
|
|
|
ctx.ResourceSets = ctx.mergeContextValues()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, contextLoadingError(filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ctx, nil
|
2017-02-08 11:50:39 +01:00
|
|
|
}
|
2017-02-08 17:14:40 +01:00
|
|
|
|
2018-06-09 16:23:09 +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() (map[string]interface{}, error) {
|
|
|
|
allImportedVars := make(map[string]interface{})
|
|
|
|
|
|
|
|
for _, file := range ctx.VariableImportFiles {
|
2018-06-09 20:14:38 +02:00
|
|
|
// Ensure that the filename is not merged with the baseDir if
|
|
|
|
// it is set to an absolute path.
|
|
|
|
var filePath string
|
|
|
|
if path.IsAbs(file) {
|
|
|
|
filePath = file
|
|
|
|
} else {
|
|
|
|
filePath = path.Join(ctx.BaseDir, file)
|
|
|
|
}
|
|
|
|
|
2017-06-22 17:01:36 +02:00
|
|
|
var importedVars map[string]interface{}
|
2018-06-09 20:14:38 +02:00
|
|
|
err := util.LoadData(filePath, &importedVars)
|
2017-06-22 17:01:36 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2018-06-09 16:23:09 +02:00
|
|
|
return nil, err
|
2017-06-22 17:01:36 +02:00
|
|
|
}
|
|
|
|
|
2018-06-09 16:23:09 +02:00
|
|
|
allImportedVars = *util.Merge(&allImportedVars, &importedVars)
|
2017-06-22 17:01:36 +02:00
|
|
|
}
|
|
|
|
|
2018-06-09 16:23:09 +02:00
|
|
|
return allImportedVars, nil
|
2017-06-22 17:01:36 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2019-09-04 11:56:07 +02:00
|
|
|
func flattenPrepareResourceSetPaths(baseDir *string, 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
|
|
|
|
}
|
|
|
|
|
2019-09-04 11:56:07 +02:00
|
|
|
// Paths are made absolute by resolving them relative to the context base,
|
|
|
|
// unless absolute paths were specified.
|
|
|
|
if !path.IsAbs(r.Path) {
|
|
|
|
r.Path = path.Join(*baseDir, r.Path)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-06-09 16:23:09 +02:00
|
|
|
// Merges the context and resource set variables according in the
|
|
|
|
// desired precedence order.
|
2018-06-26 12:23:03 +02:00
|
|
|
//
|
|
|
|
// For now the reasoning behind the merge order is from least specific
|
|
|
|
// in relation to the cluster configuration, which means that the
|
|
|
|
// precedence is (in ascending order):
|
|
|
|
//
|
|
|
|
// 1. Default values in resource sets.
|
|
|
|
// 2. Values imported from files (via `import:`)
|
|
|
|
// 3. Global values in a cluster configuration
|
|
|
|
// 4. Values set in a resource set's `include`-section
|
|
|
|
// 5. Explicit values set on the CLI (`--var`)
|
|
|
|
//
|
|
|
|
// For a discussion on the reasoning behind this order, please consult
|
|
|
|
// https://github.com/tazjin/kontemplate/issues/142
|
2018-06-09 16:23:09 +02:00
|
|
|
func (ctx *Context) mergeContextValues() []ResourceSet {
|
|
|
|
updated := make([]ResourceSet, len(ctx.ResourceSets))
|
2017-04-04 14:28:22 +02:00
|
|
|
|
2018-06-26 12:23:03 +02:00
|
|
|
// Merging has to happen separately for every individual
|
|
|
|
// resource set to make use of the default values:
|
2018-06-09 16:23:09 +02:00
|
|
|
for i, rs := range ctx.ResourceSets {
|
2018-06-26 12:23:03 +02:00
|
|
|
// Begin by loading default values from the resource
|
|
|
|
// sets configuration.
|
|
|
|
//
|
|
|
|
// Resource sets are used across different cluster
|
|
|
|
// contexts and the default values in them have the
|
|
|
|
// lowest precedence.
|
|
|
|
defaultValues := loadDefaultValues(&rs, ctx)
|
|
|
|
|
|
|
|
// Continue by merging default values with values
|
|
|
|
// imported from external files. Those values are also
|
|
|
|
// used across cluster contexts, but have higher
|
|
|
|
// precedence than defaults.
|
|
|
|
merged := util.Merge(defaultValues, &ctx.ImportedVars)
|
|
|
|
|
|
|
|
// Merge global values defined in the cluster context:
|
2018-06-09 20:45:42 +02:00
|
|
|
merged = util.Merge(merged, &ctx.Global)
|
2018-06-26 12:23:03 +02:00
|
|
|
|
|
|
|
// Merge values configured in the resource set's
|
|
|
|
// `include` section:
|
|
|
|
merged = util.Merge(merged, &rs.Values)
|
|
|
|
|
|
|
|
// Merge values defined explicitly on the CLI:
|
2018-06-09 16:23:09 +02:00
|
|
|
merged = util.Merge(merged, &ctx.ExplicitVars)
|
2018-06-26 12:23:03 +02:00
|
|
|
|
|
|
|
// Continue with the newly merged resource set:
|
2017-04-04 14:28:22 +02:00
|
|
|
rs.Values = *merged
|
|
|
|
updated[i] = rs
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated
|
|
|
|
}
|
|
|
|
|
2018-06-26 12:23:03 +02:00
|
|
|
// Loads default values for a resource set collection from
|
|
|
|
// path/to/set/default.{json|yaml}.
|
2017-04-04 14:28:22 +02:00
|
|
|
func loadDefaultValues(rs *ResourceSet, c *Context) *map[string]interface{} {
|
|
|
|
var defaultVars map[string]interface{}
|
|
|
|
|
2017-08-31 18:37:57 +02:00
|
|
|
for _, filename := range util.DefaultFilenames {
|
2019-09-04 11:56:07 +02:00
|
|
|
err := util.LoadData(path.Join(rs.Path, filename), &defaultVars)
|
2017-08-25 14:35:54 +02:00
|
|
|
if err == nil {
|
2018-06-26 12:23:03 +02:00
|
|
|
return &defaultVars
|
2017-08-25 14:35:54 +02:00
|
|
|
}
|
2017-04-04 14:28:22 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 12:23:03 +02:00
|
|
|
// 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.
|
2017-04-04 14:28:22 +02:00
|
|
|
return &rs.Values
|
2017-02-08 17:14:40 +01:00
|
|
|
}
|
2018-05-30 21:43:31 +02:00
|
|
|
|
2018-06-09 16:23:09 +02:00
|
|
|
// Prepares the variables specified explicitly via `--var` when
|
|
|
|
// executing kontemplate for adding to the context.
|
|
|
|
func loadExplicitVars(vars *[]string) (map[string]interface{}, error) {
|
|
|
|
explicitVars := make(map[string]interface{}, len(*vars))
|
2018-05-30 21:43:31 +02:00
|
|
|
|
|
|
|
for _, v := range *vars {
|
2018-06-21 13:55:59 +02:00
|
|
|
varParts := strings.SplitN(v, "=", 2)
|
2018-05-30 21:43:31 +02:00
|
|
|
if len(varParts) != 2 {
|
2018-06-09 16:23:09 +02:00
|
|
|
return nil, fmt.Errorf(`invalid explicit variable provided (%s), name and value should be separated with "="`, v)
|
2018-05-30 21:43:31 +02:00
|
|
|
}
|
|
|
|
|
2018-06-09 16:23:09 +02:00
|
|
|
explicitVars[varParts[0]] = varParts[1]
|
2018-05-30 21:43:31 +02:00
|
|
|
}
|
|
|
|
|
2018-06-09 16:23:09 +02:00
|
|
|
return explicitVars, nil
|
2018-05-30 21:43:31 +02:00
|
|
|
}
|