-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubectl.go
137 lines (115 loc) · 2.82 KB
/
kubectl.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
package kubectl
import (
"context"
"io/ioutil"
"os"
"os/exec"
"strings"
"fmt"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
const (
tmpDir = "./management-state/tmp"
)
func Apply(yaml []byte, kubeConfig *clientcmdapi.Config) ([]byte, error) {
kubeConfigFile, err := tempFile("kubeconfig-")
if err != nil {
return nil, err
}
defer os.Remove(kubeConfigFile.Name())
yamlFile, err := tempFile("yaml-")
if err != nil {
return nil, err
}
defer os.Remove(yamlFile.Name())
if err := ioutil.WriteFile(yamlFile.Name(), yaml, 0600); err != nil {
return nil, err
}
if err := clientcmd.WriteToFile(*kubeConfig, kubeConfigFile.Name()); err != nil {
return nil, err
}
cmd := exec.Command("kubectl",
"--kubeconfig",
kubeConfigFile.Name(),
"apply",
"-f",
yamlFile.Name())
return runWithHTTP2(cmd)
}
func Drain(ctx context.Context, kubeConfig *clientcmdapi.Config, nodeName string, args []string) ([]byte, string, error) {
kubeConfigFile, err := tempFile("kubeconfig-")
if err != nil {
return nil, "", err
}
defer os.Remove(kubeConfigFile.Name())
if err := clientcmd.WriteToFile(*kubeConfig, kubeConfigFile.Name()); err != nil {
return nil, "", err
}
cmd := exec.CommandContext(ctx, "kubectl",
"--kubeconfig",
kubeConfigFile.Name(),
"drain",
nodeName)
cmd.Args = append(cmd.Args, args...)
var newEnv []string
for _, env := range os.Environ() {
if strings.HasPrefix(env, "DISABLE_HTTP2") {
continue
}
newEnv = append(newEnv, env)
}
cmd.Env = newEnv
output, err := cmd.CombinedOutput()
return output, fmt.Sprint(cmd.Stderr), err
}
func tempFile(prefix string) (*os.File, error) {
if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
if err = os.MkdirAll(tmpDir, 0755); err != nil {
return nil, err
}
}
f, err := ioutil.TempFile(tmpDir, prefix)
if err != nil {
return nil, err
}
return f, f.Close()
}
func ApplyWithNamespace(yaml []byte, namespace string, kubeConfig *clientcmdapi.Config) ([]byte, error) {
kubeConfigFile, err := tempFile("kubeconfig-")
if err != nil {
return nil, err
}
defer os.Remove(kubeConfigFile.Name())
yamlFile, err := tempFile("yaml-")
if err != nil {
return nil, err
}
defer os.Remove(yamlFile.Name())
if err := ioutil.WriteFile(yamlFile.Name(), yaml, 0600); err != nil {
return nil, err
}
if err := clientcmd.WriteToFile(*kubeConfig, kubeConfigFile.Name()); err != nil {
return nil, err
}
cmd := exec.Command("kubectl",
"--kubeconfig",
kubeConfigFile.Name(),
"-n",
namespace,
"apply",
"-f",
yamlFile.Name())
return runWithHTTP2(cmd)
}
func runWithHTTP2(cmd *exec.Cmd) ([]byte, error) {
var newEnv []string
for _, env := range os.Environ() {
if strings.HasPrefix(env, "DISABLE_HTTP2") {
continue
}
newEnv = append(newEnv, env)
}
cmd.Env = newEnv
return cmd.CombinedOutput()
}