Matteo Scandolo | 04287a8 | 2018-10-02 15:25:26 -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 |
Matteo Scandolo | 8202625 | 2019-06-20 12:11:45 -0700 | [diff] [blame] | 20 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 21 | from xossynchronizer.event_steps.eventstep import EventStep |
| 22 | from xossynchronizer.modelaccessor import VOLTService, VOLTServiceInstance, Service |
Matteo Scandolo | 04287a8 | 2018-10-02 15:25:26 -0700 | [diff] [blame] | 23 | from xosconfig import Config |
| 24 | from multistructlog import create_logger |
Matteo Scandolo | 8202625 | 2019-06-20 12:11:45 -0700 | [diff] [blame] | 25 | from helpers import Helpers |
Matteo Scandolo | 04287a8 | 2018-10-02 15:25:26 -0700 | [diff] [blame] | 26 | |
| 27 | log = create_logger(Config().get('logging')) |
| 28 | |
| 29 | class KubernetesPodDetailsEventStep(EventStep): |
| 30 | topics = ["xos.kubernetes.pod-details"] |
| 31 | technology = "kafka" |
| 32 | |
| 33 | def __init__(self, *args, **kwargs): |
| 34 | super(KubernetesPodDetailsEventStep, self).__init__(*args, **kwargs) |
| 35 | |
Matteo Scandolo | 04287a8 | 2018-10-02 15:25:26 -0700 | [diff] [blame] | 36 | def process_event(self, event): |
| 37 | value = json.loads(event.value) |
| 38 | |
| 39 | if (value.get("status") != "created"): |
| 40 | return |
| 41 | |
| 42 | if "labels" not in value: |
| 43 | return |
| 44 | |
| 45 | xos_service = value["labels"].get("xos_service") |
| 46 | if not xos_service: |
| 47 | return |
| 48 | |
| 49 | for service in VOLTService.objects.all(): |
Matteo Scandolo | 8202625 | 2019-06-20 12:11:45 -0700 | [diff] [blame] | 50 | onos = Helpers.get_onos_service_name(service) |
| 51 | # NOTE do we really need to dynamically fetch the ONOS name? |
| 52 | if (onos.lower() != xos_service.lower()): |
Matteo Scandolo | 04287a8 | 2018-10-02 15:25:26 -0700 | [diff] [blame] | 53 | continue |
| 54 | |
| 55 | for service_instance in service.service_instances.all(): |
| 56 | log.info("Dirtying VOLTServiceInstance", service_instance=service_instance) |
| 57 | service_instance.backend_code=0 |
| 58 | service_instance.backend_status="resynchronize due to kubernetes event" |
| 59 | service_instance.save(update_fields=["updated", "backend_code", "backend_status"], always_update_timestamp=True) |