From 189cc9ce3c1c43e69e6f4ff40c5385cd4ab0f026 Mon Sep 17 00:00:00 2001 From: Ray Tung Date: Wed, 15 May 2019 23:52:28 +1000 Subject: [PATCH] Fixes #2232 This commit fixes the bug in #2232 where cadvisor was not able to detect the cloud provider if it's running on a custom AMI derived from Amazon Linux 2. It does so by checking /etc/os-release. However, from what I've read, /etc/os-release is pretty much a systemd thing. Although Amazon Linux 2 comes with systemd, cadvisor cannot assume the existence of systemd in other AMIs / OSes, therefore we would only be checking for /etc/os-release if all other methods fail us. --- utils/cloudinfo/aws/aws.go | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/utils/cloudinfo/aws/aws.go b/utils/cloudinfo/aws/aws.go index 3b699516..2c1f611e 100644 --- a/utils/cloudinfo/aws/aws.go +++ b/utils/cloudinfo/aws/aws.go @@ -16,7 +16,6 @@ package cloudinfo import ( "io/ioutil" - "os" "strings" "github.com/aws/aws-sdk-go/aws" @@ -28,9 +27,10 @@ import ( ) const ( - productVerFileName = "/sys/class/dmi/id/product_version" - biosVerFileName = "/sys/class/dmi/id/bios_vendor" - amazon = "amazon" + productVerFileName = "/sys/class/dmi/id/product_version" + biosVerFileName = "/sys/class/dmi/id/bios_vendor" + systemdOSReleaseFileName = "/etc/os-release" + amazon = "amazon" ) func init() { @@ -42,23 +42,18 @@ type provider struct{} var _ cloudinfo.CloudProvider = provider{} func (provider) IsActiveProvider() bool { - var dataProduct []byte - var dataBios []byte - if _, err := os.Stat(productVerFileName); err == nil { - dataProduct, err = ioutil.ReadFile(productVerFileName) - if err != nil { - return false - } + return fileContainsAmazonIdentifier(productVerFileName) || + fileContainsAmazonIdentifier(biosVerFileName) || + fileContainsAmazonIdentifier(systemdOSReleaseFileName) +} + +func fileContainsAmazonIdentifier(filename string) bool { + fileContent, err := ioutil.ReadFile(filename) + if err != nil { + return false } - if _, err := os.Stat(biosVerFileName); err == nil { - dataBios, err = ioutil.ReadFile(biosVerFileName) - if err != nil { - return false - } - } - - return strings.Contains(string(dataProduct), amazon) || strings.Contains(strings.ToLower(string(dataBios)), amazon) + return strings.Contains(string(fileContent), amazon) } func getAwsMetadata(name string) string {