github.com/cilium/cilium@v1.16.2/pkg/k8s/watchers/k8s_event_processor.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package watchers
     5  
     6  import (
     7  	"strconv"
     8  
     9  	k8smetrics "github.com/cilium/cilium/pkg/k8s/metrics"
    10  	k8sSynced "github.com/cilium/cilium/pkg/k8s/synced"
    11  	"github.com/cilium/cilium/pkg/metrics"
    12  )
    13  
    14  func newK8sEventReporter(k8sResourceSynced *k8sSynced.Resources) *K8sEventReporter {
    15  	return &K8sEventReporter{
    16  		k8sResourceSynced,
    17  	}
    18  }
    19  
    20  type K8sEventReporter struct {
    21  	// k8sResourceSynced maps a resource name to a channel. Once the given
    22  	// resource name is synchronized with k8s, the channel for which that
    23  	// resource name maps to is closed.
    24  	k8sResourceSynced *k8sSynced.Resources
    25  }
    26  
    27  // K8sEventProcessed is called to do metrics accounting for each processed
    28  // Kubernetes event
    29  func (k *K8sEventReporter) K8sEventProcessed(scope, action string, status bool) {
    30  	result := "success"
    31  	if !status {
    32  		result = "failed"
    33  	}
    34  
    35  	metrics.KubernetesEventProcessed.WithLabelValues(scope, action, result).Inc()
    36  }
    37  
    38  // K8sEventReceived does metric accounting for each received Kubernetes event, as well
    39  // as notifying of events for k8s resources synced.
    40  func (k *K8sEventReporter) K8sEventReceived(apiResourceName, scope, action string, valid, equal bool) {
    41  	k8smetrics.LastInteraction.Reset()
    42  
    43  	metrics.EventTS.WithLabelValues(metrics.LabelEventSourceK8s, scope, action).SetToCurrentTime()
    44  	validStr := strconv.FormatBool(valid)
    45  	equalStr := strconv.FormatBool(equal)
    46  	metrics.KubernetesEventReceived.WithLabelValues(scope, action, validStr, equalStr).Inc()
    47  
    48  	k.k8sResourceSynced.SetEventTimestamp(apiResourceName)
    49  }