Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -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_trustdomain.py |
| 18 | |
| 19 | Synchronize TrustDomain. TrustDomains correspond roughly to Kubernetes namespaces. |
| 20 | """ |
| 21 | |
| 22 | from synchronizers.new_base.syncstep import SyncStep |
| 23 | from synchronizers.new_base.modelaccessor import TrustDomain |
| 24 | |
| 25 | from xosconfig import Config |
| 26 | from multistructlog import create_logger |
| 27 | |
Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -0700 | [diff] [blame] | 28 | log = create_logger(Config().get('logging')) |
| 29 | |
| 30 | class SyncTrustDomain(SyncStep): |
| 31 | |
| 32 | """ |
| 33 | SyncTrustsDomain |
| 34 | |
| 35 | Implements sync step for syncing trust domains. |
| 36 | """ |
| 37 | |
| 38 | provides = [TrustDomain] |
| 39 | observes = TrustDomain |
| 40 | requested_interval = 0 |
| 41 | |
| 42 | def __init__(self, *args, **kwargs): |
| 43 | super(SyncTrustDomain, self).__init__(*args, **kwargs) |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 44 | self.init_kubernetes_client() |
| 45 | |
| 46 | def init_kubernetes_client(self): |
| 47 | from kubernetes.client.rest import ApiException |
| 48 | from kubernetes import client as kubernetes_client, config as kubernetes_config |
Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -0700 | [diff] [blame] | 49 | kubernetes_config.load_incluster_config() |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 50 | self.kubernetes_client = kubernetes_client |
| 51 | self.v1core = kubernetes_client.CoreV1Api() |
| 52 | self.ApiException = ApiException |
Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -0700 | [diff] [blame] | 53 | |
| 54 | def fetch_pending(self, deleted): |
| 55 | """ Figure out which TrustDomains are interesting to the K8s synchronizer. It's necessary to filter as we're |
| 56 | synchronizing a core model, and we only want to synchronize trust domains that will exist within |
| 57 | Kubernetes. |
| 58 | """ |
| 59 | objs = super(SyncTrustDomain, self).fetch_pending(deleted) |
| 60 | for obj in objs[:]: |
| 61 | # If the TrustDomain isn't part of the K8s service, then it's someone else's trust domain |
| 62 | if "KubernetesService" not in obj.owner.leaf_model.class_names: |
| 63 | objs.remove(obj) |
| 64 | return objs |
| 65 | |
| 66 | def get_namespace(self, o): |
| 67 | """ Give an XOS TrustDomain object, return the corresponding namespace from Kubernetes. |
| 68 | Return None if no namespace exists. |
| 69 | """ |
| 70 | try: |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 71 | ns = self.v1core.read_namespace(o.name) |
| 72 | except self.ApiException, e: |
Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -0700 | [diff] [blame] | 73 | if e.status == 404: |
| 74 | return None |
| 75 | raise |
| 76 | return ns |
| 77 | |
| 78 | def sync_record(self, o): |
| 79 | ns = self.get_namespace(o) |
| 80 | if not ns: |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 81 | ns = self.kubernetes_client.V1Namespace() |
| 82 | ns.metadata = self.kubernetes_client.V1ObjectMeta(name=o.name) |
Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -0700 | [diff] [blame] | 83 | |
| 84 | log.info("creating namespace %s" % o.name) |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 85 | ns=self.v1core.create_namespace(ns) |
Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -0700 | [diff] [blame] | 86 | |
| 87 | if (not o.backend_handle): |
| 88 | o.backend_handle = ns.metadata.self_link |
| 89 | o.save(update_fields=["backend_handle"]) |
| 90 | |
Scott Baker | 393d015 | 2018-05-21 09:17:49 -0700 | [diff] [blame] | 91 | def delete_record(self, o): |
| 92 | namespace = self.get_namespace(o) |
| 93 | if not namespace: |
| 94 | log.info("Kubernetes trust domain does not exist; Nothing to delete.", o=o) |
| 95 | return |
| 96 | delete_options = self.kubernetes_client.V1DeleteOptions() |
| 97 | self.v1core.delete_namespace(o.name, delete_options) |
| 98 | log.info("Deleted trust domain from kubernetes", handle=o.backend_handle) |
Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -0700 | [diff] [blame] | 99 | |