37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
minio.go 2.92 KB
一键复制 编辑 原始数据 按行查看 历史
phli 提交于 2018-10-15 13:18 . catalog integration
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.4-rc20

搜索帮助