From 102e9c2a25e24b5d42a8ca5e8b166fe8198f8d9d Mon Sep 17 00:00:00 2001 From: kunlongli <16629885+cnlkl@users.noreply.github.com> Date: Tue, 11 Jul 2023 10:05:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87K8S?= =?UTF-8?q?=20Deployment=E6=89=A7=E8=A1=8C=E5=88=86=E6=9E=90=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=20#45?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- analysis-tool-sdk-golang/api/bkrepo.go | 33 ++++++++++++++++----- analysis-tool-sdk-golang/object/argument.go | 14 +++++---- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/analysis-tool-sdk-golang/api/bkrepo.go b/analysis-tool-sdk-golang/api/bkrepo.go index 3af6c6b..d124ffe 100644 --- a/analysis-tool-sdk-golang/api/bkrepo.go +++ b/analysis-tool-sdk-golang/api/bkrepo.go @@ -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 } @@ -160,11 +162,17 @@ 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 } @@ -172,6 +180,17 @@ func (c *BkRepoClient) initToolInput() error { // 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 diff --git a/analysis-tool-sdk-golang/object/argument.go b/analysis-tool-sdk-golang/object/argument.go index a20e93f..76d6a8b 100644 --- a/analysis-tool-sdk-golang/object/argument.go +++ b/analysis-tool-sdk-golang/object/argument.go @@ -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 @@ -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", "", "输出文件路径") @@ -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 != "") }