/
define.go
68 lines (54 loc) · 1.93 KB
/
define.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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package types
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/pipeline/spec"
)
// Kind return the task executor type
type Kind string
func (k Kind) String() string {
return string(k)
}
// Name return the task executor name
type Name string
func (n Name) String() string {
return string(n)
}
type TaskExecutor interface {
Kind() Kind
Name() Name
Status(ctx context.Context, task *spec.PipelineTask) (apistructs.StatusDesc, error)
Create(ctx context.Context, task *spec.PipelineTask) (interface{}, error)
Remove(ctx context.Context, task *spec.PipelineTask) (interface{}, error)
BatchDelete(ctx context.Context, tasks []*spec.PipelineTask) (interface{}, error)
Inspect(ctx context.Context, task *spec.PipelineTask) (apistructs.TaskInspect, error)
}
type CreateFn func(name Name, clusterName string, cluster apistructs.ClusterInfo) (TaskExecutor, error)
var Factory = map[Kind]CreateFn{}
func Register(kind Kind, create CreateFn) error {
if _, ok := Factory[kind]; ok {
return errors.Errorf("duplicate to register task executor: %s", kind)
}
Factory[kind] = create
return nil
}
func MustRegister(kind Kind, create CreateFn) {
err := Register(kind, create)
if err != nil {
panic(fmt.Errorf("failed to register action executor, kind: %s, err: %v", kind, err))
}
}