Skip to content

Commit

Permalink
refactor: restart all objects on config change
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgiga1993 committed Oct 10, 2024
1 parent 89d0dd0 commit 9b38d8f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
13 changes: 7 additions & 6 deletions octoploy/api/Kubectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ def get_pods(self, dc_name: str = None, pod_name: str = None,
raise NotImplemented

@abstractmethod
def rollout(self, name: str, namespace: Optional[str] = None):
def rollout(self, kind: str, name: str, namespace: Optional[str] = None):
"""
Re-Deploys the latest DC with the given name
:param name: Deployment name
Restarts the given kind/name
:param kind: Object kind
:param name: Object name
:param namespace: Namespace
"""
raise NotImplemented
Expand Down Expand Up @@ -232,7 +233,7 @@ def get_pods(self, dc_name: str = None, pod_name: str = None, namespace: Optiona

return pods

def rollout(self, name: str, namespace: Optional[str] = None):
def rollout(self, kind: str, name: str, namespace: Optional[str] = None):
"""
Re-Deploys the latest DC with the given name
:param name: DC name
Expand Down Expand Up @@ -298,8 +299,8 @@ def _get_bin(self) -> str:


class K8s(Oc):
def rollout(self, name: str, namespace: Optional[str] = None):
self._exec(['rollout', 'restart', 'deployment', name], namespace=namespace)
def rollout(self, kind: str, name: str, namespace: Optional[str] = None):
self._exec(['rollout', 'restart', kind, name], namespace=namespace)

def tag(self, source: str, dest: str, namespace: Optional[str] = None):
raise NotImplemented('Not available for k8')
Expand Down
34 changes: 18 additions & 16 deletions octoploy/config/DeploymentActionConfig.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, List, Optional
from typing import TYPE_CHECKING, List

from octoploy.k8s.BaseObj import BaseObj
from octoploy.utils.Log import Log
Expand All @@ -24,18 +24,17 @@ def __init__(self, app_config: AppConfig, data):
def run(self, k8s: K8sApi, all_objects: List[BaseObj]):
namespace = self._app_config.get_root().get_namespace_name()
if self._data == 'deploy':
deployment_name = self._app_config.get_name()
deployment_obj = self.get_deployment_object(deployment_name, all_objects)
if deployment_obj is None:
self.log.warning(f'Deployment object {deployment_name} not found')
objects = self.get_rollout_objects(all_objects)
if len(objects) == 0:
self.log.warning(f'No objects to restart found')
return

try:
k8s.rollout(deployment_name, namespace=deployment_obj.namespace)
except Exception as e:
if '(NotFound)' in str(e):
self.log.warning(f'Could not restart {deployment_name} {deployment_obj.namespace}:\n{e}')
return
for obj in objects:
try:
k8s.rollout(obj.kind, obj.name, namespace=obj.namespace)
except Exception as e:
if '(NotFound)' in str(e):
self.log.warning(f'Could not restart {obj.get_fqn()} in namespace {obj.namespace}: {e}')
return

exec_config = self._data.get('exec', None)
Expand All @@ -51,9 +50,12 @@ def run(self, k8s: K8sApi, all_objects: List[BaseObj]):
return

@staticmethod
def get_deployment_object(deployment_name: str, all_objects: List[BaseObj]) -> Optional[BaseObj]:
def get_rollout_objects(all_objects: List[BaseObj]) -> List[BaseObj]:
out = []
for obj in all_objects:
if (obj.is_kind('Deployment') or obj.is_kind('DeploymentConfig')) \
and obj.name == deployment_name:
return obj
return None
if obj.is_kind('Deployment') \
or obj.is_kind('DaemonSet') \
or obj.is_kind('StatefulSet') \
or obj.is_kind('DeploymentConfig'):
out.append(obj)
return out

0 comments on commit 9b38d8f

Please sign in to comment.