Edit Kubernetes spec files

parent d86a7fda
Pipeline #1971 passed with stages
in 1 minute and 16 seconds
......@@ -7,6 +7,7 @@ from akinaka_cleanup.commands import cleanup as cleanup_commands
from akinaka_rds.commands import copy as copy_commands
from akinaka_reporting.commands import reporting as reporting_commands
from akinaka_container.commands import container as container_commands
from akinaka_k8s.commands import k8s as k8s_commands
@click.group()
def cli():
......@@ -17,5 +18,6 @@ cli.add_command(cleanup_commands)
cli.add_command(copy_commands)
cli.add_command(reporting_commands)
cli.add_command(container_commands)
cli.add_command(k8s_commands)
cli()
import click
from akinaka_client.aws_client import AWS_Client
from akinaka_libs import helpers
from time import gmtime, strftime
import logging
helpers.set_logger()
@click.group()
def k8s():
pass
@k8s.command()
@click.option("--application", required=True, help="The container to update")
@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):
from .update_deployment import update_deployment
k8s_update = update_deployment.UpdateDeployment(application, new_image, new_tag, file_path)
k8s_update.write_new_spec()
#!/usr/bin/env python3
from time import sleep
from akinaka_libs import helpers
from akinaka_libs import exceptions
import yaml
import logging
class UpdateDeployment():
"""Modify a Kubernetes YAML deployment spec"""
def __init__(self, application, new_image=None, new_tag=None, file_path=None):
self.application = application
self.new_image = new_image
self.new_tag = new_tag
self.file_path = file_path
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):
try:
with open(self.file_path, 'r') as stream:
try:
return yaml.safe_load(stream)
except yaml.YAMLError as exception:
logging.error(exception)
exit(1)
except Exception as exception:
logging.error(exception)
exit(1)
def update_spec(self):
spec = self.read_spec()
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]
if self.new_tag:
tag = self.new_tag
else:
tag = container['image'].split(":")[-1]
container['image'] = "{image}:{tag}".format(image = image, tag = tag)
return spec
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()
......@@ -6,7 +6,7 @@ with open("README.md", "r") as fh:
setuptools.setup(
name="akinaka",
python_requires='>=3.3.0',
version="0.2.16",
version="0.2.17",
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