Skip to content

Incorrect KAS address when running GitLab on a non-standard port

Summary

With external_url set to e.g. https://example.com:8443/gitlab/, attempts to connect a cluster agent tell the user to install their agent with a kasAddress of wss://example.org:/gitlab/-/kubernetes-agent/. Note the lone : without a port number after the host name.

Steps to reproduce

  • run GitLab on a non-standard port
  • connect a cluster agent

What is the current bug behavior?

The popup tells the user to deploy an agent to the cluster with

helm upgrade --install easier gitlab/gitlab-agent \
    --namespace gitlab-agent-easier \
    --create-namespace \
    --set image.tag=v16.8.0-rc1 \
    --set config.token=glagent-[REDACTED] \
    --set config.kasAddress=wss://example.com:/gitlab/-/kubernetes-agent/

What is the expected correct behavior?

The popup tells the user to deploy an agent to the cluster with

helm upgrade --install easier gitlab/gitlab-agent \
    --namespace gitlab-agent-easier \
    --create-namespace \
    --set image.tag=v16.8.0-rc1 \
    --set config.token=glagent-[REDACTED] \
    --set config.kasAddress=wss://example.com:8443/gitlab/-/kubernetes-agent/

Relevant logs

N/A

Details of package version

Running the gitlab/gitlab-ce:16.7.0-ce.0 Docker image.

Environment details

Running the gitlab/gitlab-ce:16.7.0-ce.0 Docker image.

Configuration details

Using GITLAB_OMNIBUS_CONFIG to specify my configuration, /etc/gitlab/gitlab.rb is a copy of the template.

My GITLAB_OMNIBUS_CONFIG has

external_url "https://example.com:8443/gitlab"

It does not set gitlab_kas_external_url.

Work-around

Add the following to GITLAB_OMNIBUS_CONFIG

gitlab_kas_external_url "wss://example.com:8443/gitlab/-/kubernetes-agent/"

Fix

diff --git a/files/gitlab-cookbooks/gitlab-kas/libraries/gitlab_kas.rb b/files/gitlab-cookbooks/gitlab-kas/librar
ies/gitlab_kas.rb
index 029da009d..e840f1202 100644
--- a/files/gitlab-cookbooks/gitlab-kas/libraries/gitlab_kas.rb
+++ b/files/gitlab-cookbooks/gitlab-kas/libraries/gitlab_kas.rb
@@ -209,10 +209,10 @@ module GitlabKas
       case gitlab_uri.scheme
       when 'https'
         scheme = gitlab_kas_attr('listen_websocket') ? 'wss' : 'grpcs'
-        port = gitlab_uri.port == 443 ? '' : ":#{port}"
+        port = gitlab_uri.port == 443 ? '' : ":#{gitlab_uri.port}"
       when 'http'
         scheme = gitlab_kas_attr('listen_websocket') ? 'ws' : 'grpc'
-        port = gitlab_uri.port == 80 ? '' : ":#{port}"
+        port = gitlab_uri.port == 80 ? '' : ":#{gitlab_uri.port}"
       else
         raise 

I am not quite sure I like the fact that gitlab_uri.port is called four times. Maybe it's better to set port = gitlab_uri.port outside the case and replace all gitlab_uri.port instance in the case-scope with port.

Let me know which coding style is preferred and I'll prepare a merge request.