Add support for DCOS host detection, improve Docker detection. (#4525)

isDocker was currently reading from `/proc/cgroup` file. But
this file alone is rather not conclusive evidence. Docker
internally has `.dockerenv` as a special file which we should
use instead.

Fixes #4456
This commit is contained in:
Harshavardhana
2017-06-12 17:33:21 -07:00
committed by Dee Koder
parent 6f4862659f
commit b8463a738c
3 changed files with 122 additions and 53 deletions

View File

@@ -17,7 +17,6 @@
package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
@@ -98,26 +97,41 @@ func GetCurrentReleaseTime() (releaseTime time.Time, err error) {
return getCurrentReleaseTime(Version, os.Args[0])
}
func isDocker(cgroupFile string) (bool, error) {
cgroup, err := ioutil.ReadFile(cgroupFile)
if os.IsNotExist(err) {
err = nil
// Check if we are indeed inside docker.
// https://github.com/moby/moby/blob/master/daemon/initlayer/setup_unix.go#L25
//
// "/.dockerenv": "file",
//
func isDocker(dockerEnvFile string) (ok bool, err error) {
_, err = os.Stat(dockerEnvFile)
if err != nil {
if os.IsNotExist(err) {
err = nil
}
return false, err
}
return bytes.Contains(cgroup, []byte("docker")), err
return true, nil
}
// IsDocker - returns if the environment minio is running
// is docker or not.
func IsDocker() bool {
found, err := isDocker("/proc/self/cgroup")
fatalIf(err, "Error in docker check.")
found, err := isDocker("/.dockerenv")
// We don't need to fail for this check, log
// an error and return false.
errorIf(err, "Error in docker check.")
return found
}
// IsKubernetes returns if the environment minio is
// running is kubernetes or not.
// IsDCOS returns true if minio is running in DCOS.
func IsDCOS() bool {
// http://mesos.apache.org/documentation/latest/docker-containerizer/
// Mesos docker containerizer sets this value
return os.Getenv("MESOS_CONTAINER_NAME") != ""
}
// IsKubernetes returns true if minio is running in kubernetes.
func IsKubernetes() bool {
// Kubernetes env used to validate if we are
// indeed running inside a kubernetes pod
@@ -139,7 +153,7 @@ func IsSourceBuild() bool {
// DO NOT CHANGE USER AGENT STYLE.
// The style should be
//
// Minio (<OS>; <ARCH>[; kubernetes][; docker][; source]) Minio/<VERSION> Minio/<RELEASE-TAG> Minio/<COMMIT-ID>
// Minio (<OS>; <ARCH>[; dcos][; kubernetes][; docker][; source]) Minio/<VERSION> Minio/<RELEASE-TAG> Minio/<COMMIT-ID> [Minio/univers-<PACKAGE_NAME>]
//
// For any change here should be discussed by openning an issue at https://github.com/minio/minio/issues.
func getUserAgent(mode string) string {
@@ -147,6 +161,9 @@ func getUserAgent(mode string) string {
if mode != "" {
userAgent += "; " + mode
}
if IsDCOS() {
userAgent += "; dcos"
}
if IsKubernetes() {
userAgent += "; kubernetes"
}
@@ -156,8 +173,15 @@ func getUserAgent(mode string) string {
if IsSourceBuild() {
userAgent += "; source"
}
userAgent += ") " + " Minio/" + Version + " Minio/" + ReleaseTag + " Minio/" + CommitID
userAgent += ") Minio/" + Version + " Minio/" + ReleaseTag + " Minio/" + CommitID
if IsDCOS() {
universePkgVersion := os.Getenv("MARATHON_APP_LABEL_DCOS_PACKAGE_VERSION")
// On DC/OS environment try to the get universe package version.
if universePkgVersion != "" {
userAgent += " Minio/" + "universe-" + universePkgVersion
}
}
return userAgent
}
@@ -241,10 +265,21 @@ func getLatestReleaseTime(timeout time.Duration, mode string) (releaseTime time.
return parseReleaseData(data)
}
// Kubernetes deploy doc link.
const kubernetesDeploymentDoc = "https://docs.minio.io/docs/deploy-minio-on-kubernetes"
const (
// Kubernetes deployment doc link.
kubernetesDeploymentDoc = "https://docs.minio.io/docs/deploy-minio-on-kubernetes"
// Mesos deployment doc link.
mesosDeploymentDoc = "https://docs.minio.io/docs/deploy-minio-on-dc-os"
)
func getDownloadURL(buildDate time.Time) (downloadURL string) {
// Check if we are in DCOS environment, return
// deployment guide for update procedures.
if IsDCOS() {
return mesosDeploymentDoc
}
// Check if we are in kubernetes environment, return
// deployment guide for update procedures.
if IsKubernetes() {