Scott Baker | 665b936 | 2018-08-15 11:07:59 -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 | import json |
Scott Baker | 806d4b7 | 2019-02-26 19:01:59 -0800 | [diff] [blame] | 18 | from xossynchronizer.event_steps.eventstep import EventStep |
| 19 | from xossynchronizer.modelaccessor import ONOSApp, ONOSService |
Scott Baker | 665b936 | 2018-08-15 11:07:59 -0700 | [diff] [blame] | 20 | from xosconfig import Config |
| 21 | from multistructlog import create_logger |
| 22 | |
| 23 | log = create_logger(Config().get('logging')) |
| 24 | |
Scott Baker | 7848fa5 | 2019-04-01 17:54:36 -0700 | [diff] [blame] | 25 | |
Scott Baker | 665b936 | 2018-08-15 11:07:59 -0700 | [diff] [blame] | 26 | class KubernetesPodDetailsEventStep(EventStep): |
| 27 | topics = ["xos.kubernetes.pod-details"] |
| 28 | technology = "kafka" |
| 29 | |
| 30 | def __init__(self, *args, **kwargs): |
| 31 | super(KubernetesPodDetailsEventStep, self).__init__(*args, **kwargs) |
| 32 | |
| 33 | def process_event(self, event): |
| 34 | value = json.loads(event.value) |
| 35 | |
| 36 | if (value.get("status") != "created"): |
| 37 | return |
| 38 | |
| 39 | if "labels" not in value: |
| 40 | return |
| 41 | |
| 42 | xos_service = value["labels"].get("xos_service") |
| 43 | if not xos_service: |
Matteo Scandolo | fba51cd | 2018-08-28 17:23:16 -0700 | [diff] [blame] | 44 | log.info("This pod has no xos_service label", labels=value["labels"]) |
Scott Baker | 665b936 | 2018-08-15 11:07:59 -0700 | [diff] [blame] | 45 | return |
| 46 | |
Matteo Scandolo | fba51cd | 2018-08-28 17:23:16 -0700 | [diff] [blame] | 47 | log.info("Looking for ONOSServices", name=xos_service) |
| 48 | for service in ONOSService.objects.all(): |
| 49 | |
| 50 | if service.name.lower() != xos_service.lower(): |
| 51 | continue |
| 52 | |
Scott Baker | 665b936 | 2018-08-15 11:07:59 -0700 | [diff] [blame] | 53 | log.info("Dirtying ONOS Service", service=service) |
Scott Baker | 7848fa5 | 2019-04-01 17:54:36 -0700 | [diff] [blame] | 54 | service.backend_code = 0 |
| 55 | service.backend_status = "resynchronize due to kubernetes event" |
Scott Baker | 665b936 | 2018-08-15 11:07:59 -0700 | [diff] [blame] | 56 | service.save(update_fields=["updated", "backend_code", "backend_status"], always_update_timestamp=True) |
| 57 | |
| 58 | for app in service.service_instances.all(): |
| 59 | log.info("Dirtying ONOS App", app=app) |
Scott Baker | 7848fa5 | 2019-04-01 17:54:36 -0700 | [diff] [blame] | 60 | app.backend_code = 0 |
| 61 | app.backend_status = "resynchronize due to kubernetes event" |
Scott Baker | 665b936 | 2018-08-15 11:07:59 -0700 | [diff] [blame] | 62 | app.save(update_fields=["updated", "backend_code", "backend_status"], always_update_timestamp=True) |
Matteo Scandolo | 7551b9a | 2018-09-10 11:30:31 -0700 | [diff] [blame] | 63 | |
| 64 | for attr in app.service_instance_attributes.all(): |
| 65 | log.info("Dirtying ServiceInstanceAttributes for App", app=app, attr=attr) |
| 66 | attr.backend_code = 0 |
| 67 | attr.backend_status = "resynchronize due to kubernetes event" |
Scott Baker | 7848fa5 | 2019-04-01 17:54:36 -0700 | [diff] [blame] | 68 | attr.save( |
| 69 | update_fields=[ |
| 70 | "updated", |
| 71 | "backend_code", |
| 72 | "backend_status"], |
| 73 | always_update_timestamp=True) |