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