|
| 1 | +/* |
| 2 | +Copyright 2020 The Skaffold Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cluster |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "log" |
| 24 | + "net/url" |
| 25 | + "os" |
| 26 | + "os/exec" |
| 27 | + |
| 28 | + "github.com/sirupsen/logrus" |
| 29 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 31 | + "k8s.io/client-go/rest" |
| 32 | + |
| 33 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" |
| 34 | + k8s "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/client" |
| 35 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context" |
| 36 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" |
| 37 | +) |
| 38 | + |
| 39 | +var GetClient = getClient |
| 40 | + |
| 41 | +// To override during tests |
| 42 | +var ( |
| 43 | + minikubeBinaryFunc = minikubeBinary |
| 44 | + getRestClientConfigFunc = context.GetRestClientConfig |
| 45 | +) |
| 46 | + |
| 47 | +type Client interface { |
| 48 | + // IsMinikube returns true if the given kubeContext maps to a minikube cluster |
| 49 | + IsMinikube(kubeContext string) bool |
| 50 | + // MinikubeExec returns the Cmd struct to execute minikube with given arguments |
| 51 | + MinikubeExec(arg ...string) (*exec.Cmd, error) |
| 52 | +} |
| 53 | + |
| 54 | +type clientImpl struct{} |
| 55 | + |
| 56 | +func getClient() Client { |
| 57 | + return clientImpl{} |
| 58 | +} |
| 59 | + |
| 60 | +func (clientImpl) IsMinikube(kubeContext string) bool { |
| 61 | + // short circuit if context is 'minikube' |
| 62 | + if kubeContext == constants.DefaultMinikubeContext { |
| 63 | + return true |
| 64 | + } |
| 65 | + _, err := minikubeBinaryFunc() |
| 66 | + if err != nil { |
| 67 | + logrus.Debugf("Minikube cluster not detected: %v", err) |
| 68 | + return false // minikube binary not found |
| 69 | + } |
| 70 | + |
| 71 | + if ok, err := matchNodeLabel(kubeContext); err != nil { |
| 72 | + logrus.Debugf("failed to check minikube node labels: %v", err) |
| 73 | + } else if ok { |
| 74 | + logrus.Debugf("Minikube cluster detected: context %q nodes have minikube labels", kubeContext) |
| 75 | + return true |
| 76 | + } |
| 77 | + |
| 78 | + if ok, err := matchProfileAndServerURL(kubeContext); err != nil { |
| 79 | + logrus.Debugf("failed to match minikube profile: %v", err) |
| 80 | + } else if ok { |
| 81 | + logrus.Debugf("Minikube cluster detected: context %q has matching profile name or server url", kubeContext) |
| 82 | + return true |
| 83 | + } |
| 84 | + logrus.Debugf("Minikube cluster not detected for context %q", kubeContext) |
| 85 | + return false |
| 86 | +} |
| 87 | + |
| 88 | +func (clientImpl) MinikubeExec(arg ...string) (*exec.Cmd, error) { |
| 89 | + return minikubeExec(arg...) |
| 90 | +} |
| 91 | + |
| 92 | +func minikubeExec(arg ...string) (*exec.Cmd, error) { |
| 93 | + b, err := minikubeBinaryFunc() |
| 94 | + if err != nil { |
| 95 | + return nil, fmt.Errorf("getting minikube executable: %w", err) |
| 96 | + } |
| 97 | + return exec.Command(b, arg...), nil |
| 98 | +} |
| 99 | + |
| 100 | +func minikubeBinary() (string, error) { |
| 101 | + execName := "minikube" |
| 102 | + if found, _ := util.DetectWSL(); found { |
| 103 | + execName = "minikube.exe" |
| 104 | + } |
| 105 | + filename, err := exec.LookPath(execName) |
| 106 | + if err != nil { |
| 107 | + return "", errors.New("unable to find minikube executable. Please add it to PATH environment variable") |
| 108 | + } |
| 109 | + if _, err := os.Stat(filename); os.IsNotExist(err) { |
| 110 | + return "", fmt.Errorf("unable to find minikube executable. File not found %s", filename) |
| 111 | + } |
| 112 | + return filename, nil |
| 113 | +} |
| 114 | + |
| 115 | +func matchNodeLabel(kubeContext string) (bool, error) { |
| 116 | + client, err := k8s.Client() |
| 117 | + if err != nil { |
| 118 | + return false, fmt.Errorf("getting Kubernetes client: %w", err) |
| 119 | + } |
| 120 | + opts := v1.ListOptions{ |
| 121 | + LabelSelector: fmt.Sprintf("minikube.k8s.io/name=%s", kubeContext), |
| 122 | + Limit: 100, |
| 123 | + } |
| 124 | + l, err := client.CoreV1().Nodes().List(opts) |
| 125 | + if err != nil { |
| 126 | + return false, fmt.Errorf("listing nodes with matching label: %w", err) |
| 127 | + } |
| 128 | + return l != nil && len(l.Items) > 0, nil |
| 129 | +} |
| 130 | + |
| 131 | +// matchProfileAndServerURL checks if kubecontext matches any valid minikube profile |
| 132 | +// and for selected drivers if the k8s server url is same as any of the minikube nodes IPs |
| 133 | +func matchProfileAndServerURL(kubeContext string) (bool, error) { |
| 134 | + config, err := getRestClientConfigFunc() |
| 135 | + if err != nil { |
| 136 | + return false, fmt.Errorf("getting kubernetes config: %w", err) |
| 137 | + } |
| 138 | + apiServerURL, _, err := rest.DefaultServerURL(config.Host, config.APIPath, schema.GroupVersion{}, false) |
| 139 | + |
| 140 | + if err != nil { |
| 141 | + return false, fmt.Errorf("getting kubernetes server url: %w", err) |
| 142 | + } |
| 143 | + |
| 144 | + logrus.Debugf("kubernetes server url: %s", apiServerURL) |
| 145 | + |
| 146 | + ok, err := matchServerURLFor(kubeContext, apiServerURL) |
| 147 | + if err != nil { |
| 148 | + return false, fmt.Errorf("checking minikube node url: %w", err) |
| 149 | + } |
| 150 | + return ok, nil |
| 151 | +} |
| 152 | + |
| 153 | +func matchServerURLFor(kubeContext string, serverURL *url.URL) (bool, error) { |
| 154 | + cmd, err := minikubeExec("profile", "list", "-o", "json") |
| 155 | + if err != nil { |
| 156 | + return false, fmt.Errorf("executing minikube command: %w", err) |
| 157 | + } |
| 158 | + |
| 159 | + out, err := util.RunCmdOut(cmd) |
| 160 | + if err != nil { |
| 161 | + return false, fmt.Errorf("getting minikube profiles: %w", err) |
| 162 | + } |
| 163 | + |
| 164 | + var data data |
| 165 | + if err = json.Unmarshal(out, &data); err != nil { |
| 166 | + log.Fatal(fmt.Errorf("failed to unmarshal data: %w", err)) |
| 167 | + } |
| 168 | + |
| 169 | + for _, v := range data.Valid { |
| 170 | + if v.Config.Name != kubeContext { |
| 171 | + continue |
| 172 | + } |
| 173 | + |
| 174 | + if v.Config.Driver != "hyperkit" && v.Config.Driver != "virtualbox" { |
| 175 | + // Since node IPs don't match server API for other drivers we assume profile name match is enough. |
| 176 | + // TODO: Revisit once https://github.com/kubernetes/minikube/issues/6642 is fixed |
| 177 | + return true, nil |
| 178 | + } |
| 179 | + for _, n := range v.Config.Nodes { |
| 180 | + if serverURL.Host == fmt.Sprintf("%s:%d", n.IP, n.Port) { |
| 181 | + return true, nil |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + return false, nil |
| 186 | +} |
| 187 | + |
| 188 | +type data struct { |
| 189 | + Valid []profile `json:"valid,omitempty"` |
| 190 | + Invalid []profile `json:"invalid,omitempty"` |
| 191 | +} |
| 192 | + |
| 193 | +type profile struct { |
| 194 | + Config config |
| 195 | +} |
| 196 | + |
| 197 | +type config struct { |
| 198 | + Name string |
| 199 | + Driver string |
| 200 | + Nodes []node |
| 201 | +} |
| 202 | + |
| 203 | +type node struct { |
| 204 | + IP string |
| 205 | + Port int32 |
| 206 | +} |
0 commit comments