Skip to content

Instantly share code, notes, and snippets.

@bomboclat
Last active October 8, 2024 19:38
Show Gist options
  • Save bomboclat/1c53b7e692b1df58af67598337cc2b88 to your computer and use it in GitHub Desktop.
Save bomboclat/1c53b7e692b1df58af67598337cc2b88 to your computer and use it in GitHub Desktop.
convert-deploymentconfig-to-deployment.py
#!/usr/bin/env python3
import sys
import re
import yaml
class YamlTransform():
def __init__(self, filename, keys_sub):
self.filename = filename
self.cbuffers = None
self.keys_sub = keys_sub
def transform(self):
try:
with open(self.filename, 'r') as file:
buffers = yaml.safe_load_all(file)
for buffer in buffers:
print('---')
print(yaml.dump(self._convert(buffer)))
except IOError as e:
print(help, file=sys.stderr)
print(e, file=sys.stderr)
exit(1)
def _convert(self, buffer):
for key in self.keys_sub:
path = key['path'].split('.')
path_len = len(path)
pointer = buffer
# Walks through the buffer tree and replaces the values in the "keys_sub" dictionary
for idx, leaf in enumerate(path, start=1):
if pointer.get(leaf) is None:
print('key: {} Not Found, skipping...'.format(key['path']), file=sys.stderr)
break
if idx == path_len:
if key.get('remove'):
pointer.pop(leaf)
else:
if key.get('subs'):
for sub in key['subs']:
pointer[leaf] = re.sub(sub[0],sub[1],pointer[leaf])
if key.get('move'):
new_path_pointer = pointer
new_path = key['move'].split('.')
# this loop stops before reaching the last element
for idy in range(len(new_path)-1):
new_path_pointer[new_path[idy]] = new_path_pointer.get(new_path[idy], {})
new_path_pointer = new_path_pointer[new_path[idy]]
# the last element gets the value of the pointer
new_path_pointer[new_path[-1]] = pointer.pop(leaf)
else:
pointer = pointer[leaf]
return buffer
if __name__ == "__main__":
help="Usage: {} [filename.yaml]".format(sys.argv[0])
keys_sub = [
{'path': 'apiVersion', 'subs': [['^apps.openshift.io/v1', 'apps/v1'],['^v1', 'apps/v1']]},
{'path': 'kind', 'subs': [['^DeploymentConfig', 'Deployment']]},
{'path': 'spec.strategy.type', 'subs': [['^Rolling', 'RollingUpdate']]},
{'path': 'spec.strategy.activeDeadlineSeconds', 'remove': True},
{'path': 'spec.strategy.resources', 'remove': True},
{'path': 'spec.strategy.rollingParams.intervalSeconds', 'remove': True},
{'path': 'spec.strategy.rollingParams.timeoutSeconds', 'remove': True},
{'path': 'spec.strategy.rollingParams.updatePeriodSeconds', 'remove': True},
{'path': 'spec.triggers', 'remove': True},
{'path': 'spec.test', 'remove': True},
{'path': 'spec.selector', 'move': 'selector.matchLabels'},
{'path': 'spec.strategy.rollingParams', 'move': 'rollingUpdate'},
]
if len(sys.argv) > 1:
filename = sys.argv[1]
else:
print(help, file=sys.stderr)
exit(1)
yaml_transform = YamlTransform(filename, keys_sub)
yaml_transform.transform()
@bomboclat
Copy link
Author

bomboclat commented Sep 29, 2023

Usage: python3 convert-deploymentconfig-to-deployment.py deploymentconfig.yaml > deployment.yaml

@domignat
Copy link

In the keys_sub 'matchLables.selector' is misspelt and it creates an error it should be 'matchLabels.selector'

@bomboclat
Copy link
Author

thanks @domignat, script updated

@CastawayEGR
Copy link

CastawayEGR commented Mar 6, 2024

This doesn't appear to work as it doesn't output the selector section properly and changing the path to the proper path seems to break it all together for the selector.

{'path': 'spec.selector', 'move': 'selector.matchLabels'}, -> {'path': 'spec.selector', 'move': 'spec.selector.matchLabels'},

Edit: found this and it seems to work: https://gist.github.com/underguiz/3f61eed7942bfb221696be6019da0d22

@danidelafuente
Copy link

I have found another caustic, when you use DeploymentConfig with spec.strategy.type==Recreate, you can have spec.strategy.recreateParams.timeoutSeconds that should be removed, since they are not used in Deployments

https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#strategy

https://docs.openshift.com/container-platform/4.12/applications/deployments/deployment-strategies.html#deployments-recreate-strategy_rolling-strategy
https://docs.openshift.com/container-platform/4.12/rest_api/workloads_apis/deploymentconfig-apps-openshift-io-v1.html#spec-strategy-recreateparams

Something like:
{'path': 'spec.strategy.recreateParams.timeoutSeconds', 'remove': True},

Thanks for the script!

@AlPashy
Copy link

AlPashy commented May 2, 2024

This doesn't appear to work as it doesn't output the selector section properly and changing the path to the proper path seems to break it all together for the selector.

{'path': 'spec.selector', 'move': 'selector.matchLabels'}, -> {'path': 'spec.selector', 'move': 'spec.selector.matchLabels'},

Edit: found this and it seems to work: https://gist.github.com/underguiz/3f61eed7942bfb221696be6019da0d22

i run the script in your link but nothing happens and the deploymentconfig stays. How i can solve it?

@dxcSithLord
Copy link

Based on https://developers.redhat.com/learning/learn:openshift:replace-deprecated-deploymentconfigs-deployments/resource/resources:convert-deploymentconfig-deployment
-with OpenShift V4.14
Update Line 78 :

{'path': 'spec.selector', 'move': 'selector.matchLabels'},

To

 {'path': 'spec.selector.app', 'move': 'matchLabels.app'},

@jlmayorga
Copy link

Thanks for sharing your script! I've been working on a similar tool with some additional features. My Go-based CLI converter offers project-specific conversion, flexible output options, metadata preservation, and reserved namespace handling. It also generates PDF reports of the conversion process, which is great for documentation and auditing.

I am happy to discuss further or collaborate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment