Skip to content

Commit

Permalink
fix: context not used for state
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgiga1993 committed Nov 23, 2023
1 parent e8d8c03 commit 630f054
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion octoploy/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.2.3'
__version__ = '1.2.4'
13 changes: 11 additions & 2 deletions octoploy/config/AppConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,25 @@ class AppConfig(BaseConfig):
Contains the configuration for the deployment of a single app
"""

def __init__(self, config_root: str, path: Optional[str], external_vars: Dict[str, str] = None):
def __init__(self, config_root: str, path: Optional[str], external_vars: Dict[str, str] = None,
root=None):
super().__init__(path, external_vars)
self._config_root = config_root
self._root = root

def get_config_maps(self) -> List[DynamicConfigMap]:
"""
Returns additional config maps which should contain the content of a file
"""
return [DynamicConfigMap(data) for data in self.data.get('configmaps', [])]

def get_root(self):
"""
Returns the root configuration
:return: Config
"""
return self._root

def enabled(self) -> bool:
"""
True if this app is enabled
Expand Down Expand Up @@ -56,7 +65,7 @@ def get_for_each(self) -> List[AppConfig]:
if dc_name is None:
raise MissingVar('APP_NAME not defined in forEach for app ' + str(self.get_name()))

config = AppConfig(self._config_root, None, instance_vars)
config = AppConfig(self._config_root, None, instance_vars, self._root)
# Inherit all parameters
config.data.update(self.data)
# Update the DC_NAME
Expand Down
20 changes: 12 additions & 8 deletions octoploy/config/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class RootConfig(BaseConfig):
def __init__(self, config_root: str, path: str):
super().__init__(path)
self._config_root = config_root
self._oc = None
self._k8s_api = None
self._library = None # type: Optional[RootConfig]
self._global_var_overrides: Dict[str, str] = {}

Expand Down Expand Up @@ -117,18 +117,22 @@ def create_api(self) -> K8sApi:
Creates a new openshift / k8s client.
:return: Client
"""
if self._oc is not None:
return self._oc
if self._k8s_api is not None:
return self._k8s_api

mode = self._get_mode()
if mode == 'oc':
oc = Oc()
k8s_api = Oc()
elif mode == 'k8s' or mode == 'k8':
oc = K8()
k8s_api = K8()
else:
raise ValueError(f'Invalid mode: {mode}')
self._oc = oc
return oc

context = self.get_kubectl_context()
if context is not None:
k8s_api.switch_context(context)
self._k8s_api = k8s_api
return k8s_api

def get_namespace_name(self) -> Optional[str]:
"""
Expand Down Expand Up @@ -230,4 +234,4 @@ def load_app_config(self, name: str) -> AppConfig:
raise FileNotFoundError('No index yml file found: ' + index_file)

variables = self.get_replacements()
return AppConfig(folder_path, index_file, variables)
return AppConfig(folder_path, index_file, variables, root=self)
9 changes: 5 additions & 4 deletions octoploy/config/DeploymentActionConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ def __init__(self, app_config: AppConfig, data):
self._data = data
self._app_config = app_config

def run(self, oc: K8sApi):
def run(self, k8s: K8sApi):
namespace = self._app_config.get_root().get_namespace_name()
if self._data == 'deploy':
oc.rollout(self._app_config.get_name())
k8s.rollout(self._app_config.get_name(), namespace=namespace)
return

exec_config = self._data.get('exec', None)
Expand All @@ -32,7 +33,7 @@ def run(self, oc: K8sApi):

dc_name = self._app_config.get_name()
self.log.info('Reloading via exec in pods of ' + dc_name)
pods = oc.get_pods(dc_name=dc_name)
pods = k8s.get_pods(dc_name=dc_name, namespace=namespace)
for pod in pods:
oc.exec(pod.name, cmd, args)
k8s.exec(pod.name, cmd, args, namespace=namespace)
return
2 changes: 0 additions & 2 deletions octoploy/deploy/DeploymentBundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ def deploy(self, deploy_runner: K8sObjectDeployer):
Deploys all objects in this bundle
:param deploy_runner: Deployment runner which should be used
"""
deploy_runner.select_context()

# First sort the objects, we want "deployments" to be the last object type
# so all prerequisites are available
def sorting(x: BaseObj):
Expand Down
8 changes: 0 additions & 8 deletions octoploy/deploy/K8sObjectDeployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ def __init__(self, root_config: RootConfig, k8sapi: K8sApi, app_config: AppConfi

self._to_be_deployed: List[BaseObj] = []

def select_context(self):
"""
Selects the cluster context
"""
context = self._root_config.get_kubectl_context()
if context is not None:
self._api.switch_context(context)

def add_object(self, k8s_object: BaseObj):
"""
Adds the given object to the deployment list
Expand Down

0 comments on commit 630f054

Please sign in to comment.