/
git_server.go
370 lines (332 loc) · 10.8 KB
/
git_server.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright (C) 2022 Red Hat
// SPDX-License-Identifier: Apache-2.0
//
// This package contains the git-server configuration.
package controllers
import (
_ "embed"
"fmt"
"strconv"
"strings"
sfv1 "github.com/softwarefactory-project/sf-operator/api/v1"
"github.com/softwarefactory-project/sf-operator/controllers/libs/base"
"github.com/softwarefactory-project/sf-operator/controllers/libs/conds"
sfmonitoring "github.com/softwarefactory-project/sf-operator/controllers/libs/monitoring"
"github.com/softwarefactory-project/sf-operator/controllers/libs/utils"
"github.com/softwarefactory-project/sf-operator/controllers/libs/zuulcf"
"gopkg.in/yaml.v3"
apiv1 "k8s.io/api/core/v1"
)
const GitServerIdent = "git-server"
const GitServerIdentRW = "git-server-rw"
const gsGitPort = 9418
const gsGitPortRW = 9419
const gsGitPortName = "git-server-port"
const gsGitMountPath = "/git"
const gsPiMountPath = "/entry"
//go:embed static/git-server/update-system-config.sh
var preInitScriptTemplate string
// This function creates dummy connections to be used during the config-check
func makeZuulConnectionConfig(spec *sfv1.ZuulSpec) string {
var sb strings.Builder
connectionNames := sfv1.GetGerritConnectionsName(spec)
connectionNames = append(connectionNames, sfv1.GetGitHubConnectionsName(spec)...)
connectionNames = append(connectionNames, sfv1.GetGitLabConnectionsName(spec)...)
connectionNames = append(connectionNames, sfv1.GetGitConnectionsName(spec)...)
connectionNames = append(connectionNames, sfv1.GetPagureConnectionsName(spec)...)
sb.WriteString("\n")
for _, name := range connectionNames {
sb.WriteString(fmt.Sprintf("[connection %s]\n", name))
sb.WriteString("driver=git\n")
sb.WriteString("baseurl=localhost\n\n")
}
return sb.String()
}
func (r *SFController) MkPreInitScript() string {
configRepoConnectionName := r.cr.Spec.ConfigRepositoryLocation.ZuulConnectionName
configRepoName := r.cr.Spec.ConfigRepositoryLocation.Name
getConnectionDriver := func(r *SFController, connName string) string {
for _, con := range r.cr.Spec.Zuul.GerritConns {
if connName == con.Name {
return "gerrit"
}
}
for _, con := range r.cr.Spec.Zuul.GitLabConns {
if connName == con.Name {
return "gitlab"
}
}
// If not found, defaults to gerrit
return "gerrit"
}
driver := getConnectionDriver(r, configRepoConnectionName)
parentJobName := "base"
semaphore := zuulcf.Semaphores{
{
Semaphore: zuulcf.SemaphoreBody{
Name: "semaphore-config-update",
Max: 1,
},
},
}
jobs := zuulcf.JobConfig{
{
Job: zuulcf.JobBody{
Name: "base",
Parent: nil,
Description: "The base job.",
PreRun: []string{
"playbooks/base/pre.yaml",
},
PostRun: []string{
"playbooks/base/post.yaml",
},
Roles: []zuulcf.JobRoles{
{
"zuul": "zuul/zuul-jobs",
},
},
Timeout: 1800,
Attempts: 3,
Secrets: []interface{}{
"site_sflogs",
},
},
},
{
Job: zuulcf.JobBody{
Name: "config-check",
Parent: &parentJobName,
Final: true,
Description: "Validate the config repo.",
Run: []interface{}{
"playbooks/config/check.yaml",
},
},
},
{
Job: zuulcf.JobBody{
Name: "config-update",
Parent: &parentJobName,
Final: true,
Description: "Deploy config repo update.",
Run: []interface{}{
"playbooks/config/update.yaml",
},
Secrets: []interface{}{
"k8s_config",
},
Semaphores: []zuulcf.JobRunNameAndSemaphore{
{
Name: "semaphore-config-update",
},
},
},
},
}
// Check Pipeline
requireCheck, err := zuulcf.GetRequireCheckByDriver(driver, configRepoConnectionName)
if err != nil {
fmt.Println(err)
}
triggerCheck, err := zuulcf.GetTriggerCheckByDriver(driver, configRepoConnectionName)
if err != nil {
fmt.Println(err)
}
reportersCheck, err := zuulcf.GetReportersCheckByDriver(driver, configRepoConnectionName)
if err != nil {
fmt.Println(err)
}
// Gate Pipeline
requireGate, err := zuulcf.GetRequireGateByDriver(driver, configRepoConnectionName)
if err != nil {
fmt.Println(err)
}
triggerGate, err := zuulcf.GetTriggerGateByDriver(driver, configRepoConnectionName)
if err != nil {
fmt.Println(err)
}
reportersGate, err := zuulcf.GetReportersGateByDriver(driver, configRepoConnectionName)
if err != nil {
fmt.Println(err)
}
// Post Pipeline
triggerPost, err := zuulcf.GetTriggerPostByDriver(driver, configRepoConnectionName)
if err != nil {
fmt.Println(err)
}
pipelines := zuulcf.PipelineConfig{
{
Pipeline: zuulcf.PipelineBody{
Name: "check",
Description: "Newly uploaded patchsets enter this pipeline to receive an initial +/-1 Verified vote.",
Manager: zuulcf.Independent,
Require: requireCheck,
Trigger: triggerCheck,
Start: reportersCheck[0],
Success: reportersCheck[1],
Failure: reportersCheck[2],
},
},
{
Pipeline: zuulcf.PipelineBody{
Name: "gate",
Description: "Changes that have been approved by core developers are enqueued in order in this pipeline, and if they pass tests, will be merged.",
SuccessMessage: "Build succeeded (gate pipeline).",
FailureMessage: "Build failed (gate pipeline).",
Manager: zuulcf.Dependent,
Precedence: zuulcf.High,
Supercedes: []string{
"check",
},
PostReview: true,
Require: requireGate,
Trigger: triggerGate,
Start: reportersGate[0],
Success: reportersGate[1],
Failure: reportersGate[2],
WindowFloor: 20,
WindowIncreaseFactor: 2,
},
},
{
Pipeline: zuulcf.PipelineBody{
Name: "post",
PostReview: true,
Description: "This pipeline runs jobs that operate after each change is merged.",
Manager: zuulcf.Supercedent,
Precedence: zuulcf.Low,
Trigger: triggerPost,
},
},
}
projects := zuulcf.ProjectConfig{
{
Project: zuulcf.ZuulProjectBody{
Name: configRepoName,
Pipeline: zuulcf.ZuulProjectPipelineMap{
"check": zuulcf.ZuulProjectPipeline{
Jobs: []string{
"config-check",
},
},
"gate": zuulcf.ZuulProjectPipeline{
Jobs: []string{
"config-check",
},
},
"post": zuulcf.ZuulProjectPipeline{
Jobs: []string{
"config-update",
},
},
},
},
},
}
semaphoreOutput, _ := yaml.Marshal(semaphore)
jobbaseOutput, _ := yaml.Marshal(jobs)
pipelineOutput, _ := yaml.Marshal(pipelines)
projectOutput, _ := yaml.Marshal(projects)
// We need to copy the global value `preInitScriptTemplate` to avoid updating the global
// and thus loosing the markers.
template := preInitScriptTemplate
template = strings.Replace(template, "# Semaphores", string(semaphoreOutput), 1)
template = strings.Replace(template, "# JobsBase", string(jobbaseOutput), 1)
template = strings.Replace(template, "# Pipelines", string(pipelineOutput), 1)
template = strings.Replace(template, "# Projects", string(projectOutput), 1)
return strings.Replace(
template,
"# ZUUL_CONNECTIONS",
makeZuulConnectionConfig(&r.cr.Spec.Zuul), 1)
}
func (r *SFController) DeployGitServer() bool {
preInitScript := r.MkPreInitScript()
cmData := make(map[string]string)
cmData["pre-init.sh"] = preInitScript
r.EnsureConfigMap(GitServerIdent+"-pi", cmData)
annotations := map[string]string{
"system-config": utils.Checksum([]byte(preInitScript)),
"image": base.GitServerImage(),
"fqdn": r.cr.Spec.FQDN,
"serial": "2",
}
if r.isConfigRepoSet() {
annotations["config-repo-name"] = r.cr.Spec.ConfigRepositoryLocation.Name
annotations["config-zuul-connection-name"] = r.cr.Spec.ConfigRepositoryLocation.ZuulConnectionName
}
logserverHost := "logserver"
if r.cr.Spec.ConfigRepositoryLocation.LogserverHost != "" {
logserverHost = r.cr.Spec.ConfigRepositoryLocation.LogserverHost
annotations["logserver_host"] = logserverHost
}
// Create the statefulset
sts := r.mkStatefulSet(GitServerIdent, base.GitServerImage(), r.getStorageConfOrDefault(r.cr.Spec.GitServer.Storage), apiv1.ReadWriteOnce)
sts.Spec.Template.ObjectMeta.Annotations = annotations
GSVolumeMountsRO := []apiv1.VolumeMount{
{
Name: GitServerIdent,
MountPath: gsGitMountPath,
ReadOnly: true,
},
}
sts.Spec.Template.Spec.Containers[0].VolumeMounts = GSVolumeMountsRO
sts.Spec.Template.Spec.Containers[0].Command = []string{"git", "daemon", "--base-path=/git", "--export-all"}
// Add a second container to serve the git-server with RW access (should not be exposed)
containerRW := base.MkContainer("git-daemon-rw", base.GitServerImage())
containerRW.Command = []string{"git", "daemon", "--base-path=/git",
"--enable=receive-pack", "--export-all", "--port=" + strconv.Itoa(gsGitPortRW)}
containerRW.VolumeMounts = []apiv1.VolumeMount{
{
Name: GitServerIdent,
MountPath: gsGitMountPath,
},
}
sts.Spec.Template.Spec.Containers = append(sts.Spec.Template.Spec.Containers, containerRW)
sts.Spec.Template.Spec.Volumes = []apiv1.Volume{
base.MkVolumeCM(GitServerIdent+"-pi", GitServerIdent+"-pi-config-map"),
}
sts.Spec.Template.Spec.Containers[0].Ports = []apiv1.ContainerPort{
base.MkContainerPort(gsGitPort, gsGitPortName),
}
// Define initContainer
initContainer := base.MkContainer("init-config", base.GitServerImage())
initContainer.Command = []string{"/bin/bash", "/entry/pre-init.sh"}
initContainer.Env = []apiv1.EnvVar{
base.MkEnvVar("FQDN", r.cr.Spec.FQDN),
base.MkEnvVar("ZUUL_LOGSERVER_HOST", logserverHost),
}
initContainer.VolumeMounts = []apiv1.VolumeMount{
{
Name: GitServerIdent,
MountPath: gsGitMountPath,
},
{
Name: GitServerIdent + "-pi",
MountPath: gsPiMountPath,
},
}
if r.isConfigRepoSet() {
initContainer.Env = append(initContainer.Env,
base.MkEnvVar("CONFIG_REPO_NAME", r.cr.Spec.ConfigRepositoryLocation.Name),
)
}
sts.Spec.Template.Spec.InitContainers = []apiv1.Container{initContainer}
// Create readiness probes
// Note: The probe is causing error message to be logged by the service
// dep.Spec.Template.Spec.Containers[0].ReadinessProbe = create_readiness_tcp_probe(GS_GIT_PORT)
statsExporter := sfmonitoring.MkNodeExporterSideCarContainer(GitServerIdent, GSVolumeMountsRO)
sts.Spec.Template.Spec.Containers = append(sts.Spec.Template.Spec.Containers, statsExporter)
current, changed := r.ensureStatefulset(sts)
if changed {
return false
}
// Create services exposed
svc := base.MkServicePod(GitServerIdent, r.ns, GitServerIdent+"-0", []int32{gsGitPort}, gsGitPortName)
r.EnsureService(&svc)
svcRW := base.MkServicePod(GitServerIdentRW, r.ns, GitServerIdent+"-0", []int32{gsGitPortRW}, gsGitPortName)
r.EnsureService(&svcRW)
ready := r.IsStatefulSetReady(current)
conds.UpdateConditions(&r.cr.Status.Conditions, GitServerIdent, ready)
return ready
}