-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add envconsul-like support and refactor environment handling #2654
Conversation
amazing job @schmichael ! Thank you |
Moves env.Builder out of drivers entirely so one less thing to worry about when implementing driver plugins.
Also inject PATH into rkt commands since we're no longer appending host env vars for it.
client/alloc_runner_test.go
Outdated
@@ -138,6 +138,14 @@ func TestAllocRunner_TerminalUpdate_Destroy(t *testing.T) { | |||
ctestutil.ExecCompatible(t) | |||
upd, ar := testAllocRunner(false) | |||
|
|||
// Shrink chroot | |||
ar.config.ChrootEnv = map[string]string{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switch to mock driver instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call! Way faster and one more test that doesn't require root.
client/consul_template.go
Outdated
} | ||
|
||
// Set the environment variables | ||
builder.SetTemplateEnv(vars) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This breaks if they have multiple templates generating environment variables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, good catch. Will add a test as well.
client/consul_template.go
Outdated
@@ -192,6 +195,12 @@ WAIT: | |||
} | |||
} | |||
|
|||
for _, t := range tm.templates { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment explaining what you are doing. Blank line between the block please
client/consul_template.go
Outdated
@@ -243,6 +252,11 @@ WAIT: | |||
} | |||
|
|||
for _, tmpl := range tmpls { | |||
if err := loadTemplateEnv(envBuilder, taskDir, tmpl); err != nil { | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra line
client/consul_template.go
Outdated
|
||
tm.hook.Kill("consul-template", err.Error(), true) | ||
return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blank below
func (t *TaskEnvironment) ClearRegionName() *TaskEnvironment { | ||
t.Region = "" | ||
return t | ||
func (b *Builder) SetTemplateEnv(m map[string]string) *Builder { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment in consul_template.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
client/driver/rkt.go
Outdated
cmdArgs := make([]string, 0, 50) | ||
|
||
// Run rkt with env command to inject PATH as rkt needs to be able to find iptables | ||
envAbsPath, err := GetAbsolutePath(envCmd) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unclear why we can just use the option -set-env
and just run Rkt with all environment variables minus those in the blacklist passed through?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switched it to use a fresh env with just the host env vars on it. Seems more reasonable than mutating TaskEnv to add the PATH in just for the rkt command.
@@ -861,6 +861,7 @@ func parseTemplates(result *[]*api.Template, list *ast.ObjectList) error { | |||
"right_delimiter", | |||
"source", | |||
"splay", | |||
"env", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parse test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
@@ -680,6 +680,7 @@ func ApiTaskToStructsTask(apiTask *api.Task, structsTask *structs.Task) { | |||
Perms: *template.Perms, | |||
LeftDelim: *template.LeftDelim, | |||
RightDelim: *template.RightDelim, | |||
Envvars: *template.Envvars, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add to test
@@ -373,6 +374,9 @@ func (tmpl *Template) Canonicalize() { | |||
if tmpl.RightDelim == nil { | |||
tmpl.RightDelim = helper.StringToPtr("}}") | |||
} | |||
if tmpl.Envvars == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Believe there is a canonicalize test as well
And use an interface for ReplaceEnv since its all getter needs.
Ready for re-review. |
|
||
FOO=bar | ||
foo=123 | ||
ANYTHING-goes=Spaces are=ok! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are newlines okay too?
assume key derp
has a value in Consul of the following, and you do DERP={{ key "derp" }}
hello
world
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Newlines need to be escaped. The 2 easiest ways are probably:
{{ key "derp" | toJSON}}
{{ key "derp" | printf "%q" }}
Both will properly escape newlines.
However, in this current PR they won't be unescaped and if your task read the environment it would look like:
derp := os.Getenv("derp")
fmt.Printf(">>%s<<\n", derp)
It would print the escaped value:
>>"hello\nworld"<<
So I'll post a followup PR that properly un-escapes values, so that the code above would print:
>>hello
world<<
Excellent work! |
Amazing @schmichael ! :D another $work owned step to remove soon'ish : D |
I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions. |
Fixes #1765
There's two major goals of this PR:
The refactor was to avoid a new circular dependency between environments and templates. Templates get an immutable TaskEnv and
task_runner
handles populating the environment from environment template files.Unfortunately there's still a circular dependency between environments and drivers in two places:
Driver.FSIsolation()
So the new env.Builder struct has to be exposed to task_runner and templates, but everything else (drivers, executors, etc) only have access to the immutable env.TaskEnv.
Graph of the dependencies: