From ca99d53fc85cccc82258d0963a1df2a32748b8cd Mon Sep 17 00:00:00 2001 From: Aphral Griffin Date: Thu, 14 Mar 2024 16:10:27 +0000 Subject: [PATCH] fix metrics --- src/core/os.go | 2 +- .../nginx/agent/v2/src/core/environment.go | 33 ++++++++++++++++++- .../github.com/nginx/agent/v2/src/core/os.go | 2 +- .../nginx/agent/v2/src/core/environment.go | 33 ++++++++++++++++++- .../github.com/nginx/agent/v2/src/core/os.go | 2 +- 5 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/core/os.go b/src/core/os.go index 65f91a0e0..e1f95cf32 100644 --- a/src/core/os.go +++ b/src/core/os.go @@ -50,7 +50,7 @@ func EnableWritePermissionForSocket(path string) error { case <-timeout: return lastError default: - lastError = os.Chmod(path, 0o600) + lastError = os.Chmod(path, 0o660) if lastError == nil { return nil } diff --git a/test/integration/vendor/github.com/nginx/agent/v2/src/core/environment.go b/test/integration/vendor/github.com/nginx/agent/v2/src/core/environment.go index 6c0f8bf89..59b2d2f1c 100644 --- a/test/integration/vendor/github.com/nginx/agent/v2/src/core/environment.go +++ b/test/integration/vendor/github.com/nginx/agent/v2/src/core/environment.go @@ -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) diff --git a/test/integration/vendor/github.com/nginx/agent/v2/src/core/os.go b/test/integration/vendor/github.com/nginx/agent/v2/src/core/os.go index 65f91a0e0..e1f95cf32 100644 --- a/test/integration/vendor/github.com/nginx/agent/v2/src/core/os.go +++ b/test/integration/vendor/github.com/nginx/agent/v2/src/core/os.go @@ -50,7 +50,7 @@ func EnableWritePermissionForSocket(path string) error { case <-timeout: return lastError default: - lastError = os.Chmod(path, 0o600) + lastError = os.Chmod(path, 0o660) if lastError == nil { return nil } diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/environment.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/environment.go index 6c0f8bf89..59b2d2f1c 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/environment.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/environment.go @@ -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) diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/os.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/os.go index 65f91a0e0..e1f95cf32 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/os.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/os.go @@ -50,7 +50,7 @@ func EnableWritePermissionForSocket(path string) error { case <-timeout: return lastError default: - lastError = os.Chmod(path, 0o600) + lastError = os.Chmod(path, 0o660) if lastError == nil { return nil }