blob: 5a326fa0f31d4c53bf647270b40721e293baf578 [file] [log] [blame]
Scott Baker3fd18e52018-04-17 09:18:21 -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_trustdomain.py
18
19 Synchronize TrustDomain. TrustDomains correspond roughly to Kubernetes namespaces.
20"""
21
22from synchronizers.new_base.syncstep import SyncStep
23from synchronizers.new_base.modelaccessor import TrustDomain
24
25from xosconfig import Config
26from multistructlog import create_logger
27
Scott Baker3fd18e52018-04-17 09:18:21 -070028log = create_logger(Config().get('logging'))
29
30class 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 Baker13e953c2018-05-17 09:19:15 -070044 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 Baker3fd18e52018-04-17 09:18:21 -070049 kubernetes_config.load_incluster_config()
Scott Baker13e953c2018-05-17 09:19:15 -070050 self.kubernetes_client = kubernetes_client
51 self.v1core = kubernetes_client.CoreV1Api()
52 self.ApiException = ApiException
Scott Baker3fd18e52018-04-17 09:18:21 -070053
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 Baker13e953c2018-05-17 09:19:15 -070071 ns = self.v1core.read_namespace(o.name)
72 except self.ApiException, e:
Scott Baker3fd18e52018-04-17 09:18:21 -070073 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 Baker13e953c2018-05-17 09:19:15 -070081 ns = self.kubernetes_client.V1Namespace()
82 ns.metadata = self.kubernetes_client.V1ObjectMeta(name=o.name)
Scott Baker3fd18e52018-04-17 09:18:21 -070083
84 log.info("creating namespace %s" % o.name)
Scott Baker13e953c2018-05-17 09:19:15 -070085 ns=self.v1core.create_namespace(ns)
Scott Baker3fd18e52018-04-17 09:18:21 -070086
87 if (not o.backend_handle):
88 o.backend_handle = ns.metadata.self_link
89 o.save(update_fields=["backend_handle"])
90
Scott Baker393d0152018-05-21 09:17:49 -070091 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 Baker3fd18e52018-04-17 09:18:21 -070099