Skip to content

Expose Consul api_url in gitlab.yml

Qingyu Zhao requested to merge 5567-expose-consul-config-in-gitlab-yml into master

What this MR does:

This MR exposes consul['api_url'] to gitlab.yml, so Rails app can get it through Gitlab.config.consul.

How Rails app uses the exposed config:

This can be used by Rails to call Consul HTTP API, such as service discovery. In our first usage scenario, we want to discover Prometheus service in gitlab!38060 (merged), for example, once we know consul['api_url'] = http://localhost:8500, we can list the nodes for prometheus service by name:

# curl http://localhost: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}]

How the value is calculated:

consul['api_url'] looks like http://10.1.2.3:8500, we need to know scheme, address and port.

To calculate the value, it uses these raw data from /etc/gitlab/gitlab.rb

  • consul['enable']
  • consul['configuration']['client_addr'] # if client_addr is set, it will overwrite default address(127.0.0.1)
  • consul['configuration']['addresses'] # if addresses is set, it will overwrite client_addr
    • consul['configuration']['addresses']['http']
    • consul['configuration']['addresses']['https']
  • consul['configuration']['ports'] # if ports is set, it will overwrite default ports
    • consul['configuration']['ports']['http']
    • consul['configuration']['ports']['https']

We do not need to expose the raw data of the user configuration. Rails application only cares consul['api_url']: if consul['api_url'] is not empty, the Rails app knows it can get information through this API interface.

The API scheme can be two types: http and https. The scheme is determined by checking ports value. By default, the ports values are:

  {
     ports: {
       http: 8500,
       https: -1
     }
  }

A negative port value means to disable the API scheme. For example, https port default value is -1, it means HTTPS communication is disabled. Refer to Consul documentation about ports

Closes #5567 (closed)

Edited by Qingyu Zhao

Merge request reports