Skip to content

Commit

Permalink
Fixes ansible-collections#5907: gitlab_runner is not idempotent on fi…
Browse files Browse the repository at this point in the history
…rst run after runner creation

This fix introduces the new boolean option 'access_level_on_creation'. It controls, whether the value of 'access_level' is used for runner registration or not. The option 'access_level' has been ignored on registration so far and was only used on updates. The user is informed by a deprecation warning, if the option is unspecified. For reasons of compatibility 'false' is assumed in that case. The option 'access_level_on_creation' will switch to 'true' for the next major release (community.general 7.0.0)

Signed-off-by: Christoph Fiehe <c.fiehe@eurodata.de>
  • Loading branch information
Christoph Fiehe committed Jan 29, 2023
1 parent 8818a6f commit 82d899d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- gitlab_runner - add new boolean option ``access_level_on_creation``. It controls, whether the value of ``access_level`` is used for runner registration or not. The option ``access_level`` has been ignored on registration so far and was only used on updates (https://github.com/ansible-collections/community.general/issues/5907).
49 changes: 32 additions & 17 deletions plugins/modules/gitlab_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@
default: ref_protected
choices: ["ref_protected", "not_protected"]
type: str
access_level_on_creation:
description:
- Whether the runner should be registered with an access level or not.
- If set to C(true), the value of I(access_level) is used for runner registration.
- If set to C(false), GitLab registers the runner with the default access level.
required: false
type: bool
maximum_timeout:
description:
- The maximum time that a runner has to complete a specific job.
Expand Down Expand Up @@ -207,27 +214,34 @@ def __init__(self, module, gitlab_instance, project=None):
def create_or_update_runner(self, description, options):
changed = False

arguments = {
'active': options['active'],
'locked': options['locked'],
'run_untagged': options['run_untagged'],
'maximum_timeout': options['maximum_timeout'],
'tag_list': options['tag_list'],
}
# Because we have already call userExists in main()
if self.runner_object is None:
runner = self.create_runner({
'description': description,
'active': options['active'],
'token': options['registration_token'],
'locked': options['locked'],
'run_untagged': options['run_untagged'],
'maximum_timeout': options['maximum_timeout'],
'tag_list': options['tag_list'],
})
arguments['description'] = description
arguments['token'] = options['registration_token']

access_level_on_creation = self._module.params['access_level_on_creation']
if access_level_on_creation is None:
message = "The option 'access_level_on_creation' is unspecified, so 'false' is assumed. "\
"That means any value of 'access_level' is ignored and GitLab registers the runner with its default value. "\
"The option 'access_level_on_creation' will switch to 'true' for the next major release (community.general 7.0.0)."
self._module.warn(message)
access_level_on_creation = False

if access_level_on_creation:
arguments['access_level'] = options['access_level']

runner = self.create_runner(arguments)
changed = True
else:
changed, runner = self.update_runner(self.runner_object, {
'active': options['active'],
'locked': options['locked'],
'run_untagged': options['run_untagged'],
'maximum_timeout': options['maximum_timeout'],
'access_level': options['access_level'],
'tag_list': options['tag_list'],
})
arguments['access_level'] = options['access_level']
changed, runner = self.update_runner(self.runner_object, arguments)

self.runner_object = runner
if changed:
Expand Down Expand Up @@ -328,6 +342,7 @@ def main():
run_untagged=dict(type='bool', default=True),
locked=dict(type='bool', default=False),
access_level=dict(type='str', default='ref_protected', choices=["not_protected", "ref_protected"]),
access_level_on_creation=dict(type='bool'),
maximum_timeout=dict(type='int', default=3600),
registration_token=dict(type='str', no_log=True),
project=dict(type='str'),
Expand Down

0 comments on commit 82d899d

Please sign in to comment.