Scott Baker | ac43a74 | 2018-05-07 16:54:03 -0700 | [diff] [blame] | 1 | |
| 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 | |
| 22 | import json |
| 23 | from synchronizers.new_base.syncstep import SyncStep |
| 24 | from synchronizers.new_base.modelaccessor import KubernetesConfigMap |
| 25 | |
| 26 | from xosconfig import Config |
| 27 | from multistructlog import create_logger |
| 28 | |
Scott Baker | ac43a74 | 2018-05-07 16:54:03 -0700 | [diff] [blame] | 29 | log = create_logger(Config().get('logging')) |
| 30 | |
| 31 | class 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 Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame^] | 45 | 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 Baker | ac43a74 | 2018-05-07 16:54:03 -0700 | [diff] [blame] | 50 | kubernetes_config.load_incluster_config() |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame^] | 51 | self.kubernetes_client = kubernetes_client |
| 52 | self.v1core = kubernetes_client.CoreV1Api() |
| 53 | self.ApiException = ApiException |
Scott Baker | ac43a74 | 2018-05-07 16:54:03 -0700 | [diff] [blame] | 54 | |
| 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 Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame^] | 60 | config_map = self.v1core.read_namespaced_config_map(o.name, o.trust_domain.name) |
| 61 | except self.ApiException, e: |
Scott Baker | ac43a74 | 2018-05-07 16:54:03 -0700 | [diff] [blame] | 62 | 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 Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame^] | 70 | config_map = self.kubernetes_client.V1ConfigMap() |
Scott Baker | ac43a74 | 2018-05-07 16:54:03 -0700 | [diff] [blame] | 71 | config_map.data = json.loads(o.data) |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame^] | 72 | config_map.metadata = self.kubernetes_client.V1ObjectMeta(name=o.name) |
Scott Baker | ac43a74 | 2018-05-07 16:54:03 -0700 | [diff] [blame] | 73 | |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame^] | 74 | config_map = self.v1core.create_namespaced_config_map(o.trust_domain.name, config_map) |
Scott Baker | ac43a74 | 2018-05-07 16:54:03 -0700 | [diff] [blame] | 75 | else: |
| 76 | config_map.data = json.loads(o.data) |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame^] | 77 | self.v1core.patch_namespaced_config_map(o.name, o.trust_domain.name, config_map) |
Scott Baker | ac43a74 | 2018-05-07 16:54:03 -0700 | [diff] [blame] | 78 | |
| 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 | |