-
Notifications
You must be signed in to change notification settings - Fork 14
/
example_test.go
309 lines (283 loc) · 11.3 KB
/
example_test.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package test
import (
"fmt"
"github.com/yulibaozi/beku"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
var (
// certificate-authority-data
caBase64 = "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUIwakNDQVRPZ0F3SUJBZ0lKQUlBKzVnYVR4ZjZOTUFvR0NDcUdTTTQ5QkFNQ01CZ3hGakFVQmdOVkJBTU0KRFd0MVltVnlibVYwWlhNdFkyRXdIaGNOTVRnd05USTFNVEV3T1RFeldoY05Namd3TlRJeU1URXdPVEV6V2pBWQpNUll3RkFZRFZRUUREQTFyZFdKbGNtNWxkR1Z6TFdOaE1JR2JNQkF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kKZ1VqaU5LdWsyQStMdXFzbnNsWVdXZnF0bDZ1TmZBbmUvRS9mZGJZbldRPT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo="
)
func Example_beku_RegisterK8sClientBase64() {
err := beku.RegisterK8sClientBase64("https://192.168.0.183:8080", caBase64, certBase64, keyBase64)
if err != nil {
panic(err)
}
}
// certificate-authority-data
var ca = `-----BEGIN CERTIFICATE-----
MIIB0jCCATOgAwIBAgIJAIA+5gaTxf6NMAoGCCqGSM49BAMCMBgxFjAUBgNVBAMM
DWt1YmVybmV0ZXMtY2EwHhcNMTgwNTI1MTEwOTEzWhcNMjgwNTIyMTEwOTEzWjAY
MRYwFAYDVQQDDA1rdWJlcm5ldGVzLWNhMIGbMBAGByqGSM49AgEGBSuBBAAjA4GG
AAQAgO5odMHPcxfeZEX3e184LiiERR92EdoWK85G01gSTJ0GOpjAUn7umegQVtQZ
1evqxPH+n4ULpfRMGAJ4qMisNasAFZgWVAZ4yLGZILdAqob4eNVN5ldtHQ17nZG0
SQ0hTjTkVO+VzIic7I/WW0uUrFzjjL71Rq5WTsX4jFWxJ4k1lVyjIzAhMA8GA1Ud
EwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgKkMAoGCCqGSM49BAMCA4GMADCBiAJC
AW4JpyDfTj2Vmne12FXFKPqtJpcjhG6mgKRMxiPUkLgE8Hq3n1o4rxKRNW5G5T6X
Af+XvfnG++jdL6DW2lIf5XROAkIA6Z6myogZpOoqPGCLFNoLIYbZDifXFUhOcUer
QJGYHn9x1+AGHN5hyXLUAs93vdxRvLNoj1WX7M7D8GSJ/ZBQC8g=
-----END CERTIFICATE-----`
// client-certificate-data
var cert = `-----BEGIN CERTIFICATE-----
MIICCTCCAWugAwIBAgIJAKKavU7qkFD2MAoGCCqGSM49BAMCMBgxFjAUBgNVBAMM
DWt1YmVybmV0ZXMtY2EwHhcNMTgwNTI1MTEwOTI2WhcNMjgwNTIyMTEwOTI2WjBB
MSYwJAYDVQQDDB1rdWJlLWFwaXNlcnZlci1rdWJlbGV0LWNsaWVudDEXMBUGA1UE
CgwOc3lzdGVtOm1hc3RlcnMwgZswEAYHKoZIzj0CAQYFK4EEACMDgYYABAGvXRM5
34dRGI0YrWHqpvqheZxBln5iLWxk3VhXfF9jZFnpPQw0LrTMEpzlKWw0uvrlicf+
zsYSDzx+R/Ey0QZ65wAvEv5OJc3u0sH5p+r18M+4vRuZU39/aE0tSmNT3fu6R8hi
gUjiNKuk2A+LuqsnslYWWfqtl6uNfAne/E/fdbYnWaMyMDAwCQYDVR0TBAIwADAO
BgNVHQ8BAf8EBAMCBaAwEwYDVR0lBAwwCgYIKwYBBQUHAwIwCgYIKoZIzj0EAwID
gYsAMIGHAkFmH4BoKWv4pTcXyWrMC0Lh9XlcVB9hgwqwOxWdj/7J5oPl4nhiIoDb
I3f60xSY50Si+JMkpHqWmYpNLKeHwLf90wJCALy3molZhEAsn+N1Sg5X0lUbLBWb
MMO7PJegNR0KiT/A3vB07QTBprRq4IRvz00CerpPY1x0+SQBm7GtOyaH+Xko
-----END CERTIFICATE-----`
// client-key
var key = `-----BEGIN EC PRIVATE KEY-----
MIHcAgEBBEIAQLa5KMAfT5br7lsOLVQmNRyEoza553hQLkvlhaarKEXPs41TXEnX
OqwKQb+MUh8s/M9cXftXkV4fHwGq6EuGyfagBwYFK4EEACOhgYkDgYYABAGvXRM5
34dRGI0YrWHqpvqheZxBln5iLWxk3VhXfF9jZFnpPQw0LrTMEpzlKWw0uvrlicf+
zsYSDzx+R/Ey0QZ65wAvEv5OJc3u0sH5p+r18M+4vRuZU39/aE0tSmNT3fu6R8hi
gUjiNKuk2A+LuqsnslYWWfqtl6uNfAne/E/fdbYnWQ==
-----END EC PRIVATE KEY-----`
func Example_beku_RegisterK8sClient() {
err := beku.RegisterK8sClient("https://192.168.0.183:8080", ca, cert, key)
if err != nil {
panic(err)
}
}
func Example_beku_autoRelease() {
err := beku.RegisterK8sClient("http://192.168.0.183:8080", "", "", "")
if err != nil {
panic(err)
}
dp, err := beku.NewDeployment().SetNamespaceAndName("roc", "http").
SetPodLabels(map[string]string{"app": "http"}).SetContainer("http", "wucong60/kube-node-demo1:v1", 8081).Release()
if err != nil {
panic(err)
}
svc, err := beku.DeploymentToSvc(dp, beku.ServiceTypeNodePort, true)
if err != nil {
panic(err)
}
byts, err := beku.ToYAML(svc)
if err != nil {
panic(err)
}
fmt.Printf("\n%s", string(byts))
fmt.Println("over!")
}
func Example_beku_PushApp() {
client := GetK8sClient()
// cretate Namespace
ns, err := beku.NewNs().SetName("roc").Finish()
if err != nil {
panic(err)
}
// push Namespace
ns, err = client.CoreV1().Namespaces().Create(ns)
if err != nil {
panic(err)
}
fmt.Println("===")
// create Deployment
dp, err := beku.NewDeployment().SetNamespaceAndName("roc", "http").
SetPodLabels(map[string]string{"app": "http"}).SetContainer("http", "wucong60/kube-node-demo1:v1", 8081).Finish()
if err != nil {
panic(err)
}
// push Deployment
dp, err = client.AppsV1().Deployments(dp.GetNamespace()).Create(dp)
if err != nil {
panic(err)
}
fmt.Println("===")
// create Service
svc, err := beku.DeploymentToSvc(dp, beku.ServiceTypeNodePort)
if err != nil {
panic(err)
}
//push Service
svc, err = client.CoreV1().Services(svc.GetNamespace()).Create(svc)
if err != nil {
panic(err)
}
byts, err := beku.ToYAML(svc)
if err != nil {
panic(err)
}
fmt.Printf("\n%s", string(byts))
fmt.Println("over!")
}
// Example_beku_SetQos exmple how auto set Qos
func Example_beku_SetQos() {
beku.RegisterResourceRequest(map[beku.ResourceName]string{beku.ResourceCPU: "200m", beku.ResourceMemory: "5G"})
beku.RegisterResourceLimit(map[beku.ResourceName]string{beku.ResourceCPU: "200m", beku.ResourceMemory: "5G"})
dep, err := beku.NewDeployment().SetContainer("http-demo", "wucong60/kube-node-demo1:v1", 8081).
SetPodLabels(map[string]string{"name": "http-demo"}).SetNamespaceAndName("yulibaozi", "http-demo").SetPodQos("Guaranteed", true).Finish()
if err != nil {
panic(err)
}
yamlbyts, err := beku.ToYAML(dep)
if err != nil {
panic(err)
}
fmt.Println("\n" + string(yamlbyts))
// create svc
svc, err := beku.DeploymentToSvc(dep, beku.ServiceTypeNodePort)
if err != nil {
panic(err)
}
yamlbyts, err = beku.ToYAML(svc)
fmt.Println("\n" + string(yamlbyts))
}
func Example_beku_NewSvc() {
svc, err := beku.NewSvc().SetNamespaceAndName("roc", "mysql-svc").
SetSelector(map[string]string{"app": "mysql"}).SetServiceType(beku.ServiceTypeNodePort).
SetPort(beku.ServicePort{Port: 3306, TargetPort: 3306}).Finish()
if err != nil {
panic(err)
}
yamlbyts, err := beku.ToYAML(svc)
jsonbyts, err := beku.ToJSON(svc)
if err != nil {
panic(err)
}
fmt.Println("\n" + string(yamlbyts))
fmt.Println("\n" + string(jsonbyts))
}
// ExampleNewDeployment how to quickly create deployment example
func Example_beku_NewDeployment() {
dep, err := beku.NewDeployment().
SetName("yulibaozi").SetLabels(map[string]string{"name": "yulibaozi"}).
SetContainer("first", "mysql", 3307).SetContainer("second", "redis", 6379).SetHTTPLiveness(8080, "/metic", 30, 10, 10).Finish()
if err != nil {
panic(err)
}
yamlByts, err := beku.ToYAML(dep)
if err != nil {
panic(err)
}
fmt.Println("\n" + string(yamlByts))
}
func Example_beku_NewSecret() {
sec, err := beku.NewSecret().SetDataString(map[string]string{"key": "beku is very good!"}).SetNamespaceAndName("yulibaozi", "beku").Finish()
if err != nil {
panic(err)
}
yamlByts, err := beku.ToYAML(sec)
if err != nil {
panic(err)
}
fmt.Println("\n" + string(yamlByts))
}
func Example_beku_NewCM() {
sec, err := beku.NewCM().SetNamespaceAndName("yulibaozi", "beku").SetData(map[string]string{"key": "beku is very good!"}).Finish()
if err != nil {
panic(err)
}
yamlByts, err := beku.ToYAML(sec)
if err != nil {
panic(err)
}
fmt.Println("\n" + string(yamlByts))
}
func Example_beku_NewSts() {
sts, err := beku.NewSts().SetNamespaceAndName("yulibaozi", "mysql").SetSelector(map[string]string{"name": "mysql"}).SetContainer("first", "mysql", 3306).Finish()
if err != nil {
panic(err)
}
yamlByts, err := beku.ToYAML(sts)
if err != nil {
panic(err)
}
fmt.Println("\n" + string(yamlByts))
}
func Example_beku_NewDS() {
ds, err := beku.NewDS().SetNamespaceAndName("roc", "api").SetContainer("first", "go-sdk", 8081).
SetPodLabels(map[string]string{"name": "go-sdk"}).SetMinReadySeconds(10).
SetPodQos("Guaranteed", true).SetHTTPLiveness(8081, "/health", 35, 10, 10, map[string]string{"userid": "1"}).SetContainer("second", "check", 8082).
Finish()
if err != nil {
panic(err)
}
yamlByts, err := beku.ToYAML(ds)
if err != nil {
panic(err)
}
fmt.Println("\n" + string(yamlByts))
}
func Example_beku_NewUnionPV() {
pv, pvc, err := beku.NewUnionPV().SetNamespaceAndName("yulibaozi", "mysql-persistent").
SetAccessMode(beku.ReadOnlyMany).SetCapacity(map[beku.ResourceName]string{beku.ResourceMemory: "5Gi"}).
SetNFS(&beku.NFSVolumeSource{Server: "192.168.0.165:2314", Path: "/data"}).
Finish()
if err != nil {
panic(err)
}
pvYamlByts, err := beku.ToYAML(pv)
if err != nil {
panic(err)
}
pvcYamlByts, err := beku.ToYAML(pvc)
if err != nil {
panic(err)
}
fmt.Println("\n" + string(pvYamlByts))
fmt.Println("---")
fmt.Println("\n" + string(pvcYamlByts))
}
func Example_beku_NewPVC() {
pvc, err := beku.NewPVC().SetNamespaceAndName("yulibaozi", "redis").SetAccessMode(beku.ReadOnlyMany).
SetResourceRequests(map[beku.ResourceName]string{beku.ResourceEphemeralStorage: "5Gi"}).
Finish()
if err != nil {
panic(err)
}
yamlByts, err := beku.ToYAML(pvc)
if err != nil {
panic(err)
}
fmt.Println("\n" + string(yamlByts))
}
func Example_beku_NewPV() {
pv, err := beku.NewPV().SetName("redis-pv").SetAccessMode(beku.ReadWriteMany).SetNFS(&beku.NFSVolumeSource{Server: "192.168.0.165:2132", Path: "/data"}).
SetCapacity(map[beku.ResourceName]string{beku.ResourceEphemeralStorage: "50G"}).Finish()
if err != nil {
panic(err)
}
yamlByts, err := beku.ToYAML(pv)
if err != nil {
panic(err)
}
fmt.Println("\n" + string(yamlByts))
}
// GetK8sClient 获取k8s的链接
func GetK8sClient() *kubernetes.Clientset {
cSet, err := kubernetes.NewForConfig(&rest.Config{
Host: "http://192.168.0.184:6443/",
TLSClientConfig: rest.TLSClientConfig{
CAData: []byte(ca),
CertData: []byte(cert),
KeyData: []byte(key),
},
})
if err != nil {
panic(err)
}
return cSet
}