Skip to content

Commit

Permalink
env variable check
Browse files Browse the repository at this point in the history
  • Loading branch information
lastpeony committed Oct 30, 2024
1 parent 60d9681 commit 8aa0528
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
24 changes: 16 additions & 8 deletions src/main/java/io/antmedia/SystemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import javax.management.MBeanServer;

import org.apache.commons.lang3.StringUtils;
import org.bytedeco.javacpp.Pointer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -774,8 +775,14 @@ public static boolean isContainerized() {
return true;
}

// 2. Check env variable
String container = System.getenv("container");
if(StringUtils.isNotBlank(container)){
logger.debug("Container detected via env variable.");
return true;
}

// 2. Check cgroup info
// 3. Check cgroup info
Path cgroupPath = Paths.get("/proc/self/cgroup");
if (Files.exists(cgroupPath)) {
List<String> cgroupContent = Files.readAllLines(cgroupPath);
Expand All @@ -790,12 +797,12 @@ public static boolean isContainerized() {
}
}

return false;

} catch (Exception e) {
logger.debug("Error during container detection: {}", e.getMessage());
return false;
logger.error("Error during container detection: {}", e.getMessage());

}

return false;
}

public static Long getMemoryLimitFromCgroup() throws IOException {
Expand All @@ -807,9 +814,10 @@ public static Long getMemoryLimitFromCgroup() throws IOException {

memoryLimit = Long.parseLong(memoryLimitString);

//in cgroups v1 if memory limit is not set on container this value returns 9223372036854771712 2^63-1, but number is not 100%, changes based on architecture
//if memory limit returned is higher than 100TB its not set using cgroups.
if(memoryLimit > MAX_CONTAINER_MEMORY_LIMIT_BYTES) {
// In cgroups v1, if the memory limit for a container isn't set, it typically returns 9223372036854771712 (2^63-1).
// However, this value may vary based on the architecture.
// Therefore, we consider it acceptable if the returned memory limit exceeds 100TB, indicating that the limit is not configured using cgroups.
if(memoryLimit > MAX_CONTAINER_MEMORY_LIMIT_BYTES || memoryLimit == 0 || memoryLimit == -1) {
memoryLimit = getTotalPhysicalMemorySize();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,8 @@ public void testIsContainerized() {
Path mockCgroupPath = Path.of("/tmp/test/cgroup");

try (MockedStatic<Files> mockedFiles = mockStatic(Files.class);
MockedStatic<Paths> mockedPaths = mockStatic(Paths.class)) {
MockedStatic<Paths> mockedPaths = mockStatic(Paths.class);
) {

// Setup path mocks
mockedPaths.when(() -> Paths.get("/.dockerenv")).thenReturn(mockDockerEnvPath);
Expand All @@ -764,6 +765,7 @@ public void testIsContainerized() {
mockedFiles.when(() -> Files.exists(mockDockerEnvPath)).thenReturn(true);
assertTrue(SystemUtils.isContainerized());


// Test 2: Docker in cgroup
mockedFiles.when(() -> Files.exists(mockDockerEnvPath)).thenReturn(false);
mockedFiles.when(() -> Files.exists(mockCgroupPath)).thenReturn(true);
Expand Down Expand Up @@ -803,6 +805,7 @@ public void testIsContainerized() {
.thenThrow(new IOException("Read error"));
assertFalse(SystemUtils.isContainerized());
}

}

@Test
Expand All @@ -813,8 +816,7 @@ public void testGetMemAvailableFromCgroup() throws IOException {
String cgroupV2LimitPath = "/sys/fs/cgroup/memory.max";
String expectedUsageStr = "5000";
String expectedLimitStr = "10000";
long expectedUsage = 5000L;
long expectedLimit = 10000L;

long expectedOsFreeMemory = 8000L;

// Test all three scenarios using nested try-with-resources
Expand Down

0 comments on commit 8aa0528

Please sign in to comment.