blob: ea5c8e251c76aba319f556c070e0ca1752d0945b [file] [log] [blame]
Scott Bakerac43a742018-05-07 16:54:03 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""
17 sync_configmap.py
18
19 Synchronize Config Maps.
20"""
21
22import json
23from synchronizers.new_base.syncstep import SyncStep
24from synchronizers.new_base.modelaccessor import KubernetesConfigMap
25
26from xosconfig import Config
27from multistructlog import create_logger
28
Scott Bakerac43a742018-05-07 16:54:03 -070029log = create_logger(Config().get('logging'))
30
31class SyncKubernetesConfigMap(SyncStep):
32
33 """
34 SyncKubernetesConfigMap
35
36 Implements sync step for syncing ConfigMaps.
37 """
38
39 provides = [KubernetesConfigMap]
40 observes = KubernetesConfigMap
41 requested_interval = 0
42
43 def __init__(self, *args, **kwargs):
44 super(SyncKubernetesConfigMap, self).__init__(*args, **kwargs)
Scott Baker13e953c2018-05-17 09:19:15 -070045 self.init_kubernetes_client()
46
47 def init_kubernetes_client(self):
48 from kubernetes import client as kubernetes_client, config as kubernetes_config
49 from kubernetes.client.rest import ApiException
Scott Bakerac43a742018-05-07 16:54:03 -070050 kubernetes_config.load_incluster_config()
Scott Baker13e953c2018-05-17 09:19:15 -070051 self.kubernetes_client = kubernetes_client
52 self.v1core = kubernetes_client.CoreV1Api()
53 self.ApiException = ApiException
Scott Bakerac43a742018-05-07 16:54:03 -070054
55 def get_config_map(self, o):
56 """ Given an XOS KubernetesConfigMap object, read the corresponding ConfigMap from Kubernetes.
57 return None if no ConfigMap exists.
58 """
59 try:
Scott Baker13e953c2018-05-17 09:19:15 -070060 config_map = self.v1core.read_namespaced_config_map(o.name, o.trust_domain.name)
61 except self.ApiException, e:
Scott Bakerac43a742018-05-07 16:54:03 -070062 if e.status == 404:
63 return None
64 raise
65 return config_map
66
67 def sync_record(self, o):
68 config_map = self.get_config_map(o)
69 if not config_map:
Scott Baker13e953c2018-05-17 09:19:15 -070070 config_map = self.kubernetes_client.V1ConfigMap()
Scott Bakerac43a742018-05-07 16:54:03 -070071 config_map.data = json.loads(o.data)
Scott Baker13e953c2018-05-17 09:19:15 -070072 config_map.metadata = self.kubernetes_client.V1ObjectMeta(name=o.name)
Scott Bakerac43a742018-05-07 16:54:03 -070073
Scott Baker13e953c2018-05-17 09:19:15 -070074 config_map = self.v1core.create_namespaced_config_map(o.trust_domain.name, config_map)
Scott Bakerac43a742018-05-07 16:54:03 -070075 else:
76 config_map.data = json.loads(o.data)
Scott Baker13e953c2018-05-17 09:19:15 -070077 self.v1core.patch_namespaced_config_map(o.name, o.trust_domain.name, config_map)
Scott Bakerac43a742018-05-07 16:54:03 -070078
79 if (not o.backend_handle):
80 o.backend_handle = config_map.metadata.self_link
81 o.save(update_fields=["backend_handle"])
82
83 def delete_record(self, port):
84 # TODO(smbaker): Implement delete step
85 pass
86