forked from kubernetes-sigs/cluster-api
/
minikube.go
118 lines (100 loc) · 3.01 KB
/
minikube.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package minikube
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/pkg/errors"
"k8s.io/klog"
)
const (
minikubeClusterName = "clusterapi"
)
type Minikube struct {
kubeconfigpath string
options []string
// minikubeExec implemented as function variable for testing hooks
minikubeExec func(env []string, args ...string) (string, error)
}
func New() *Minikube {
return WithOptions([]string{})
}
func WithOptions(options []string) *Minikube {
return WithOptionsAndKubeConfigPath(options, "")
}
func WithOptionsAndKubeConfigPath(options []string, kubeconfigpath string) *Minikube {
if kubeconfigpath == "" {
// Arbitrary file name. Can potentially be randomly generated.
kubeconfigpath = "minikube.kubeconfig"
}
// Set profile if it is not provided.
if func() bool {
for _, opt := range options {
if strings.HasPrefix(opt, "p=") {
return false
}
}
return true
}() {
options = append(options, fmt.Sprintf("profile=%s", minikubeClusterName))
}
return &Minikube{
minikubeExec: minikubeExec,
options: options,
kubeconfigpath: kubeconfigpath,
}
}
var minikubeExec = func(env []string, args ...string) (string, error) {
const executable = "minikube"
klog.V(3).Infof("Running: %v %v", executable, args)
cmd := exec.Command(executable, args...)
cmd.Env = env
cmdOut, err := cmd.CombinedOutput()
klog.V(2).Infof("Ran: %v %v Output: %v", executable, args, string(cmdOut))
if err != nil {
err = errors.Wrapf(err, "error running command '%v %v'", executable, strings.Join(args, " "))
}
return string(cmdOut), err
}
func (m *Minikube) Create() error {
args := []string{"start", "--bootstrapper=kubeadm"}
for _, opt := range m.options {
args = append(args, fmt.Sprintf("--%v", opt))
}
_, err := m.exec(args...)
return err
}
func (m *Minikube) Delete() error {
_, err := m.exec("delete")
os.Remove(m.kubeconfigpath)
return err
}
func (m *Minikube) GetKubeconfig() (string, error) {
b, err := ioutil.ReadFile(m.kubeconfigpath)
if err != nil {
return "", err
}
return string(b), nil
}
func (m *Minikube) exec(args ...string) (string, error) {
// Override kubeconfig environment variable in call
// so that minikube will generate and reference
// the kubeconfig in the desired location.
// Note that the last value set for a key is the final value.
const kubeconfigEnvVar = "KUBECONFIG"
env := append(os.Environ(), fmt.Sprintf("%v=%v", kubeconfigEnvVar, m.kubeconfigpath))
return m.minikubeExec(env, args...)
}