public void Record(string action, string reason, string message, V1ObjectReference objRef) { var occurrence = DateTime.Now; var key = new EventKey($"{objRef.ApiVersion}/{objRef.Kind}", action, reason, message, $"{objRef.NamespaceProperty}/{objRef.Name}"); if (!events.ContainsKey(key)) { Logger.LogDebug("Initializing new EventDetail entry for {objName}", key.ToString()); events.Add(key, EventDetails.Initialize(objRef.Name, occurrence)); } EventDetails details = events[key]; details.AddOccurrence(occurrence); Logger.LogDebug("Writing event to Kubernetes ({key}, Count: {count}", key.ToString(), details.Count); V1Event ev = new V1Event(objRef, new V1ObjectMeta() { Name = details.Name }, action: action, message: message, reason: reason, firstTimestamp: details.FirstSeen, lastTimestamp: details.LastSeen, count: details.Count); Kubernetes.ReplaceNamespacedEventAsync(ev, details.Name, objRef.NamespaceProperty); }
private async Task AddIngress(V1Ingress ingress, CancellationToken cancellation) { var legacyIngressClass = ingress.LegacyIngressClass(); if (!IngressMatch(ingress, legacyIngressClass)) { _logger.LogInformation($"Ignoring ingress ingress={ingress.Metadata.NamespaceProperty}/{ingress.Metadata.Name} kubernetes.io/ingress.class={legacyIngressClass} ingressClassName={ingress.Spec.IngressClassName}"); return; } var model = ingress.ToModel(); var objRef = new V1ObjectReference { Kind = V1Ingress.KubeKind, ApiVersion = V1Ingress.KubeGroup + "/" + V1Ingress.KubeApiVersion, Name = model.Name, NamespaceProperty = model.Namespace, Uid = ingress.Metadata.Uid, ResourceVersion = ingress.Metadata.ResourceVersion, }; await _eventRecorder.CreateEvent(objRef, KubeEvent.EventType.Normal, "Sync", "Scheduled for sync", cancellation); var key = ingress.Metadata.Uid; _ingresses.AddOrUpdate(key, a => model, (s, u) => model); RaiseChanged(); }
public void EmitEvent(string action, string reason, string message, CustomResource involvedObject) { var objRef = new V1ObjectReference(involvedObject.ApiVersion, kind: involvedObject.Kind, name: involvedObject.Metadata.Name, namespaceProperty: involvedObject.Metadata.NamespaceProperty); V1Event ev = new V1Event(objRef, new V1ObjectMeta() { GenerateName = involvedObject.Metadata.Name }, action: action, message: message, reason: reason, firstTimestamp: DateTime.Now); client.CreateNamespacedEvent(ev, involvedObject.Metadata.NamespaceProperty); }
public async ValueTask CreateEvent(V1ObjectReference runtimeObject, KubeEvent.EventType eventType, string reason, string message, CancellationToken cancellation) { var kubeEvent = new KubeEvent { Regarding = runtimeObject, Timestamp = DateTime.UtcNow, EvtType = eventType, Reason = reason, Message = message, }; _logger.LogInformation($"Event(Kind={runtimeObject.Kind}, Resource={runtimeObject.NamespaceProperty}/{runtimeObject.Name}, UID={runtimeObject.Uid}, APIVersion={runtimeObject.ApiVersion}, ResourceVersion={runtimeObject.ResourceVersion}, type={eventType}, reason='{reason}', '{message}')"); await _channel.Writer.WriteAsync(kubeEvent, cancellation); }
/// <summary>Determines whether an object reference references the given object.</summary> /// <param name="objref">the object reference<see cref="V1ObjectReference"/></param> /// <param name="obj">the object meta<see cref="V1ObjectMeta"/></param> /// <returns>true if the object reference references the given object.</returns> public static bool Matches(this V1ObjectReference objref, IKubernetesObject <V1ObjectMeta> obj) { if (objref == null) { throw new ArgumentNullException(nameof(objref)); } if (obj == null) { throw new ArgumentNullException(nameof(obj)); } return(objref.ApiVersion == obj.ApiVersion && objref.Kind == obj.Kind && objref.Name == obj.Name() && objref.Uid == obj.Uid() && objref.NamespaceProperty == obj.Namespace()); }
public void TestReferences() { // test object references var pod = new V1Pod() { ApiVersion = "v1", Kind = "Pod" }; pod.Metadata = new V1ObjectMeta() { Name = "name", NamespaceProperty = "ns", ResourceVersion = "ver", Uid = "id" }; var objr = new V1ObjectReference() { ApiVersion = pod.ApiVersion, Kind = pod.Kind, Name = pod.Name(), NamespaceProperty = pod.Namespace(), Uid = pod.Uid() }; Assert.True(objr.Matches(pod)); (pod.ApiVersion, pod.Kind) = (null, null); Assert.False(objr.Matches(pod)); (pod.ApiVersion, pod.Kind) = ("v1", "Pod"); Assert.True(objr.Matches(pod)); pod.Metadata.Name = "nome"; Assert.False(objr.Matches(pod)); // test owner references (pod.ApiVersion, pod.Kind) = ("abc/xyz", "sometimes"); var ownr = new V1OwnerReference() { ApiVersion = "abc/xyz", Kind = "sometimes", Name = pod.Name(), Uid = pod.Uid() }; Assert.True(ownr.Matches(pod)); (pod.ApiVersion, pod.Kind) = (null, null); Assert.False(ownr.Matches(pod)); (ownr.ApiVersion, ownr.Kind) = ("v1", "Pod"); Assert.False(ownr.Matches(pod)); (pod.ApiVersion, pod.Kind) = (ownr.ApiVersion, ownr.Kind); Assert.True(ownr.Matches(pod)); ownr.Name = "nim"; Assert.False(ownr.Matches(pod)); ownr.Name = pod.Name(); var svc = new V1Service(); svc.AddOwnerReference(ownr); Assert.Equal(0, svc.FindOwnerReference(pod)); Assert.Equal(-1, svc.FindOwnerReference(svc)); Assert.Same(ownr, svc.GetOwnerReference(pod)); Assert.Null(svc.GetOwnerReference(svc)); Assert.Null(svc.GetController()); svc.OwnerReferences()[0].Controller = true; Assert.Same(ownr, svc.GetController()); Assert.Same(ownr, svc.RemoveOwnerReference(pod)); Assert.Equal(0, svc.OwnerReferences().Count); svc.AddOwnerReference(new V1OwnerReference() { ApiVersion = pod.ApiVersion, Kind = pod.Kind, Name = pod.Name(), Uid = pod.Uid(), Controller = true }); svc.AddOwnerReference(new V1OwnerReference() { ApiVersion = pod.ApiVersion, Kind = pod.Kind, Name = pod.Name(), Uid = pod.Uid(), Controller = false }); svc.AddOwnerReference(new V1OwnerReference() { ApiVersion = pod.ApiVersion, Kind = pod.Kind, Name = pod.Name(), Uid = pod.Uid() }); Assert.Equal(3, svc.OwnerReferences().Count); Assert.NotNull(svc.RemoveOwnerReference(pod)); Assert.Equal(2, svc.OwnerReferences().Count); Assert.True(svc.RemoveOwnerReferences(pod)); Assert.Equal(0, svc.OwnerReferences().Count); }