Skip to content

Commit

Permalink
using kuberhealthy package struct for parsing payload
Browse files Browse the repository at this point in the history
  • Loading branch information
vijeyash1 committed Feb 14, 2024
1 parent 7c54a65 commit 75bfbf0
Show file tree
Hide file tree
Showing 6 changed files with 507 additions and 45 deletions.
46 changes: 11 additions & 35 deletions agent/kubviz/plugins/kuberhealthy/kuberhealthy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"io"
"log"
"net/http"
"strings"
"time"

"github.com/intelops/kubviz/agent/config"
"github.com/intelops/kubviz/constants"
"github.com/intelops/kubviz/model"
"github.com/intelops/kubviz/pkg/opentelemetry"
"github.com/kuberhealthy/kuberhealthy/v2/pkg/health"
"github.com/nats-io/nats.go"
"go.opentelemetry.io/otel"
)
Expand Down Expand Up @@ -45,52 +44,29 @@ func pollAndPublishKuberhealthy(url string, js nats.JetStreamContext) error {
return fmt.Errorf("error reading response body: %w", err)
}

var state model.State
var state health.State
if err := json.Unmarshal(body, &state); err != nil {
return fmt.Errorf("error unmarshaling response: %w", err)
}

return PublishKuberhealthyMetrics(js, state)
}
func boolToUInt8(b bool) uint8 {
if b {
return 1
}
return 0
}

func errorsToString(errors []string) string {
return strings.Join(errors, ", ")
}
func PublishKuberhealthyMetrics(js nats.JetStreamContext, state model.State) error {
func PublishKuberhealthyMetrics(js nats.JetStreamContext, state health.State) error {
ctx := context.Background()
tracer := otel.Tracer("kuberhealthy")
_, span := tracer.Start(opentelemetry.BuildContext(ctx), "PublishKuberhealthyMetrics")
defer span.End()

for checkName, details := range state.CheckDetails {
metrics := model.KuberhealthyCheckDetail{
CurrentUUID: details.CurrentUUID,
CheckName: checkName,
OK: boolToUInt8(details.OK),
Errors: errorsToString(details.Errors),
RunDuration: details.RunDuration,
Namespace: details.Namespace,
Node: details.Node,
LastRun: details.LastRun.Time,
AuthoritativePod: details.AuthoritativePod,
}

metricsJSON, err := json.Marshal(metrics)
if err != nil {
log.Printf("Error marshaling metrics of kuberhealthy %s: %v", checkName, err)
continue
}
metricsJSON, err := json.Marshal(state)
if err != nil {
log.Printf("Error marshaling metrics of kuberhealthy %v", err)
return err
}

if _, err := js.Publish(constants.KUBERHEALTHY_SUBJECT, metricsJSON); err != nil {
log.Printf("Error publishing metrics for kuberhealthy %s: %v", checkName, err)
continue
}
if _, err := js.Publish(constants.KUBERHEALTHY_SUBJECT, metricsJSON); err != nil {
log.Printf("Error publishing metrics for kuberhealthy %v", err)
return err
}

log.Printf("Kuberhealthy metrics have been published")
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.12
version: 1.1.13

# 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
12 changes: 9 additions & 3 deletions charts/agent/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ spec:
# path: /
# port: http
env:
- name: KUBERHEALTHY_URL
value: {{ .Values.kuberhealthy.url }}
- name: POLL_INTERVAL
value: {{ .Values.kuberhealthy.pollInterval }}
- name: CLUSTER_NAME
value: {{ .Values.clusterName }}
- name: NATS_TOKEN
Expand All @@ -57,6 +61,8 @@ spec:
name: {{ .Values.nats.auth.secret.name }}
key: {{ .Values.nats.auth.secret.key }}
{{- end }}
- name: KUBERHEALTHY_ENABLE
value: {{ .Values.kuberhealthy.enabled }}
- name: NATS_ADDRESS
value: {{ .Values.nats.host }}
- name: SCHEDULING_INTERVAL
Expand All @@ -81,7 +87,7 @@ spec:
value: {{ .Values.opentelemetry.url }}
- name : APPLICATION_NAME
value : {{ .Values.opentelemetry.appName }}
{{- if .Values.persistence.enabled }}
{{- if .Values.persistence.enabled }}
volumeMounts:
- name: data
mountPath: {{ .Values.persistence.mountPath }}
Expand Down Expand Up @@ -127,7 +133,7 @@ spec:
value: {{ .Values.opentelemetry.url }}
- name : APPLICATION_NAME
value : {{ .Values.opentelemetry.appName }}
{{- if .Values.git_bridge.persistence.enabled }}
{{- if .Values.git_bridge.persistence.enabled }}
volumeMounts:
- name: data
mountPath: {{ .Values.git_bridge.persistence.mountPath }}
Expand Down Expand Up @@ -174,7 +180,7 @@ spec:
value: {{ .Values.opentelemetry.url }}
- name : APPLICATION_NAME
value : {{ .Values.opentelemetry.appName }}
{{- if .Values.container_bridge.persistence.enabled }}
{{- if .Values.container_bridge.persistence.enabled }}
volumeMounts:
- name: data
mountPath: {{ .Values.container_bridge.persistence.mountPath }}
Expand Down
17 changes: 11 additions & 6 deletions charts/agent/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ serviceAccount:
podAnnotations: {}

podSecurityContext:
fsGroup: 1001
fsGroup: 1001
runAsUser: 1001
runAsGroup: 1001

Expand All @@ -52,7 +52,7 @@ git_bridge:
repository: ghcr.io/intelops/kubviz/git-agent
pullPolicy: Always
tag: "v1.1.4"
resources:
resources:
limits:
cpu: 200m
memory: 256Mi
Expand Down Expand Up @@ -96,7 +96,7 @@ container_bridge:
repository: ghcr.io/intelops/kubviz/container-agent
pullPolicy: Always
tag: "v1.1.4"
resources:
resources:
limits:
cpu: 200m
memory: 256Mi
Expand Down Expand Up @@ -131,7 +131,7 @@ container_bridge:
# hosts:
# - chart-example.local


ingress:
enabled: false
annotations: {}
Expand Down Expand Up @@ -162,7 +162,7 @@ persistence:
mountPath: /mnt/agent/kbz
accessMode: ReadWriteOnce
size: 5Gi

autoscaling:
enabled: false
minReplicas: 1
Expand All @@ -186,6 +186,11 @@ schedule:
kubepreupgradeInterval: "@every 22h"
trivyInterval: "@every 24h"

kuberhealthy:
enabled: true
pollInterval: "15m"
url: "http://localhost:8080"

opentelemetry:
isEnabled: false
url: "otelcollector.local"
Expand All @@ -197,7 +202,7 @@ externalSecrets:
create: false

nats:
host: kubviz-client-nats
host: kubviz-client-nats
auth:
# Use token if you want to provide the token via Helm Values
token: ""
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/hashicorp/go-version v1.6.0
github.com/intelops/go-common v1.0.19
github.com/kelseyhightower/envconfig v1.4.0
github.com/kuberhealthy/kuberhealthy/v2 v2.7.1
github.com/nats-io/nats.go v1.27.1
github.com/pkg/errors v0.9.1
github.com/robfig/cron/v3 v3.0.1
Expand Down
Loading

0 comments on commit 75bfbf0

Please sign in to comment.