From a95872d6d1b33a36589dbcd63fad3dfc968c0085 Mon Sep 17 00:00:00 2001 From: fserucas Date: Thu, 21 Sep 2023 20:35:14 +0100 Subject: [PATCH] Add GitHub connection support to Zuul connections Change-Id: If2cf66977aff55af7df8fc884c871d3c14244eda --- api/v1/softwarefactory_types.go | 37 ++++++++++++++++- api/v1/zz_generated.deepcopy.go | 20 ++++++++++ ...efactory-project.io_softwarefactories.yaml | 40 +++++++++++++++++++ controllers/git_server.go | 7 +++- controllers/zuul.go | 30 ++++++++++++++ .../zuul-connections/tasks/main.yaml | 28 ++++++++++--- 6 files changed, 154 insertions(+), 8 deletions(-) diff --git a/api/v1/softwarefactory_types.go b/api/v1/softwarefactory_types.go index 6eb9cc91..95b644a2 100644 --- a/api/v1/softwarefactory_types.go +++ b/api/v1/softwarefactory_types.go @@ -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 @@ -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 @@ -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") @@ -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 diff --git a/api/v1/zz_generated.deepcopy.go b/api/v1/zz_generated.deepcopy.go index 494726cd..3db7f382 100644 --- a/api/v1/zz_generated.deepcopy.go +++ b/api/v1/zz_generated.deepcopy.go @@ -58,6 +58,21 @@ func (in *GerritConnection) DeepCopy() *GerritConnection { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *GitHubConnection) DeepCopyInto(out *GitHubConnection) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GitHubConnection. +func (in *GitHubConnection) DeepCopy() *GitHubConnection { + if in == nil { + return nil + } + out := new(GitHubConnection) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *GitServerSpec) DeepCopyInto(out *GitServerSpec) { *out = *in @@ -519,6 +534,11 @@ func (in *ZuulSpec) DeepCopyInto(out *ZuulSpec) { *out = make([]GerritConnection, len(*in)) copy(*out, *in) } + if in.GitHubConns != nil { + in, out := &in.GitHubConns, &out.GitHubConns + *out = make([]GitHubConnection, len(*in)) + copy(*out, *in) + } in.Executor.DeepCopyInto(&out.Executor) in.Scheduler.DeepCopyInto(&out.Scheduler) out.Web = in.Web diff --git a/config/crd/bases/sf.softwarefactory-project.io_softwarefactories.yaml b/config/crd/bases/sf.softwarefactory-project.io_softwarefactories.yaml index b0133a14..0fc38fed 100644 --- a/config/crd/bases/sf.softwarefactory-project.io_softwarefactories.yaml +++ b/config/crd/bases/sf.softwarefactory-project.io_softwarefactories.yaml @@ -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: diff --git a/controllers/git_server.go b/controllers/git_server.go index d0171363..611cdd6a 100644 --- a/controllers/git_server.go +++ b/controllers/git_server.go @@ -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") diff --git a/controllers/zuul.go b/controllers/zuul.go index 591b3367..4d8c88d7 100644 --- a/controllers/zuul.go +++ b/controllers/zuul.go @@ -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) @@ -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) diff --git a/roles/health-check/zuul-connections/tasks/main.yaml b/roles/health-check/zuul-connections/tasks/main.yaml index 0bd54403..1627632a 100644 --- a/roles/health-check/zuul-connections/tasks/main.yaml +++ b/roles/health-check/zuul-connections/tasks/main.yaml @@ -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: @@ -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: @@ -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 }}" @@ -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: @@ -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 }}" +