Add support for kubernetes host detection (#4514)

Additionally improve what we print for `docker pull`
such that its precisely the relevant release tag.

Fixes #4456
This commit is contained in:
Harshavardhana 2017-06-09 02:42:12 -07:00 committed by GitHub
parent 3dfe254a11
commit 48dbd49980
2 changed files with 72 additions and 6 deletions

View File

@ -107,7 +107,8 @@ func isDocker(cgroupFile string) (bool, error) {
return bytes.Contains(cgroup, []byte("docker")), err
}
// IsDocker - returns if the environment is docker or not.
// 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.")
@ -115,6 +116,16 @@ func IsDocker() bool {
return found
}
// IsKubernetes returns if the environment minio is
// running is kubernetes or not.
func IsKubernetes() bool {
// Kubernetes env used to validate if we are
// indeed running inside a kubernetes pod
// is KUBERNETES_SERVICE_HOST but in future
// we might need to enhance this.
return os.Getenv("KUBERNETES_SERVICE_HOST") != ""
}
func isSourceBuild(minioVersion string) bool {
_, err := time.Parse(time.RFC3339, minioVersion)
return err != nil
@ -127,7 +138,8 @@ func IsSourceBuild() bool {
// DO NOT CHANGE USER AGENT STYLE.
// The style should be
// Minio (<OS>; <ARCH>[; docker][; source]) Minio/<VERSION> Minio/<RELEASE-TAG> Minio/<COMMIT-ID>
//
// Minio (<OS>; <ARCH>[; kubernetes][; docker][; source]) Minio/<VERSION> Minio/<RELEASE-TAG> Minio/<COMMIT-ID>
//
// For any change here should be discussed by openning an issue at https://github.com/minio/minio/issues.
func getUserAgent(mode string) string {
@ -135,6 +147,9 @@ func getUserAgent(mode string) string {
if mode != "" {
userAgent += "; " + mode
}
if IsKubernetes() {
userAgent += "; kubernetes"
}
if IsDocker() {
userAgent += "; docker"
}
@ -226,11 +241,24 @@ func getLatestReleaseTime(timeout time.Duration, mode string) (releaseTime time.
return parseReleaseData(data)
}
func getDownloadURL() (downloadURL string) {
if IsDocker() {
return "docker pull minio/minio"
// Kubernetes deploy doc link.
const kubernetesDeploymentDoc = "https://docs.minio.io/docs/deploy-minio-on-kubernetes"
func getDownloadURL(buildDate time.Time) (downloadURL string) {
// Check if we are in kubernetes environment, return
// deployment guide for update procedures.
if IsKubernetes() {
return kubernetesDeploymentDoc
}
// Check if we are docker environment, return docker update command
if IsDocker() {
// Construct release tag name.
rTag := "RELEASE." + buildDate.Format(minioReleaseTagTimeLayout)
return fmt.Sprintf("docker pull minio/minio:%s", rTag)
}
// For binary only installations, then we just show binary download link.
if runtime.GOOS == "windows" {
return minioReleaseURL + "minio.exe"
}
@ -254,7 +282,7 @@ func getUpdateInfo(timeout time.Duration, mode string) (older time.Duration,
if latestReleaseTime.After(currentReleaseTime) {
older = latestReleaseTime.Sub(currentReleaseTime)
downloadURL = getDownloadURL()
downloadURL = getDownloadURL(latestReleaseTime)
}
return older, downloadURL, nil

View File

@ -30,6 +30,28 @@ import (
"time"
)
func TestDownloadURL(t *testing.T) {
minioVersion1 := UTCNow()
durl := getDownloadURL(minioVersion1)
if runtime.GOOS == "windows" {
if durl != minioReleaseURL+"minio.exe" {
t.Errorf("Expected %s, got %s", minioReleaseURL+"minio.exe", durl)
}
} else {
if durl != minioReleaseURL+"minio" {
t.Errorf("Expected %s, got %s", minioReleaseURL+"minio", durl)
}
}
os.Setenv("KUBERNETES_SERVICE_HOST", "10.11.148.5")
defer os.Unsetenv("KUBERNETES_SERVICE_HOST")
durl = getDownloadURL(minioVersion1)
if durl != kubernetesDeploymentDoc {
t.Errorf("Expected %s, got %s", kubernetesDeploymentDoc, durl)
}
}
func TestGetCurrentReleaseTime(t *testing.T) {
minioVersion1 := UTCNow().Format(time.RFC3339)
releaseTime1, _ := time.Parse(time.RFC3339, minioVersion1)
@ -136,6 +158,21 @@ func TestGetCurrentReleaseTime(t *testing.T) {
}
}
// Tests if the environment we are running is in kubernetes.
func TestIsKubernetes(t *testing.T) {
os.Setenv("KUBERNETES_SERVICE_HOST", "10.11.148.5")
kubernetes := IsKubernetes()
if !kubernetes {
t.Fatalf("Expected %t, got %t", true, kubernetes)
}
os.Unsetenv("KUBERNETES_SERVICE_HOST")
kubernetes = IsKubernetes()
if kubernetes {
t.Fatalf("Expected %t, got %t", false, kubernetes)
}
}
// Tests if the environment we are running is in docker.
func TestIsDocker(t *testing.T) {
createTempFile := func(content string) string {
tmpfile, err := ioutil.TempFile("", "isdocker-testcase")
@ -164,6 +201,7 @@ func TestIsDocker(t *testing.T) {
1:name=systemd:/user.slice/user-1000.slice/user@1000.service/gnome-terminal-server.service
`)
defer os.Remove(filename1)
filename2 := createTempFile(`14:name=systemd:/docker/d5eb950884d828237f60f624ff575a1a7a4daa28a8d4d750040527ed9545e727
13:pids:/docker/d5eb950884d828237f60f624ff575a1a7a4daa28a8d4d750040527ed9545e727
12:hugetlb:/docker/d5eb950884d828237f60f624ff575a1a7a4daa28a8d4d750040527ed9545e727