2018-11-30 11:18:39 +00:00
|
|
|
package taskenv
|
2016-01-05 22:50:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-11-28 18:38:54 +00:00
|
|
|
"net"
|
2016-03-23 18:45:03 +00:00
|
|
|
"os"
|
2016-01-05 22:50:25 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-05-17 00:33:50 +00:00
|
|
|
"sync"
|
2016-01-05 22:50:25 +00:00
|
|
|
|
2017-03-09 00:44:46 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2016-01-05 22:50:25 +00:00
|
|
|
hargs "github.com/hashicorp/nomad/helper/args"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2019-01-04 23:01:35 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-11-06 01:39:02 +00:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2016-01-05 22:50:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// A set of environment variables that are exported by each driver.
|
|
|
|
const (
|
2016-03-02 00:08:21 +00:00
|
|
|
// AllocDir is the environment variable with the path to the alloc directory
|
|
|
|
// that is shared across tasks within a task group.
|
2016-01-05 22:50:25 +00:00
|
|
|
AllocDir = "NOMAD_ALLOC_DIR"
|
|
|
|
|
2016-03-02 00:08:21 +00:00
|
|
|
// TaskLocalDir is the environment variable with the path to the tasks local
|
|
|
|
// directory where it can store data that is persisted to the alloc is
|
|
|
|
// removed.
|
2016-01-05 22:50:25 +00:00
|
|
|
TaskLocalDir = "NOMAD_TASK_DIR"
|
|
|
|
|
2016-11-01 17:36:59 +00:00
|
|
|
// SecretsDir is the environment variable with the path to the tasks secret
|
2016-09-02 19:44:05 +00:00
|
|
|
// directory where it can store sensitive data.
|
2016-11-01 17:36:59 +00:00
|
|
|
SecretsDir = "NOMAD_SECRETS_DIR"
|
2016-09-02 19:44:05 +00:00
|
|
|
|
2016-03-02 00:08:21 +00:00
|
|
|
// MemLimit is the environment variable with the tasks memory limit in MBs.
|
2016-01-05 22:50:25 +00:00
|
|
|
MemLimit = "NOMAD_MEMORY_LIMIT"
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// CpuLimit is the environment variable with the tasks CPU limit in MHz.
|
2016-01-05 22:50:25 +00:00
|
|
|
CpuLimit = "NOMAD_CPU_LIMIT"
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// AllocID is the environment variable for passing the allocation ID.
|
2016-03-02 00:08:21 +00:00
|
|
|
AllocID = "NOMAD_ALLOC_ID"
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// AllocName is the environment variable for passing the allocation name.
|
2016-03-02 00:08:21 +00:00
|
|
|
AllocName = "NOMAD_ALLOC_NAME"
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// TaskName is the environment variable for passing the task name.
|
2016-03-02 00:08:21 +00:00
|
|
|
TaskName = "NOMAD_TASK_NAME"
|
|
|
|
|
2017-07-20 23:22:54 +00:00
|
|
|
// GroupName is the environment variable for passing the task group name.
|
|
|
|
GroupName = "NOMAD_GROUP_NAME"
|
|
|
|
|
2016-10-11 17:57:12 +00:00
|
|
|
// JobName is the environment variable for passing the job name.
|
|
|
|
JobName = "NOMAD_JOB_NAME"
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// AllocIndex is the environment variable for passing the allocation index.
|
2016-03-10 02:09:51 +00:00
|
|
|
AllocIndex = "NOMAD_ALLOC_INDEX"
|
|
|
|
|
2017-03-09 01:57:38 +00:00
|
|
|
// Datacenter is the environment variable for passing the datacenter in which the alloc is running.
|
|
|
|
Datacenter = "NOMAD_DC"
|
|
|
|
|
2019-08-27 15:38:07 +00:00
|
|
|
// Namespace is the environment variable for passing the namespace in which the alloc is running.
|
|
|
|
Namespace = "NOMAD_NAMESPACE"
|
|
|
|
|
2017-03-09 01:57:38 +00:00
|
|
|
// Region is the environment variable for passing the region in which the alloc is running.
|
|
|
|
Region = "NOMAD_REGION"
|
|
|
|
|
2016-03-02 00:08:21 +00:00
|
|
|
// AddrPrefix is the prefix for passing both dynamic and static port
|
2017-06-22 00:01:40 +00:00
|
|
|
// allocations to tasks.
|
2017-03-10 20:17:43 +00:00
|
|
|
// E.g $NOMAD_ADDR_http=127.0.0.1:80
|
2017-06-16 23:35:16 +00:00
|
|
|
//
|
2017-06-22 00:01:40 +00:00
|
|
|
// The ip:port are always the host's.
|
2016-01-25 19:46:01 +00:00
|
|
|
AddrPrefix = "NOMAD_ADDR_"
|
|
|
|
|
2017-06-22 00:01:40 +00:00
|
|
|
// IpPrefix is the prefix for passing the host IP of a port allocation
|
|
|
|
// to a task.
|
2016-04-15 17:27:51 +00:00
|
|
|
IpPrefix = "NOMAD_IP_"
|
|
|
|
|
|
|
|
// PortPrefix is the prefix for passing the port allocation to a task.
|
2017-06-22 00:01:40 +00:00
|
|
|
// It will be the task's port if a port map is specified. Task's should
|
|
|
|
// bind to this port.
|
2016-04-15 17:27:51 +00:00
|
|
|
PortPrefix = "NOMAD_PORT_"
|
|
|
|
|
2017-06-22 00:01:40 +00:00
|
|
|
// HostPortPrefix is the prefix for passing the host port when a port
|
|
|
|
// map is specified.
|
2016-01-25 19:46:01 +00:00
|
|
|
HostPortPrefix = "NOMAD_HOST_PORT_"
|
2016-01-05 22:50:25 +00:00
|
|
|
|
2016-03-02 00:08:21 +00:00
|
|
|
// MetaPrefix is the prefix for passing task meta data.
|
2016-01-05 22:50:25 +00:00
|
|
|
MetaPrefix = "NOMAD_META_"
|
2016-09-14 20:30:01 +00:00
|
|
|
|
2019-08-28 03:41:38 +00:00
|
|
|
// UpstreamPrefix is the prefix for passing upstream IP and ports to the alloc
|
|
|
|
UpstreamPrefix = "NOMAD_UPSTREAM_"
|
|
|
|
|
2016-09-14 20:30:01 +00:00
|
|
|
// VaultToken is the environment variable for passing the Vault token
|
|
|
|
VaultToken = "VAULT_TOKEN"
|
2019-04-12 15:05:49 +00:00
|
|
|
|
|
|
|
// VaultNamespace is the environment variable for passing the Vault namespace, if applicable
|
|
|
|
VaultNamespace = "VAULT_NAMESPACE"
|
2016-01-05 22:50:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// The node values that can be interpreted.
|
|
|
|
const (
|
2017-03-29 23:31:23 +00:00
|
|
|
nodeIdKey = "node.unique.id"
|
|
|
|
nodeDcKey = "node.datacenter"
|
|
|
|
nodeRegionKey = "node.region"
|
|
|
|
nodeNameKey = "node.unique.name"
|
|
|
|
nodeClassKey = "node.class"
|
2016-01-05 22:50:25 +00:00
|
|
|
|
|
|
|
// Prefixes used for lookups.
|
|
|
|
nodeAttributePrefix = "attr."
|
|
|
|
nodeMetaPrefix = "meta."
|
|
|
|
)
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// TaskEnv is a task's environment as well as node attribute's for
|
|
|
|
// interpolation.
|
|
|
|
type TaskEnv struct {
|
|
|
|
// NodeAttrs is the map of node attributes for interpolation
|
|
|
|
NodeAttrs map[string]string
|
|
|
|
|
|
|
|
// EnvMap is the map of environment variables
|
|
|
|
EnvMap map[string]string
|
|
|
|
|
2018-12-19 22:23:09 +00:00
|
|
|
// deviceEnv is the environment variables populated from the device hooks.
|
|
|
|
deviceEnv map[string]string
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// envList is a memoized list created by List()
|
|
|
|
envList []string
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 22:23:09 +00:00
|
|
|
// NewTaskEnv creates a new task environment with the given environment, device
|
|
|
|
// environment and node attribute maps.
|
|
|
|
func NewTaskEnv(env, deviceEnv, node map[string]string) *TaskEnv {
|
2017-05-17 00:33:50 +00:00
|
|
|
return &TaskEnv{
|
|
|
|
NodeAttrs: node,
|
2018-12-19 22:23:09 +00:00
|
|
|
deviceEnv: deviceEnv,
|
2017-05-17 00:33:50 +00:00
|
|
|
EnvMap: env,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 18:28:53 +00:00
|
|
|
// NewEmptyTaskEnv creates a new empty task environment.
|
|
|
|
func NewEmptyTaskEnv() *TaskEnv {
|
|
|
|
return &TaskEnv{
|
|
|
|
NodeAttrs: make(map[string]string),
|
|
|
|
deviceEnv: make(map[string]string),
|
|
|
|
EnvMap: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// List returns the task's environment as a slice of NAME=value pair strings.
|
|
|
|
func (t *TaskEnv) List() []string {
|
|
|
|
if t.envList != nil {
|
|
|
|
return t.envList
|
|
|
|
}
|
|
|
|
|
|
|
|
env := []string{}
|
|
|
|
for k, v := range t.EnvMap {
|
|
|
|
env = append(env, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
2018-12-19 22:23:09 +00:00
|
|
|
// DeviceEnv returns the task's environment variables set by device hooks.
|
|
|
|
func (t *TaskEnv) DeviceEnv() map[string]string {
|
|
|
|
m := make(map[string]string, len(t.deviceEnv))
|
|
|
|
for k, v := range t.deviceEnv {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Map of the task's environment variables.
|
|
|
|
func (t *TaskEnv) Map() map[string]string {
|
|
|
|
m := make(map[string]string, len(t.EnvMap))
|
|
|
|
for k, v := range t.EnvMap {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// All of the task's environment variables and the node's attributes in a
|
|
|
|
// single map.
|
|
|
|
func (t *TaskEnv) All() map[string]string {
|
|
|
|
m := make(map[string]string, len(t.EnvMap)+len(t.NodeAttrs))
|
|
|
|
for k, v := range t.EnvMap {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range t.NodeAttrs {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 01:39:02 +00:00
|
|
|
// AllValues is a map of the task's environment variables and the node's
|
2018-11-09 19:27:18 +00:00
|
|
|
// attributes with cty.Value (String) values. Errors including keys are
|
|
|
|
// returned in a map by key name.
|
|
|
|
//
|
|
|
|
// In the rare case of a fatal error, only an error value is returned. This is
|
|
|
|
// likely a programming error as user input should not be able to cause a fatal
|
|
|
|
// error.
|
|
|
|
func (t *TaskEnv) AllValues() (map[string]cty.Value, map[string]error, error) {
|
|
|
|
errs := make(map[string]error)
|
|
|
|
|
|
|
|
// Intermediate map for building up nested go types
|
|
|
|
allMap := make(map[string]interface{}, len(t.EnvMap)+len(t.NodeAttrs))
|
|
|
|
|
|
|
|
// Intermediate map for all env vars including those whose keys that
|
|
|
|
// cannot be nested (eg foo...bar)
|
|
|
|
envMap := make(map[string]cty.Value, len(t.EnvMap))
|
|
|
|
|
|
|
|
// Prepare job-based variables (eg job.meta, job.group.task.env, etc)
|
2018-11-06 01:39:02 +00:00
|
|
|
for k, v := range t.EnvMap {
|
2018-11-09 19:27:18 +00:00
|
|
|
if err := addNestedKey(allMap, k, v); err != nil {
|
|
|
|
errs[k] = err
|
|
|
|
}
|
|
|
|
envMap[k] = cty.StringVal(v)
|
2018-11-06 01:39:02 +00:00
|
|
|
}
|
2018-11-09 19:27:18 +00:00
|
|
|
|
|
|
|
// Prepare node-based variables (eg node.*, attr.*, meta.*)
|
2018-11-06 01:39:02 +00:00
|
|
|
for k, v := range t.NodeAttrs {
|
2018-11-09 19:27:18 +00:00
|
|
|
if err := addNestedKey(allMap, k, v); err != nil {
|
|
|
|
errs[k] = err
|
|
|
|
}
|
2018-11-06 01:39:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:27:18 +00:00
|
|
|
// Add flat envMap as a Map to allMap so users can access any key via
|
|
|
|
// HCL2's indexing syntax: ${env["foo...bar"]}
|
|
|
|
allMap["env"] = cty.MapVal(envMap)
|
|
|
|
|
|
|
|
// Add meta and attr to node if they exist to properly namespace things
|
|
|
|
// a bit.
|
|
|
|
nodeMapI, ok := allMap["node"]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("missing node variable")
|
|
|
|
}
|
|
|
|
nodeMap, ok := nodeMapI.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("invalid type for node variable: %T", nodeMapI)
|
|
|
|
}
|
|
|
|
if attrMap, ok := allMap["attr"]; ok {
|
|
|
|
nodeMap["attr"] = attrMap
|
|
|
|
}
|
|
|
|
if metaMap, ok := allMap["meta"]; ok {
|
|
|
|
nodeMap["meta"] = metaMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// ctyify the entire tree of strings and maps
|
|
|
|
tree, err := ctyify(allMap)
|
|
|
|
if err != nil {
|
|
|
|
// This should not be possible and is likely a programming
|
|
|
|
// error. Invalid user input should be cleaned earlier.
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tree, errs, nil
|
2018-11-06 01:39:02 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 22:50:25 +00:00
|
|
|
// ParseAndReplace takes the user supplied args replaces any instance of an
|
2017-05-17 00:33:50 +00:00
|
|
|
// environment variable or Nomad variable in the args with the actual value.
|
|
|
|
func (t *TaskEnv) ParseAndReplace(args []string) []string {
|
2018-01-23 22:05:00 +00:00
|
|
|
if args == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-05 22:50:25 +00:00
|
|
|
replaced := make([]string, len(args))
|
|
|
|
for i, arg := range args {
|
2017-05-17 00:33:50 +00:00
|
|
|
replaced[i] = hargs.ReplaceEnv(arg, t.EnvMap, t.NodeAttrs)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return replaced
|
|
|
|
}
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// ReplaceEnv takes an arg and replaces all occurrences of environment variables
|
2017-05-17 00:33:50 +00:00
|
|
|
// and Nomad variables. If the variable is found in the passed map it is
|
2016-01-05 22:50:25 +00:00
|
|
|
// replaced, otherwise the original string is returned.
|
2017-05-17 00:33:50 +00:00
|
|
|
func (t *TaskEnv) ReplaceEnv(arg string) string {
|
|
|
|
return hargs.ReplaceEnv(arg, t.EnvMap, t.NodeAttrs)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Builder is used to build task environment's and is safe for concurrent use.
|
|
|
|
type Builder struct {
|
|
|
|
// envvars are custom set environment variables
|
|
|
|
envvars map[string]string
|
2016-07-09 00:42:34 +00:00
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// templateEnv are env vars set from templates
|
|
|
|
templateEnv map[string]string
|
|
|
|
|
|
|
|
// hostEnv are environment variables filtered from the host
|
|
|
|
hostEnv map[string]string
|
|
|
|
|
|
|
|
// nodeAttrs are Node attributes and metadata
|
|
|
|
nodeAttrs map[string]string
|
|
|
|
|
|
|
|
// taskMeta are the meta attributes on the task
|
|
|
|
taskMeta map[string]string
|
|
|
|
|
|
|
|
// allocDir from task's perspective; eg /alloc
|
|
|
|
allocDir string
|
|
|
|
|
|
|
|
// localDir from task's perspective; eg /local
|
|
|
|
localDir string
|
|
|
|
|
2018-03-11 18:53:26 +00:00
|
|
|
// secretsDir from task's perspective; eg /secrets
|
2017-05-17 00:33:50 +00:00
|
|
|
secretsDir string
|
|
|
|
|
2018-10-16 22:34:32 +00:00
|
|
|
cpuLimit int64
|
|
|
|
memLimit int64
|
2017-05-17 00:33:50 +00:00
|
|
|
taskName string
|
|
|
|
allocIndex int
|
|
|
|
datacenter string
|
2019-08-27 15:38:07 +00:00
|
|
|
namespace string
|
2017-05-17 00:33:50 +00:00
|
|
|
region string
|
|
|
|
allocId string
|
|
|
|
allocName string
|
2017-07-20 23:22:54 +00:00
|
|
|
groupName string
|
2017-05-17 00:33:50 +00:00
|
|
|
vaultToken string
|
2019-04-12 15:05:49 +00:00
|
|
|
vaultNamespace string
|
2017-05-17 00:33:50 +00:00
|
|
|
injectVaultToken bool
|
|
|
|
jobName string
|
|
|
|
|
|
|
|
// otherPorts for tasks in the same alloc
|
|
|
|
otherPorts map[string]string
|
|
|
|
|
2017-06-09 17:29:41 +00:00
|
|
|
// driverNetwork is the network defined by the driver (or nil if none
|
|
|
|
// was defined).
|
2019-01-04 23:01:35 +00:00
|
|
|
driverNetwork *drivers.DriverNetwork
|
2017-06-09 17:29:41 +00:00
|
|
|
|
2017-05-18 18:34:40 +00:00
|
|
|
// network resources from the task; must be lazily turned into env vars
|
2017-06-09 17:29:41 +00:00
|
|
|
// because portMaps and advertiseIP can change after builder creation
|
|
|
|
// and affect network env vars.
|
2017-05-18 18:34:40 +00:00
|
|
|
networks []*structs.NetworkResource
|
2017-05-17 00:33:50 +00:00
|
|
|
|
2018-11-27 19:53:47 +00:00
|
|
|
// hookEnvs are env vars set by hooks and stored by hook name to
|
|
|
|
// support adding/removing vars from multiple hooks (eg HookA adds A:1,
|
|
|
|
// HookB adds A:2, HookA removes A, A should equal 2)
|
|
|
|
hookEnvs map[string]map[string]string
|
|
|
|
|
|
|
|
// hookNames is a slice of hooks in hookEnvs to apply hookEnvs in the
|
|
|
|
// order the hooks are run.
|
|
|
|
hookNames []string
|
|
|
|
|
2018-12-19 22:23:09 +00:00
|
|
|
// deviceHookName is the device hook name. It is set only if device hooks
|
|
|
|
// are set. While a bit round about, this enables us to return device hook
|
|
|
|
// environment variables without having to hardcode the name of the hook.
|
|
|
|
deviceHookName string
|
|
|
|
|
2019-08-28 03:41:38 +00:00
|
|
|
// upstreams from the group connect enabled services
|
|
|
|
upstreams []structs.ConsulUpstream
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
mu *sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBuilder creates a new task environment builder.
|
2017-05-18 18:34:40 +00:00
|
|
|
func NewBuilder(node *structs.Node, alloc *structs.Allocation, task *structs.Task, region string) *Builder {
|
2018-11-27 19:53:47 +00:00
|
|
|
b := NewEmptyBuilder()
|
|
|
|
b.region = region
|
2017-05-18 18:34:40 +00:00
|
|
|
return b.setTask(task).setAlloc(alloc).setNode(node)
|
2017-05-17 00:33:50 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 22:24:14 +00:00
|
|
|
// NewEmptyBuilder creates a new environment builder.
|
|
|
|
func NewEmptyBuilder() *Builder {
|
|
|
|
return &Builder{
|
2018-11-27 19:53:47 +00:00
|
|
|
mu: &sync.RWMutex{},
|
|
|
|
hookEnvs: map[string]map[string]string{},
|
|
|
|
envvars: make(map[string]string),
|
2017-05-26 22:24:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Build must be called after all the tasks environment values have been set.
|
|
|
|
func (b *Builder) Build() *TaskEnv {
|
|
|
|
nodeAttrs := make(map[string]string)
|
|
|
|
envMap := make(map[string]string)
|
2018-12-19 22:23:09 +00:00
|
|
|
var deviceEnvs map[string]string
|
2017-05-17 00:33:50 +00:00
|
|
|
|
|
|
|
b.mu.RLock()
|
|
|
|
defer b.mu.RUnlock()
|
2016-01-05 22:50:25 +00:00
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Add the directories
|
|
|
|
if b.allocDir != "" {
|
|
|
|
envMap[AllocDir] = b.allocDir
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.localDir != "" {
|
|
|
|
envMap[TaskLocalDir] = b.localDir
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.secretsDir != "" {
|
|
|
|
envMap[SecretsDir] = b.secretsDir
|
2016-09-02 19:44:05 +00:00
|
|
|
}
|
2016-01-05 22:50:25 +00:00
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Add the resource limits
|
|
|
|
if b.memLimit != 0 {
|
2018-10-16 22:34:32 +00:00
|
|
|
envMap[MemLimit] = strconv.FormatInt(b.memLimit, 10)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.cpuLimit != 0 {
|
2018-10-16 22:34:32 +00:00
|
|
|
envMap[CpuLimit] = strconv.FormatInt(b.cpuLimit, 10)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Add the task metadata
|
|
|
|
if b.allocId != "" {
|
|
|
|
envMap[AllocID] = b.allocId
|
2016-03-02 00:08:21 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.allocName != "" {
|
|
|
|
envMap[AllocName] = b.allocName
|
2016-03-02 00:08:21 +00:00
|
|
|
}
|
2017-07-20 23:22:54 +00:00
|
|
|
if b.groupName != "" {
|
|
|
|
envMap[GroupName] = b.groupName
|
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.allocIndex != -1 {
|
|
|
|
envMap[AllocIndex] = strconv.Itoa(b.allocIndex)
|
2016-03-10 02:09:51 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.taskName != "" {
|
|
|
|
envMap[TaskName] = b.taskName
|
2016-03-02 00:08:21 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.jobName != "" {
|
|
|
|
envMap[JobName] = b.jobName
|
2016-10-11 17:57:12 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.datacenter != "" {
|
|
|
|
envMap[Datacenter] = b.datacenter
|
2017-03-29 22:21:21 +00:00
|
|
|
}
|
2019-08-27 15:38:07 +00:00
|
|
|
if b.namespace != "" {
|
|
|
|
envMap[Namespace] = b.namespace
|
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.region != "" {
|
|
|
|
envMap[Region] = b.region
|
|
|
|
|
|
|
|
// Copy region over to node attrs
|
|
|
|
nodeAttrs[nodeRegionKey] = b.region
|
2017-03-29 22:21:21 +00:00
|
|
|
}
|
2016-03-02 00:08:21 +00:00
|
|
|
|
2017-06-09 17:29:41 +00:00
|
|
|
// Build the network related env vars
|
|
|
|
buildNetworkEnv(envMap, b.networks, b.driverNetwork)
|
2017-05-18 18:34:40 +00:00
|
|
|
|
2017-01-20 21:43:22 +00:00
|
|
|
// Build the addr of the other tasks
|
2017-05-17 00:33:50 +00:00
|
|
|
for k, v := range b.otherPorts {
|
|
|
|
envMap[k] = v
|
2017-01-20 21:43:22 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 03:41:38 +00:00
|
|
|
// Build the Consul Connect upstream env vars
|
|
|
|
buildUpstreamsEnv(envMap, b.upstreams)
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Build the Vault Token
|
|
|
|
if b.injectVaultToken && b.vaultToken != "" {
|
|
|
|
envMap[VaultToken] = b.vaultToken
|
|
|
|
}
|
2016-01-05 22:50:25 +00:00
|
|
|
|
2019-04-12 15:05:49 +00:00
|
|
|
// Build the Vault Namespace
|
|
|
|
if b.injectVaultToken && b.vaultNamespace != "" {
|
|
|
|
envMap[VaultNamespace] = b.vaultNamespace
|
|
|
|
}
|
|
|
|
|
2017-05-23 00:46:40 +00:00
|
|
|
// Copy task meta
|
|
|
|
for k, v := range b.taskMeta {
|
|
|
|
envMap[k] = v
|
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Copy node attributes
|
|
|
|
for k, v := range b.nodeAttrs {
|
|
|
|
nodeAttrs[k] = v
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Interpolate and add environment variables
|
|
|
|
for k, v := range b.hostEnv {
|
|
|
|
envMap[k] = hargs.ReplaceEnv(v, nodeAttrs, envMap)
|
2016-09-14 20:30:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 22:40:38 +00:00
|
|
|
// Copy interpolated task env vars second as they override host env vars
|
2017-05-17 00:33:50 +00:00
|
|
|
for k, v := range b.envvars {
|
2017-05-19 22:40:38 +00:00
|
|
|
envMap[k] = hargs.ReplaceEnv(v, nodeAttrs, envMap)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 19:53:47 +00:00
|
|
|
// Copy hook env vars in the order the hooks were run
|
|
|
|
for _, h := range b.hookNames {
|
|
|
|
for k, v := range b.hookEnvs[h] {
|
2018-12-19 22:23:09 +00:00
|
|
|
e := hargs.ReplaceEnv(v, nodeAttrs, envMap)
|
|
|
|
envMap[k] = e
|
|
|
|
|
|
|
|
if h == b.deviceHookName {
|
|
|
|
if deviceEnvs == nil {
|
|
|
|
deviceEnvs = make(map[string]string, len(b.hookEnvs[h]))
|
|
|
|
}
|
|
|
|
|
|
|
|
deviceEnvs[k] = e
|
|
|
|
}
|
2018-11-27 19:53:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy template env vars as they override task env vars
|
2017-05-17 00:33:50 +00:00
|
|
|
for k, v := range b.templateEnv {
|
|
|
|
envMap[k] = v
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 01:09:44 +00:00
|
|
|
// Clean keys (see #2405)
|
2019-08-16 08:11:47 +00:00
|
|
|
prefixesToClean := [...]string{AddrPrefix, IpPrefix, PortPrefix, HostPortPrefix, MetaPrefix}
|
2017-05-17 00:33:50 +00:00
|
|
|
cleanedEnv := make(map[string]string, len(envMap))
|
|
|
|
for k, v := range envMap {
|
2019-08-16 08:11:47 +00:00
|
|
|
cleanedK := k
|
|
|
|
for i := range prefixesToClean {
|
|
|
|
if strings.HasPrefix(k, prefixesToClean[i]) {
|
2019-08-13 16:23:13 +00:00
|
|
|
cleanedK = helper.CleanEnvVar(k, '_')
|
2019-08-11 09:51:42 +00:00
|
|
|
}
|
2018-12-19 22:23:09 +00:00
|
|
|
}
|
2019-08-16 08:11:47 +00:00
|
|
|
cleanedEnv[cleanedK] = v
|
2018-12-19 22:23:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 08:11:47 +00:00
|
|
|
return NewTaskEnv(cleanedEnv, deviceEnvs, nodeAttrs)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 22:57:35 +00:00
|
|
|
// Update task updates the environment based on a new alloc and task.
|
|
|
|
func (b *Builder) UpdateTask(alloc *structs.Allocation, task *structs.Task) *Builder {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
return b.setTask(task).setAlloc(alloc)
|
|
|
|
}
|
|
|
|
|
2018-11-27 19:53:47 +00:00
|
|
|
// SetHookEnv sets environment variables from a hook. Variables are
|
|
|
|
// Last-Write-Wins, so if a hook writes a variable that's also written by a
|
|
|
|
// later hook, the later hooks value always gets used.
|
|
|
|
func (b *Builder) SetHookEnv(hook string, envs map[string]string) *Builder {
|
2018-06-22 00:35:07 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2018-12-19 22:23:09 +00:00
|
|
|
return b.setHookEnvLocked(hook, envs)
|
|
|
|
}
|
2018-11-27 19:53:47 +00:00
|
|
|
|
2019-01-03 12:31:30 +00:00
|
|
|
// setHookEnvLocked is the implementation of setting hook environment variables
|
2018-12-19 22:23:09 +00:00
|
|
|
// and should be called with the lock held
|
|
|
|
func (b *Builder) setHookEnvLocked(hook string, envs map[string]string) *Builder {
|
2018-11-27 19:53:47 +00:00
|
|
|
if _, exists := b.hookEnvs[hook]; !exists {
|
|
|
|
b.hookNames = append(b.hookNames, hook)
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
2018-11-27 19:53:47 +00:00
|
|
|
b.hookEnvs[hook] = envs
|
2018-06-22 00:35:07 +00:00
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-12-19 22:23:09 +00:00
|
|
|
// SetDeviceHookEnv sets environment variables from a device hook. Variables are
|
|
|
|
// Last-Write-Wins, so if a hook writes a variable that's also written by a
|
|
|
|
// later hook, the later hooks value always gets used.
|
|
|
|
func (b *Builder) SetDeviceHookEnv(hookName string, envs map[string]string) *Builder {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
|
|
|
// Store the device hook name
|
|
|
|
b.deviceHookName = hookName
|
|
|
|
return b.setHookEnvLocked(hookName, envs)
|
|
|
|
}
|
|
|
|
|
2017-05-18 18:34:40 +00:00
|
|
|
// setTask is called from NewBuilder to populate task related environment
|
|
|
|
// variables.
|
|
|
|
func (b *Builder) setTask(task *structs.Task) *Builder {
|
2019-11-18 18:04:01 +00:00
|
|
|
if task == nil {
|
|
|
|
return b
|
|
|
|
}
|
2017-05-18 18:34:40 +00:00
|
|
|
b.taskName = task.Name
|
2017-05-23 22:57:35 +00:00
|
|
|
b.envvars = make(map[string]string, len(task.Env))
|
2017-05-18 18:34:40 +00:00
|
|
|
for k, v := range task.Env {
|
|
|
|
b.envvars[k] = v
|
|
|
|
}
|
2018-10-03 16:47:18 +00:00
|
|
|
|
|
|
|
// COMPAT(0.11): Remove in 0.11
|
2017-05-23 22:57:35 +00:00
|
|
|
if task.Resources == nil {
|
|
|
|
b.memLimit = 0
|
|
|
|
b.cpuLimit = 0
|
|
|
|
} else {
|
2018-10-16 22:34:32 +00:00
|
|
|
b.memLimit = int64(task.Resources.MemoryMB)
|
|
|
|
b.cpuLimit = int64(task.Resources.CPU)
|
2017-05-18 18:34:40 +00:00
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// setAlloc is called from NewBuilder to populate alloc related environment
|
|
|
|
// variables.
|
|
|
|
func (b *Builder) setAlloc(alloc *structs.Allocation) *Builder {
|
|
|
|
b.allocId = alloc.ID
|
|
|
|
b.allocName = alloc.Name
|
2017-07-20 23:22:54 +00:00
|
|
|
b.groupName = alloc.TaskGroup
|
2017-05-31 18:34:46 +00:00
|
|
|
b.allocIndex = int(alloc.Index())
|
2017-05-18 18:34:40 +00:00
|
|
|
b.jobName = alloc.Job.Name
|
2019-08-27 15:38:07 +00:00
|
|
|
b.namespace = alloc.Namespace
|
2017-05-18 18:34:40 +00:00
|
|
|
|
|
|
|
// Set meta
|
|
|
|
combined := alloc.Job.CombinedTaskMeta(alloc.TaskGroup, b.taskName)
|
2018-06-11 15:59:23 +00:00
|
|
|
// taskMetaSize is double to total meta keys to account for given and upper
|
|
|
|
// cased values
|
|
|
|
taskMetaSize := len(combined) * 2
|
|
|
|
|
|
|
|
// if job is parameterized initialize optional meta to empty strings
|
2018-06-11 16:01:19 +00:00
|
|
|
if alloc.Job.Dispatched {
|
2018-06-11 17:50:50 +00:00
|
|
|
optionalMetaCount := len(alloc.Job.ParameterizedJob.MetaOptional)
|
|
|
|
b.taskMeta = make(map[string]string, taskMetaSize+optionalMetaCount*2)
|
2018-06-11 15:59:23 +00:00
|
|
|
|
|
|
|
for _, k := range alloc.Job.ParameterizedJob.MetaOptional {
|
|
|
|
b.taskMeta[fmt.Sprintf("%s%s", MetaPrefix, strings.ToUpper(k))] = ""
|
|
|
|
b.taskMeta[fmt.Sprintf("%s%s", MetaPrefix, k)] = ""
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
b.taskMeta = make(map[string]string, taskMetaSize)
|
|
|
|
}
|
|
|
|
|
2017-05-18 18:34:40 +00:00
|
|
|
for k, v := range combined {
|
|
|
|
b.taskMeta[fmt.Sprintf("%s%s", MetaPrefix, strings.ToUpper(k))] = v
|
|
|
|
b.taskMeta[fmt.Sprintf("%s%s", MetaPrefix, k)] = v
|
|
|
|
}
|
|
|
|
|
2019-08-28 03:41:38 +00:00
|
|
|
tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)
|
|
|
|
|
|
|
|
b.otherPorts = make(map[string]string, len(tg.Tasks)*2)
|
2020-01-15 13:57:05 +00:00
|
|
|
|
|
|
|
// Protect against invalid allocs where AllocatedResources isn't set.
|
|
|
|
// TestClient_AddAllocError explicitly tests for this condition
|
2018-10-03 16:47:18 +00:00
|
|
|
if alloc.AllocatedResources != nil {
|
|
|
|
// Populate task resources
|
|
|
|
if tr, ok := alloc.AllocatedResources.Tasks[b.taskName]; ok {
|
|
|
|
b.cpuLimit = tr.Cpu.CpuShares
|
|
|
|
b.memLimit = tr.Memory.MemoryMB
|
|
|
|
|
|
|
|
// Copy networks to prevent sharing
|
|
|
|
b.networks = make([]*structs.NetworkResource, len(tr.Networks))
|
|
|
|
for i, n := range tr.Networks {
|
|
|
|
b.networks[i] = n.Copy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add ports from other tasks
|
|
|
|
for taskName, resources := range alloc.AllocatedResources.Tasks {
|
|
|
|
// Add ports from other tasks
|
|
|
|
if taskName == b.taskName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, nw := range resources.Networks {
|
|
|
|
for _, p := range nw.ReservedPorts {
|
|
|
|
addPort(b.otherPorts, taskName, nw.IP, p.Label, p.Value)
|
|
|
|
}
|
|
|
|
for _, p := range nw.DynamicPorts {
|
|
|
|
addPort(b.otherPorts, taskName, nw.IP, p.Label, p.Value)
|
|
|
|
}
|
|
|
|
}
|
2017-05-18 18:34:40 +00:00
|
|
|
}
|
2019-10-18 22:42:28 +00:00
|
|
|
|
|
|
|
// Add ports from group networks
|
|
|
|
//TODO Expose IPs but possibly only via variable interpolation
|
|
|
|
for _, nw := range alloc.AllocatedResources.Shared.Networks {
|
|
|
|
for _, p := range nw.ReservedPorts {
|
|
|
|
addGroupPort(b.otherPorts, p)
|
|
|
|
}
|
|
|
|
for _, p := range nw.DynamicPorts {
|
|
|
|
addGroupPort(b.otherPorts, p)
|
|
|
|
}
|
|
|
|
}
|
2017-05-18 18:34:40 +00:00
|
|
|
}
|
2019-08-28 03:41:38 +00:00
|
|
|
|
|
|
|
upstreams := []structs.ConsulUpstream{}
|
|
|
|
for _, svc := range tg.Services {
|
|
|
|
if svc.Connect.HasSidecar() && svc.Connect.SidecarService.HasUpstreams() {
|
|
|
|
upstreams = append(upstreams, svc.Connect.SidecarService.Proxy.Upstreams...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(upstreams) > 0 {
|
|
|
|
b.SetUpstreams(upstreams)
|
|
|
|
}
|
|
|
|
|
2017-05-18 18:34:40 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// setNode is called from NewBuilder to populate node attributes.
|
|
|
|
func (b *Builder) setNode(n *structs.Node) *Builder {
|
2017-05-23 22:57:35 +00:00
|
|
|
b.nodeAttrs = make(map[string]string, 4+len(n.Attributes)+len(n.Meta))
|
2017-05-17 00:33:50 +00:00
|
|
|
b.nodeAttrs[nodeIdKey] = n.ID
|
|
|
|
b.nodeAttrs[nodeNameKey] = n.Name
|
|
|
|
b.nodeAttrs[nodeClassKey] = n.NodeClass
|
2017-05-23 00:46:40 +00:00
|
|
|
b.nodeAttrs[nodeDcKey] = n.Datacenter
|
|
|
|
b.datacenter = n.Datacenter
|
2016-01-05 22:50:25 +00:00
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Set up the attributes.
|
|
|
|
for k, v := range n.Attributes {
|
|
|
|
b.nodeAttrs[fmt.Sprintf("%s%s", nodeAttributePrefix, k)] = v
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Set up the meta.
|
|
|
|
for k, v := range n.Meta {
|
|
|
|
b.nodeAttrs[fmt.Sprintf("%s%s", nodeMetaPrefix, k)] = v
|
2017-03-27 21:58:04 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
return b
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
func (b *Builder) SetAllocDir(dir string) *Builder {
|
|
|
|
b.mu.Lock()
|
|
|
|
b.allocDir = dir
|
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
2016-09-02 19:44:05 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
func (b *Builder) SetTaskLocalDir(dir string) *Builder {
|
|
|
|
b.mu.Lock()
|
|
|
|
b.localDir = dir
|
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
2016-09-02 19:44:05 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
func (b *Builder) SetSecretsDir(dir string) *Builder {
|
|
|
|
b.mu.Lock()
|
|
|
|
b.secretsDir = dir
|
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 17:29:41 +00:00
|
|
|
// SetDriverNetwork defined by the driver.
|
2019-01-04 23:01:35 +00:00
|
|
|
func (b *Builder) SetDriverNetwork(n *drivers.DriverNetwork) *Builder {
|
2017-06-09 17:29:41 +00:00
|
|
|
ncopy := n.Copy()
|
2017-05-17 00:33:50 +00:00
|
|
|
b.mu.Lock()
|
2017-06-09 17:29:41 +00:00
|
|
|
b.driverNetwork = ncopy
|
2017-05-17 00:33:50 +00:00
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 17:29:41 +00:00
|
|
|
// buildNetworkEnv env vars in the given map.
|
|
|
|
//
|
2017-06-23 22:20:04 +00:00
|
|
|
// Auto: NOMAD_PORT_<label>
|
|
|
|
// Host: NOMAD_IP_<label>, NOMAD_ADDR_<label>, NOMAD_HOST_PORT_<label>
|
2017-06-09 17:29:41 +00:00
|
|
|
//
|
|
|
|
// Handled by setAlloc -> otherPorts:
|
|
|
|
//
|
|
|
|
// Task: NOMAD_TASK_{IP,PORT,ADDR}_<task>_<label> # Always host values
|
|
|
|
//
|
2019-01-04 23:01:35 +00:00
|
|
|
func buildNetworkEnv(envMap map[string]string, nets structs.Networks, driverNet *drivers.DriverNetwork) {
|
2017-06-09 17:29:41 +00:00
|
|
|
for _, n := range nets {
|
|
|
|
for _, p := range n.ReservedPorts {
|
|
|
|
buildPortEnv(envMap, p, n.IP, driverNet)
|
|
|
|
}
|
|
|
|
for _, p := range n.DynamicPorts {
|
|
|
|
buildPortEnv(envMap, p, n.IP, driverNet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 23:01:35 +00:00
|
|
|
func buildPortEnv(envMap map[string]string, p structs.Port, ip string, driverNet *drivers.DriverNetwork) {
|
2017-06-22 00:01:40 +00:00
|
|
|
// Host IP, port, and address
|
2017-06-09 17:29:41 +00:00
|
|
|
portStr := strconv.Itoa(p.Value)
|
2017-06-22 00:01:40 +00:00
|
|
|
envMap[IpPrefix+p.Label] = ip
|
2017-06-16 23:35:16 +00:00
|
|
|
envMap[HostPortPrefix+p.Label] = portStr
|
2017-06-22 00:01:40 +00:00
|
|
|
envMap[AddrPrefix+p.Label] = net.JoinHostPort(ip, portStr)
|
2017-06-09 17:29:41 +00:00
|
|
|
|
2017-06-22 00:01:40 +00:00
|
|
|
// Set Port to task's value if there's a port map
|
2017-06-20 19:26:52 +00:00
|
|
|
if driverNet != nil && driverNet.PortMap[p.Label] != 0 {
|
2017-06-22 00:01:40 +00:00
|
|
|
envMap[PortPrefix+p.Label] = strconv.Itoa(driverNet.PortMap[p.Label])
|
2017-06-09 17:29:41 +00:00
|
|
|
} else {
|
2017-06-22 00:01:40 +00:00
|
|
|
// Default to host's
|
|
|
|
envMap[PortPrefix+p.Label] = portStr
|
2017-06-13 21:02:11 +00:00
|
|
|
}
|
2017-06-09 17:29:41 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 03:41:38 +00:00
|
|
|
// SetUpstreams defined by connect enabled group services
|
|
|
|
func (b *Builder) SetUpstreams(upstreams []structs.ConsulUpstream) *Builder {
|
|
|
|
b.mu.Lock()
|
|
|
|
b.upstreams = upstreams
|
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildUpstreamsEnv builds NOMAD_UPSTREAM_{IP,PORT,ADDR}_{destination} vars
|
|
|
|
func buildUpstreamsEnv(envMap map[string]string, upstreams []structs.ConsulUpstream) {
|
|
|
|
// Proxy sidecars always bind to localhost
|
|
|
|
const ip = "127.0.0.1"
|
|
|
|
for _, u := range upstreams {
|
|
|
|
port := strconv.Itoa(u.LocalBindPort)
|
|
|
|
envMap[UpstreamPrefix+"IP_"+u.DestinationName] = ip
|
|
|
|
envMap[UpstreamPrefix+"PORT_"+u.DestinationName] = port
|
|
|
|
envMap[UpstreamPrefix+"ADDR_"+u.DestinationName] = net.JoinHostPort(ip, port)
|
|
|
|
|
|
|
|
// Also add cleaned version
|
|
|
|
cleanName := helper.CleanEnvVar(u.DestinationName, '_')
|
|
|
|
envMap[UpstreamPrefix+"ADDR_"+cleanName] = net.JoinHostPort(ip, port)
|
|
|
|
envMap[UpstreamPrefix+"IP_"+cleanName] = ip
|
|
|
|
envMap[UpstreamPrefix+"PORT_"+cleanName] = port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 13:33:35 +00:00
|
|
|
// SetPortMapEnvs sets the PortMap related environment variables on the map
|
|
|
|
func SetPortMapEnvs(envs map[string]string, ports map[string]int) map[string]string {
|
|
|
|
if envs == nil {
|
|
|
|
envs = map[string]string{}
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:46:26 +00:00
|
|
|
for portLabel, port := range ports {
|
2019-09-04 13:33:35 +00:00
|
|
|
portEnv := helper.CleanEnvVar(PortPrefix+portLabel, '_')
|
2019-09-03 19:46:26 +00:00
|
|
|
envs[portEnv] = strconv.Itoa(port)
|
|
|
|
}
|
|
|
|
return envs
|
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// SetHostEnvvars adds the host environment variables to the tasks. The filter
|
|
|
|
// parameter can be use to filter host environment from entering the tasks.
|
|
|
|
func (b *Builder) SetHostEnvvars(filter []string) *Builder {
|
|
|
|
filterMap := make(map[string]struct{}, len(filter))
|
2016-03-23 18:45:03 +00:00
|
|
|
for _, f := range filter {
|
2017-05-17 00:33:50 +00:00
|
|
|
filterMap[f] = struct{}{}
|
2016-03-23 18:45:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
fullHostEnv := os.Environ()
|
|
|
|
filteredHostEnv := make(map[string]string, len(fullHostEnv))
|
|
|
|
for _, e := range fullHostEnv {
|
2016-04-22 00:08:47 +00:00
|
|
|
parts := strings.SplitN(e, "=", 2)
|
2016-03-23 18:45:03 +00:00
|
|
|
key, value := parts[0], parts[1]
|
2016-03-23 21:07:12 +00:00
|
|
|
|
|
|
|
// Skip filtered environment variables
|
2017-05-17 00:33:50 +00:00
|
|
|
if _, filtered := filterMap[key]; filtered {
|
2016-03-23 21:07:12 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
filteredHostEnv[key] = value
|
2016-03-23 18:45:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
b.hostEnv = filteredHostEnv
|
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
2016-03-02 00:08:21 +00:00
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
func (b *Builder) SetTemplateEnv(m map[string]string) *Builder {
|
|
|
|
b.mu.Lock()
|
|
|
|
b.templateEnv = m
|
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
2017-03-29 23:20:10 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 15:05:49 +00:00
|
|
|
func (b *Builder) SetVaultToken(token, namespace string, inject bool) *Builder {
|
2017-05-17 00:33:50 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
b.vaultToken = token
|
2019-04-12 15:05:49 +00:00
|
|
|
b.vaultNamespace = namespace
|
2017-05-17 00:33:50 +00:00
|
|
|
b.injectVaultToken = inject
|
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
2016-09-14 20:30:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// addPort keys and values for other tasks to an env var map
|
|
|
|
func addPort(m map[string]string, taskName, ip, portLabel string, port int) {
|
|
|
|
key := fmt.Sprintf("%s%s_%s", AddrPrefix, taskName, portLabel)
|
|
|
|
m[key] = fmt.Sprintf("%s:%d", ip, port)
|
|
|
|
key = fmt.Sprintf("%s%s_%s", IpPrefix, taskName, portLabel)
|
|
|
|
m[key] = ip
|
|
|
|
key = fmt.Sprintf("%s%s_%s", PortPrefix, taskName, portLabel)
|
|
|
|
m[key] = strconv.Itoa(port)
|
2016-09-14 20:30:01 +00:00
|
|
|
}
|
2019-10-18 22:42:28 +00:00
|
|
|
|
|
|
|
// addGroupPort adds a group network port. The To value is used if one is
|
|
|
|
// specified.
|
|
|
|
func addGroupPort(m map[string]string, port structs.Port) {
|
|
|
|
if port.To > 0 {
|
|
|
|
m[PortPrefix+port.Label] = strconv.Itoa(port.To)
|
|
|
|
} else {
|
|
|
|
m[PortPrefix+port.Label] = strconv.Itoa(port.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
m[HostPortPrefix+port.Label] = strconv.Itoa(port.Value)
|
|
|
|
}
|