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_service.py |
| 18 | |
| 19 | Synchronize Services. The only type of Service this step knows how to deal with are services that use Kubernetes |
| 20 | NodePort to expose ports. |
| 21 | """ |
| 22 | |
| 23 | from synchronizers.new_base.syncstep import SyncStep |
| 24 | from synchronizers.new_base.modelaccessor import Service |
| 25 | |
| 26 | from xosconfig import Config |
| 27 | from multistructlog import create_logger |
| 28 | |
Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -0700 | [diff] [blame] | 29 | log = create_logger(Config().get('logging')) |
| 30 | |
| 31 | class SyncService(SyncStep): |
| 32 | |
| 33 | """ |
| 34 | SyncService |
| 35 | |
| 36 | Implements sync step for syncing Services. |
| 37 | """ |
| 38 | |
| 39 | provides = [Service] |
| 40 | observes = Service |
| 41 | requested_interval = 0 |
| 42 | |
| 43 | def __init__(self, *args, **kwargs): |
| 44 | super(SyncService, 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.client.rest import ApiException |
| 49 | from kubernetes import client as kubernetes_client, config as kubernetes_config |
Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -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 | 3fd18e5 | 2018-04-17 09:18:21 -0700 | [diff] [blame] | 54 | |
| 55 | def fetch_pending(self, deletion=False): |
| 56 | """ Filter the set of pending objects. |
| 57 | As this syncstep can only create Service that exist within Trust Domains, filter out those services that |
| 58 | don't have Trust Domains associated with them. |
| 59 | """ |
| 60 | models = super(SyncService, self).fetch_pending(deletion) |
| 61 | |
| 62 | if (not deletion): |
| 63 | for model in models[:]: |
| 64 | if not self.get_trust_domain(model): |
| 65 | log.info("Unable to determine Trust Domain for service %s. Ignoring." % model.name) |
| 66 | models.remove(model) |
| 67 | elif not model.serviceports.exists(): |
| 68 | # If there are not ServicePorts, then there's not much for us to do at this time... |
| 69 | log.info("Service %s is not interesting. Ignoring." % model.name) |
| 70 | models.remove(model) |
| 71 | |
| 72 | return models |
| 73 | |
| 74 | def get_trust_domain(self, o): |
| 75 | """ Given a service, determine its Trust Domain. |
| 76 | |
| 77 | The design we've chosen to go with is that a service is pinned to a Trust Domain based on the slices |
| 78 | that it contains. It's an error for a service to be directly comprised of slices from multiple |
| 79 | trust domains. |
| 80 | |
| 81 | This allows for "logical services", that contain no slices of their own, but are comprised of multiple |
| 82 | subservices. For example, EPC. |
| 83 | """ |
| 84 | |
| 85 | trust_domain = None |
| 86 | for slice in o.slices.all(): |
| 87 | if slice.trust_domain: |
| 88 | if (trust_domain is None): |
| 89 | trust_domain = slice.trust_domain |
| 90 | elif (trust_domain.id != slice.trust_domain.id): |
| 91 | # Bail out of we've encountered a situation where a service spans multiple trust domains. |
| 92 | log.warning("Service %s is comprised of slices from multiple trust domains." % o.name) |
| 93 | return None |
| 94 | |
| 95 | return trust_domain |
| 96 | |
| 97 | def get_service(self, o, trust_domain): |
| 98 | """ Given an XOS Service, read the associated Service from Kubernetes. |
| 99 | If no Kubernetes service exists, return None |
| 100 | """ |
| 101 | try: |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame^] | 102 | k8s_service = self.v1core.read_namespaced_service(o.name, trust_domain.name) |
| 103 | except self.ApiException, e: |
Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -0700 | [diff] [blame] | 104 | if e.status == 404: |
| 105 | return None |
| 106 | raise |
| 107 | return k8s_service |
| 108 | |
| 109 | def sync_record(self, o): |
| 110 | trust_domain = self.get_trust_domain(o) |
| 111 | k8s_service = self.get_service(o,trust_domain) |
| 112 | |
| 113 | if not k8s_service: |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame^] | 114 | k8s_service = self.kubernetes_client.V1Service() |
| 115 | k8s_service.metadata = self.kubernetes_client.V1ObjectMeta(name=o.name) |
Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -0700 | [diff] [blame] | 116 | |
| 117 | ports=[] |
| 118 | for service_port in o.serviceports.all(): |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame^] | 119 | port=self.kubernetes_client.V1ServicePort(name = service_port.name, |
Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -0700 | [diff] [blame] | 120 | node_port = service_port.external_port, |
| 121 | port = service_port.internal_port, |
| 122 | target_port = service_port.internal_port, |
| 123 | protocol = service_port.protocol) |
| 124 | ports.append(port) |
| 125 | |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame^] | 126 | k8s_service.spec = self.kubernetes_client.V1ServiceSpec(ports=ports, |
Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -0700 | [diff] [blame] | 127 | type="NodePort") |
| 128 | |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame^] | 129 | k8s_service = self.v1core.create_namespaced_service(trust_domain.name, k8s_service) |
| 130 | |
| 131 | if (not o.backend_handle): |
| 132 | o.backend_handle = k8s_service.metadata.self_link |
| 133 | o.save(update_fields=["backend_handle"]) |
Scott Baker | 3fd18e5 | 2018-04-17 09:18:21 -0700 | [diff] [blame] | 134 | |
| 135 | def delete_record(self, o): |
| 136 | # TODO(smbaker): Implement delete step |
| 137 | pass |
| 138 | |