From 39bd5b5fee9ed9b642cc65bc7da9cc12cdf09198 Mon Sep 17 00:00:00 2001 From: marshyski Date: Fri, 20 Nov 2020 16:52:42 -0500 Subject: [PATCH] Add containers --- README.md | 20 +++++++-------- azure.go | 1 - detect.go | 29 +++++++++++++-------- digitalocean.go | 1 - gce.go | 2 -- go.mod | 4 +-- go.sum | 2 -- hypervisor.go | 67 ------------------------------------------------- kube.go | 27 ++++++++++++++++++++ softlayer.go | 2 -- vultr.go | 1 - 11 files changed, 57 insertions(+), 99 deletions(-) delete mode 100644 hypervisor.go create mode 100644 kube.go diff --git a/README.md b/README.md index 0ee86c7..d86b421 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,21 @@ -# detectcloud +# libdetectcloud -http.Client timeout is set to `120ms`. Sometimes hitting the metadata service to fast will return unknown instead of the cloud provider detected. +http.Client timeout is set to `300ms`. Sometimes hitting the metadata service to fast will return empty instead of the cloud provider detected. package main - + import ( "fmt" "gitlab.com/taskfitio/lib/detectcloud" ) - + func main() { - - // detectcloud.Detect() will return a string - // Amazon Web Services, Microsoft Azure, Digital Ocean - // Google Compute Engine, OpenStack, SoftLayer, Vultr - // unknown + + // detectcloud.Detect() will return an empty string or + // Amazon Web Services, Microsoft Azure, Digital Ocean + // Google Compute Engine, OpenStack, SoftLayer, Vultr + // K8S Container, Container fmt.Println(detectcloud.Detect()) - + } diff --git a/azure.go b/azure.go index 4ba53a2..f28b2bf 100644 --- a/azure.go +++ b/azure.go @@ -4,7 +4,6 @@ import ( "net/http" ) -// https://azure.microsoft.com/en-us/blog/what-just-happened-to-my-vm-in-vm-metadata-service/ func detectAzure() string { resp, err := hc.Get("http://169.254.169.254/metadata/v1/InstanceInfo") if err == nil && resp.StatusCode == http.StatusOK { diff --git a/detect.go b/detect.go index 588877d..081fd93 100644 --- a/detect.go +++ b/detect.go @@ -7,17 +7,18 @@ import ( "time" ) -var hc = &http.Client{Timeout: 120 * time.Millisecond} +var hc = &http.Client{Timeout: 300 * time.Millisecond} // Clouds type type Clouds struct { - Aws string - Azure string - Do string - Gce string - Ost string - Sl string - Vr string + Aws string + Azure string + Do string + Gce string + Ost string + Sl string + Vr string + Container string } // Detect function @@ -25,7 +26,7 @@ func Detect() string { if runtime.GOOS != "darwin" { var c Clouds var wg sync.WaitGroup - wg.Add(7) + wg.Add(8) go func() { defer wg.Done() c.Aws = detectAWS() @@ -54,7 +55,12 @@ func Detect() string { defer wg.Done() c.Vr = detectVultr() }() + go func() { + defer wg.Done() + c.Container = detectContainer() + }() wg.Wait() + if c.Aws != "" { return c.Aws } @@ -76,6 +82,9 @@ func Detect() string { if c.Vr != "" { return c.Vr } + if c.Container != "" { + return c.Container + } } - return "unknown" + return "" } diff --git a/digitalocean.go b/digitalocean.go index 15e5725..4de0abb 100644 --- a/digitalocean.go +++ b/digitalocean.go @@ -4,7 +4,6 @@ import ( "net/http" ) -// https://developers.digitalocean.com/documentation/metadata/#metadata-in-json func detectDigitalOcean() string { resp, err := hc.Get("http://169.254.169.254/metadata/v1.json") if err == nil && resp.StatusCode == http.StatusOK { diff --git a/gce.go b/gce.go index 76216a9..d5cca5f 100644 --- a/gce.go +++ b/gce.go @@ -4,8 +4,6 @@ import ( "net/http" ) -// https://cloud.google.com/compute/docs/storing-retrieving-metadata#endpoints -// curl "http://metadata.google.internal/computeMetadata/v1/instance/tags" -H "Metadata-Flavor: Google" func detectGCE() string { r, err := http.NewRequest("GET", "http://metadata.google.internal/computeMetadata/v1/instance/tags", nil) if err != nil { diff --git a/go.mod b/go.mod index 89499b2..eb6ffad 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,3 @@ module github.com/yeticloud/libdetectcloud -go 1.14 - -require github.com/digitalocean/go-smbios v0.0.0-20180907143718-390a4f403a8e +go 1.15 diff --git a/go.sum b/go.sum index b20a30a..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +0,0 @@ -github.com/digitalocean/go-smbios v0.0.0-20180907143718-390a4f403a8e h1:vUmf0yezR0y7jJ5pceLHthLaYf4bA5T14B6q39S4q2Q= -github.com/digitalocean/go-smbios v0.0.0-20180907143718-390a4f403a8e/go.mod h1:YTIHhz/QFSYnu/EhlF2SpU2Uk+32abacUYA5ZPljz1A= diff --git a/hypervisor.go b/hypervisor.go deleted file mode 100644 index bb72d09..0000000 --- a/hypervisor.go +++ /dev/null @@ -1,67 +0,0 @@ -package libdetectcloud - -import ( - "regexp" - - "github.com/digitalocean/go-smbios/smbios" -) - -// BIOS type -type BIOS struct { - Manufacturer string - ProductName string - SerialNumber string - IsVM bool - Hypervisor string -} - -var hypervisors = map[string][]string{ - "VMWare": []string{"vmware", "vm ware"}, - "Xen": []string{"xen"}, - "Hyper-V": []string{"microsoft", "hyperv", "hyper-v", "hyper v"}, - "VirtualBox": []string{"virtual box", "virtual-box", "virtualbox"}, - "KVM": []string{"virt-manager", "virt manager", "virtmanager", "hvm", "kvm"}, - "Parallels": []string{"parallels"}, -} - -//BIOSInfo function gets information from the SMBIOS -func BIOSInfo() (*BIOS, error) { - rc, _, err := smbios.Stream() - if err != nil { - return nil, err - } - // Be sure to close the stream! - defer rc.Close() - - // Decode SMBIOS structures from the stream. - d := smbios.NewDecoder(rc) - ss, err := d.Decode() - if err != nil { - return nil, err - } - info := []string{} - for _, i := range ss { - if i.Header.Type == 1 { - info = i.Strings - } - } - b := &BIOS{ - Manufacturer: info[0], - ProductName: info[1], - SerialNumber: info[3], - } - for h, s := range hypervisors { - for _, i := range s { - re, err := regexp.Compile(`(?igm)` + i) - if err != nil { - return b, nil - } - if re.MatchString(b.Manufacturer) || re.MatchString(b.ProductName) { - b.IsVM = true - b.Hypervisor = h - return b, nil - } - } - } - return b, nil -} diff --git a/kube.go b/kube.go new file mode 100644 index 0000000..ecf83be --- /dev/null +++ b/kube.go @@ -0,0 +1,27 @@ +package libdetectcloud + +import ( + "io/ioutil" + "strings" +) + +func detectContainer() string { + b, err := ioutil.ReadFile("/proc/self/cgroup") + if err != nil { + return "" + } + + fc := string(b) + kube := strings.Contains(fc, "kube") + container := strings.Contains(fc, "containerd") + + if kube { + return "K8S Container" + } + + if container { + return "Container" + } + + return "" +} diff --git a/softlayer.go b/softlayer.go index c9a8eb2..74c06b5 100644 --- a/softlayer.go +++ b/softlayer.go @@ -4,8 +4,6 @@ import ( "net/http" ) -// https://sldn.softlayer.com/blog/jarteche/getting-started-user-data-and-post-provisioning-scripts -// https://github.com/bodenr/cci/wiki/SL-user-metadata func detectSoftlayer() string { resp, err := hc.Get("https://api.service.softlayer.com/rest/v3/SoftLayer_Resource_Metadata/UserMetadata.txt") if err == nil && (resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNotFound) { diff --git a/vultr.go b/vultr.go index 284e163..2329493 100644 --- a/vultr.go +++ b/vultr.go @@ -4,7 +4,6 @@ import ( "net/http" ) -// https://www.vultr.com/metadata/ func detectVultr() string { resp, err := hc.Get("http://169.254.169.254/v1.json") if err == nil && resp.StatusCode == http.StatusOK {