Skip to content

Commit

Permalink
Merge pull request #328 from intelops/img
Browse files Browse the repository at this point in the history
img
  • Loading branch information
vijeyashintelops authored Feb 20, 2024
2 parents 73e8f6c + 82eb5aa commit f2555bf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
52 changes: 48 additions & 4 deletions agent/kubviz/plugins/events/event_metrics_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package events
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
},
},
)
Expand Down
2 changes: 1 addition & 1 deletion charts/agent/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion charts/agent/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f2555bf

Please sign in to comment.