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 1fbfce557505b34286de17a082fde72511703508
Author: thomas loubrieu <[email protected]>
AuthorDate: Mon Jun 8 17:27:42 2020 -0700

    run config operator as a k8s operator
---
 config_operator/README.md                          | 18 ++++-
 config_operator/config_operator/config_operator.py | 39 -----------
 .../config_operator/k8s/K8sConfigMap.py            |  1 +
 config_operator/config_operator/main.py            | 32 +++++++++
 config_operator/containers/docker/Dockerfile       |  3 +-
 .../containers/k8s/config-operator-crd.yml         | 77 ++++++++++++++++++++++
 config_operator/requirements.txt                   |  1 +
 7 files changed, 130 insertions(+), 41 deletions(-)

diff --git a/config_operator/README.md b/config_operator/README.md
index 371a604..2e1ea0d 100644
--- a/config_operator/README.md
+++ b/config_operator/README.md
@@ -35,4 +35,20 @@ To publish the docker image on dockerhub do (step necessary 
for kubernetes deplo
     
 ## Kubernetes
     
-     kubectl apply -f containers/k8s/deployment-git-src.yml -n sdap 
\ No newline at end of file
+Deploy the gitbasedconfig operator:
+
+     kubectl apply -f containers/k8s/config-operator-crd.yml -n sdap
+     
+Deploy the git custom resource which will be synchronize with a k8s configmap
+
+     kubectl apply -f containers/k8s/git-repo-test.yml -n sdap
+     
+Check that the custom resource is deployed:
+
+    kubectl get gitbasedconfigs -n sdap
+    
+Check that the configMap has been generated:
+
+    kubectl get configmaps -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
deleted file mode 100644
index 6b512db..0000000
--- a/config_operator/config_operator/config_operator.py
+++ /dev/null
@@ -1,39 +0,0 @@
-import argparse
-from config_operator.config_source import RemoteGitConfig, LocalDirConfig
-from config_operator.k8s import K8sConfigMap
-
-
-def main():
-    parser = argparse.ArgumentParser(description="Run git configuration 
synchronization operator, work on local-dir or git-url")
-    input_group = parser.add_mutually_exclusive_group(required=True)
-    input_group.add_argument("-l", "--local-dir",
-                             help="local directory where the configuration 
files are")
-    input_group.add_argument("-gu", "--git-url",
-                             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")
-
-    options = parser.parse_args()
-
-    if options.local_dir:
-        config = LocalDirConfig(options.local_dir)
-    else:
-        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()
-
-    if options.updated_continuously:
-        config.when_updated(config_map.publish)
-
-
-if __name__ == "__main__":
-    main()
-
diff --git a/config_operator/config_operator/k8s/K8sConfigMap.py 
b/config_operator/config_operator/k8s/K8sConfigMap.py
index f7784cb..32ee844 100644
--- a/config_operator/config_operator/k8s/K8sConfigMap.py
+++ b/config_operator/config_operator/k8s/K8sConfigMap.py
@@ -22,6 +22,7 @@ class K8sConfigMap:
         configuration = client.Configuration()
         self._api_instance = client.ApiClient(configuration)
         self._api_core_v1_instance = client.CoreV1Api(self._api_instance)
+        self.publish()
 
     def __del__(self):
         self._api_instance.close()
diff --git a/config_operator/config_operator/main.py 
b/config_operator/config_operator/main.py
new file mode 100644
index 0000000..3d6ad2e
--- /dev/null
+++ b/config_operator/config_operator/main.py
@@ -0,0 +1,32 @@
+import kopf
+from config_operator.config_source import RemoteGitConfig
+from config_operator.k8s import K8sConfigMap
+
+
[email protected]('sdap.apache.org', 'v1', 'git-repo-configs')
+def create_fn(body, spec, **kwargs):
+    # Get info from Git Repo Config object
+    name = body['metadata']['name']
+    namespace = body['metadata']['namespace']
+
+    if 'git-url' not in spec.keys():
+        raise kopf.HandlerFatalError(f"git-url must be set.")
+    if 'config-map' not in spec.keys():
+        raise kopf.HandlerFatalError(f"config-map must be set.")
+
+    git_url = spec['git-url']
+    config_map = spec['config-map']
+
+    _kargs = {}
+    for k in {'git-branch', 'git-token'}:
+        if k in spec.keys():
+            _kargs[k.split('-')[0]] = spec[k]
+
+    config = RemoteGitConfig(git_url, **_kargs)
+
+    config_map = K8sConfigMap(config_map, namespace, config)
+
+    config.when_updated(config_map.publish)
+
+    msg = f"configmap {config_map} created from git repo {git_url}"
+    return {'message': msg}
diff --git a/config_operator/containers/docker/Dockerfile 
b/config_operator/containers/docker/Dockerfile
index 4e82c98..4765d30 100644
--- a/config_operator/containers/docker/Dockerfile
+++ b/config_operator/containers/docker/Dockerfile
@@ -6,5 +6,6 @@ COPY /requirements.txt /config_operator/requirements.txt
 COPY /README.md /config_operator/README.md
 
 RUN cd /config_operator && pip install .
+COPY /config_operator/k8_config_operator.py /k8_config_operator.py
 
-CMD bash
+CMD ["kopf",  "run",  "/k8_config_operator.py",  "--verbose"]
diff --git a/config_operator/containers/k8s/config-operator-crd.yml 
b/config_operator/containers/k8s/config-operator-crd.yml
new file mode 100644
index 0000000..ea9bbba
--- /dev/null
+++ b/config_operator/containers/k8s/config-operator-crd.yml
@@ -0,0 +1,77 @@
+apiVersion: apiextensions.k8s.io/v1beta1
+kind: CustomResourceDefinition
+metadata:
+  name: gitbasedconfigs.sdap.apache.org
+spec:
+  group: sdap.apache.org
+  versions:
+  - name: v1
+    served: true
+    storage: true
+  scope: Namespaced
+  names:
+    plural: gitbasedconfigs
+    singular: gitbasedconfig
+    kind: gitBasedConfig
+    shortNames:
+    - gitcfg
+  validation:
+    openAPIV3Schema:
+      type: object
+      properties:
+        git-url:
+          type: string
+        git-branch:
+          type: string
+        git-token:
+          type: string
+        local-dir:
+          type: string
+        namespace:
+          type: string
+        config-map:
+          type: string
+
+
+---
+
+apiVersion: v1
+kind: ServiceAccount
+metadata:
+  name: git-repo-config-operator
+
+---
+
+apiVersion: rbac.authorization.k8s.io/v1
+kind: ClusterRoleBinding
+metadata:
+  name: git-repo-config-operator
+roleRef:
+  apiGroup: rbac.authorization.k8s.io
+  kind: ClusterRole
+  name: cluster-admin
+subjects:
+  - kind: ServiceAccount
+    name: git-repo-config-operator
+    namespace: default
+
+---
+
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  name: git-repo-config-operator
+spec:
+  selector:
+    matchLabels:
+      app: git-repo-config-operator
+  template:
+    metadata:
+      labels:
+        app: git-repo-config-operator
+    spec:
+      serviceAccountName: git-repo-config-operator
+      containers:
+      - image: tloubrieu/config-operator:latest
+        name: git-repo-config-operator
+        imagePullPolicy: IfNotPresent
\ No newline at end of file
diff --git a/config_operator/requirements.txt b/config_operator/requirements.txt
index 4365d3d..5d452e2 100644
--- a/config_operator/requirements.txt
+++ b/config_operator/requirements.txt
@@ -1,3 +1,4 @@
 GitPython==3.1.2
 kubernetes==11.0
+kopf==0.26
 

Reply via email to