拉取代码采用id确保每次都是最新配置

This commit is contained in:
somunslotus 2025-05-13 10:57:50 +08:00
parent 720e2cf186
commit 4beee720ee
4 changed files with 76 additions and 12 deletions

View File

@ -3,7 +3,10 @@ package parse
import (
"encoding/json"
"fmt"
"pipeline-convert/internal/log"
"pipeline-convert/internal/model"
"pipeline-convert/pkg/manageclient"
"time"
)
const (
@ -16,10 +19,13 @@ const (
)
type refCodeTypeParser struct {
manageClient manageclient.ManageInterface
}
func NewRefCodeTypeParser() InputTypeParser {
return &refCodeTypeParser{}
func NewRefCodeTypeParser(baseUrl string, timeout time.Duration) InputTypeParser {
return &refCodeTypeParser{
manageClient: manageclient.NewManageClient(baseUrl, timeout),
}
}
type CodeParam struct {
@ -37,19 +43,24 @@ func (r *refCodeTypeParser) ParseType(key string, param model.Param) (*TypeResul
if err := json.Unmarshal([]byte(param.Value), &codeParam); err != nil {
return nil, err
}
codeConfig, err := r.manageClient.GetCodeCodeConfigByID(int32(codeParam.ID))
if err != nil {
log.Errorf("get code config by id error: %v, id is %d", err, codeParam.ID)
return nil, err
}
var args []string
args = append(args, fmt.Sprintf("%s=%s", codePath, codeParam.CodePath),
fmt.Sprintf("%s=%s", branch, codeParam.Branch),
args = append(args, fmt.Sprintf("%s=%s", codePath, codeConfig.GitUrl),
fmt.Sprintf("%s=%s", branch, codeConfig.GitBranch),
fmt.Sprintf("%s=%d", depth, 1))
if codeParam.SshPrivateKey != "" {
args = append(args, fmt.Sprintf("%s=%s", sshKey, codeParam.SshPrivateKey))
args = append(args, fmt.Sprintf("%s=%s", sshKey, codeConfig.SshKey))
}
if codeParam.Username != "" && codeParam.Password != "" {
args = append(args, fmt.Sprintf("%s=%s", username, codeParam.Username),
fmt.Sprintf("%s=%s", password, codeParam.Password))
args = append(args, fmt.Sprintf("%s=%s", username, codeConfig.GitUserName),
fmt.Sprintf("%s=%s", password, codeConfig.GitPassword))
}
return &TypeResult{

View File

@ -5,6 +5,8 @@ import (
corev1 "k8s.io/api/core/v1"
"pipeline-convert/internal/log"
"pipeline-convert/internal/model"
"pipeline-convert/pkg/manageclient"
"time"
)
type InputTypeParser interface {
@ -33,7 +35,7 @@ func NewJuicefsInputTypesParser(param model.Param) InputTypeParser {
// ref code type
case fmt.Sprintf("%s-%s", RefType, RefCodeType):
log.Debug("parse ref code type")
return NewRefCodeTypeParser()
return NewRefCodeTypeParser(manageclient.ManageBaeUrl, 10*time.Second)
// 数据集和模型需要判断值是否是引用类型
case fmt.Sprintf("%s-%s", RefType, RefDatasetType), fmt.Sprintf("%s-%s", RefType, RefModelType):
@ -65,7 +67,7 @@ func NewDefaultInputTypeParser(param model.Param) InputTypeParser {
// ref code type
case fmt.Sprintf("%s-%s", RefType, RefCodeType):
log.Debug("parse ref code type")
return NewRefCodeTypeParser()
return NewRefCodeTypeParser(manageclient.ManageBaeUrl, 10*time.Second)
// 数据集和模型需要判断值是否是引用类型
case fmt.Sprintf("%s-%s", RefType, RefDatasetType), fmt.Sprintf("%s-%s", RefType, RefModelType):

View File

@ -15,6 +15,7 @@ const (
GetTokenUri = "/auth/loginByKey"
AutoExportType = "auto_export"
GetResourceByIDUriTemplate = "/mmp/computingResource/%d"
GetCodeConfigByID = "/mmp/codeConfig/%d"
)
type TokenResponse struct {
@ -32,6 +33,12 @@ type ResourceResponse struct {
Data Resource `json:"data"`
}
type CodeConfigResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data OriginalCodeConfig `json:"data"`
}
func (m *manageClient) GetAuthToken(username, password string) (string, error) {
data := map[string]string{
"username": username,
@ -76,9 +83,35 @@ func (m *manageClient) GetResourceByID(id int32) (*Resource, error) {
return nil, err
}
if resourceResp.Code != http.StatusOK {
log.Errorf("Failed to get resource by id: %v", resourceResp.Msg)
return nil, nil
err := fmt.Errorf("failed to get resource by id: %v", resourceResp.Msg)
return nil, err
}
log.Info("Get resource by id successfully, resourceResp: %+v", resourceResp)
log.Infof("Get resource by id successfully, resourceResp: %+v", resourceResp)
return &resourceResp.Data, nil
}
func (m *manageClient) GetCodeCodeConfigByID(id int32) (*OriginalCodeConfig, error) {
token, err := m.GetAuthToken(Admin, Password)
if err != nil {
return nil, err
}
path := fmt.Sprintf(GetCodeConfigByID, id)
m.httpClient.SetHeader("Authorization", "Bearer "+token)
resp, err := m.httpClient.Get(path)
if err != nil {
log.Errorf("Failed to get codeConfig by id: %v", err)
return nil, err
}
var codeConfigResponse CodeConfigResponse
if err := json.Unmarshal(resp, &codeConfigResponse); err != nil {
log.Errorf("Failed to parse codeConfig response: %v", err)
return nil, err
}
if codeConfigResponse.Code != http.StatusOK {
err := fmt.Errorf("failed to get codeConfig by id: %v", codeConfigResponse.Msg)
return nil, err
}
log.Infof("Get codeConfig by id successfully, codeConfigResponse: %+v", codeConfigResponse)
return &codeConfigResponse.Data, nil
}

View File

@ -8,6 +8,7 @@ import (
type ManageInterface interface {
GetAuthToken(username, password string) (string, error)
GetResourceByID(id int32) (*Resource, error)
GetCodeCodeConfigByID(id int32) (*OriginalCodeConfig, error)
}
type manageClient struct {
@ -38,3 +39,20 @@ type Resource struct {
State int `json:"state"`
Node interface{} `json:"node"`
}
type OriginalCodeConfig struct {
Id int `json:"id"`
CodeRepoName string `json:"code_repo_name"`
CodeRepoVis int `json:"code_repo_vis"`
GitUrl string `json:"git_url"`
GitBranch string `json:"git_branch"`
VerifyMode interface{} `json:"verify_mode"`
GitUserName interface{} `json:"git_user_name"`
GitPassword interface{} `json:"git_password"`
SshKey interface{} `json:"ssh_key"`
CreateBy string `json:"create_by"`
CreateTime time.Time `json:"create_time"`
UpdateBy string `json:"update_by"`
UpdateTime time.Time `json:"update_time"`
State int `json:"state"`
}