Last active
June 25, 2024 21:28
-
-
Save bomboclat/1c53b7e692b1df58af67598337cc2b88 to your computer and use it in GitHub Desktop.
convert-deploymentconfig-to-deployment.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
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?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have found another caustic, when you use DeploymentConfig with
spec.strategy.type==Recreate
, you can havespec.strategy.recreateParams.timeoutSeconds
that should be removed, since they are not used in Deploymentshttps://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!