-
Notifications
You must be signed in to change notification settings - Fork 0
/
mappper.go
200 lines (177 loc) · 6 KB
/
mappper.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package jenkins
import (
"bytes"
"fmt"
"strings"
"github.com/pkg/errors"
images "github.com/rancher/rancher/pkg/image"
"github.com/rancher/rancher/pkg/pipeline/utils"
mv3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/apis/project.cattle.io/v3"
)
func ConvertPipelineExecutionToJenkinsPipeline(execution *v3.PipelineExecution) (*PipelineJob, error) {
if execution == nil {
return nil, errors.New("nil pipeline execution")
}
if err := utils.ValidPipelineConfig(execution.Spec.PipelineConfig); err != nil {
return nil, err
}
copyExecution := execution.DeepCopy()
parsePreservedEnvVar(copyExecution)
pipelineJob := &PipelineJob{
Plugin: WorkflowJobPlugin,
Definition: Definition{
Class: FlowDefinitionClass,
Plugin: FlowDefinitionPlugin,
Sandbox: true,
Script: convertPipelineExecution(copyExecution),
},
}
return pipelineJob, nil
}
func convertStep(execution *v3.PipelineExecution, stageOrdinal int, stepOrdinal int) string {
stepName := fmt.Sprintf("step-%d-%d", stageOrdinal, stepOrdinal)
jStep := toJenkinsStep(execution, stageOrdinal, stepOrdinal)
return fmt.Sprintf(stepBlock, stepName, stepName, stepName, jStep.command)
}
func convertStage(execution *v3.PipelineExecution, stageOrdinal int) string {
var buffer bytes.Buffer
pipelineConfig := execution.Spec.PipelineConfig
stage := pipelineConfig.Stages[stageOrdinal]
for i := range stage.Steps {
buffer.WriteString(convertStep(execution, stageOrdinal, i))
if i != len(stage.Steps)-1 {
buffer.WriteString(",")
}
}
skipOption := ""
if !utils.MatchAll(stage.When, execution) {
skipOption = fmt.Sprintf(markSkipScript, stage.Name)
}
return fmt.Sprintf(stageBlock, stage.Name, skipOption, buffer.String())
}
func convertPipelineExecution(execution *v3.PipelineExecution) string {
var containerbuffer bytes.Buffer
var pipelinebuffer bytes.Buffer
for j, stage := range execution.Spec.PipelineConfig.Stages {
pipelinebuffer.WriteString(convertStage(execution, j))
pipelinebuffer.WriteString("\n")
for k := range stage.Steps {
stepName := fmt.Sprintf("step-%d-%d", j, k)
jStep := toJenkinsStep(execution, j, k)
containerDef := fmt.Sprintf(containerBlock, stepName, jStep.image, jStep.containerOptions)
containerbuffer.WriteString(containerDef)
}
}
ns := utils.GetPipelineCommonName(execution)
jenkinsURL := fmt.Sprintf("http://%s:%d", utils.JenkinsName, utils.JenkinsPort)
timeout := utils.DefaultTimeout
if execution.Spec.PipelineConfig.Timeout > 0 {
timeout = execution.Spec.PipelineConfig.Timeout
}
return fmt.Sprintf(pipelineBlock, ns, ns, containerbuffer.String(), images.Resolve(mv3.ToolsSystemImages.PipelineSystemImages.JenkinsJnlp), jenkinsURL, execution.Name, timeout, pipelinebuffer.String())
}
func getStepContainerOptions(execution *v3.PipelineExecution, privileged bool, optional map[string]string, envFrom []v3.EnvFrom) string {
var buffer bytes.Buffer
for k, v := range utils.GetEnvVarMap(execution) {
buffer.WriteString(fmt.Sprintf(envVarSkel, k, v))
}
for k, v := range optional {
buffer.WriteString(fmt.Sprintf(envVarSkel, k, v))
}
if execution.Spec.Event != utils.WebhookEventPullRequest {
//expose no secrets on pull_request events
for _, e := range envFrom {
envName := e.SourceKey
if e.TargetKey != "" {
envName = e.TargetKey
}
buffer.WriteString(fmt.Sprintf(secretEnvSkel, envName, e.SourceName, e.SourceKey))
}
}
result := fmt.Sprintf(envVarsSkel, buffer.String())
if privileged {
result = ", privileged: true" + result
}
return result
}
func parsePreservedEnvVar(execution *v3.PipelineExecution) {
m := utils.GetEnvVarMap(execution)
pipelineConfig := execution.Spec.PipelineConfig
//environment variables substitution in configs
for _, stage := range pipelineConfig.Stages {
for _, step := range stage.Steps {
if step.RunScriptConfig != nil {
step.RunScriptConfig.Image = substituteEnvVar(m, step.RunScriptConfig.Image)
} else if step.PublishImageConfig != nil {
step.PublishImageConfig.Tag = substituteEnvVar(m, step.PublishImageConfig.Tag)
} else if step.ApplyYamlConfig != nil {
step.ApplyYamlConfig.Path = substituteEnvVar(m, step.ApplyYamlConfig.Path)
step.ApplyYamlConfig.Content = substituteEnvVar(m, step.ApplyYamlConfig.Content)
}
for k, v := range step.Env {
step.Env[k] = substituteEnvVar(m, v)
}
}
}
}
func substituteEnvVar(envvar map[string]string, raw string) string {
result := raw
for k, v := range envvar {
result = strings.Replace(result, "${"+k+"}", v, -1)
}
return result
}
const stageBlock = `stage('%s'){
%s
parallel %s
}
`
const stepBlock = `'%s': {
stage('%s'){
container(name: '%s') {
%s
}
}
}`
const pipelineBlock = `import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
def label = "buildpod.${env.JOB_NAME}.${env.BUILD_NUMBER}".replace('-', '_').replace('/', '_')
podTemplate(label: label, namespace: '%s', instanceCap: 1, serviceAccount: 'jenkins',volumes: [emptyDirVolume(mountPath: '/var/lib/docker', memory: false), secretVolume(mountPath: '/etc/docker/certs.d/docker-registry.%s', secretName: 'registry-crt')], containers: [
%s
containerTemplate(name: 'jnlp', image: '%s', envVars: [
envVar(key: 'JENKINS_URL', value: '%s')], args: '${computer.jnlpmac} ${computer.name}', ttyEnabled: false)], yaml: """
apiVersion: v1
kind: Pod
metadata:
labels:
app: jenkins
execution: %s
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- jenkins
topologyKey: kubernetes.io/hostname
"""
) {
node(label) {
timestamps {
timeout(%d) {
%s
}
}
}
}`
const containerBlock = `containerTemplate(name: '%s', image: '%s', ttyEnabled: true, command: 'cat' %s),`
const envVarSkel = "envVar(key: '%s', value: '%s'),"
const secretEnvSkel = "secretEnvVar(key: '%s', secretName: '%s', secretKey: '%s'),"
const envVarsSkel = `, envVars: [
%s
]`