Skip to content

util/def,cmd/containerboot: add LookupEnv, simplify env parsing - #20277

Merged
sfllaw merged 1 commit into
tailscale:mainfrom
nicholasrossi0530:def-env-helpers
Jul 18, 2026
Merged

sfllaw merged 1 commit into
tailscale:mainfrom
nicholasrossi0530:def-env-helpers

Conversation

@nicholasrossi0530

@nicholasrossi0530 nicholasrossi0530 commented Jun 28, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • TS_SOCKET, TS_LOCAL_ADDR_PORT and TS_TEST_ONLY_ROOT now treat an explicitly
    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.
  • defaultEnvStringPointer and defaultEnvBoolPointer are left private; moving
    them to opt.Value is a follow-up.

Tests (containerboot is linux-only, run in a linux container):

  • go test ./util/def
  • go test ./cmd/containerboot
  • GOOS=linux go build/vet ./cmd/containerboot/...

Updates #20018

@sfllaw sfllaw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Comment thread util/def/def.go Resolved
Comment thread cmd/containerboot/settings.go Resolved
Comment thread util/def/def.go Resolved
Comment thread cmd/containerboot/settings.go Resolved
Comment thread util/def/def.go Outdated
Comment on lines +64 to +82
// 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
}

@sfllaw sfllaw Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A follow-up PR is fine. You can stack it on top of this one.

@nicholasrossi0530 nicholasrossi0530 changed the title util: add env default parse helpers cmd/containerboot: simplify env var parsing Jul 16, 2026
@nicholasrossi0530

nicholasrossi0530 commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor Author

@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.

@sfllaw Thank you for the review, this was all fair. Pushed an update: util/def is untouched now, so this is just a containerboot simplification. One open question in the pointer thread (#20277 (comment)).

@nicholasrossi0530
nicholasrossi0530 requested a review from sfllaw July 16, 2026 06:40
@sfllaw

sfllaw commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

@sfllaw Thank you for the review, this was all fair. Pushed an update: util/def is untouched now, so this is just a containerboot simplification. One open question in the pointer thread (#20277 (comment)).

@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?

@nicholasrossi0530

Copy link
Copy Markdown
Contributor Author

@sfllaw Thank you for the review, this was all fair. Pushed an update: util/def is untouched now, so this is just a containerboot simplification. One open question in the pointer thread (#20277 (comment)).

@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?

Yes, it fixes a few latent bugs at the three non-empty-default sites. Before, an explicit "" produced a broken config; cmp.Or folds empty into the default, so all three now fall back correctly. Only the explicit-empty case changes, always from broken to working. Added TestConfigFromEnvEmptyDefaults pinning it.

Comment thread cmd/containerboot/settings_test.go Resolved
Comment thread util/def/def.go Resolved
@nicholasrossi0530 nicholasrossi0530 changed the title cmd/containerboot: simplify env var parsing util/def,cmd/containerboot: add LookupEnv, simplify env parsing Jul 17, 2026
@nicholasrossi0530
nicholasrossi0530 requested a review from sfllaw July 17, 2026 20:00

@sfllaw sfllaw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the most recent revision. I feel we are getting very close.

Comment thread util/def/def_test.go Resolved
Comment thread util/def/def_test.go Resolved
Comment thread util/def/def.go Resolved
Comment thread util/def/def.go Resolved
Comment thread cmd/containerboot/settings_test.go Resolved
Comment thread util/def/def.go Resolved
@nicholasrossi0530
nicholasrossi0530 requested a review from sfllaw July 17, 2026 21:00
@sfllaw
sfllaw requested a review from a team July 17, 2026 21:09

@sfllaw sfllaw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

@nicholasrossi0530

Copy link
Copy Markdown
Contributor Author

@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 sfllaw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

Comment thread cmd/containerboot/settings.go Resolved
@nicholasrossi0530

Copy link
Copy Markdown
Contributor Author

@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?

No worries, good catch. Better to have that guardrail locked in.

@sfllaw
sfllaw merged commit b91e844 into tailscale:main Jul 18, 2026
60 of 61 checks passed
@sfllaw

sfllaw commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

@nicholasrossi0530: Merged. Thanks again for your contribution!

eFog-R pushed a commit to eFog-R/tailscale that referenced this pull request Aug 3, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants