-
Notifications
You must be signed in to change notification settings - Fork 0
/
minio.go
115 lines (98 loc) · 2.67 KB
/
minio.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
package jenkins
import (
"fmt"
"github.com/minio/minio-go"
"github.com/rancher/rancher/pkg/pipeline/utils"
"github.com/rancher/types/apis/project.cattle.io/v3"
"github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"strings"
"time"
)
type minioClient struct {
client minio.Client
}
func (j *Engine) getMinioURL(ns string) (string, error) {
MinioName := utils.MinioName
svc, err := j.ServiceLister.Get(ns, MinioName)
if err != nil {
return "", err
}
ip := svc.Spec.ClusterIP
return fmt.Sprintf("%s:%d", ip, utils.MinioPort), nil
}
func (j *Engine) getMinioClient(ns string) (*minio.Client, error) {
url, err := j.getMinioURL(ns)
if err != nil {
return nil, err
}
user := utils.PipelineSecretDefaultUser
secret, err := j.SecretLister.Get(ns, utils.PipelineSecretName)
if err != nil || secret.Data == nil {
return nil, fmt.Errorf("error get minio token - %v", err)
}
token := string(secret.Data[utils.PipelineSecretTokenKey])
client, err := minio.New(url, user, token, false)
if err != nil {
return nil, err
}
if j.HTTPClient == nil {
dial, err := j.Dialer.ClusterDialer(j.ClusterName)
if err != nil {
return nil, err
}
j.HTTPClient = &http.Client{
Transport: &http.Transport{
Dial: dial,
},
Timeout: 15 * time.Second,
}
}
client.SetCustomTransport(j.HTTPClient.Transport)
return client, nil
}
func (j Engine) getStepLogFromMinioStore(execution *v3.PipelineExecution, stage int, step int) (string, error) {
bucketName := utils.MinioLogBucket
logName := fmt.Sprintf("%s-%d-%d", execution.Name, stage, step)
ns := utils.GetPipelineCommonName(execution)
client, err := j.getMinioClient(ns)
if err != nil {
return "", err
}
reader, err := client.GetObject(bucketName, logName, minio.GetObjectOptions{})
//stat, err := reader.Stat()
if err != nil {
return "", err
}
content, err := ioutil.ReadAll(reader)
if err != nil {
return "", err
}
return string(content), nil
}
func (j *Engine) saveStepLogToMinio(execution *v3.PipelineExecution, stage int, step int) error {
bucketName := utils.MinioLogBucket
logName := fmt.Sprintf("%s-%d-%d", execution.Name, stage, step)
ns := utils.GetPipelineCommonName(execution)
client, err := j.getMinioClient(ns)
if err != nil {
return err
}
//Make Bucket
exists, err := client.BucketExists(bucketName)
if err != nil {
logrus.Error(err)
}
if !exists {
if err := client.MakeBucket(bucketName, utils.MinioBucketLocation); err != nil {
return err
}
}
message, err := j.getStepLogFromJenkins(execution, stage, step)
if err != nil {
return err
}
_, err = client.PutObject(bucketName, logName, strings.NewReader(message), int64(len(message)), minio.PutObjectOptions{})
return err
}