Repository navigation
util/def,cmd/containerboot: add LookupEnv, simplify env parsing - #20277
Conversation
sfllaw
left a comment
There was a problem hiding this comment.
@nicholasrossi0530. thank you for submitting this pull request.
I appreciate you tackling this, but I think we could really improve the approach here. Please check out my comments below and let me know what you think.
| // EnvStringPointer returns a pointer to the value of environment variable name, | ||
| // or nil when name is unset. It distinguishes a variable set to the empty | ||
| // string (returns a pointer to "") from one that is unset (returns nil). | ||
| func EnvStringPointer(name string) *string { | ||
| if v, ok := os.LookupEnv(name); ok { | ||
| return &v | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // EnvBoolPointer returns a pointer to the parsed bool value of environment | ||
| // variable name, or nil when name is unset or not a valid bool. | ||
| func EnvBoolPointer(name string) *bool { | ||
| v, err := strconv.ParseBool(os.Getenv(name)) | ||
| if err != nil { | ||
| return nil | ||
| } | ||
| return &v | ||
| } |
There was a problem hiding this comment.
💬 Discussion (blocking): I wonder if using pointers is even correct here? I am tempted to look at whether opt.Value and would be less weird. Especially since these aren’t defaults at all?
There was a problem hiding this comment.
Agreed, they are not defaults, so def is the wrong home either way. Left them private in containerboot, untouched. This PR no longer touches util/def at all.
opt.Value is a behavior change at the callers, though: Routes and AcceptDNS use nil to mean "not specified", and AcceptDNS feeds the TS_EXTRA_ARGS reconciliation just below. Follow-up, or do you want it here?
There was a problem hiding this comment.
A follow-up PR is fine. You can stack it on top of this one.
6c483e4 to
7b7ea11
Compare
@sfllaw Thank you for the review, this was all fair. Pushed an update: |
@nicholasrossi0530, I have re-reviewed and replied. Thanks for investigating the non-empty-string defaults to make sure they were semantically correct. I think that providing default values actually fixes some latent bugs? |
7b7ea11 to
d591055
Compare
Yes, it fixes a few latent bugs at the three non-empty-default sites. Before, an explicit "" produced a broken config; |
d591055 to
45a5499
Compare
sfllaw
left a comment
There was a problem hiding this comment.
Thanks for the most recent revision. I feel we are getting very close.
45a5499 to
74b657d
Compare
74b657d to
ca482f7
Compare
sfllaw
left a comment
There was a problem hiding this comment.
@nicholasrossi0530, I kicked off the CI/CD tests and all the actual tests have passed.
I have tagged @tailscale/k8s-devs as reviewers, but mostly for awareness, since we didn’t change anything fundamental.
Thank you @sfllaw! I appreciate you working through all the iterations with me. This PR landed a lot cleaner than where it started. |
sfllaw
left a comment
There was a problem hiding this comment.
@nicholasrossi0530, sorry for the last minute request, but I realized that we did all this work for TS_KUBE_SECRET but didn’t protect ourselves from someone accidentally switching out def.LookupEnv for cmp.Or without understanding the implications. If you have time, would you mind?
Updates tailscale#20018 Signed-off-by: Nick Rossi <[email protected]>
ca482f7 to
f86104b
Compare
No worries, good catch. Better to have that guardrail locked in. |
|
@nicholasrossi0530: Merged. Thanks again for your contribution! |
…scale#20277) Simplifies cmd/containerboot env var parsing. Most of the private helpers did not earn their abstraction: defaultEnv(name, "") is just os.Getenv(name), and the rest collapse into cmp.Or and the existing def.Bool. defaultEnv, defaultEnvs and defaultBool are gone. Adds def.LookupEnv, the env companion to def.Bool, for the one case that needs it: TS_KUBE_SECRET, where an explicit "" disables Kubernetes secret storage and must stay distinct from unset (cmp.Or cannot express that). Updates tailscale#20018 Signed-off-by: Nick Rossi <[email protected]>
Simplifies cmd/containerboot env var parsing. Most of the private helpers did
not earn their abstraction: defaultEnv(name, "") is just os.Getenv(name), and
the rest collapse into cmp.Or and the existing def.Bool. defaultEnv,
defaultEnvs and defaultBool are gone.
Adds def.LookupEnv, the env companion to def.Bool, for the one case that needs
it: TS_KUBE_SECRET, where an explicit "" disables kube Secret storage and must
stay distinct from unset (cmp.Or cannot express that).
Behaviour notes:
empty value as unset, falling back to the default. Empty was never useful at
those, so this also fixes a few latent bugs. Pinned by
TestConfigFromEnvEmptyDefaults.
them to opt.Value is a follow-up.
Tests (containerboot is linux-only, run in a linux container):
Updates #20018