Page MenuHomePhabricator

Migrate to using new GitLab CI runner authentication scheme
Closed, ResolvedPublic

Description

GitLab has deprecated the use of registration tokens with gitlab-runner register. See https://gitlab.com/gitlab-org/gitlab/-/issues/380872

We'll need to refactor the registration command in our puppet module and upgrade gitlab-cloud-runner to use newer runner helm chart.

Estimated time frame for planned upstream changes

    In GitLab 15.10 and later, you can use the new runner registration workflow.
    In GitLab 17.0, we plan to disable runner registration tokens.
    In GitLab 18.0, we plan to completely remove support for runner registration tokens.

Relevant documentation:

Register with a runner authentication token
Migrating to the new runner registration workflow
Creating runners programmatically
Reusing a runner configuration

Runners registered with the same authentication token and different system_id values become grouped under a single runner. Grouped runners can be reused to run different jobs by multiple runner managers.

Installing GitLab Runner with Helm chart

In the legacy runner registration workflow, fields were specified with runner-registration-token, In the new runner registration workflow, you must use runner-token instead:

Automate runner installation and registration

Todos

Details

Related Changes in Gerrit:
Related Changes in GitLab:
TitleReferenceAuthorSource BranchDest Branch
Restore cluster wide gitlab runner job concurrency of 40repos/releng/gitlab-cloud-runner!302dduvallreview/double-runner-concurrencymain
Change default value of replicas from 2 to 1repos/releng/gitlab-cloud-runner!301dancymain-Ib026fbbc5a26c85e1c4b5d00a06cfa19b4adeabamain
gitlab_runner: use new registration workflowrepos/releng/gitlab-cloud-runner!298jeltogitlab-runner-use-new-registrationmain
improve output of generate-tokens commandrepos/releng/gitlab-settings!48jeltorunner-token-improve-outputmain
add runners generate-tokens commandrepos/releng/gitlab-settings!47jeltorunner-generate-tokensmain
Customize query in GitLab

Event Timeline

There are a very large number of changes, so older changes are hidden. Show Older Changes
Jelto subscribed.

SRE can take care of refactoring the puppet code. Thanks for bringing that up!

LSobanski moved this task from Incoming to Backlog on the Collaboration-Services board.

I've done some tests and registered two new runners (two vms in Digital Ocean) to the GitLab test instance. I could verify the Reusing a runner configuration docs. I was able to register two runners with one token.

However there are some significant changes compared to the old workflow:

  • the configuration (tags/untagged changes, trusted branches, description) is linked to the token, not the registration command
  • the two runners appear as one "grouped" runner in the UI with arbitrary names in the drop down menu, which makes individual maintenance (pausing, unpausing) and troubleshooting (lookup of jobs) harder
  • it's not obvious which runner in a group executed the job. The information can (only?) be found in the job output.

gitlab-runner-group-ui.png (1,296×856 px, 99 KB)

Grouping runners is quite different from how we use runners at the moment (except the Kubernetes Runner). So some changes to our cookbooks and dashboards are needed most probably.

With the approach of using the same token for multiple runners we would need three different tokens for Trusted, Shared and Cloud Runners. This tokens could be created by hand and added to private puppet. But we would have to make sure to use the proper settings (tags, untagged, protected) when creating the tokens. So most probably we want some kind of cookbook or script for that.

If we have a script we could think about generating individual tokens for every runner programmatically. It should not be a big difference if we create 3 or 30 tokens. This would create quite similar user experience and maintenance, as we don't group runners. However the tokens would have to be added to private puppet as well, which is quite annoying (also with a token ttl of one year).

I'll do a bit more digging and see if there is another solution, also by looking at the runner api.

Currently the python GitLab api client does not support the new runner creation API in version 3.15.0. Support was added in https://github.com/python-gitlab/python-gitlab/releases/tag/v4.0.0, which was released last week. So we have to wait a bit until that's available in pip.

Change 968988 had a related patch set uploaded (by Jelto; author: Jelto):

[operations/puppet@production] gitlab_runner: Migrate to new runner registration scheme

https://gerrit.wikimedia.org/r/968988

Change 968996 had a related patch set uploaded (by Jelto; author: Jelto):

[labs/private@master] gitlab_runner: add token for new authentication scheme

https://gerrit.wikimedia.org/r/968996

Change 968996 merged by Jelto:

[labs/private@master] gitlab_runner: add token for new authentication scheme

https://gerrit.wikimedia.org/r/968996

There is a script in gitlab-settings which can be used to create the tokens for runner registration with the proper settings. I used the script to create new tokens for all runners and added them to the according private puppet repos.

Currently the python GitLab api client does not support the new runner creation API in version 3.15.0. Support was added in https://github.com/python-gitlab/python-gitlab/releases/tag/v4.0.0, which was released last week. So we have to wait a bit until that's available in pip.

python-gitlab v4.0.0 is available in pip now.

The next steps are getting some review of https://gerrit.wikimedia.org/r/c/operations/puppet/+/968988 and create a plan on how to properly migrate the runners to the new registration workflow (meaning unregister, clean configs, register again, validate).

@dancy @dduvall I noticed the cloud runners in Digital Ocean use a single registration token for the default and memory optimized Kubernetes Runners. Unfortunately this will no longer work with the new registration workflow and the code for deploying this runners probably needs some adjusting (like to dedicated deploys or merging of both runners). This is not urgent, as they will keep working in GitLab version 17. But we have to find a solution there as well.
I added the new tokens as TF_VAR_runner_token and TF_VAR_runner_token_mem_optimized to the gitlab-cloud-runner ci variables for now.

@dancy @dduvall I noticed the cloud runners in Digital Ocean use a single registration token for the default and memory optimized Kubernetes Runners. Unfortunately this will no longer work with the new registration workflow and the code for deploying this runners probably needs some adjusting (like to dedicated deploys or merging of both runners). This is not urgent, as they will keep working in GitLab version 17. But we have to find a solution there as well.
I added the new tokens as TF_VAR_runner_token and TF_VAR_runner_token_mem_optimized to the gitlab-cloud-runner ci variables for now.

Thanks! I'll work on the migration.

In https://gitlab.wikimedia.org/repos/releng/gitlab-settings/-/merge_requests/49 tokens for the staging cloud runners were added. I saved the tokens in the CI variables: TF_VAR_runner_token_staging and TF_VAR_runner_token_staging_mem_optimized.

I also rotated the new tokens one more time to make sure they are created by the system user (root) and not linked to my personal account.

Thanks! I'll work on the migration.

Thanks!

A rough checklist for the actual migration of a runner:

  • merge puppet code to use the new tokens and new workflow https://gerrit.wikimedia.org/r/c/operations/puppet/+/968988
  • set profile::gitlab::runner::ensure to absent for a specific runner to trigger a de-registration
  • run-puppet-agent on the specific runner
  • make sure gitlab-runner is de-registered
  • check if gitlab-runner config directories needs a manual cleanup (/etc/gitlab-runner/.*toml or /home/gitlab-runner/.gitlab-runner/.*toml files)
  • set profile::gitlab::runner::ensure to present for a specific runner
  • run-puppet-agent on the specific runner
  • make sure gitlab-runner registered properly (puppet-agent logs and in https://gitlab.wikimedia.org/admin/runners)

I'll try that first with the test instance runners in devtools. After that I'll repeat the checklist for one Shared WMCS Runner and one Trusted Runner. Then we can monitor the behavior of the new Runners and if they work as expected migrate the remaining ones.

Change 968988 merged by Jelto:

[operations/puppet@production] gitlab_runner: Migrate to new runner registration scheme

https://gerrit.wikimedia.org/r/968988

Change 973133 had a related patch set uploaded (by Jelto; author: Jelto):

[operations/puppet@production] gitlab_runner: move profile::gitlab::runner::token to private/labs

https://gerrit.wikimedia.org/r/973133

Change 973133 merged by Jelto:

[operations/puppet@production] gitlab_runner: move profile::gitlab::runner::token to private/labs

https://gerrit.wikimedia.org/r/973133

I migrated the two devtools test runners in WMCS to the new authentication scheme. The steps were:

  • set profile::gitlab::runner::ensure to absent for a specific runner to trigger a de-registration
  • run-puppet-agent on the specific runner
  • make sure gitlab-runner is de-registered
  • ~check if gitlab-runner config directories needs a manual cleanup (/etc/gitlab-runner/.*toml or /home/gitlab-runner/.gitlab-runner/.*toml files)~ not needed
  • set profile::gitlab::runner::ensure to present for a specific runner
  • run-puppet-agent on the specific runner
  • make sure gitlab-runner registered properly (puppet-agent logs and in https://gitlab.wikimedia.org/admin/runners)

I'll do some more verification. After that I'll convert one Shared Runner and one Trusted Runner to the new authentication scheme.

Unregistering and re-registering runners with the new authentication scheme still works and settings (like protected, tags) look good.

Tomorrow I'll migrate one of the Trusted and one of the Shared runners to the new authentication scheme.

Change 973290 had a related patch set uploaded (by Jelto; author: Jelto):

[operations/puppet@production] gitlab_runner: unregister gitlab-runner1002

https://gerrit.wikimedia.org/r/973290

Change 973290 merged by Jelto:

[operations/puppet@production] gitlab_runner: unregister gitlab-runner1002

https://gerrit.wikimedia.org/r/973290

Change 973255 had a related patch set uploaded (by Jelto; author: Jelto):

[operations/puppet@production] Revert "gitlab_runner: unregister gitlab-runner1002"

https://gerrit.wikimedia.org/r/973255

Change 973255 merged by Jelto:

[operations/puppet@production] Revert "gitlab_runner: unregister gitlab-runner1002"

https://gerrit.wikimedia.org/r/973255

One Shared Runner runner-1021.gitlab-runners.eqiad1.wikimedia.cloud and one Trusted Runner gitlab-runner1002.eqiad.wmnet were migrated to the new authentication scheme. I'll leave them running and migrate the other Runners next week, if everything looks good.

We discussed if cookbook modifications are needed. The sre.gitlab.reboot-runner cookbook pauses the runner by running /usr/bin/systemctl kill -s 3 gitlab-runner.service (see https://gerrit.wikimedia.org/r/plugins/gitiles/operations/cookbooks/+/refs/heads/master/cookbooks/sre/gitlab/reboot-runner.py#40). This should still work, as we are not using the API and this should not be affected by the new api-level grouping of runners.

The sre.gitlab.failover and sre.gitlab.upgrade cookbooks also pause runners. However they pause all available runners (see https://gerrit.wikimedia.org/r/plugins/gitiles/operations/cookbooks/+/refs/heads/master/cookbooks/sre/gitlab/__init__.py#41). This should still work. Pausing individual runners or groups of runners should be similar API-wise. However I'll do some cookbook runs after we migrated to verify they still work.

The converted Shared Runner (https://gitlab.wikimedia.org/admin/runners/1479#/) and Trusted Runner (https://gitlab.wikimedia.org/admin/runners/1484#/) behave as expected and executed jobs successfully.

Tomorrow I'll migrate additional Runners to the new registration workflow.

I added a new chapter to https://wikitech.wikimedia.org/wiki/GitLab/Gitlab_Runner#Adding_new_runners_(administrators) with a description how registration of runners works (for end-users and admins).

Change 974116 had a related patch set uploaded (by Jelto; author: Jelto):

[operations/puppet@production] gitlab_runner: unregister gitlab-runner1003

https://gerrit.wikimedia.org/r/974116

Change 974116 merged by Jelto:

[operations/puppet@production] gitlab_runner: unregister gitlab-runner1003

https://gerrit.wikimedia.org/r/974116

Change 974129 had a related patch set uploaded (by Jelto; author: Jelto):

[operations/puppet@production] Revert "gitlab_runner: unregister gitlab-runner1003"

https://gerrit.wikimedia.org/r/974129

Change 974129 merged by Jelto:

[operations/puppet@production] Revert "gitlab_runner: unregister gitlab-runner1003"

https://gerrit.wikimedia.org/r/974129

Change 974123 had a related patch set uploaded (by Jelto; author: Jelto):

[operations/puppet@production] gitlab_runner: unregister gitlab-runner1004

https://gerrit.wikimedia.org/r/974123

Change 974123 merged by Jelto:

[operations/puppet@production] gitlab_runner: unregister gitlab-runner1004

https://gerrit.wikimedia.org/r/974123

Change 974134 had a related patch set uploaded (by Jelto; author: Jelto):

[operations/puppet@production] Revert "gitlab_runner: unregister gitlab-runner1004"

https://gerrit.wikimedia.org/r/974134

Change 974134 merged by Jelto:

[operations/puppet@production] Revert "gitlab_runner: unregister gitlab-runner1004"

https://gerrit.wikimedia.org/r/974134

Change 974155 had a related patch set uploaded (by Jelto; author: Jelto):

[operations/puppet@production] gitlab_runner: unregister gitlab-runners in codfw

https://gerrit.wikimedia.org/r/974155

Change 974155 merged by Jelto:

[operations/puppet@production] gitlab_runner: unregister gitlab-runners in codfw

https://gerrit.wikimedia.org/r/974155

Change 974135 had a related patch set uploaded (by Jelto; author: Jelto):

[operations/puppet@production] Revert "gitlab_runner: unregister gitlab-runners in codfw"

https://gerrit.wikimedia.org/r/974135

Change 974135 merged by Jelto:

[operations/puppet@production] Revert "gitlab_runner: unregister gitlab-runners in codfw"

https://gerrit.wikimedia.org/r/974135

Trusted Runners were migrated to the new authentication scheme.

I started converting additional WMCS Shared Runners but there is an issue with their UUID. gitlab-runner automagically creates a runner_system_id which is used in GitLab to distinguish the runners. It seems all Shared Runners in WMCS have the same system id in /etc/gitlab-runner/.runner_system_id. This leads to only one Shared Runner in the new Shared Runner group.

All WMCS instances share the same linux system ID /etc/machine-id and gitlab-runner uses this id as well. I paused the new Shared Runner for now until I figured out how to modify the system id or force gitlab-runner to use another ID.

I migrated all WMCS Shared Runners to the new authentication scheme. It was necessary to unregister the runner, clear the machine-id and generate a new one:

sudo rm /etc/machine-id 
sudo rm /var/lib/dbus/machine-id
sudo rm /etc/gitlab-runner/.runner_system_id # probably not needed, just to be sure
sudo dbus-uuidgen --ensure
sudo systemd-machine-id-setup

All runners have a unique machine-id and runner_system_id now: https://gitlab.wikimedia.org/admin/runners/1479#/
I'll open a task for WMCS folks to raise the issue with the duplicate machine-id in cloud.

The last runners on the old authentication scheme are the Digital Ocean Cloud Runners. The required code changes are here in review: https://gitlab.wikimedia.org/repos/releng/gitlab-cloud-runner/-/merge_requests/298

Cloud Runners have been migrated to the new authentication scheme. Thanks @dancy for the help deploying and testing the new runners!

Cookbook cookbooks.sre.gitlab.upgrade was started by jelto@cumin1001 on GitLab host gitlab1003.wikimedia.org with reason: Test Upgrade GitLab Replica gitlab1003 with new runners

Cookbook cookbooks.sre.gitlab.upgrade started by jelto@cumin1001 on GitLab host gitlab1003.wikimedia.org with reason: Test Upgrade GitLab Replica gitlab1003 with new runners completed successfully on GitLab host gitlab1003.wikimedia.org with reason: Test Upgrade GitLab Replica gitlab1003 with new runners

Cookbook cookbooks.sre.hosts.reimage was started by jelto@cumin1001 for host gitlab-runner2004.codfw.wmnet with OS bullseye

Cookbook cookbooks.sre.hosts.reimage started by jelto@cumin1001 for host gitlab-runner2004.codfw.wmnet with OS bullseye completed:

  • gitlab-runner2004 (WARN)
    • Downtimed on Icinga/Alertmanager
    • Disabled Puppet
    • Removed from Puppet and PuppetDB if present and deleted any certificates
    • Removed from Debmonitor if present
    • Forced PXE for next reboot
    • Host rebooted via IPMI
    • Host up (Debian installer)
    • Add puppet_version metadata to Debian installer
    • Checked BIOS boot parameters are back to normal
    • Host up (new fresh bullseye OS)
    • Generated Puppet certificate
    • Signed new Puppet certificate
    • Run Puppet in NOOP mode to populate exported resources in PuppetDB
    • Found Nagios_host resource for this host in PuppetDB
    • Downtimed the new host on Icinga/Alertmanager
    • Removed previous downtime on Alertmanager (old OS)
    • First Puppet run completed and logged in /var/log/spicerack/sre/hosts/reimage/202311200918_jelto_4168668_gitlab-runner2004.out
    • Unable to run puppet on config-master2001.codfw.wmnet,config-master1001.eqiad.wmnet to update configmaster.wikimedia.org with the new host SSH public key for wmf-update-known-hosts-production
    • Rebooted
    • Automatic Puppet run was successful
    • Forced a re-check of all Icinga services for the host
    • Icinga status is optimal
    • Icinga downtime removed
    • Updated Netbox data from PuppetDB

Reimage of a Trusted Runner worked, the Runner is available again after the reimage in GitLab.

However the old Runner is not deleted properly. The old Runner (system_id before the reimage) remains as offline in the Trusted Runners group.

I have to go over the API and see if there is a way to cleanup the old Runner from the group without deleting the whole group.

There is no proper way of deleting the old reimaged runner beside deleting the row from ci_runner_machines table. There is no API endpoint for deleting a single grouped runner. We could have used the gitlab-runner unregister command, but that needs the old authentication token. So I'll leave the stale Trusted Runner in the group of Runners for now. If it causes any issues, we can test dropping the row in postgres or extracting it's authentication token from the database.

The old profile::gitlab::runner::registration_token were removed from private puppet and the two CI variables TF_VAR_runner_registration_token and RUNNER_REGISTRATION_TOKEN_STAGING were removed from https://gitlab.wikimedia.org/repos/releng/gitlab-cloud-runner/-/settings/ci_cd.

I might open a upstream issue for deleting individual grouped runners by machine_id. But for now I'll resolve this task. If anything comes up feel free to reopen.