Skip to content

Expose Consul api_url in gitlab.yml

What we need:

In issue gitlab#222343 (closed), we need to know consul['api_url']. For example, once we know consul['api_url'] = http://127.0.0.1:8500, we can list the nodes for prometheus service by name:

# curl http://127.0.0.1:8500/v1/catalog/service/prometheus

[{"ID":"2ec8de15-f96a-8eb5-e800-d912f83ec714","Node":"gitlab_4.example.com","Address":"172.17.0.6","Datacenter":"gitlab_consul","TaggedAddresses":{"lan":"172.17.0.6","wan":"172.17.0.6"},"NodeMeta":{"consul-network-segment":""},"ServiceKind":"","ServiceID":"prometheus","ServiceName":"prometheus","ServiceTags":[],"ServiceAddress":"172.17.0.6","ServiceWeights":{"Passing":1,"Warning":1},"ServiceMeta":{},"ServicePort":9090,"ServiceEnableTagOverride":false,"ServiceProxy":{"MeshGateway":{}},"ServiceConnect":{},"CreateIndex":85343,"ModifyIndex":85343}]

What we have today:

We allow users to config Consul in /etc/gitlab/gitlab.rb https://gitlab.com/gitlab-org/omnibus-gitlab/-/blob/master/files/gitlab-config-template/gitlab.rb.template#L2460-2505.

User could set the HTTP(s) address/port as:

 consul['configuration'] = {
   'client_addr' => nil, # default to 127.0.0.1
   'addresses' => {
      'http' => '192.168.0.1',  # default to `client_addr`
      'https' => '10.1.2.3',  # default to `client_addr`
   },
   'ports' => {
      'http' => -1, # default 8500
      'https' => 8501, # default -1
   },
   'datacenter' => 'gitlab_consul',
   'enable_script_checks' => true,
   'server' => false
 }

Refer to consul documentation to see how to config addresses and ports

The consul['configuration'] is deep merged with default values, and write the result JSON to the config file(for example /var/opt/gitlab/consul/config.json). https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/files/gitlab-cookbooks/consul/libraries/consul_helper.rb#L36-47

The config.json is used by consul agent in https://gitlab.com/gitlab-org/omnibus-gitlab/-/blob/master/files/gitlab-cookbooks/consul/templates/default/sv-consul-run.erb#L12-13

But no Consul configurations are available in gitlab.yml yet.

The proposed solution:

We do not need to expose the raw data of the consul configuration. Omnibus can calculate the api_url based on the raw data. Then it exposes the consul['api_url'] setting to gitlab.yml, so Rails app can know the api_url through Gitlab.config.consul.

Here is the related slack discussion.

Edited by Qingyu Zhao