Skip to content

Commit

Permalink
feat: 支持通过K8S Deployment执行分析任务 #45
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlkl authored Jul 11, 2023
1 parent 62e5e4b commit 102e9c2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
33 changes: 26 additions & 7 deletions analysis-tool-sdk-golang/api/bkrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,20 @@ func GetClient(args *object.Arguments) *BkRepoClient {

// Start 开始分析
func (c *BkRepoClient) Start() (*object.ToolInput, error) {
if c.ToolInput == nil && c.Args.Online() {
if err := c.updateSubtaskStatus(); err != nil {
return nil, err
}
util.Info("update subtask status success")
}
if c.ToolInput == nil {
if err := c.initToolInput(); err != nil {
return nil, err
}
toolInputJson, _ := json.Marshal(c.ToolInput)
util.Info("init tool input success:\n %s", toolInputJson)

// 是在线任务时,更新任务状态为执行中
if c.Args.Online() {
if err := c.updateSubtaskStatus(); err != nil {
return nil, err
}
util.Info("update subtask status success")
}
}
return c.ToolInput, nil
}
Expand Down Expand Up @@ -160,18 +162,35 @@ func (c *BkRepoClient) initToolInput() error {
return err
}
c.ToolInput = toolInput
} else {
} else if c.Args.TaskId != "" {
var err error
if c.ToolInput, err = c.fetchToolInput(); err != nil {
return err
}
} else {
var err error
if c.ToolInput, err = c.pullToolInput(); err != nil {
return err
}
c.Args.TaskId = c.ToolInput.TaskId
}
return nil
}

// fetchToolInput 从制品分析服务拉取工具输入
func (c *BkRepoClient) fetchToolInput() (*object.ToolInput, error) {
url := c.Args.Url + analystTemporaryPrefix + "/scan/subtask/" + c.Args.TaskId + "/input?token=" + c.Args.Token
return c.doFetchToolInput(url)
}

// pullTooInput 从制品分析服务拉取工具输入
func (c *BkRepoClient) pullToolInput() (*object.ToolInput, error) {
url := c.Args.Url + analystTemporaryPrefix + "/scan/subtask/input?executionCluster=" + c.Args.ExecutionCluster +
"&token=" + c.Args.Token
return c.doFetchToolInput(url)
}

func (c *BkRepoClient) doFetchToolInput(url string) (*object.ToolInput, error) {
response, err := http.DefaultClient.Get(url)
if err != nil {
return nil, err
Expand Down
14 changes: 8 additions & 6 deletions analysis-tool-sdk-golang/object/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (

// Arguments 输入参数
type Arguments struct {
Url string
Token string
TaskId string
InputFilePath string
OutputFilePath string
Url string
Token string
TaskId string
ExecutionCluster string
InputFilePath string
OutputFilePath string
}

var args *Arguments
Expand All @@ -21,6 +22,7 @@ func newArguments() *Arguments {
flag.StringVar(&args.Url, "url", "", "制品库地址")
flag.StringVar(&args.Token, "token", "", "制品库临时令牌")
flag.StringVar(&args.TaskId, "task-id", "", "扫描任务Id")
flag.StringVar(&args.ExecutionCluster, "execution-cluster", "", "所在扫描执行集群名")
flag.StringVar(&args.InputFilePath, "input", "", "输入文件路径")
flag.StringVar(&args.OutputFilePath, "output", "", "输出文件路径")

Expand Down Expand Up @@ -51,5 +53,5 @@ func (arg *Arguments) Offline() bool {

// Online 在线扫描
func (arg *Arguments) Online() bool {
return arg.Url != "" && arg.Token != "" && arg.TaskId != ""
return arg.Url != "" && arg.Token != "" && (arg.TaskId != "" || arg.ExecutionCluster != "")
}

0 comments on commit 102e9c2

Please sign in to comment.