@@ -2,11 +2,14 @@ package dockergen
22
33import (
44 "bufio"
5+ "bytes"
6+ "fmt"
7+ "io"
58 "os"
69 "regexp"
710 "sync"
8- "fmt"
9- "github.com/fsouza/go-dockerclient"
11+
12+ docker "github.com/fsouza/go-dockerclient"
1013)
1114
1215var (
@@ -158,57 +161,85 @@ type Docker struct {
158161 CurrentContainerID string
159162}
160163
161- func GetCurrentContainerID () string {
162- filepaths := []string {"/proc/self/cgroup" , "/proc/self/mountinfo" }
164+ // GetCurrentContainerID attempts to extract the current container ID from the provided file paths.
165+ // If no files paths are provided, it will default to /proc/1/cpuset, /proc/self/cgroup and /proc/self/mountinfo.
166+ // It attempts to match the HOSTNAME first then use the fallback method, and returns with the first valid match.
167+ func GetCurrentContainerID (filepaths ... string ) (id string ) {
168+ if len (filepaths ) == 0 {
169+ filepaths = []string {"/proc/1/cpuset" , "/proc/self/cgroup" , "/proc/self/mountinfo" }
170+ }
171+
172+ var files []io.Reader
163173
164174 for _ , filepath := range filepaths {
165175 file , err := os .Open (filepath )
166176 if err != nil {
167177 continue
168178 }
169- reader := bufio .NewReader (file )
170- scanner := bufio .NewScanner (reader )
171- scanner .Split (bufio .ScanLines )
172- for scanner .Scan () {
173- _ , lines , err := bufio .ScanLines ([]byte (scanner .Text ()), true )
174- if err == nil {
175- strLines := string (lines )
176- if id := matchDockerCurrentContainerID (strLines ); id != "" {
177- return id
178- } else if id := matchECSCurrentContainerID (strLines ); id != "" {
179- return id
180- }
179+ defer file .Close ()
180+ files = append (files , file )
181+ }
182+
183+ reader := io .MultiReader (files ... )
184+ var buffer bytes.Buffer
185+ tee := io .TeeReader (reader , & buffer )
186+
187+ // We try to match a 64 character hex string starting with the hostname first
188+ scanner := bufio .NewScanner (tee )
189+ scanner .Split (bufio .ScanLines )
190+ for scanner .Scan () {
191+ _ , lines , err := bufio .ScanLines ([]byte (scanner .Text ()), true )
192+ if err == nil {
193+ strLines := string (lines )
194+ if id = matchContainerIDWithHostname (strLines ); len (id ) == 64 {
195+ return
181196 }
182197 }
183198 }
184199
185- return ""
200+ // If we didn't get any ID that matches the hostname, fall back to matching the first 64 character hex string
201+ scanner = bufio .NewScanner (& buffer )
202+ scanner .Split (bufio .ScanLines )
203+ for scanner .Scan () {
204+ _ , lines , err := bufio .ScanLines ([]byte (scanner .Text ()), true )
205+ if err == nil {
206+ strLines := string (lines )
207+ if id = matchContainerID (strLines ); len (id ) == 64 {
208+ return
209+ }
210+ }
211+ }
212+
213+ return
186214}
187215
188- func matchDockerCurrentContainerID (lines string ) string {
216+ func matchContainerIDWithHostname (lines string ) string {
189217 hostname := os .Getenv ("HOSTNAME" )
190- regex := fmt .Sprintf ("(%s[[:alnum:]]{52})" , hostname )
191- re := regexp .MustCompilePOSIX (regex )
218+ re := regexp .MustCompilePOSIX ("^[[:alnum:]]{12}$" )
192219
193- if re .MatchString (lines ) {
194- submatches := re . FindStringSubmatch ( string ( lines ) )
195- containerID := submatches [ 1 ]
220+ if re .MatchString (hostname ) {
221+ regex := fmt . Sprintf ( "(%s[[:alnum:]]{52})" , hostname )
222+ re := regexp . MustCompilePOSIX ( regex )
196223
197- return containerID
224+ if re .MatchString (lines ) {
225+ submatches := re .FindStringSubmatch (string (lines ))
226+ containerID := submatches [1 ]
227+
228+ return containerID
229+ }
198230 }
199231 return ""
200232}
201233
202- func matchECSCurrentContainerID (lines string ) string {
203- regex := "/ecs \\ /[^ \\ /]+ \\ /(.+)$ "
234+ func matchContainerID (lines string ) string {
235+ regex := "([[:alnum:]]{64}) "
204236 re := regexp .MustCompilePOSIX (regex )
205237
206- if re .MatchString (string ( lines ) ) {
238+ if re .MatchString (lines ) {
207239 submatches := re .FindStringSubmatch (string (lines ))
208240 containerID := submatches [1 ]
209241
210242 return containerID
211243 }
212-
213244 return ""
214245}
0 commit comments