Skip to content

Commit 648fe77

Browse files
Support setting CONTAINER_LOG_PATH via an environment variable (#1773)
* Support CONTAINER_LOG_PATH. Signed-off-by: Josh Baird <[email protected]> * Lint fixes. Signed-off-by: Josh Baird <[email protected]> * Update comments. Signed-off-by: Josh Baird <[email protected]> * Abstract into function. Signed-off-by: Josh Baird <[email protected]> * Handle errors in test. Signed-off-by: Josh Baird <[email protected]> * Remove AI agent comments They don't add value Signed-off-by: Marco Franssen <[email protected]> --------- Signed-off-by: Josh Baird <[email protected]> Signed-off-by: Marco Franssen <[email protected]> Co-authored-by: Marco Franssen <[email protected]>
1 parent 4a48ffd commit 648fe77

File tree

2 files changed

+137
-3
lines changed

2 files changed

+137
-3
lines changed

cmd/fluent-manager/main.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,7 @@ func main() {
230230
}
231231
}
232232

233-
if envs, err := godotenv.Read("/fluent-operator/fluent-bit.env"); err == nil {
234-
logPath = envs["CONTAINER_ROOT_DIR"] + "/containers"
235-
}
233+
logPath = getContainerLogPath("/fluent-operator/fluent-bit.env")
236234

237235
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrlOpts)
238236
if err != nil {
@@ -325,3 +323,20 @@ func main() {
325323
os.Exit(1)
326324
}
327325
}
326+
327+
// getContainerLogPath determines the container log path for FluentBit.
328+
func getContainerLogPath(envFilePath string) string {
329+
var logPath string
330+
331+
if envLogPath := os.Getenv("CONTAINER_LOG_PATH"); envLogPath != "" {
332+
logPath = envLogPath
333+
} else if envs, err := godotenv.Read(envFilePath); err == nil {
334+
logPath = envs["CONTAINER_ROOT_DIR"] + "/containers"
335+
}
336+
337+
if logPath == "" {
338+
logPath = "/var/log/containers"
339+
}
340+
341+
return logPath
342+
}

cmd/fluent-manager/main_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Copyright 2021.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
"testing"
23+
)
24+
25+
func TestGetContainerLogPath(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
envValue string
29+
setupEnvFile bool
30+
envFileContent string
31+
expectedPath string
32+
}{
33+
{
34+
name: "returns path from CONTAINER_LOG_PATH environment variable",
35+
envValue: "/custom/log/path",
36+
setupEnvFile: false,
37+
expectedPath: "/custom/log/path",
38+
},
39+
{
40+
name: "returns path from fluent-bit.env file when env var not set",
41+
envValue: "",
42+
setupEnvFile: true,
43+
envFileContent: "CONTAINER_ROOT_DIR=/var/lib/docker",
44+
expectedPath: "/var/lib/docker/containers",
45+
},
46+
{
47+
name: "returns path with different CONTAINER_ROOT_DIR",
48+
envValue: "",
49+
setupEnvFile: true,
50+
envFileContent: "CONTAINER_ROOT_DIR=/custom/container/root",
51+
expectedPath: "/custom/container/root/containers",
52+
},
53+
{
54+
name: "returns default path when neither env var nor file is available",
55+
envValue: "",
56+
setupEnvFile: false,
57+
expectedPath: "/var/log/containers",
58+
},
59+
{
60+
name: "prefers env var over file when both are set",
61+
envValue: "/priority/path",
62+
setupEnvFile: true,
63+
envFileContent: "CONTAINER_ROOT_DIR=/var/lib/docker",
64+
expectedPath: "/priority/path",
65+
},
66+
{
67+
name: "handles env file with multiple variables",
68+
envValue: "",
69+
setupEnvFile: true,
70+
envFileContent: "OTHER_VAR=value\nCONTAINER_ROOT_DIR=/multi/var/test\nANOTHER_VAR=other",
71+
expectedPath: "/multi/var/test/containers",
72+
},
73+
}
74+
75+
for _, tt := range tests {
76+
t.Run(tt.name, func(t *testing.T) {
77+
originalEnv := os.Getenv("CONTAINER_LOG_PATH")
78+
defer func() {
79+
if originalEnv != "" {
80+
if err := os.Setenv("CONTAINER_LOG_PATH", originalEnv); err != nil {
81+
t.Errorf("Failed to restore CONTAINER_LOG_PATH: %v", err)
82+
}
83+
} else {
84+
if err := os.Unsetenv("CONTAINER_LOG_PATH"); err != nil {
85+
t.Errorf("Failed to unset CONTAINER_LOG_PATH: %v", err)
86+
}
87+
}
88+
}()
89+
90+
if tt.envValue != "" {
91+
if err := os.Setenv("CONTAINER_LOG_PATH", tt.envValue); err != nil {
92+
t.Fatalf("Failed to set CONTAINER_LOG_PATH: %v", err)
93+
}
94+
} else {
95+
if err := os.Unsetenv("CONTAINER_LOG_PATH"); err != nil {
96+
t.Fatalf("Failed to unset CONTAINER_LOG_PATH: %v", err)
97+
}
98+
}
99+
100+
var envFilePath string
101+
if tt.setupEnvFile {
102+
tempDir := t.TempDir()
103+
envFilePath = filepath.Join(tempDir, "fluent-bit.env")
104+
105+
err := os.WriteFile(envFilePath, []byte(tt.envFileContent), 0644)
106+
if err != nil {
107+
t.Fatalf("Failed to create test env file: %v", err)
108+
}
109+
} else {
110+
envFilePath = "/non/existent/path/fluent-bit.env"
111+
}
112+
113+
result := getContainerLogPath(envFilePath)
114+
if result != tt.expectedPath {
115+
t.Errorf("getContainerLogPath() = %q, want %q", result, tt.expectedPath)
116+
}
117+
})
118+
}
119+
}

0 commit comments

Comments
 (0)