Skip to content

Commit

Permalink
hostinfo,build_docker.sh,tailcfg: more reliably detect being in a con…
Browse files Browse the repository at this point in the history
…tainer (tailscale#13826)

Our existing container-detection tricks did not work on Kubernetes,
where Docker is no longer used as a container runtime. Extends the
existing go build tags for containers to the other container packages
and uses that to reliably detect builds that were created by Tailscale
for use in a container. Unfortunately this doesn't necessarily improve
detection for users' custom builds, but that's a separate issue.

Updates tailscale#13825

Signed-off-by: Tom Proctor <[email protected]>
  • Loading branch information
tomhjp authored Oct 15, 2024
1 parent a8f9c0d commit 5f22f72
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions build_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ case "$TARGET" in
-X tailscale.com/version.gitCommitStamp=${VERSION_GIT_HASH}" \
--base="${BASE}" \
--tags="${TAGS}" \
--gotags="ts_kube,ts_package_container" \
--repos="${REPOS}" \
--push="${PUSH}" \
--target="${PLATFORM}" \
Expand All @@ -72,6 +73,7 @@ case "$TARGET" in
-X tailscale.com/version.gitCommitStamp=${VERSION_GIT_HASH}" \
--base="${BASE}" \
--tags="${TAGS}" \
--gotags="ts_kube,ts_package_container" \
--repos="${REPOS}" \
--push="${PUSH}" \
--target="${PLATFORM}" \
Expand Down
13 changes: 11 additions & 2 deletions hostinfo/hostinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,22 @@ func getEnvType() EnvType {
return ""
}

// inContainer reports whether we're running in a container.
// inContainer reports whether we're running in a container. Best-effort only,
// there's no foolproof way to detect this, but the build tag should catch all
// official builds from 1.78.0.
func inContainer() opt.Bool {
if runtime.GOOS != "linux" {
return ""
}
var ret opt.Bool
ret.Set(false)
if packageType != nil && packageType() == "container" {
// Go build tag ts_package_container was set during build.
ret.Set(true)
return ret
}
// Only set if using docker's container runtime. Not guaranteed by
// documentation, but it's been in place for a long time.
if _, err := os.Stat("/.dockerenv"); err == nil {
ret.Set(true)
return ret
Expand Down Expand Up @@ -362,7 +371,7 @@ func inFlyDotIo() bool {
}

func inReplit() bool {
// https://docs.replit.com/programming-ide/getting-repl-metadata
// https://docs.replit.com/replit-workspace/configuring-repl#environment-variables
if os.Getenv("REPL_OWNER") != "" && os.Getenv("REPL_SLUG") != "" {
return true
}
Expand Down
16 changes: 16 additions & 0 deletions hostinfo/hostinfo_container_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause

//go:build linux && !android && ts_package_container

package hostinfo

import (
"testing"
)

func TestInContainer(t *testing.T) {
if got := inContainer(); !got.EqualBool(true) {
t.Errorf("inContainer = %v; want true due to ts_package_container build tag", got)
}
}
8 changes: 7 additions & 1 deletion hostinfo/hostinfo_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause

//go:build linux && !android
//go:build linux && !android && !ts_package_container

package hostinfo

Expand Down Expand Up @@ -34,3 +34,9 @@ remotes/origin/QTSFW_5.0.0`
t.Errorf("got %q; want %q", got, want)
}
}

func TestInContainer(t *testing.T) {
if got := inContainer(); !got.EqualBool(false) {
t.Errorf("inContainer = %v; want false due to absence of ts_package_container build tag", got)
}
}
2 changes: 1 addition & 1 deletion tailcfg/tailcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ type Hostinfo struct {
// "5.10.0-17-amd64".
OSVersion string `json:",omitempty"`

Container opt.Bool `json:",omitempty"` // whether the client is running in a container
Container opt.Bool `json:",omitempty"` // best-effort whether the client is running in a container
Env string `json:",omitempty"` // a hostinfo.EnvType in string form
Distro string `json:",omitempty"` // "debian", "ubuntu", "nixos", ...
DistroVersion string `json:",omitempty"` // "20.04", ...
Expand Down

0 comments on commit 5f22f72

Please sign in to comment.