/
step.go
137 lines (126 loc) · 3.21 KB
/
step.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package logic
import (
v1 "github.com/klovercloud-ci-cd/core-engine/core/v1"
"github.com/klovercloud-ci-cd/core-engine/core/v1/service"
"github.com/klovercloud-ci-cd/core-engine/enums"
"strings"
)
type stepService struct {
step v1.Step
}
func (service *stepService) SetInput(url, revision string) {
if service.step.Type == enums.BUILD {
service.step.Params[enums.IMAGE_URL] = url
service.step.Params[enums.REVISION] = revision
}
}
func (service *stepService) SetArgs(k8s service.K8s) {
service.step.ArgData = map[string]string{}
if service.step.Params[enums.ARGS] != "" {
for _, each := range strings.Split(service.step.Params[enums.ARGS], ",") {
arg := strings.Split(each, ":")
if len(arg) == 2 {
service.step.ArgData[arg[0]] = arg[1]
}
if len(arg) == 1 {
service.step.ArgData[arg[0]] = ""
}
}
}
for _, each := range strings.Split(service.step.Params[enums.ARGS_FROM_SECRETS], ",") {
secret := strings.Split(each, "/")
var name, namespace string
if len(secret) == 2 {
name = secret[0]
namespace = secret[1]
}
if len(secret) == 1 {
name = secret[0]
namespace = "default"
}
sec, err := k8s.GetSecret(name, namespace)
if err != nil {
continue
}
for key, val := range sec.StringData {
service.step.ArgData[key] = string(val)
}
}
for _, each := range strings.Split(service.step.Params[enums.ARGS_FROM_CONFIGMAPS], ",") {
configMap := strings.Split(each, "/")
var name, namespace string
if len(configMap) == 2 {
namespace = configMap[0]
name = configMap[1]
}
if len(configMap) == 1 {
name = configMap[0]
namespace = "default"
}
cm, err := k8s.GetConfigMap(name, namespace)
if err != nil {
continue
}
for key, val := range cm.Data {
service.step.ArgData[key] = val
}
}
}
func (service *stepService) SetEnvs(k8s service.K8s) {
service.step.EnvData = map[string]string{}
if service.step.Params[enums.ENVS] != "" {
for _, each := range strings.Split(service.step.Params[enums.ENVS], ",") {
env := strings.Split(each, ":")
if len(env) == 2 {
service.step.EnvData[env[0]] = env[1]
}
if len(env) == 1 {
service.step.EnvData[env[0]] = ""
}
}
}
for _, each := range strings.Split(service.step.Params[enums.ENVS_FROM_SECRETS], ",") {
secret := strings.Split(each, "/")
var name, namespace string
if len(secret) == 2 {
name = secret[0]
namespace = secret[1]
}
if len(secret) == 1 {
name = secret[0]
namespace = "default"
}
sec, err := k8s.GetSecret(name, namespace)
if err != nil {
continue
}
for key, val := range sec.StringData {
service.step.EnvData[key] = string(val)
}
}
for _, each := range strings.Split(service.step.Params[enums.ENVS_FROM_CONFIGMAPS], ",") {
configMap := strings.Split(each, "/")
var name, namespace string
if len(configMap) == 2 {
namespace = configMap[0]
name = configMap[1]
}
if len(configMap) == 1 {
name = configMap[0]
namespace = "default"
}
cm, err := k8s.GetConfigMap(name, namespace)
if err != nil {
continue
}
for key, val := range cm.Data {
service.step.EnvData[key] = val
}
}
}
// NewStepService returns step type service.
func NewStepService(step v1.Step) service.Step {
return &stepService{
step: step,
}
}