/
main.go
599 lines (520 loc) · 18.2 KB
/
main.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
package main
import (
"archive/tar"
"compress/gzip"
"context"
"encoding/base64"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/blang/semver"
"github.com/golang/protobuf/ptypes/empty"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/grpcplugin/integrationplugin"
)
/*
This plugin have to be used as a deployment integration plugin
Kubernetes deployment plugin must configured as following:
name: plugin-kubernetes-deployment
type: integration
author: "Benjamin Coenen"
description: "Kubernetes Deployment Plugin"
$ cdsctl admin plugins import plugin-kubernetes-deployment.yml
Build the present binaries and import in CDS:
os: linux
arch: amd64
cmd: <path-to-binary-file>
$ cdsctl admin plugins binary-add plugin-kubernetes-deployment plugin-kubernetes-deployment-bin.yml <path-to-binary-file>
*/
const (
kubectlLink = "https://storage.googleapis.com/kubernetes-release/release/v1.13.0/bin/"
)
type kubernetesDeploymentPlugin struct {
integrationplugin.Common
}
func (k8sPlugin *kubernetesDeploymentPlugin) Manifest(ctx context.Context, _ *empty.Empty) (*integrationplugin.IntegrationPluginManifest, error) {
return &integrationplugin.IntegrationPluginManifest{
Name: "Kubernetes Deployment Plugin",
Author: "Benjamin Coenen",
Description: "Kubernetes Deployment Plugin",
Version: sdk.VERSION,
}, nil
}
// getStartingConfig implements ConfigAccess
func getStartingConfig(token, timeout string) clientcmd.KubeconfigGetter {
return func() (*clientcmdapi.Config, error) {
defaultClientConfigRules := clientcmd.NewDefaultClientConfigLoadingRules()
overrideCfg := clientcmd.ConfigOverrides{
AuthInfo: clientcmdapi.AuthInfo{
Token: token,
},
Timeout: timeout + "s",
}
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(defaultClientConfigRules, &overrideCfg)
rawConfig, err := clientConfig.RawConfig()
if os.IsNotExist(err) {
return clientcmdapi.NewConfig(), nil
}
if err != nil {
return nil, err
}
return &rawConfig, nil
}
}
func (k8sPlugin *kubernetesDeploymentPlugin) Run(ctx context.Context, q *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
k8sAPIURL := q.GetOptions()["cds.integration.deployment.api_url"]
k8sToken := q.GetOptions()["cds.integration.deployment.token"]
if k8sToken == "" {
k8sToken = q.GetOptions()["cds.integration.token"]
}
k8sCaCertificate := q.GetOptions()["cds.integration.deployment.ca_certificate"]
deploymentFilepath := q.GetOptions()["cds.integration.deployment.deployment_files"]
helmChart := q.GetOptions()["cds.integration.deployment.helm_chart"]
if k8sToken == "" {
return fail("Kubernetes token should not be empty")
}
certb64 := base64.StdEncoding.EncodeToString([]byte(k8sCaCertificate))
kubecfg := fmt.Sprintf(`apiVersion: v1
kind: Config
users:
- name: cds
user:
token: %s
clusters:
- cluster:
certificate-authority-data: %s
server: %s
name: self-hosted-cluster
contexts:
- context:
cluster: self-hosted-cluster
user: cds
name: default-context
current-context: default-context`, k8sToken, certb64, k8sAPIURL)
if _, err := os.Stat(".kube"); !os.IsNotExist(err) {
return fail(".kube directory already exists, you have to delete it before using this action")
}
if err := os.Mkdir(".kube", 0755); err != nil {
return fail("Cannot create directory .kube : %v", err)
}
defer func() {
if err := os.RemoveAll(".kube"); err != nil {
fmt.Printf("Cannot delete .kube directory : %v\n", err)
}
}()
if err := os.WriteFile(".kube/config", []byte(kubecfg), 0755); err != nil {
return fail("Cannot write kubeconfig : %v", err)
}
switch {
case helmChart != "":
if err := executeHelm(q); err != nil {
return fail(err.Error())
}
case deploymentFilepath != "":
if err := executeK8s(q); err != nil {
return fail(err.Error())
}
default:
return fail("Must have deployment_files or helm_chart not empty")
}
return &integrationplugin.RunResult{
Status: sdk.StatusSuccess,
}, nil
}
func main() {
e := kubernetesDeploymentPlugin{}
if err := integrationplugin.Start(context.Background(), &e); err != nil {
panic(err)
}
}
func fail(format string, args ...interface{}) (*integrationplugin.RunResult, error) {
msg := fmt.Sprintf(format, args...)
fmt.Println(msg)
return &integrationplugin.RunResult{
Details: msg,
Status: sdk.StatusFail,
}, nil
}
func executeK8s(q *integrationplugin.RunQuery) error {
k8sAPIURL := q.GetOptions()["cds.integration.deployment.api_url"]
k8sToken := q.GetOptions()["cds.integration.deployment.token"]
if k8sToken == "" {
k8sToken = q.GetOptions()["cds.integration.token"]
}
k8sCaCertificate := q.GetOptions()["cds.integration.deployment.ca_certificate"]
namespace := q.GetOptions()["cds.integration.deployment.namespace"]
deploymentFilepath := q.GetOptions()["cds.integration.deployment.deployment_files"]
timeoutStr := q.GetOptions()["cds.integration.deployment.timeout"]
project := q.GetOptions()["cds.project"]
workflow := q.GetOptions()["cds.workflow"]
if namespace == "" {
namespace = "default"
}
kubectlFound := false
if _, err := exec.LookPath("kubectl"); err == nil {
kubectlFound = true
}
binaryName := "kubectl"
if !kubectlFound {
fmt.Println("Download kubectl in progress...")
netClient := &http.Client{
Timeout: time.Second * 600,
}
response, err := netClient.Get(kubectlLink + sdk.GOOS + "/" + sdk.GOARCH + "/kubectl")
if err != nil {
return fmt.Errorf("Cannot download kubectl : %v", err)
}
if response.StatusCode > 400 {
return fmt.Errorf("Cannot download kubectl binary (status code %d)", response.StatusCode)
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("Cannot read body http response: %v", err)
}
fmt.Println("Download kubectl done...")
binaryName = project + "-" + workflow + "-kubectl"
if err := os.WriteFile(binaryName, body, 0755); err != nil {
return fmt.Errorf("Cannot write file %s for kubectl : %v", binaryName, err)
}
defer func(binName string) {
if err := os.Remove(binName); err != nil {
fmt.Printf("Cannot delete binary file : %v\n", err)
}
}(binaryName)
binaryName = "./" + binaryName
}
configK8s, err := clientcmd.BuildConfigFromKubeconfigGetter(k8sAPIURL, getStartingConfig(k8sToken, timeoutStr))
if err != nil {
return fmt.Errorf("Cannot build kubernetes config from config getter : %v", err)
}
configK8s.TLSClientConfig = rest.TLSClientConfig{
CAData: []byte(k8sCaCertificate),
}
// creates the clientset
clientset, errCl := kubernetes.NewForConfig(configK8s)
if errCl != nil {
return fmt.Errorf("Cannot create new config for kubernetes: %v", errCl)
}
if namespace != "" && namespace != apiv1.NamespaceDefault {
if _, err := clientset.CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{}); err != nil {
ns := apiv1.Namespace{}
ns.SetName(namespace)
if _, errC := clientset.CoreV1().Namespaces().Create(context.Background(), &ns, metav1.CreateOptions{}); errC != nil {
return fmt.Errorf("Cannot create namespace %s in kubernetes: %v", namespace, errC)
}
}
}
// All files matching filePath
filesPath, err := filepath.Glob(deploymentFilepath)
if err != nil {
return fmt.Errorf("Could not find paths : %v", err)
}
if len(filesPath) == 0 {
return fmt.Errorf("Pattern '%s' matched no file", deploymentFilepath)
}
cmdSetContext := exec.Command(binaryName, "config", "set-context", "default-context")
cmdSetContext.Stderr = os.Stderr
cmdSetContext.Stdout = os.Stdout
if err := cmdSetContext.Run(); err != nil {
return fmt.Errorf("Cannot execute kubectl config set-context : %v", err)
}
args := append([]string{"apply", "--timeout=" + timeoutStr + "s", "--wait=true", "-f"}, filesPath...)
cmd := exec.Command(binaryName, args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return fmt.Errorf("Cannot execute kubectl apply : %v", err)
}
return nil
}
func executeHelm(q *integrationplugin.RunQuery) error {
project := q.GetOptions()["cds.project"]
workflow := q.GetOptions()["cds.workflow"]
helmVersion := q.GetOptions()["cds.integration.deployment.helm_version"]
if helmVersion == "" {
helmVersion = "2.12.2"
}
version, err := semver.Parse(helmVersion)
if err != nil {
return fmt.Errorf("Invalid Helm Version %s - err: %v", helmVersion, err)
}
supportedVersion := ">=2.0.0 <4.0.0"
expectedRange, err := semver.ParseRange(supportedVersion)
if err != nil {
return fmt.Errorf("Fail to parse semver range : %v", err)
}
if !expectedRange(version) {
return fmt.Errorf("Unsupported helm version, should be : %s", supportedVersion)
}
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("cannot get current working directory : %v", err)
}
binaryName := "helm"
kubeCfg := "KUBECONFIG=" + path.Join(cwd, ".kube/config")
helmFound := false
if _, err := exec.LookPath("helm"); err == nil {
helmFound = true
}
if helmFound {
out, err := exec.Command(binaryName, "version", "--client", "--short").Output()
if err != nil {
return fmt.Errorf("Cannot check helm version : %v", err)
}
installedHelm := strings.TrimPrefix(string(out), "Client: ")
installedVersion, err := semver.ParseTolerant(installedHelm)
if err != nil {
return fmt.Errorf("Invalid installed Helm Version %s - err: %v", installedHelm, err)
}
if !version.Equals(installedVersion) {
fmt.Println("Helm in path is not at correct version, need installation")
fmt.Printf("Path version : %s\n", installedVersion.String())
fmt.Printf("Target version : %s\n", version.String())
helmFound = false
}
}
if !helmFound {
fmt.Printf("Download helm %s in progress...\n", version.String())
netClient := &http.Client{
Timeout: time.Second * 600,
}
response, err := netClient.Get(fmt.Sprintf("https://get.helm.sh/helm-v%s-%s-%s.tar.gz", version.String(), sdk.GOOS, sdk.GOARCH))
if err != nil {
return fmt.Errorf("Cannot download helm : %v", err)
}
if response.StatusCode > 400 {
return fmt.Errorf("Cannot download helm binary (status code %d)", response.StatusCode)
}
defer response.Body.Close()
binaryName = project + "-" + workflow + "-helm"
if err := os.Mkdir(binaryName, 0755); err != nil {
return fmt.Errorf("Cannot write directory for helm : %v", err)
}
if err := writeHelmBinary(binaryName, response.Body); err != nil {
return fmt.Errorf("Cannot write helm binary : %v", err)
}
fmt.Println("Download helm done...")
defer func(binName string) {
if err := os.RemoveAll(binName); err != nil {
fmt.Printf("Cannot delete binary file : %v\n", err)
}
}(binaryName)
binaryName = path.Join(".", binaryName, sdk.GOOS+"-"+sdk.GOARCH, "helm")
}
switch version.Major {
case 2:
return executeHelmV2(binaryName, kubeCfg, q)
case 3:
return executeHelmV3(binaryName, kubeCfg, q)
}
return fmt.Errorf("Unsupported helm version")
}
func executeHelmV2(binaryName, kubeCfg string, q *integrationplugin.RunQuery) error {
releaseName := q.GetOptions()["cds.integration.deployment.release_name"]
namespace := q.GetOptions()["cds.integration.deployment.namespace"]
helmChart := q.GetOptions()["cds.integration.deployment.helm_chart"]
helmValues := q.GetOptions()["cds.integration.deployment.helm_values"]
timeoutStr := q.GetOptions()["cds.integration.deployment.timeout"]
application := q.GetOptions()["cds.application"]
if namespace == "" {
namespace = "default"
}
if releaseName != "" {
application = releaseName
}
if d, err := time.ParseDuration(timeoutStr); err == nil {
timeoutStr = strconv.Itoa(int(d.Seconds()))
fmt.Println("timeout is a duration, converting timeout in seconds to " + timeoutStr)
}
cmdInit := exec.Command(binaryName, "init", "--client-only")
cmdInit.Env = os.Environ()
cmdInit.Stderr = os.Stderr
cmdInit.Stdout = os.Stdout
if err := cmdInit.Run(); err != nil {
return fmt.Errorf("Cannot execute helm init : %v", err)
}
if _, err := os.Stat(helmChart); err == nil {
fmt.Println("Helm dependency update")
cmdDependency := exec.Command(binaryName, "dependency", "update", helmChart)
cmdDependency.Env = os.Environ()
cmdDependency.Env = append(cmdDependency.Env, kubeCfg)
cmdDependency.Stderr = os.Stderr
cmdDependency.Stdout = os.Stdout
if errCmd := cmdDependency.Run(); errCmd != nil {
return fmt.Errorf("Cannot execute helm dependency update : %v", errCmd)
}
}
cmdGet := exec.Command(binaryName, "get", application)
cmdGet.Env = os.Environ()
cmdGet.Env = append(cmdGet.Env, kubeCfg)
errCmd := cmdGet.Run()
var args []string
if errCmd != nil { // Install
fmt.Printf("Install helm release '%s' with chart '%s'...\n", application, helmChart)
args = []string{"install", "--name=" + application, "--debug", "--timeout=" + timeoutStr, "--wait=true", "--namespace=" + namespace}
if helmValues != "" {
args = append(args, "-f", helmValues)
}
helmChartArgs := strings.Split(helmChart, " ")
if len(helmChartArgs) > 1 {
args = append(args, "--repo="+helmChartArgs[0], helmChartArgs[1])
} else {
args = append(args, helmChart)
}
} else {
fmt.Printf("Update helm release '%s' with chart '%s'...\n", application, helmChart)
args = []string{"upgrade", "--timeout=" + timeoutStr, "--wait=true", "--namespace=" + namespace}
if helmValues != "" {
args = append(args, "-f", helmValues)
}
helmChartArgs := strings.Split(helmChart, " ")
if len(helmChartArgs) > 1 {
args = append(args, "--repo="+helmChartArgs[0], application, helmChartArgs[1])
} else {
args = append(args, application, helmChart)
}
}
fmt.Printf("Execute: helm %s\n", strings.Join(args, " "))
cmd := exec.Command(binaryName, args...)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, kubeCfg)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return fmt.Errorf("Cannot execute helm install/update : %v", err)
}
return nil
}
func executeHelmV3(binaryName, kubeCfg string, q *integrationplugin.RunQuery) error {
releaseName := q.GetOptions()["cds.integration.deployment.release_name"]
namespace := q.GetOptions()["cds.integration.deployment.namespace"]
helmChart := q.GetOptions()["cds.integration.deployment.helm_chart"]
helmValues := q.GetOptions()["cds.integration.deployment.helm_values"]
timeoutStr := q.GetOptions()["cds.integration.deployment.timeout"]
application := q.GetOptions()["cds.application"]
if namespace == "" {
namespace = "default"
}
if releaseName != "" {
application = releaseName
}
if _, err := time.ParseDuration(timeoutStr); err != nil {
timeoutStr = timeoutStr + "s"
fmt.Println("timeout is not a duration, setting timeout to " + timeoutStr)
}
cmdRepoAdd := exec.Command(binaryName, "repo", "add", "stable", "https://charts.helm.sh/stable")
cmdRepoAdd.Env = os.Environ()
cmdRepoAdd.Stderr = os.Stderr
cmdRepoAdd.Stdout = os.Stdout
if err := cmdRepoAdd.Run(); err != nil {
return fmt.Errorf("Cannot execute helm repo add stable : %v", err)
}
if _, err := os.Stat(helmChart); err == nil {
fmt.Println("Helm dependency update")
cmdDependency := exec.Command(binaryName, "dependency", "update", helmChart)
cmdDependency.Env = os.Environ()
cmdDependency.Env = append(cmdDependency.Env, kubeCfg)
cmdDependency.Stderr = os.Stderr
cmdDependency.Stdout = os.Stdout
if errCmd := cmdDependency.Run(); errCmd != nil {
return fmt.Errorf("Cannot execute helm dependency update : %v", errCmd)
}
}
cmdGet := exec.Command(binaryName, "get", "all", "--namespace="+namespace, application)
cmdGet.Env = os.Environ()
cmdGet.Env = append(cmdGet.Env, kubeCfg)
errCmd := cmdGet.Run()
var args []string
if errCmd != nil { // Install
fmt.Printf("Install helm release '%s' with chart '%s'...\n", application, helmChart)
args = []string{"install", "--debug", "--timeout=" + timeoutStr, "--wait=true", "--namespace=" + namespace}
if helmValues != "" {
args = append(args, "-f", helmValues)
}
helmChartArgs := strings.Split(helmChart, " ")
if len(helmChartArgs) > 1 {
args = append(args, "--repo="+helmChartArgs[0], helmChartArgs[1])
args = append(args, application, helmChartArgs[1])
} else {
args = append(args, application, helmChart)
}
} else {
fmt.Printf("Update helm release '%s' with chart '%s'...\n", application, helmChart)
args = []string{"upgrade", "--timeout=" + timeoutStr, "--wait=true", "--namespace=" + namespace}
if helmValues != "" {
args = append(args, "-f", helmValues)
}
helmChartArgs := strings.Split(helmChart, " ")
if len(helmChartArgs) > 1 {
args = append(args, "--repo="+helmChartArgs[0], application, helmChartArgs[1])
} else {
args = append(args, application, helmChart)
}
}
fmt.Printf("Execute: helm %s\n", strings.Join(args, " "))
cmd := exec.Command(binaryName, args...)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, kubeCfg)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return fmt.Errorf("Cannot execute helm install/update : %v", err)
}
return nil
}
func writeHelmBinary(pathname string, gzipStream io.Reader) error {
uncompressedStream, err := gzip.NewReader(gzipStream)
if err != nil {
log.Fatal("writeHelmBinary: NewReader failed")
}
tarReader := tar.NewReader(uncompressedStream)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("writeHelmBinary: Next() failed: %s", err.Error())
}
path := filepath.Join(pathname, header.Name)
switch header.Typeflag {
case tar.TypeDir:
if err := os.Mkdir(path, 0755); err != nil {
return fmt.Errorf("writeHelmBinary: mkdir %s failed: %v", path, err)
}
case tar.TypeReg:
outFile, err := os.Create(path)
if err != nil {
return fmt.Errorf("writeHelmBinary: file %s creation failed: %v", path, err)
}
if err := outFile.Chmod(0755); err != nil {
return fmt.Errorf("cannot change permission of file : %v", err)
}
defer outFile.Close()
if _, err := io.Copy(outFile, tarReader); err != nil {
return fmt.Errorf("writeHelmBinary: copy() failed: %v", err)
}
default:
return fmt.Errorf(
"writeHelmBinary: unknown type: %v in %s",
header.Typeflag,
path)
}
}
return nil
}