Handle multiple deployment specs + cronjobs

parent 84d9289b
Pipeline #1972 passed with stages
in 1 minute and 14 seconds
......@@ -15,8 +15,9 @@ def k8s():
@click.option("--new-image", help="Full image path, minus the tag")
@click.option("--new-tag", help="Tag to update the container's image to")
@click.option("--file-path", default="kubernetes/deployment.yml", help="Alternative path to deployment spec. Defaults to 'kubernetes/deployment.yml'")
def update_deployment(application, new_image, new_tag, file_path):
@click.option("--dry-run", is_flag=True, help="If passed, then the new config is written to stdout instead of the originating file")
def update_deployment(application, new_image, new_tag, file_path, dry_run):
from .update_deployment import update_deployment
k8s_update = update_deployment.UpdateDeployment(application, new_image, new_tag, file_path)
k8s_update = update_deployment.UpdateDeployment(application, new_image, new_tag, file_path, dry_run)
k8s_update.write_new_spec()
......@@ -9,49 +9,68 @@ import logging
class UpdateDeployment():
"""Modify a Kubernetes YAML deployment spec"""
def __init__(self, application, new_image=None, new_tag=None, file_path=None):
def __init__(self, application, new_image=None, new_tag=None, file_path=None, dry_run=False):
self.application = application
self.new_image = new_image
self.new_tag = new_tag
self.file_path = file_path
self.dry_run = dry_run
if not self.new_image and not self.new_tag:
logging.error("At least --new-image or --new-tag need to be given")
exit(1)
def read_spec(self):
def read_specs(self):
yammies = []
try:
with open(self.file_path, 'r') as stream:
try:
return yaml.safe_load(stream)
for yammy in yaml.safe_load_all(stream):
yammies.append(yammy)
except yaml.YAMLError as exception:
logging.error(exception)
exit(1)
return yammies
except Exception as exception:
logging.error(exception)
exit(1)
def update_spec(self):
spec = self.read_spec()
specs = self.read_specs()
for container in spec['spec']['template']['spec']['containers']:
if container['name'] == self.application:
if self.new_image:
image = self.new_image
else:
image = container['image'].split(":")[0]
for spec in specs:
if spec['kind'] == 'Deployment' or spec['kind'] == 'Pod':
spec_path = spec['spec']['template']['spec']['containers']
elif spec['kind'] == 'CronJob':
spec_path = spec['spec']['jobTemplate']['spec']['template']['spec']['containers']
else:
logging.error("Can't handle spec file type of {}".format(spec['kind']))
exit(1)
if self.new_tag:
tag = self.new_tag
else:
tag = container['image'].split(":")[-1]
for container in spec_path:
if container['name'] == self.application:
if self.new_image:
image = self.new_image
else:
image = container['image'].split(":")[0]
container['image'] = "{image}:{tag}".format(image = image, tag = tag)
if self.new_tag:
tag = self.new_tag
else:
tag = container['image'].split(":")[-1]
return spec
container['image'] = "{image}:{tag}".format(image = image, tag = tag)
return specs
def write_new_spec(self):
new_spec = self.update_spec()
spec_file = open(self.file_path, "w")
spec_file.write(yaml.dump(new_spec, default_flow_style=False))
spec_file.close()
new_spec = yaml.dump_all(self.update_spec(), default_flow_style=False)
if not self.dry_run:
spec_file = open(self.file_path, "w")
spec_file.write(new_spec)
spec_file.close()
else:
print(new_spec)
......@@ -5,13 +5,13 @@ import logging
# Take a duration in seconds and work out the datetime value for the datetime at that date and time ago
def datetime_this_seconds_ago(duration):
return (datetime.now(timezone.utc) + timedelta(seconds=-duration))
return (datetime.now(timezone.utc) + timedelta(seconds=-duration))
def seconds_from_hours(hours):
return (60*60)*hours
return (60*60)*hours
def log(message):
print(message)
def set_logger():
return logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S%z')
return logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S%z')
......@@ -6,7 +6,7 @@ with open("README.md", "r") as fh:
setuptools.setup(
name="akinaka",
python_requires='>=3.3.0',
version="0.2.17",
version="0.2.18",
author="Afraz",
author_email="afraz@olindata.com",
description="OlinData's aws cli Extras",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment