This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
kubeclient.go
220 lines (199 loc) · 5.98 KB
/
kubeclient.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2018 Datadog, Inc.
package setup
import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/golang/glog"
"io/ioutil"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"strings"
)
func getHome() string {
user := os.Getenv("SUDO_USER")
if user == "" {
return os.Getenv("HOME")
}
return path.Join("/home", user)
}
func (e *Environment) setupKubectl() error {
glog.V(4).Infof("Building kubectl configuration in %s ...", e.kubeConfigUserPath)
// cluster
b, err := exec.Command(e.GetHyperkubePath(),
"kubectl",
"config",
"--kubeconfig="+e.kubeConfigUserPath,
"set-cluster",
defaultKubectlClusterName,
"--server=https://127.0.0.1:6443",
"--certificate-authority="+path.Join(e.secretsABSPath, "kubernetes.issuing_ca"),
).CombinedOutput()
output := string(b)
if err != nil {
glog.Errorf("Cannot set kubectl config/set-cluster: %v, %s", err, output)
return err
}
glog.V(4).Infof("kubectl config/set-cluster: %s", output)
// user
b, err = exec.Command(e.GetHyperkubePath(),
"kubectl",
"config",
"--kubeconfig="+e.kubeConfigUserPath,
"set-credentials",
defaultKubectlUserName,
"--username="+defaultKubectlUserName,
"--client-certificate="+path.Join(e.secretsABSPath, "kubernetes.certificate"),
"--client-key="+path.Join(e.secretsABSPath, "kubernetes.private_key"),
).CombinedOutput()
output = string(b)
if err != nil {
glog.Errorf("Cannot set kubectl set-credentials: %v, %s", err, output)
return err
}
glog.V(4).Infof("kubectl config/set-credentials: %s", output)
// context
b, err = exec.Command(e.GetHyperkubePath(),
"kubectl",
"config",
"--kubeconfig="+e.kubeConfigUserPath,
"set-context",
defaultKubectlContextName,
"--user="+defaultKubectlUserName,
"--cluster="+defaultKubectlClusterName,
"--namespace=default",
).CombinedOutput()
output = string(b)
if err != nil {
glog.Errorf("Cannot set kubectl set-context: %v, %s", err, output)
return err
}
glog.V(4).Infof("kubectl config/set-context: %s", output)
// use context
b, err = exec.Command(e.GetHyperkubePath(),
"kubectl",
"config",
"--kubeconfig="+e.kubeConfigUserPath,
"use-context",
defaultKubectlContextName,
).CombinedOutput()
output = string(b)
if err != nil {
glog.Errorf("Cannot use kubectl context: %v, %s", err, output)
return err
}
glog.V(4).Infof("kubectl use-context: %s", output)
/*
If the kubeconfig file doesn't exists, the kubectl binary will create it
To let the ${SUDO_USER} continues to use kubectl without privileges,
the kubeDirPath is chown recursively
*/
sudoUser := os.Getenv("SUDO_USER")
if sudoUser != "" {
cmdLine := []string{"chown", "-R", fmt.Sprintf("%s:", sudoUser), path.Dir(e.kubeConfigUserPath)}
glog.V(5).Infof("%s", strings.Join(cmdLine, " "))
b, err := exec.Command(cmdLine[0], cmdLine[1:]...).CombinedOutput()
output = string(b)
if err != nil {
glog.Warningf("Cannot execute %s: %v, %s", strings.Join(cmdLine, " "), err, output)
}
glog.V(4).Infof("%s output: %q", strings.Join(cmdLine, " "), output)
}
return e.createKubectlLink()
}
func (e *Environment) createKubectlLink() error {
if e.kubectlLink == "" {
return nil
}
glog.V(4).Infof("Creating a kubectl link %s -> %s", e.kubectlLink, e.GetHyperkubePath())
_, err := os.Stat(e.kubectlLink)
if err == nil {
target, err := os.Readlink(e.kubectlLink)
if err != nil {
glog.Errorf("Unexpected error: %v", err)
return err
}
glog.V(4).Infof("Already existent link: %s -> %s", e.kubectlLink, target)
if target != e.GetHyperkubePath() {
err = fmt.Errorf("link %s already created and pointing to %s", e.kubectlLink, target)
glog.Errorf("Unexpected error: %v, please remove %s", err, e.kubectlLink)
return err
}
glog.V(3).Infof("Already valid link: %s -> %s", e.kubectlLink, target)
return nil
}
err = os.Symlink(e.GetHyperkubePath(), e.kubectlLink)
if err != nil {
glog.Errorf("Cannot create kubectl link: %v", err)
return err
}
glog.V(3).Infof("Successfully created kubectl link %s", e.kubectlLink)
return nil
}
func (e *Environment) setupKubeletClient() error {
glog.V(4).Infof("Building kubelet client ...")
cert, err := tls.LoadX509KeyPair(path.Join(e.secretsABSPath, "kubernetes.certificate"), path.Join(e.secretsABSPath, "kubernetes.private_key"))
if err != nil {
glog.Errorf("Cannot load x509 key pair: %v", err)
return err
}
caCertBytes, err := ioutil.ReadFile(path.Join(e.secretsABSPath, "kubernetes.issuing_ca"))
if err != nil {
glog.Errorf("Cannot read CA: %v", err)
return err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCertBytes)
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
}
tlsConfig.BuildNameToCertificate()
e.kubeletClient = &http.Client{}
e.kubeletClient.Transport = &http.Transport{TLSClientConfig: tlsConfig}
podListURL, err := url.Parse("https://127.0.0.1:10250/pods")
if err != nil {
glog.Errorf("Cannot parse Kubelet URL: %s", err)
return err
}
e.podListRequest = &http.Request{
Method: http.MethodGet,
URL: podListURL,
}
return nil
}
func (e *Environment) setupAPIServerClient() error {
var err error
glog.V(4).Infof("Building restConfig from %s", e.GetKubeconfigInsecurePath())
e.restConfig, err = clientcmd.BuildConfigFromFlags("", e.GetKubeconfigInsecurePath())
if err != nil {
glog.Errorf("Cannot build restConfig: %v", err)
return err
}
e.clientSet, err = kubernetes.NewForConfig(e.restConfig)
if err != nil {
glog.Errorf("Cannot build clientSet: %v", err)
return err
}
return nil
}
func (e *Environment) setupKubeClients() error {
glog.V(4).Infof("Creating kubeclients configuration ...")
err := e.setupKubectl()
if err != nil {
return err
}
err = e.setupKubeletClient()
if err != nil {
return err
}
return e.setupAPIServerClient()
}