Skip to content

Commit

Permalink
fix: add additional container checks during instance registration (#592)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Desmarais <[email protected]>
  • Loading branch information
mattdesmarais and Matthew Desmarais authored Mar 13, 2024
1 parent 30313d9 commit 808c064
Showing 1 changed file with 32 additions and 1 deletion.
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

0 comments on commit 808c064

Please sign in to comment.