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 | |
| 28 | from kubernetes.client.rest import ApiException |
| 29 | from kubernetes import client as kubernetes_client, config as kubernetes_config |
| 30 | |
| 31 | log = create_logger(Config().get('logging')) |
| 32 | |
| 33 | class SyncTrustDomain(SyncStep): |
| 34 | |
| 35 | """ |
| 36 | SyncTrustsDomain |
| 37 | |
| 38 | Implements sync step for syncing trust domains. |
| 39 | """ |
| 40 | |
| 41 | provides = [TrustDomain] |
| 42 | observes = TrustDomain |
| 43 | requested_interval = 0 |
| 44 | |
| 45 | def __init__(self, *args, **kwargs): |
| 46 | super(SyncTrustDomain, self).__init__(*args, **kwargs) |
| 47 | kubernetes_config.load_incluster_config() |
| 48 | self.v1 = kubernetes_client.CoreV1Api() |
| 49 | |
| 50 | def fetch_pending(self, deleted): |
| 51 | """ Figure out which TrustDomains are interesting to the K8s synchronizer. It's necessary to filter as we're |
| 52 | synchronizing a core model, and we only want to synchronize trust domains that will exist within |
| 53 | Kubernetes. |
| 54 | """ |
| 55 | objs = super(SyncTrustDomain, self).fetch_pending(deleted) |
| 56 | for obj in objs[:]: |
| 57 | # If the TrustDomain isn't part of the K8s service, then it's someone else's trust domain |
| 58 | if "KubernetesService" not in obj.owner.leaf_model.class_names: |
| 59 | objs.remove(obj) |
| 60 | return objs |
| 61 | |
| 62 | def get_namespace(self, o): |
| 63 | """ Give an XOS TrustDomain object, return the corresponding namespace from Kubernetes. |
| 64 | Return None if no namespace exists. |
| 65 | """ |
| 66 | try: |
| 67 | ns = self.v1.read_namespace(o.name) |
| 68 | except ApiException, e: |
| 69 | if e.status == 404: |
| 70 | return None |
| 71 | raise |
| 72 | return ns |
| 73 | |
| 74 | def sync_record(self, o): |
| 75 | ns = self.get_namespace(o) |
| 76 | if not ns: |
| 77 | ns = kubernetes_client.V1Namespace() |
| 78 | ns.metadata = kubernetes_client.V1ObjectMeta(name=o.name) |
| 79 | |
| 80 | log.info("creating namespace %s" % o.name) |
| 81 | ns=self.v1.create_namespace(ns) |
| 82 | |
| 83 | if (not o.backend_handle): |
| 84 | o.backend_handle = ns.metadata.self_link |
| 85 | o.save(update_fields=["backend_handle"]) |
| 86 | |
| 87 | def delete_record(self, port): |
| 88 | # TODO(smbaker): Implement delete step |
| 89 | pass |
| 90 | |