Skip to content
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

fix: add additional container checks during instance registration #592

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/core/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,12 +868,43 @@ func virtualization() (string, string) {
return "", "host"
}

if virtualizationSystem == "docker" {
if virtualizationSystem == "docker" || isContainer() {
log.Debugf("Virtualization detected as container with role %v", virtualizationRole)
return "container", virtualizationRole
}
log.Debugf("Virtualization detected as %v with role %v", virtualizationSystem, virtualizationRole)
return virtualizationSystem, virtualizationRole
}

func isContainer() bool {
const (
dockerEnv = "/.dockerenv"
containerEnv = "/run/.containerenv"
selfCgroup = "/proc/self/cgroup"
k8sServiceAcct = "/var/run/secrets/kubernetes.io/serviceaccount"
)

res, err, _ := singleflightGroup.Do(IsContainerKey, func() (interface{}, error) {
for _, filename := range []string{dockerEnv, containerEnv, k8sServiceAcct} {
if _, err := os.Stat(filename); err == nil {
log.Debugf("Is a container because (%s) exists", filename)
return true, nil
}
}
// v1 check
if result, err := cGroupV1Check(selfCgroup); err == nil && result {
return result, err
}
return false, nil
})

if err != nil {
log.Warnf("Unable to retrieve values from cache (%v)", err)
}

return res.(bool)
}

func releaseInfo(osReleaseFile string) (release *proto.ReleaseInfo) {
hostReleaseInfo := getHostReleaseInfo()
osRelease, err := getOsRelease(osReleaseFile)
Expand Down
Loading