Skip to content

Commit f41b9c1

Browse files
committed
worked on PR review comments
1 parent d02a2ac commit f41b9c1

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

pkg/host/info.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -223,33 +223,25 @@ func (i *Info) releaseInfo(ctx context.Context, osReleaseLocation string) (*v1.R
223223

224224
// containerID returns the container ID of the current running environment.
225225
func (i *Info) containerID(ctx context.Context) (string, error) {
226-
var (
227-
containerIDMount string
228-
errMount error
229-
)
226+
var errs error
230227

231-
containerIDMount, errMount = containerIDFromMountInfo(i.mountInfoLocation)
232-
if containerIDMount != "" {
228+
// Try to get container ID from mount info first
229+
if containerIDMount, err := containerIDFromMountInfo(i.mountInfoLocation); err == nil && containerIDMount != "" {
233230
return uuid.NewMD5(uuid.NameSpaceDNS, []byte(containerIDMount)).String(), nil
231+
} else if err != nil {
232+
errs = errors.Join(errs, err)
234233
}
235234

235+
// Try to get container ID from ECS metadata if available
236236
if metadataURI := os.Getenv(ecsMetadataEnvV4); metadataURI != "" {
237-
if cid, errEcs := i.containerIDFromECS(ctx, metadataURI); errEcs == nil && cid != "" {
237+
if cid, err := i.containerIDFromECS(ctx, metadataURI); err == nil && cid != "" {
238238
return uuid.NewMD5(uuid.NameSpaceDNS, []byte(cid)).String(), nil
239-
} else if errEcs != nil {
240-
if errMount != nil {
241-
return "", errMount
242-
}
243-
244-
return "", errEcs
239+
} else if err != nil {
240+
errs = errors.Join(errs, err)
245241
}
246242
}
247243

248-
if errMount != nil {
249-
return "", errMount
250-
}
251-
252-
return "", errors.New("container ID not found")
244+
return "", errs
253245
}
254246

255247
// containsContainerReference checks if the cgroup file contains references to container runtimes.
@@ -426,7 +418,7 @@ func (i *Info) containerIDFromECS(ctx context.Context, uri string) (string, erro
426418
defer resp.Body.Close()
427419

428420
if resp.StatusCode != http.StatusOK {
429-
return "", fmt.Errorf("metadata endpoint returned status %d", resp.StatusCode)
421+
return "", fmt.Errorf("metadata endpoint %s returned status %d", uri, resp.StatusCode)
430422
}
431423

432424
var metadata struct {

0 commit comments

Comments
 (0)