Skip to content

Commit f61ff5c

Browse files
Microzuul CIGerrit Code Review
Microzuul CI
authored and
Gerrit Code Review
committed
Merge "Check for duplicated zuul connection"
2 parents 0ca6602 + 1f7ad67 commit f61ff5c

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

controllers/softwarefactory_controller.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,32 @@ func resolveConfigBaseURL(cr sfv1.SoftwareFactory) string {
191191
return ""
192192
}
193193

194+
func GetUserDefinedConnections(cr *sfv1.SoftwareFactory) []string {
195+
var conns []string
196+
for _, conn := range cr.Spec.Zuul.GerritConns {
197+
conns = append(conns, conn.Name)
198+
}
199+
for _, conn := range cr.Spec.Zuul.GitHubConns {
200+
conns = append(conns, conn.Name)
201+
}
202+
for _, conn := range cr.Spec.Zuul.GitLabConns {
203+
conns = append(conns, conn.Name)
204+
}
205+
for _, conn := range cr.Spec.Zuul.GitConns {
206+
conns = append(conns, conn.Name)
207+
}
208+
for _, conn := range cr.Spec.Zuul.PagureConns {
209+
conns = append(conns, conn.Name)
210+
}
211+
for _, conn := range cr.Spec.Zuul.ElasticSearchConns {
212+
conns = append(conns, conn.Name)
213+
}
214+
for _, conn := range cr.Spec.Zuul.SMTPConns {
215+
conns = append(conns, conn.Name)
216+
}
217+
return conns
218+
}
219+
194220
func (r *SFController) IsCodesearchEnabled() bool {
195221
return r.cr.Spec.Codesearch.Enabled == nil || *r.cr.Spec.Codesearch.Enabled
196222
}
@@ -406,9 +432,27 @@ func CheckOpenShift() bool {
406432
return openshiftUser
407433
}
408434

435+
func HasDuplicate(conns []string) string {
436+
for i, conn := range conns {
437+
if slices.Contains(conns[i+1:], conn) {
438+
return conn
439+
}
440+
}
441+
return ""
442+
}
443+
409444
func (r *SoftwareFactoryReconciler) mkSFController(
410445
ctx context.Context, ns string, owner client.Object, cr sfv1.SoftwareFactory,
411446
standalone bool) SFController {
447+
conns := GetUserDefinedConnections(&cr)
448+
if dup := HasDuplicate(conns); dup != "" {
449+
fmt.Fprintf(os.Stderr, "Duplicate zuul connection: %s", dup)
450+
os.Exit(1)
451+
}
452+
if slices.Contains(conns, "git-server") {
453+
fmt.Fprintf(os.Stderr, "The git-server connection name is reserved, please rename it")
454+
os.Exit(1)
455+
}
412456
return SFController{
413457
SFUtilContext: SFUtilContext{
414458
Client: r.Client,

controllers/suite_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ func TestAPIs(t *testing.T) {
4646
RunSpecs(t, "Controller Suite")
4747
}
4848

49+
func TestDuplicateConn(t *testing.T) {
50+
var cr sfv1.SoftwareFactory
51+
if HasDuplicate(GetUserDefinedConnections(&cr)) != "" {
52+
t.Errorf("CR does not have dup")
53+
}
54+
cr.Spec.Zuul.GitConns = []sfv1.GitConnection{
55+
sfv1.GitConnection{
56+
Name: "opendev.org",
57+
},
58+
}
59+
cr.Spec.Zuul.GerritConns = []sfv1.GerritConnection{
60+
sfv1.GerritConnection{
61+
Name: "opendev.org",
62+
},
63+
}
64+
if HasDuplicate(GetUserDefinedConnections(&cr)) != "opendev.org" {
65+
t.Errorf("CR have dup")
66+
}
67+
}
68+
4969
var _ = BeforeSuite(func() {
5070
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
5171

doc/reference/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
55
## [in development]
66

77
### Added
8+
9+
- zuul: the operator now validates that the user provided connections doesn't have any duplicate names.
10+
811
### Changed
912

1013
- logjuicer service is based on a ubi9 base image

0 commit comments

Comments
 (0)