blob: d319eb0a873a6348151ccd4ecb2fe92376bb0143 [file] [log] [blame]
Scott Baker665b9362018-08-15 11:07:59 -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
17import json
Scott Baker806d4b72019-02-26 19:01:59 -080018from xossynchronizer.event_steps.eventstep import EventStep
19from xossynchronizer.modelaccessor import ONOSApp, ONOSService
Scott Baker665b9362018-08-15 11:07:59 -070020from xosconfig import Config
21from multistructlog import create_logger
22
23log = create_logger(Config().get('logging'))
24
Scott Baker7848fa52019-04-01 17:54:36 -070025
Scott Baker665b9362018-08-15 11:07:59 -070026class 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 Scandolofba51cd2018-08-28 17:23:16 -070044 log.info("This pod has no xos_service label", labels=value["labels"])
Scott Baker665b9362018-08-15 11:07:59 -070045 return
46
Matteo Scandolofba51cd2018-08-28 17:23:16 -070047 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 Baker665b9362018-08-15 11:07:59 -070053 log.info("Dirtying ONOS Service", service=service)
Scott Baker7848fa52019-04-01 17:54:36 -070054 service.backend_code = 0
55 service.backend_status = "resynchronize due to kubernetes event"
Scott Baker665b9362018-08-15 11:07:59 -070056 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 Baker7848fa52019-04-01 17:54:36 -070060 app.backend_code = 0
61 app.backend_status = "resynchronize due to kubernetes event"
Scott Baker665b9362018-08-15 11:07:59 -070062 app.save(update_fields=["updated", "backend_code", "backend_status"], always_update_timestamp=True)
Matteo Scandolo7551b9a2018-09-10 11:30:31 -070063
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 Baker7848fa52019-04-01 17:54:36 -070068 attr.save(
69 update_fields=[
70 "updated",
71 "backend_code",
72 "backend_status"],
73 always_update_timestamp=True)