Skip to content

Commit b0d976c

Browse files
committed
ensure host platform are files and have contents
In a containerized deployment, it is common to mount several files from /etc. Within the container, those files will be created regardless if they exist on the host or not. In those instances, the existing code would erroneously return empty platform information.
1 parent 83c941c commit b0d976c

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

host/host_linux.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,47 +229,47 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil
229229
version = contents[0]
230230
}
231231
}
232-
} else if common.PathExists(common.HostEtcWithContext(ctx, "neokylin-release")) {
232+
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "neokylin-release")) {
233233
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "neokylin-release"))
234234
if err == nil {
235235
version = getRedhatishVersion(contents)
236236
platform = getRedhatishPlatform(contents)
237237
}
238-
} else if common.PathExists(common.HostEtcWithContext(ctx, "redhat-release")) {
238+
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "redhat-release")) {
239239
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "redhat-release"))
240240
if err == nil {
241241
version = getRedhatishVersion(contents)
242242
platform = getRedhatishPlatform(contents)
243243
}
244-
} else if common.PathExists(common.HostEtcWithContext(ctx, "system-release")) {
244+
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "system-release")) {
245245
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "system-release"))
246246
if err == nil {
247247
version = getRedhatishVersion(contents)
248248
platform = getRedhatishPlatform(contents)
249249
}
250-
} else if common.PathExists(common.HostEtcWithContext(ctx, "gentoo-release")) {
250+
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "gentoo-release")) {
251251
platform = "gentoo"
252252
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "gentoo-release"))
253253
if err == nil {
254254
version = getRedhatishVersion(contents)
255255
}
256-
} else if common.PathExists(common.HostEtcWithContext(ctx, "SuSE-release")) {
256+
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "SuSE-release")) {
257257
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "SuSE-release"))
258258
if err == nil {
259259
version = getSuseVersion(contents)
260260
platform = getSusePlatform(contents)
261261
}
262262
// TODO: slackware detecion
263-
} else if common.PathExists(common.HostEtcWithContext(ctx, "arch-release")) {
263+
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "arch-release")) {
264264
platform = "arch"
265265
version = lsb.Release
266-
} else if common.PathExists(common.HostEtcWithContext(ctx, "alpine-release")) {
266+
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "alpine-release")) {
267267
platform = "alpine"
268268
contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "alpine-release"))
269269
if err == nil && len(contents) > 0 && contents[0] != "" {
270270
version = contents[0]
271271
}
272-
} else if common.PathExists(common.HostEtcWithContext(ctx, "os-release")) {
272+
} else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "os-release")) {
273273
p, v, err := common.GetOSReleaseWithContext(ctx)
274274
if err == nil {
275275
platform = p

internal/common/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func PathExistsWithContents(filename string) bool {
343343
if err != nil {
344344
return false
345345
}
346-
return info.Size() > 4 // at least 4 bytes
346+
return info.Size() > 4 && !info.IsDir() // at least 4 bytes
347347
}
348348

349349
// GetEnvWithContext retrieves the environment variable key. If it does not exist it returns the default.

0 commit comments

Comments
 (0)