This is an automated email from the ASF dual-hosted git repository. eamonford pushed a commit to branch config_map in repository https://gitbox.apache.org/repos/asf/incubator-sdap-ingester.git
commit 3268385e005ebfcc52ba7645b151ce72c6f9dfa8 Author: thomas loubrieu <[email protected]> AuthorDate: Sun Jun 7 10:57:57 2020 -0700 integrates with kubernetes --- config_operator/README.md | 2 +- config_operator/config_operator/config_operator.py | 5 +++-- config_operator/config_operator/config_source/LocalDirConfig.py | 8 ++++++-- config_operator/config_operator/config_source/RemoteGitConfig.py | 8 +++++--- config_operator/config_operator/k8s/K8sConfigMap.py | 6 +++++- .../containers/k8s/{deployment.yml => deployment-git-src.yml} | 2 ++ 6 files changed, 22 insertions(+), 9 deletions(-) diff --git a/config_operator/README.md b/config_operator/README.md index 61889c4..371a604 100644 --- a/config_operator/README.md +++ b/config_operator/README.md @@ -35,4 +35,4 @@ To publish the docker image on dockerhub do (step necessary for kubernetes deplo ## Kubernetes - kubectl apply -f containers/k8s/deployment.yml -n sdap \ No newline at end of file + kubectl apply -f containers/k8s/deployment-git-src.yml -n sdap \ No newline at end of file diff --git a/config_operator/config_operator/config_operator.py b/config_operator/config_operator/config_operator.py index b032384..6b512db 100644 --- a/config_operator/config_operator/config_operator.py +++ b/config_operator/config_operator/config_operator.py @@ -12,10 +12,11 @@ def main(): help="git repository from which the configuration files are pulled/saved") parser.add_argument("-gb", "--git-branch", help="git branch from which the configuration files are pulled/saved", default="master") + parser.add_argument("-gl", "--git-local", help="local git repository", required=False) parser.add_argument("-gt", "--git-token", help="git personal access token used to access the repository") - parser.add_argument("-n", "--namespace", help="kubernetes namespace where the configuration will be deployed", required=True) parser.add_argument("-cm", "--config-map", help="configmap name in kubernetes", required=True) + parser.add_argument("-u", "--updated-continuously", nargs='?', const=True, default=False, help="k8 configMap is updated as soon as a syntactically correct configuration file is updated") @@ -24,7 +25,7 @@ def main(): if options.local_dir: config = LocalDirConfig(options.local_dir) else: - config = RemoteGitConfig(options.git_url, branch=options.git_branch, token=options.git_token) + config = RemoteGitConfig(options.git_url, branch=options.git_branch, token=options.git_token, local_dir=options.git_local) config_map = K8sConfigMap(options.config_map, options.namespace, config) config_map.publish() diff --git a/config_operator/config_operator/config_source/LocalDirConfig.py b/config_operator/config_operator/config_source/LocalDirConfig.py index 89c0e5a..f37e41b 100644 --- a/config_operator/config_operator/config_source/LocalDirConfig.py +++ b/config_operator/config_operator/config_source/LocalDirConfig.py @@ -49,13 +49,17 @@ class LocalDirConfig: def _get_latest_update(self): - return time.ctime(max(os.path.getmtime(root) for root,_,_ in os.walk(self._local_dir))) + m_times = [os.path.getmtime(root) for root, _, _ in os.walk(self._local_dir)] + if m_times: + return time.ctime(max(m_times)) + else: + return None def when_updated(self, callback): while True: time.sleep(LISTEN_FOR_UPDATE_INTERVAL_SECONDS) latest_update = self._get_latest_update() - if latest_update > self._latest_update: + if latest_update is None or (latest_update > self._latest_update): logger.info("local config dir has been updated") callback() self._latest_update = latest_update diff --git a/config_operator/config_operator/config_source/RemoteGitConfig.py b/config_operator/config_operator/config_source/RemoteGitConfig.py index 24e614a..15c0f01 100644 --- a/config_operator/config_operator/config_source/RemoteGitConfig.py +++ b/config_operator/config_operator/config_source/RemoteGitConfig.py @@ -9,12 +9,13 @@ logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) LISTEN_FOR_UPDATE_INTERVAL_SECONDS = 5 - +DEFAULT_LOCAL_REPO_DIR = os.path.join(sys.prefix, 'sdap', 'conf') class RemoteGitConfig(LocalDirConfig): def __init__(self, git_url, branch='master', - token=None + token=None, + local_dir=DEFAULT_LOCAL_REPO_DIR ): """ @@ -25,7 +26,8 @@ class RemoteGitConfig(LocalDirConfig): self._git_url = git_url if git_url.endswith(".git") else git_url + '.git' self._git_branch = branch self._git_token = token - local_dir = os.path.join(sys.prefix, 'sdap', 'conf') + if local_dir is None: + local_dir = DEFAULT_LOCAL_REPO_DIR super().__init__(local_dir) self._repo = None self._init_local_config_repo() diff --git a/config_operator/config_operator/k8s/K8sConfigMap.py b/config_operator/config_operator/k8s/K8sConfigMap.py index b16b58c..f7784cb 100644 --- a/config_operator/config_operator/k8s/K8sConfigMap.py +++ b/config_operator/config_operator/k8s/K8sConfigMap.py @@ -1,3 +1,4 @@ +import os import logging from kubernetes import client, config from kubernetes.client.rest import ApiException @@ -14,7 +15,10 @@ class K8sConfigMap: self._namespace = namespace self._configmap_name = configmap_name - config.load_kube_config() + if os.getenv('KUBERNETES_SERVICE_HOST'): + config.load_incluster_config() + else: + config.load_kube_config() configuration = client.Configuration() self._api_instance = client.ApiClient(configuration) self._api_core_v1_instance = client.CoreV1Api(self._api_instance) diff --git a/config_operator/containers/k8s/deployment.yml b/config_operator/containers/k8s/deployment-git-src.yml similarity index 99% rename from config_operator/containers/k8s/deployment.yml rename to config_operator/containers/k8s/deployment-git-src.yml index 1501a75..99b268d 100644 --- a/config_operator/containers/k8s/deployment.yml +++ b/config_operator/containers/k8s/deployment-git-src.yml @@ -19,3 +19,5 @@ spec: image: tloubrieu/config-operator:latest imagePullPolicy: IfNotPresent command: ['config-operator', '--git-url', 'https://github.com/tloubrieu-jpl/sdap-ingester-config' , '--namespace', 'sdap', '--config-map', 'collection-ingester-conf', '-u'] + +
