Skip to content

Instantly share code, notes, and snippets.

@underguiz
Last active July 15, 2024 18:15
Show Gist options
  • Save underguiz/3f61eed7942bfb221696be6019da0d22 to your computer and use it in GitHub Desktop.
Save underguiz/3f61eed7942bfb221696be6019da0d22 to your computer and use it in GitHub Desktop.
Convert Openshift's DeploymentConfig to Deployment
import yaml
import sys
import json
with open(sys.argv[1]) as file:
deploymentconfig = list(yaml.load_all(file, Loader=yaml.SafeLoader))
if deploymentconfig[-1] == None:
del deploymentconfig[-1]
for manifest in deploymentconfig:
if manifest != None and manifest['kind'] == 'DeploymentConfig':
manifest['kind'] = 'Deployment'
manifest['apiVersion'] = 'apps/v1'
try:
manifest['spec']['strategy']['type'] = 'RollingUpdate'
except:
pass
try:
selector = json.dumps(manifest['spec']['selector'])
matchLabels = json.loads(selector)
manifest['spec']['selector']['matchLabels'] = matchLabels
for label in matchLabels:
del manifest['spec']['selector'][label]
except:
pass
try:
del manifest['spec']['strategy']['rollingParams']['intervalSeconds']
except:
pass
try:
del manifest['spec']['strategy']['rollingParams']['updatePeriodSeconds']
except:
pass
try:
rollingParams = json.dumps(manifest['spec']['strategy']['rollingParams'])
rollingUpdate = json.loads(rollingParams)
manifest['spec']['strategy']['rollingUpdate'] = rollingUpdate
del manifest['spec']['strategy']['rollingParams']
except:
pass
try:
del manifest['spec']['triggers']
except:
pass
try:
del manifest['spec']['test']
except:
pass
try:
del manifest['spec']['strategy']['activeDeadlineSeconds']
except:
pass
try:
del manifest['spec']['strategy']['resources']
except:
pass
with open(sys.argv[1], "w") as deployment:
yaml.dump_all(deploymentconfig, deployment, default_flow_style=False)
@lsloan
Copy link

lsloan commented Jul 15, 2024

Why do each deletion in individual try…except:pass blocks? Try putting them all in the same block and catch a specific exception, most likely KeyError.

Also, I don't understand why lines 42–44 encode the data in JSON, only to decode it on the next line. Why not do something like…

manifest['spec']['strategy']['rollingUpdate'] = manifest['spec']['strategy']['rollingParams']

Maybe a simple assignment won't do the job and you'll need to do some sort of copy operation, but it may be easier to understand and you wouldn't need to call JSON module functions.

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