Skip to content

Commit

Permalink
Bugfix: Allow retries on existing resources
Browse files Browse the repository at this point in the history
This PR fixes a bug where Ansible was retrying resources with a cached resourceVersion, so if the resourceVersion changed it would fall into a failure loop of not applying against the latest resourceVersion.

Now, Ansible will be using the latest resourceVersion when applying a resource.
  • Loading branch information
Tunguska55 committed Oct 10, 2023
1 parent bc69137 commit cf30d71
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions library/openshift_provision.py
Original file line number Diff line number Diff line change
Expand Up @@ -1386,30 +1386,19 @@ def get_resource_version_and_last_applied_configuration(self, resource):

metadata = resource.get('metadata', {})
resource_version = metadata \
.get('resourceVersion', None)
.pop('resourceVersion', None)
last_applied_configuration = metadata \
.get('annotations', {}) \
.get('kubectl.kubernetes.io/last-applied-configuration', None)
.pop('kubectl.kubernetes.io/last-applied-configuration', None)
return resource_version, last_applied_configuration

def set_resource_version_and_last_applied_configuration(self, resource_version, last_applied_configuration):
if not resource_version or not last_applied_configuration:
return
merge_dict(self.resource, {
'metadata': {
'annotations': {
'kubectl.kubernetes.io/last-applied-configuration': last_applied_configuration
},
'resourceVersion': resource_version
}
}, overwrite=True)

def provision(self):
current_resource = self.get_current_resource()
current_resource_version, current_last_applied_configuration = \
self.get_resource_version_and_last_applied_configuration(current_resource)
if current_resource and self.action in ['apply', 'replace']:
self.set_dynamic_values(current_resource)
self.current_resource_version = current_resource_version

# Check if changes are required and if we need to reset the apply metadata.
reset_last_applied_configuration = False
Expand Down Expand Up @@ -1495,17 +1484,22 @@ def provision(self):
command += ['-n', self.namespace]
self.run_oc(command, data=json.dumps(self.resource), check_rc=True)
else: # apply, create, delete, replace
resource = copy.deepcopy(self.resource)
if self.action == 'apply':
self.set_resource_version_and_last_applied_configuration(
current_resource_version,
current_last_applied_configuration
)
merge_dict(resource, {
'metadata': {
'annotations': {
'kubectl.kubernetes.io/last-applied-configuration': current_last_applied_configuration
},
'resourceVersion': current_resource_version
}
})
command = [self.action, '-f', '-']
if self.namespace:
command += ['-n', self.namespace]
if reset_last_applied_configuration:
command += ['--save-config']
self.run_oc(command, data=json.dumps(self.resource), check_rc=True)
self.run_oc(command, data=json.dumps(resource), check_rc=True)

def run_module():
module_args = {
Expand Down Expand Up @@ -1559,14 +1553,16 @@ def run_module():
msg=str(e),
action=provisioner.action,
traceback=traceback.format_exc().split('\n'),
resource=provisioner.resource
resource=provisioner.resource,
current_resource_version=provisioner.current_resource_version
)

module.exit_json(
action=provisioner.action,
changed=provisioner.changed,
patch=provisioner.patch,
resource=provisioner.resource
resource=provisioner.resource,
current_resource_version=provisioner.current_resource_version
)

def main():
Expand Down

0 comments on commit cf30d71

Please sign in to comment.