forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minio.go
123 lines (105 loc) · 2.92 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
116
117
118
119
120
121
122
123
package jenkins
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"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"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
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
var secret *corev1.Secret
if j.UseCache {
secret, err = j.SecretLister.Get(ns, utils.PipelineSecretName)
} else {
secret, err = j.Secrets.GetNamespaced(ns, utils.PipelineSecretName, metav1.GetOptions{})
}
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.Spec.ProjectName)
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.Spec.ProjectName)
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
}