Skip to content

util/def: add def.Bool and def.Duration default parse helpers - #20022

Merged
sfllaw merged 3 commits into
tailscale:mainfrom
ibobgunardi:bobi/parse-fallback-helper
Jun 15, 2026
Merged

sfllaw merged 3 commits into
tailscale:mainfrom
ibobgunardi:bobi/parse-fallback-helper

Conversation

@ibobgunardi

@ibobgunardi ibobgunardi commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Keeps this one small: adds util/def for parse-with-default helpers, then uses it in the routecheck and debugportmapper localapi form parsing paths.

Tests:

  • go test ./util/def -count=1
  • go test ./feature/routecheck -run TestDoesNotExist -count=0
  • go test ./feature/debugportmapper -run TestDoesNotExist -count=0
  • $env:GOOS='linux'; go list ./feature/routecheck ./feature/debugportmapper

Updates #20018

@ibobgunardi
ibobgunardi force-pushed the bobi/parse-fallback-helper branch from 717125a to e750128 Compare June 5, 2026 15:17

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

@ibobgunardi, thanks for picking up this issue that was just filed.

I left some feedback, with only one blocking concern. To keep things bite-sized, you might want to just start with the obviously correct replacements and then submit a follow-up PR for the defaultEnv functions.

Comment thread cmd/k8s-operator/sts.go Outdated
}
v, _ := opt.Bool(vs).Get()
return v
return parsefallback.Bool(os.Getenv(envName), defVal)

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.

🐞 Bug (blocking): As you called out in your PR description, this is a behaviour change. I’m pretty sure we chose to use opt.Bool on purpose, so let’s not break that.

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.

I think there might be a way to make this behave exactly the same using your new Bool function, but I would double-check with a fuzz-test just to be sure.

Comment thread feature/routecheck/localapi.go Outdated
Comment on lines +42 to +43
if defBool(r.FormValue("probe"), false) {
timeout := defDuration(r.FormValue("timeout"), routecheck.DefaultTimeout)
if parsefallback.Bool(r.FormValue("probe"), false) {
timeout := parsefallback.Duration(r.FormValue("timeout"), routecheck.DefaultTimeout)

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.

📛 Naming (non-blocking): I suggest naming this package tailscale.com/util/def, so that you call def.Bool and def.Duration.

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

package parsefallback

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.

🧪 Testing (non-blocking): Unless strictly necessary, let’s prefer blackbox testing by putting this test in a *_test package and using only exported symbols.

@ibobgunardi
ibobgunardi force-pushed the bobi/parse-fallback-helper branch from e750128 to 19f8c1c Compare June 6, 2026 07:29
@ibobgunardi

Copy link
Copy Markdown
Contributor Author

Thanks, I narrowed this down. The k8s/containerboot env helpers are back to their original code now, so this PR only covers the localapi form cases. I also renamed the package to util/def, moved the tests to def_test, and amended the commit with Updates #20018.

@ibobgunardi
ibobgunardi force-pushed the bobi/parse-fallback-helper branch from 19f8c1c to 9324239 Compare June 6, 2026 07:30
@ibobgunardi

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I trimmed this back to the straightforward replacements only: the cmd/k8s-operator defaultEnv/defaultBool change is gone, the helper package is now util/def, and the tests use package def_test.

I reran:

go test ./util/def ./feature/routecheck ./feature/debugportmapper

@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 revisions! I have noticed a couple new blocking concerns, which we definitely should address. And then some non-blocking suggestions that you can either incorporate or disagree.

Comment thread util/def/def_test.go Resolved
Comment thread util/def/def_test.go Resolved
Comment thread util/def/def_test.go Resolved
@ibobgunardi
ibobgunardi force-pushed the bobi/parse-fallback-helper branch from 994ecdc to e54ed56 Compare June 9, 2026 00:15
@sfllaw sfllaw added the good first issue Good for newcomers label Jun 9, 2026
"tailscale.com/net/netmon"
"tailscale.com/net/portmapper"
"tailscale.com/types/logger"
"tailscale.com/util/def"

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.

🐞 Bug (blocking): One last problem, you need to run make updatedeps:

❌ Failing: https://github.com/tailscale/tailscale/actions/runs/27175393509/job/80449521523?pr=20022#step:5:84

+ exec /home/runner/.cache/tsgo/c803676bcc7f2b195b167a53d49d728045cd9b36/bin/go run github.com/tailscale/depaware --check --vendor --internal tailscale.com/cmd/tailscaled tailscale.com/cmd/tailscale tailscale.com/cmd/derper tailscale.com/cmd/k8s-operator tailscale.com/cmd/stund tailscale.com/cmd/tsidp
go: downloading github.com/tailscale/depaware v0.0.0-20251001183927-9c2ad255ef3f
go: downloading github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
go: downloading golang.org/x/tools v0.44.0
go: downloading golang.org/x/sync v0.20.0
go: downloading golang.org/x/mod v0.35.0
The list of dependencies in /home/runner/work/tailscale/tailscale/src/cmd/tailscaled/depaware.txt is out of date.

--- before
+++ after
@@ -443,6 +443,7 @@
         tailscale.com/util/cmpver                                    from tailscale.com/net/dns+
         tailscale.com/util/ctxkey                                    from tailscale.com/ipn/ipnlocal+
      💣 tailscale.com/util/deephash                                  from tailscale.com/util/syspolicy/setting
+        tailscale.com/util/def                                       from tailscale.com/feature/debugportmapper+
    L 💣 tailscale.com/util/dirwalk                                   from tailscale.com/metrics+
         tailscale.com/util/dnsname                                   from tailscale.com/appc+
         tailscale.com/util/eventbus                                  from tailscale.com/tsd+
exit status 1

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.

I have done this in ad18daf so this PR gets unstuck.

Signed-off-by: Simon Law <[email protected]>

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

All tests pass. Thanks for your first contribution, @ibobgunardi!

Please submit another PR if you want to tackle the default Getenv parsers.

@sfllaw
sfllaw merged commit ca20611 into tailscale:main Jun 15, 2026
62 checks passed
@sfllaw sfllaw changed the title util: add parse fallback helpers util/def: add def.Bool and def.Duration default parse helpers Jun 15, 2026
eFog-R pushed a commit to eFog-R/tailscale that referenced this pull request Aug 3, 2026
util/def: add def.Bool and def.Duration default parse helpers

Replace multiple instances of def.Bool and def.Duration with a new util/def
package.

Updates tailscale#20018

Co-authored-by: Bobby <[email protected]>
Co-authored-by: Simon Law <[email protected]>
Signed-off-by: Bobby <[email protected]>
Signed-off-by: Simon Law <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

good first issue Good for newcomers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants