diff --git a/agent/kubviz/plugins/events/event_metrics_utils.go b/agent/kubviz/plugins/events/event_metrics_utils.go index 15b2a8f2..17ef114f 100644 --- a/agent/kubviz/plugins/events/event_metrics_utils.go +++ b/agent/kubviz/plugins/events/event_metrics_utils.go @@ -3,6 +3,7 @@ package events import ( "context" "encoding/json" + "errors" "fmt" "log" "os" @@ -40,7 +41,7 @@ func PublishMetrics(clientset *kubernetes.Clientset, js nats.JetStreamContext, e errCh <- nil } -func publishK8sMetrics(id string, mtype string, mdata *v1.Event, js nats.JetStreamContext) (bool, error) { +func publishK8sMetrics(id string, mtype string, mdata *v1.Event, js nats.JetStreamContext, imageName string) (bool, error) { ctx := context.Background() tracer := otel.Tracer("kubviz-publish-k8smetrics") @@ -53,6 +54,7 @@ func publishK8sMetrics(id string, mtype string, mdata *v1.Event, js nats.JetStre Type: mtype, Event: mdata, ClusterName: ClusterName, + ImageName: imageName, } metricsJson, _ := json.Marshal(metrics) _, err := js.Publish(constants.EventSubject, metricsJson) @@ -63,6 +65,27 @@ func publishK8sMetrics(id string, mtype string, mdata *v1.Event, js nats.JetStre return false, nil } +func getK8sPodImages(clientset *kubernetes.Clientset, namespace, podName string) ([]string, error) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + pod, err := clientset.CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{}) + if err != nil { + return nil, err + } + + var images []string + for _, container := range pod.Spec.Containers { + images = append(images, container.Image) + } + + if len(images) == 0 { + return nil, errors.New("no containers found in the pod") + } + + return images, nil +} + // createStream creates a stream by using JetStreamContext func CreateStream(js nats.JetStreamContext) error { // Check if the METRICS stream already exists; if not, create it. @@ -162,15 +185,36 @@ func watchK8sEvents(clientset *kubernetes.Clientset, js nats.JetStreamContext) { cache.ResourceEventHandlerFuncs{ AddFunc: func(obj interface{}) { event := obj.(*v1.Event) - publishK8sMetrics(string(event.ObjectMeta.UID), "ADD", event, js) + images, err := getK8sPodImages(clientset, event.InvolvedObject.Namespace, event.InvolvedObject.Name) + if err != nil { + log.Println("Error retrieving image names:", err) + return + } + for _, image := range images { + publishK8sMetrics(string(event.ObjectMeta.UID), "ADD", event, js, image) + } }, DeleteFunc: func(obj interface{}) { event := obj.(*v1.Event) - publishK8sMetrics(string(event.ObjectMeta.UID), "DELETE", event, js) + images, err := getK8sPodImages(clientset, event.InvolvedObject.Namespace, event.InvolvedObject.Name) + if err != nil { + log.Println("Error retrieving image names:", err) + return + } + for _, image := range images { + publishK8sMetrics(string(event.ObjectMeta.UID), "DELETE", event, js, image) + } }, UpdateFunc: func(oldObj, newObj interface{}) { event := newObj.(*v1.Event) - publishK8sMetrics(string(event.ObjectMeta.UID), "UPDATE", event, js) + images, err := getK8sPodImages(clientset, event.InvolvedObject.Namespace, event.InvolvedObject.Name) + if err != nil { + log.Println("Error retrieving image names:", err) + return + } + for _, image := range images { + publishK8sMetrics(string(event.ObjectMeta.UID), "UPDATE", event, js, image) + } }, }, ) diff --git a/charts/agent/Chart.yaml b/charts/agent/Chart.yaml index 35de376e..694dc262 100644 --- a/charts/agent/Chart.yaml +++ b/charts/agent/Chart.yaml @@ -15,7 +15,7 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 1.1.13 +version: 1.1.14 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to diff --git a/charts/agent/templates/deployment.yaml b/charts/agent/templates/deployment.yaml index 5321f029..db0c1603 100644 --- a/charts/agent/templates/deployment.yaml +++ b/charts/agent/templates/deployment.yaml @@ -62,7 +62,7 @@ spec: key: {{ .Values.nats.auth.secret.key }} {{- end }} - name: KUBERHEALTHY_ENABLE - value: {{ .Values.kuberhealthy.enabled }} + value: "{{ .Values.kuberhealthy.enabled }}" - name: NATS_ADDRESS value: {{ .Values.nats.host }} - name: SCHEDULING_INTERVAL