|
| 1 | +// This file is part of MinIO Kubernetes Cloud |
| 2 | +// Copyright (c) 2019 MinIO, Inc. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package cluster |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "io/ioutil" |
| 23 | + "net" |
| 24 | + "net/http" |
| 25 | + "regexp" |
| 26 | + "strings" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/minio/minio/pkg/env" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + errCantDetermineMinIOImage = errors.New("can't determine MinIO Image") |
| 34 | + errCantDetermineMCImage = errors.New("can't determine MC Image") |
| 35 | +) |
| 36 | + |
| 37 | +func getK8sAPIServer() string { |
| 38 | + // if m3 is running inside a k8s pod KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT will contain the k8s api server apiServerAddress |
| 39 | + // if m3 is not running inside k8s by default will look for the k8s api server on localhost:8001 (kubectl proxy) |
| 40 | + // NOTE: using kubectl proxy is for local development only, since every request send to localhost:8001 will bypass service account authentication |
| 41 | + // more info here: https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/#directly-accessing-the-rest-api |
| 42 | + // you can override this using M3_K8S_API_SERVER, ie use the k8s cluster from `kubectl config view` |
| 43 | + host, port := env.Get("KUBERNETES_SERVICE_HOST", ""), env.Get("KUBERNETES_SERVICE_PORT", "") |
| 44 | + apiServerAddress := "http://localhost:8001" |
| 45 | + if host != "" && port != "" { |
| 46 | + apiServerAddress = "https://" + net.JoinHostPort(host, port) |
| 47 | + } |
| 48 | + return env.Get(M3K8sAPIServer, apiServerAddress) |
| 49 | +} |
| 50 | + |
| 51 | +// getK8sAPIServerInsecure allow to tell the k8s client to skip TLS certificate verification, ie: when connecting to a k8s cluster |
| 52 | +// that uses certificate not trusted by your machine |
| 53 | +func getK8sAPIServerInsecure() bool { |
| 54 | + return strings.ToLower(env.Get(m3k8SAPIServerInsecure, "off")) == "on" |
| 55 | +} |
| 56 | + |
| 57 | +// GetNsFromFile assumes mkube is running inside a k8s pod and extract the current namespace from the |
| 58 | +// /var/run/secrets/kubernetes.io/serviceaccount/namespace file |
| 59 | +func GetNsFromFile() string { |
| 60 | + dat, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") |
| 61 | + if err != nil { |
| 62 | + return "default" |
| 63 | + } |
| 64 | + return string(dat) |
| 65 | +} |
| 66 | + |
| 67 | +// This operation will run only once at mkube startup |
| 68 | +var namespace = GetNsFromFile() |
| 69 | + |
| 70 | +// Returns the namespace in which the controller is installed |
| 71 | +func GetNs() string { |
| 72 | + return env.Get(M3Namespace, namespace) |
| 73 | +} |
| 74 | + |
| 75 | +// getLatestMinIOImage returns the latest docker image for MinIO if found on the internet |
| 76 | +func getLatestMinIOImage(client HTTPClientI) (*string, error) { |
| 77 | + resp, err := client.Get("https://dl.min.io/server/minio/release/linux-amd64/") |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + defer resp.Body.Close() |
| 82 | + |
| 83 | + body, err := ioutil.ReadAll(resp.Body) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + var re = regexp.MustCompile(`(?m)\.\/minio\.(RELEASE.*?Z)"`) |
| 88 | + // look for a single match |
| 89 | + matches := re.FindAllStringSubmatch(string(body), 1) |
| 90 | + for i := range matches { |
| 91 | + release := matches[i][1] |
| 92 | + dockerImage := fmt.Sprintf("minio/minio:%s", release) |
| 93 | + return &dockerImage, nil |
| 94 | + } |
| 95 | + return nil, errCantDetermineMinIOImage |
| 96 | +} |
| 97 | + |
| 98 | +var latestMinIOImage, errLatestMinIOImage = getLatestMinIOImage( |
| 99 | + &HTTPClient{ |
| 100 | + Client: &http.Client{ |
| 101 | + Timeout: 4 * time.Second, |
| 102 | + }, |
| 103 | + }) |
| 104 | + |
| 105 | +// GetMinioImage returns the image URL to be used when deploying a MinIO instance, if there is |
| 106 | +// a preferred image to be used (configured via ENVIRONMENT VARIABLES) GetMinioImage will return that |
| 107 | +// if not, GetMinioImage will try to obtain the image URL for the latest version of MinIO and return that |
| 108 | +func GetMinioImage() (*string, error) { |
| 109 | + image := strings.TrimSpace(env.Get(M3MinioImage, "")) |
| 110 | + // if there is a preferred image configured by the user we'll always return that |
| 111 | + if image != "" { |
| 112 | + return &image, nil |
| 113 | + } |
| 114 | + if errLatestMinIOImage != nil { |
| 115 | + return nil, errLatestMinIOImage |
| 116 | + } |
| 117 | + return latestMinIOImage, nil |
| 118 | +} |
| 119 | + |
| 120 | +// GetLatestMinioImage returns the latest image URL on minio repository |
| 121 | +func GetLatestMinioImage(client HTTPClientI) (*string, error) { |
| 122 | + latestMinIOImage, err := getLatestMinIOImage(client) |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + return latestMinIOImage, nil |
| 127 | +} |
| 128 | + |
| 129 | +// getLatestMCImage returns the latest docker image for MC if found on the internet |
| 130 | +func getLatestMCImage() (*string, error) { |
| 131 | + // Create an http client with a 4 second timeout |
| 132 | + client := http.Client{ |
| 133 | + Timeout: 4 * time.Second, |
| 134 | + } |
| 135 | + resp, err := client.Get("https://dl.min.io/client/mc/release/linux-amd64/") |
| 136 | + if err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + defer resp.Body.Close() |
| 140 | + |
| 141 | + body, err := ioutil.ReadAll(resp.Body) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + var re = regexp.MustCompile(`(?m)\.\/mc\.(RELEASE.*?Z)"`) |
| 146 | + // look for a single match |
| 147 | + matches := re.FindAllStringSubmatch(string(body), 1) |
| 148 | + for i := range matches { |
| 149 | + release := matches[i][1] |
| 150 | + dockerImage := fmt.Sprintf("minio/mc:%s", release) |
| 151 | + return &dockerImage, nil |
| 152 | + } |
| 153 | + return nil, errCantDetermineMCImage |
| 154 | +} |
| 155 | + |
| 156 | +var latestMCImage, errLatestMCImage = getLatestMCImage() |
| 157 | + |
| 158 | +func GetMCImage() (*string, error) { |
| 159 | + image := strings.TrimSpace(env.Get(M3MCImage, "")) |
| 160 | + // if there is a preferred image configured by the user we'll always return that |
| 161 | + if image != "" { |
| 162 | + return &image, nil |
| 163 | + } |
| 164 | + if errLatestMCImage != nil { |
| 165 | + return nil, errLatestMCImage |
| 166 | + } |
| 167 | + return latestMCImage, nil |
| 168 | +} |
0 commit comments