-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
kind.go
141 lines (112 loc) · 3.05 KB
/
kind.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Copyright 2019 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 kind
import (
"fmt"
"io/ioutil"
"os/exec"
"strings"
"github.com/pkg/errors"
"k8s.io/klog"
)
const (
kindClusterName = "clusterapi"
)
var (
// ignoredOptions lists the options not supported by delete and kubeconfig-path.
ignoredOptions = []string{"config", "image", "retain", "wait"}
)
type Kind struct {
options []string
// execFunc implemented as function variable for testing hooks
execFunc func(args ...string) (string, error)
}
func New() *Kind {
return WithOptions([]string{})
}
func WithOptions(options []string) *Kind {
// Set name if it is not provided.
if func() bool {
for _, opt := range options {
if strings.HasPrefix(opt, "name=") {
return false
}
}
return true
}() {
options = append(options, fmt.Sprintf("name=%s", kindClusterName))
}
return &Kind{
execFunc: execFunc,
options: options,
}
}
var execFunc = func(args ...string) (string, error) {
const executable = "kind"
klog.V(3).Infof("Running: %v %v", executable, args)
cmd := exec.Command(executable, args...)
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 (k *Kind) Create() error {
args := []string{"create", "cluster"}
args = k.appendOptions(args)
_, err := k.exec(args...)
return err
}
func (k *Kind) Delete() error {
args := []string{"delete", "cluster"}
args = k.appendOptions(args, ignoredOptions...)
_, err := k.exec(args...)
return err
}
func (k *Kind) GetKubeconfig() (string, error) {
path, err := k.getKubeConfigPath()
if err != nil {
return "", err
}
b, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return string(b), nil
}
func (k *Kind) getKubeConfigPath() (string, error) {
args := []string{"get", "kubeconfig-path"}
args = k.appendOptions(args, ignoredOptions...)
out, err := k.exec(args...)
if err != nil {
return "", err
}
return strings.TrimSpace(out), nil
}
func (k *Kind) exec(args ...string) (string, error) {
return k.execFunc(args...)
}
// appendOptions enriches the args with all options but the ignored ones
func (k *Kind) appendOptions(args []string, ignoredOptions ...string) []string {
outer:
for _, opt := range k.options {
for _, ignoredOption := range ignoredOptions {
if strings.HasPrefix(opt, ignoredOption) {
continue outer
}
}
args = append(args, fmt.Sprintf("--%v", opt))
}
return args
}