@@ -19,26 +19,74 @@ import (
1919 "github.com/sirupsen/logrus"
2020)
2121
22- // static paths
2322const (
24- s3FileDirectoryPath = "/init/fluent-bit-init-s3-files/"
25- mainConfigFile = "/init/fluent-bit-init.conf"
23+ // Default paths
24+ primaryS3FileDirectoryPath = "/init/fluent-bit-init-s3-files/"
25+ primaryMainConfigFile = "/init/fluent-bit-init.conf"
26+ primaryInvokeFile = "/init/invoke_fluent_bit.sh"
27+
28+ // Fallback paths when we do not have write permissions to the /init/ directory
29+ fallbackS3FileDirectoryPath = "/tmp/init/fluent-bit-init-s3-files/"
30+ fallbackMainConfigFile = "/tmp/init/fluent-bit-init.conf"
31+ fallbackInvokeFile = "/tmp/init/invoke_fluent_bit.sh"
32+
33+ // This is typically configured by the orchestrator like ECS during container start
2634 originalMainConfigFile = "/fluent-bit/etc/fluent-bit.conf"
27- invokeFile = "/init/invoke_fluent_bit.sh"
2835)
2936
3037var (
31- // default Fluent Bit command
38+ // Default Fluent Bit command
3239 baseCommand = "exec /fluent-bit/bin/fluent-bit -e /fluent-bit/firehose.so -e /fluent-bit/cloudwatch.so -e /fluent-bit/kinesis.so"
3340
34- // global s3 client and flag
41+ // Global S3 client and flag
3542 s3Client * s3.Client
3643 s3ClientCreated bool = false
3744
38- // global ecs metadata region
45+ // Global ECS metadata region
3946 metadataRegion string = ""
47+
48+ // Runtime-determined paths (set in the init() function)
49+ s3FileDirectoryPath string
50+ mainConfigFile string
51+ invokeFile string
52+
53+ // osCreate holds the os.Create function, allowing it to be mocked in tests
54+ osCreate = os .Create
4055)
4156
57+ // Initialize paths based on write permissions
58+ func init () {
59+ if canWriteToDir ("/init" ) {
60+ logrus .Info ("[FluentBit Init Process] Using /init/ directory" )
61+ s3FileDirectoryPath = primaryS3FileDirectoryPath
62+ mainConfigFile = primaryMainConfigFile
63+ invokeFile = primaryInvokeFile
64+ } else {
65+ logrus .Info ("[FluentBit Init Process] Using /tmp/init/ directory since I do not have write access to the /init/ directory" )
66+ s3FileDirectoryPath = fallbackS3FileDirectoryPath
67+ mainConfigFile = fallbackMainConfigFile
68+ invokeFile = fallbackInvokeFile
69+ }
70+ }
71+
72+ // canWriteToDir tests whether we can write to the specified directory
73+ func canWriteToDir (dir string ) bool {
74+ testFile := filepath .Join (dir , ".write-test" )
75+ file , err := osCreate (testFile )
76+ if err != nil {
77+ return false
78+ }
79+ err = file .Close ()
80+ if err != nil {
81+ logrus .Warnf ("[FluentBit Init Process] Unable to close test file %s, err: %v" , testFile , err )
82+ }
83+ err = os .Remove (testFile )
84+ if err != nil {
85+ logrus .Warnf ("[FluentBit Init Process] Unable to remove test file %s, err: %v" , testFile , err )
86+ }
87+ return true
88+ }
89+
4290// HTTPClient interface
4391type HTTPClient interface {
4492 Get (url string ) (* http.Response , error )
@@ -152,7 +200,7 @@ func createCommand(command *string, filePath string) {
152200
153201// get our built in config files or files from s3
154202// process built-in config files directly
155- // add S3 config files to directory "/ init/fluent-bit-init-s3-files/"
203+ // add S3 config files to directory "{ init-directory} /fluent-bit-init-s3-files/"
156204func getAllConfigFiles () {
157205 // get all env vars in the container
158206 envs := os .Environ ()
@@ -246,7 +294,7 @@ func getS3ConfigFile(userInput string) string {
246294 // create a downloader
247295 s3Downloader := createS3Downloader (bucketRegion )
248296
249- // download file from S3 and store in the directory "/ init/fluent-bit-init-s3-files/"
297+ // download file from S3 and store in the directory "{ init-directory} /fluent-bit-init-s3-files/"
250298 downloadS3ConfigFile (s3Downloader , s3FilePath , bucketName , s3FileDirectoryPath )
251299
252300 return s3FilePath
@@ -399,7 +447,7 @@ func main() {
399447
400448 // get our built in config files or files from s3
401449 // process built-in config files directly
402- // add S3 config files to directory "/ init/fluent-bit-init-s3-files/"
450+ // add S3 config files to directory "{ init-directory} /fluent-bit-init-s3-files/"
403451 getAllConfigFiles ()
404452
405453 // modify invoke_fluent_bit.sh, invoke fluent bit
0 commit comments