Skip to content

Commit

Permalink
Add GitHub connection support to Zuul connections
Browse files Browse the repository at this point in the history
Change-Id: If2cf66977aff55af7df8fc884c871d3c14244eda
  • Loading branch information
fserucas committed Oct 17, 2023
1 parent 2a9c18a commit a95872d
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 8 deletions.
37 changes: 36 additions & 1 deletion api/v1/softwarefactory_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ type ConfigLocationSpec struct {
ZuulConnectionName string `json:"zuul-connection-name"`
}

// Describes a Zuul connection using the `github` driver: https://zuul-ci.org/docs/zuul/latest/drivers/github.html#
type GitHubConnection struct {
// How the connection will be named in Zuul's configuration and appear in zuul-web
Name string `json:"name"`
// https://zuul-ci.org/docs/zuul/latest/drivers/github.html#attr-%3Cgithub%20connection%3E.app_id
AppID string `json:"appId"`
// https://zuul-ci.org/docs/zuul/latest/drivers/github.html#attr-%3Cgithub%20connection%3E.app_key
AppKey string `json:"appKey"`
// https://zuul-ci.org/docs/zuul/latest/drivers/github.html#attr-%3Cgithub%20connection%3E.api_token
APIToken string `json:"apiToken"`
// https://zuul-ci.org/docs/zuul/latest/drivers/github.html#attr-%3Cgithub%20connection%3E.webhook_token
// +optional
WebhookToken string `json:"webHookToken,omitempty"`
// https://zuul-ci.org/docs/zuul/latest/drivers/github.html#attr-%3Cgithub%20connection%3E.server
// +optional
Server string `json:"server,omitempty"`
// https://zuul-ci.org/docs/zuul/latest/drivers/github.html#attr-%3Cgithub%20connection%3E.canonical_hostname
// +optional
Canonicalhostname string `json:"canonicalHostname,omitempty"`
// https://zuul-ci.org/docs/zuul/latest/drivers/github.html#attr-%3Cgithub%20connection%3E.verify_ssl
// +kubebuilder:default:=true
// +optional
VerifySSL bool `json:"verifySsl,omitempty"`
}

// Describes a Zuul connection using the `gerrit` driver: https://zuul-ci.org/docs/zuul/latest/drivers/gerrit.html#connection-configuration
type GerritConnection struct {
// How the connection will be named in Zuul's configuration and appear in zuul-web
Expand Down Expand Up @@ -200,6 +225,8 @@ type ZuulSpec struct {
DefaultAuthenticator string `json:"defaultAuthenticator,omitempty"`
// The list of Gerrit-based connections to add to Zuul's configuration
GerritConns []GerritConnection `json:"gerritconns,omitempty"`
// The list of GitHub-based connections to add to Zuul's configuration
GitHubConns []GitHubConnection `json:"githubconns,omitempty"`
// Configuration of the executor microservices
Executor ZuulExecutorSpec `json:"executor,omitempty"`
// Configuration of the scheduler microservice
Expand All @@ -210,7 +237,7 @@ type ZuulSpec struct {
Merger ZuulMergerSpec `json:"merger,omitempty"`
}

func GetConnectionsName(spec *ZuulSpec) []string {
func GetGerritConnectionsName(spec *ZuulSpec) []string {
var res []string
res = append(res, "git-server")
res = append(res, "opendev.org")
Expand All @@ -220,6 +247,14 @@ func GetConnectionsName(spec *ZuulSpec) []string {
return res
}

func GetGitHubConnectionsName(spec *ZuulSpec) []string {
var res []string
for _, conn := range spec.GitHubConns {
res = append(res, conn.Name)
}
return res
}

// +kubebuilder:validation:Enum=INFO;WARN;DEBUG
// +kubebuilder:default:=INFO
type LogLevel string
Expand Down
20 changes: 20 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,46 @@ spec:
- name
type: object
type: array
githubconns:
description: The list of GitHub-based connections to add to Zuul's
configuration
items:
description: 'Describes a Zuul connection using the `github`
driver: https://zuul-ci.org/docs/zuul/latest/drivers/github.html#'
properties:
apiToken:
description: https://zuul-ci.org/docs/zuul/latest/drivers/github.html#attr-%3Cgithub%20connection%3E.api_token
type: string
appId:
description: https://zuul-ci.org/docs/zuul/latest/drivers/github.html#attr-%3Cgithub%20connection%3E.app_id
type: string
appKey:
description: https://zuul-ci.org/docs/zuul/latest/drivers/github.html#attr-%3Cgithub%20connection%3E.app_key
type: string
canonicalHostname:
description: https://zuul-ci.org/docs/zuul/latest/drivers/github.html#attr-%3Cgithub%20connection%3E.canonical_hostname
type: string
name:
description: How the connection will be named in Zuul's
configuration and appear in zuul-web
type: string
server:
description: https://zuul-ci.org/docs/zuul/latest/drivers/github.html#attr-%3Cgithub%20connection%3E.server
type: string
verifySsl:
default: true
description: https://zuul-ci.org/docs/zuul/latest/drivers/github.html#attr-%3Cgithub%20connection%3E.verify_ssl
type: boolean
webHookToken:
description: https://zuul-ci.org/docs/zuul/latest/drivers/github.html#attr-%3Cgithub%20connection%3E.webhook_token
type: string
required:
- apiToken
- appId
- appKey
- name
type: object
type: array
merger:
description: Configuration of the merger microservice
properties:
Expand Down
7 changes: 6 additions & 1 deletion controllers/git_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ var preInitScriptTemplate string
func makeZuulConnectionConfig(spec *sfv1.ZuulSpec) string {
var sb strings.Builder
sb.WriteString("\n")
for _, name := range sfv1.GetConnectionsName(spec) {
for _, name := range sfv1.GetGerritConnectionsName(spec) {
sb.WriteString(fmt.Sprintf("[connection %s]\n", name))
sb.WriteString("driver=git\n")
sb.WriteString("baseurl=localhost\n\n")
}
for _, name := range sfv1.GetGitHubConnectionsName(spec) {
sb.WriteString(fmt.Sprintf("[connection %s]\n", name))
sb.WriteString("driver=git\n")
sb.WriteString("baseurl=localhost\n\n")
Expand Down
30 changes: 30 additions & 0 deletions controllers/zuul.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,31 @@ func (r *SFController) AddGerritConnection(cfg *ini.File, conn sfv1.GerritConnec
cfg.Section(section).NewKey("git_over_ssh", strconv.FormatBool(conn.GitOverSSH))
}

func (r *SFController) AddGitHubConnection(cfg *ini.File, conn sfv1.GitHubConnection) {
section := "connection " + conn.Name
cfg.NewSection(section)

addKey := func(fieldKey string, fieldValue string) {
if fieldValue != "" {
cfg.Section(section).NewKey(fieldKey, fieldValue)
}
}
for key, value := range map[string]string{
"driver": "github",
"app_id": conn.AppID,
"app_key": conn.AppKey,
"app_token": conn.APIToken,
"webhook_token": conn.WebhookToken,
"sshkey": "/var/lib/zuul-ssh/..data/priv",
"server": conn.Server,
"canonical_hostname": conn.Canonicalhostname,
"verify_ssl": fmt.Sprint(conn.VerifySSL),
} {
addKey(key, value)
}

}

func AddGitConnection(cfg *ini.File, name string, baseurl string) {
section := "connection " + name
cfg.NewSection(section)
Expand Down Expand Up @@ -751,6 +776,11 @@ func (r *SFController) DeployZuul() bool {
for _, conn := range r.cr.Spec.Zuul.GerritConns {
r.AddGerritConnection(cfgINI, conn)
}

for _, conn := range r.cr.Spec.Zuul.GitHubConns {
r.AddGitHubConnection(cfgINI, conn)
}

// Add default connections
r.AddDefaultConnections(cfgINI)

Expand Down
28 changes: 22 additions & 6 deletions roles/health-check/zuul-connections/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
---
- name: Setting Dummy Connections
set_fact:
dummy_gerritconn:
- name: dummy-gerrit-conn
hostname: dummy-gerrit.local
username: zuul
dummy_githubconns:
- name: dummy-github-conn
appId: githubId
appKey: githubKey
apiToken: githubToken
webHookToken: githubwebtoken

- name: Get current Zuul gerrit connections
command: kubectl get sf my-sf -o jsonpath='{.spec.zuul.gerritconns}'
register: gerritconns

- set_fact:
gerritconns_orig: "{{ gerritconns.stdout | from_json }}"
dummy_gerritconn:
- name: dummy-gerrit-conn
hostname: dummy-gerrit.local
username: zuul

- name: Add a dummy Zuul Gerrit connection
ansible.builtin.include_role:
Expand All @@ -17,6 +26,7 @@
cr_spec:
zuul:
gerritconns: "{{ gerritconns_orig + dummy_gerritconn }}"
githubconns: "{{ dummy_githubconns }}"

- name: Wait for the new Zuul connection to appear in the Zuul API
ansible.builtin.uri:
Expand All @@ -25,7 +35,9 @@
return_content: true
validate_certs: "{{ validate_certs }}"
register: this
until: "'dummy-gerrit-conn' in this.content"
until:
- "'dummy-gerrit-conn' in this.content"
- "'dummy-github-conn' in this.content"
retries: "{{ retries }}"
delay: "{{ delay }}"

Expand All @@ -36,6 +48,7 @@
cr_spec:
zuul:
gerritconns: "{{ gerritconns_orig }}"
githubconns: []

- name: Wait for the dummy Zuul connection to be removed from the API
ansible.builtin.uri:
Expand All @@ -44,6 +57,9 @@
return_content: true
validate_certs: "{{ validate_certs }}"
register: this
until: "'dummy-gerrit-conn' not in this.content"
until:
- "'dummy-gerrit-conn' not in this.content"
- "'dummy-github-conn' not in this.content"
retries: "{{ retries }}"
delay: "{{ delay }}"

0 comments on commit a95872d

Please sign in to comment.