def event_body(self): if self.unique_name: obj_meta = client.V1ObjectMeta(name="{}".format(self.event_name)) else: obj_meta = client.V1ObjectMeta( generate_name="{}".format(self.event_name)) # field_path is needed to prevent problems in the namespacewatcher when # deleted event are received obj_ref = client.V1ObjectReference(kind="CephCluster", field_path='spec.containers{mgr}', name=self.event_name, namespace=self.namespace) event_source = client.V1EventSource(component="ceph-mgr", host=self.host) return client.V1Event(involved_object=obj_ref, metadata=obj_meta, message=self.message, count=self.count, type=self.event_type, reason=self.event_reason, source=event_source, first_timestamp=self.first_timestamp, last_timestamp=self.last_timestamp)
def warn_no_solution_found(self, event, namespace='default'): '''Add event message to pod description when optimizer is unable to find solution.''' object = client.V1ObjectReference( api_version='v1', kind='Pod', name=event['object'].metadata.name, namespace=namespace, resource_version=event['object'].metadata.resource_version, uid=event['object'].metadata.uid) meta = client.V1ObjectMeta(name=event['object'].metadata.name) source = client.V1EventSource(component=self.name) timestamp = datetime.now(pytz.utc) body = client.V1Event( count=1, first_timestamp=timestamp, involved_object=object, last_timestamp=timestamp, message= "Optimizer was unable to find solution for batch containing pod.", metadata=meta, reason='FailedScheduling', source=source, type='Warning') self.api.create_namespaced_event(namespace=namespace, body=body, _preload_content=False)
def CreateEvents(eventsDetails, chaosDetails, kind, eventName, clients): event = client.V1Event( first_timestamp=datetime.now(pytz.utc), last_timestamp=datetime.now(pytz.utc), event_time=datetime.now(pytz.utc), involved_object=client.V1ObjectReference( api_version="litmuschaos.io/v1alpha1", kind=kind, name=eventsDetails.ResourceName, namespace=chaosDetails.ChaosNamespace, uid=eventsDetails.ResourceUID, ), message=eventsDetails.Message, metadata=client.V1ObjectMeta( name=eventName, namespace=chaosDetails.ChaosNamespace, ), reason=eventsDetails.Reason, related=None, action="ChaosEvent", reporting_component="litmuschaos.io/v1alpha1", reporting_instance=eventsDetails.ResourceName, series=None, source=client.V1EventSource(component=chaosDetails.ChaosPodName, ), type=eventsDetails.Type, local_vars_configuration=None, count=1, ) try: clients.clientCoreV1.create_namespaced_event( chaosDetails.ChaosNamespace, body=event) except Exception as exp: return ValueError("Failed to create event with err: {}".format(exp)) return None
def log_k8s_event(self, asg_name, price="", useSpot=False): msg_str = '{"apiVersion":"v1alpha1","spotPrice":"' + price + '", "useSpot": ' + str( useSpot).lower() + '}' event_namespace = os.getenv('EVENT_NAMESPACE', 'default') if not self.incluster: logger.info(msg_str) return try: config.load_incluster_config() v1 = client.CoreV1Api() event_timestamp = datetime.now(pytz.utc) event_name = "spot-instance-update" new_event = client.V1Event( count=1, first_timestamp=event_timestamp, involved_object=client.V1ObjectReference( kind="SpotPriceInfo", name=asg_name, namespace=event_namespace, ), last_timestamp=event_timestamp, metadata=client.V1ObjectMeta(generate_name=event_name, ), message=msg_str, reason="SpotRecommendationGiven", source=client.V1EventSource(component="minion-manager", ), type="Normal", ) v1.create_namespaced_event(namespace=event_namespace, body=new_event) logger.info("Spot price info event logged") except Exception as e: logger.info("Failed to log event: " + str(e))
def GeneratePodEvent(self, podobj, podname, ns, reason, _type, message): """ Generates a pod event on the kubernetes API server """ try: meta = client.V1ObjectMeta() meta.name = f'{podname}.{self.GetRandomUid()}' meta.namespace = ns invobj = client.V1ObjectReference() invobj.name = podname invobj.kind = "Pod" invobj.namespace = ns invobj.api_version = 'v1' invobj.uid = podobj.metadata.uid evtsrc = client.V1EventSource() evtsrc.component = 'NHD Scheduler' if _type == K8SEventType.EVENT_TYPE_NORMAL: etype = "Normal" lg = self.logger.info else: etype = "Warning" lg = self.logger.warning timestamp = self.GetTimeNow() # Log an event in our pod too instead of duplicating externally lg(f'Event for pod {ns}/{podname} -- Reason={reason}, message={message}' ) event = client.V1Event(involved_object=invobj, source=evtsrc, metadata=meta, reason=reason, message=f'NHD: {message}', count=1, type=etype, first_timestamp=timestamp, last_timestamp=timestamp) self.v1.create_namespaced_event(namespace=ns, body=event) except ApiException as e: self.logger.error(f'Failed to send event for pod {podname}: {e}')
def create_event(reason: str, involved_object: api.V1ObjectReference, message: str, timestamp: datetime.datetime, namespace: str = ''): msg_hash = hashlib.sha1(message.encode()).hexdigest()[:8] event_name = f'{involved_object.name}.{int(timestamp.timestamp())}.{msg_hash}' event = api.V1Event( metadata=api.V1ObjectMeta( name=event_name, namespace=namespace, ), involved_object=involved_object, reason=reason, message=message, first_timestamp=timestamp, last_timestamp=timestamp, count=1, type='Warning', source=api.V1EventSource(component='kube-resources-analyzer', ), ) v1 = api.CoreV1Api() v1.create_namespaced_event(namespace, event)
def event_body(self): test = self.service.get_test(self.component_name, self.namespace) obj_meta = client.V1ObjectMeta( generate_name="{}".format(self.__event_body.reason.value)) obj_ref = client.V1ObjectReference( kind=PilotService.test_crd_config.kind, api_version=PilotService.test_crd_config.get_full_api_version(), name=self.component_name, resource_version=test['metadata']['resourceVersion'], uid=test['metadata']['uid'], namespace=self.namespace) event_source = client.V1EventSource(component=self.component_name) return client.V1Event(involved_object=obj_ref, metadata=obj_meta, message=self.__event_body.message, count=1, type=self.__event_body.event_type.name, reason=self.__event_body.reason.name, source=event_source, first_timestamp=self.first_timestamp, last_timestamp=self.last_timestamp)
# config.load_incluster_config() ui_server_deploy = client.AppsV1Api().\ read_namespaced_deployment('ui-server', 'tmc-dearingj') first_seen = datetime.now(timezone.utc) involved_obj = client.V1ObjectReference( api_version=ui_server_deploy.api_version, kind=ui_server_deploy.kind, name=ui_server_deploy.metadata.name, namespace=ui_server_deploy.metadata.namespace, uid=ui_server_deploy.metadata.uid, resource_version=ui_server_deploy.metadata.resource_version, ) event = client.V1Event( involved_object=involved_obj, first_timestamp=first_seen, last_timestamp=first_seen, metadata=client.V1ObjectMeta( name=f"ui-server.{cuid()}", namespace="tmc-dearingj", ), source=client.V1EventSource(component="ci-approver"), type="Normal", reason="Approval", message="Manually tested and approved", ) client.CoreV1Api().create_namespaced_event("tmc-dearingj", event)